Skip to content

Commit 63de5e3

Browse files
committed
Updated unit test framework
1 parent f0859a2 commit 63de5e3

7 files changed

Lines changed: 279 additions & 252 deletions

File tree

OpenStackNetAnalyzers/OpenStackNetAnalyzers.Test/Helpers/CodeFixVerifier.Helper.cs

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,48 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Threading;
8+
using System.Threading.Tasks;
9+
using System.Collections.Immutable;
810

911
namespace TestHelper
1012
{
1113
/// <summary>
12-
/// Diagnostic Producer class with extra methods dealing with applying codefixes
14+
/// Diagnostic Producer class with extra methods dealing with applying code fixes.
1315
/// All methods are static
1416
/// </summary>
1517
public abstract partial class CodeFixVerifier : DiagnosticVerifier
1618
{
1719
/// <summary>
18-
/// Apply the inputted CodeAction to the inputted document.
19-
/// Meant to be used to apply codefixes.
20+
/// Apply the inputted <see cref="CodeAction"/> to the inputted document.
21+
/// Meant to be used to apply code fixes.
2022
/// </summary>
21-
/// <param name="document">The Document to apply the fix on</param>
22-
/// <param name="codeAction">A CodeAction that will be applied to the Document.</param>
23-
/// <returns>A Document with the changes from the CodeAction</returns>
24-
private static Document ApplyFix(Document document, CodeAction codeAction)
23+
/// <param name="document">The <see cref="Document"/> to apply the fix on</param>
24+
/// <param name="codeAction">A <see cref="CodeAction"/> that will be applied to the
25+
/// <paramref name="document"/>.</param>
26+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
27+
/// <returns>A <see cref="Document"/> with the changes from the <see cref="CodeAction"/>.</returns>
28+
private static async Task<Document> ApplyFixAsync(Document document, CodeAction codeAction, CancellationToken cancellationToken)
2529
{
26-
var operations = codeAction.GetOperationsAsync(CancellationToken.None).Result;
30+
var operations = await codeAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
2731
var solution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
2832
return solution.GetDocument(document.Id);
2933
}
3034

3135
/// <summary>
32-
/// Compare two collections of Diagnostics,and return a list of any new diagnostics that appear only in the second collection.
33-
/// Note: Considers Diagnostics to be the same if they have the same Ids. In the case of multiple diagnostics with the same Id in a row,
34-
/// this method may not necessarily return the new one.
36+
/// Compare two collections of <see cref="Diagnostic"/>s, and return a list of any new diagnostics that appear
37+
/// only in the second collection.
38+
/// <note type="note">
39+
/// <para>Considers <see cref="Diagnostic"/> to be the same if they have the same <see cref="Diagnostic.Id"/>s.
40+
/// In the case of multiple diagnostics with the same <see cref="Diagnostic.Id"/> in a row, this method may not
41+
/// necessarily return the new one.</para>
42+
/// </note>
3543
/// </summary>
36-
/// <param name="diagnostics">The Diagnostics that existed in the code before the CodeFix was applied</param>
37-
/// <param name="newDiagnostics">The Diagnostics that exist in the code after the CodeFix was applied</param>
38-
/// <returns>A list of Diagnostics that only surfaced in the code after the CodeFix was applied</returns>
44+
/// <param name="diagnostics">The <see cref="Diagnostic"/>s that existed in the code before the code fix was
45+
/// applied.</param>
46+
/// <param name="newDiagnostics">The <see cref="Diagnostic"/>s that exist in the code after the code fix was
47+
/// applied.</param>
48+
/// <returns>A list of <see cref="Diagnostic"/>s that only surfaced in the code after the code fix was
49+
/// applied.</returns>
3950
private static IEnumerable<Diagnostic> GetNewDiagnostics(IEnumerable<Diagnostic> diagnostics, IEnumerable<Diagnostic> newDiagnostics)
4051
{
4152
var oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
@@ -59,27 +70,29 @@ private static IEnumerable<Diagnostic> GetNewDiagnostics(IEnumerable<Diagnostic>
5970
}
6071

6172
/// <summary>
62-
/// Get the existing compiler diagnostics on the inputted document.
73+
/// Get the existing compiler diagnostics on the input document.
6374
/// </summary>
64-
/// <param name="document">The Document to run the compiler diagnostic analyzers on</param>
65-
/// <returns>The compiler diagnostics that were found in the code</returns>
66-
private static IEnumerable<Diagnostic> GetCompilerDiagnostics(Document document)
75+
/// <param name="document">The <see cref="Document"/> to run the compiler diagnostic analyzers on.</param>
76+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
77+
/// <returns>The compiler diagnostics that were found in the code.</returns>
78+
private static async Task<ImmutableArray<Diagnostic>> GetCompilerDiagnosticsAsync(Document document, CancellationToken cancellationToken)
6779
{
68-
return document.GetSemanticModelAsync().Result.GetDiagnostics();
80+
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
81+
return semanticModel.GetDiagnostics(cancellationToken: cancellationToken);
6982
}
7083

7184
/// <summary>
72-
/// Given a document, turn it into a string based on the syntax root
85+
/// Given a document, turn it into a string based on the syntax root.
7386
/// </summary>
74-
/// <param name="document">The Document to be converted to a string</param>
75-
/// <returns>A string containing the syntax of the Document after formatting</returns>
76-
private static string GetStringFromDocument(Document document)
87+
/// <param name="document">The <see cref="Document"/> to be converted to a string.</param>
88+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
89+
/// <returns>A string containing the syntax of the <see cref="Document"/> after formatting.</returns>
90+
private static async Task<string> GetStringFromDocumentAsync(Document document, CancellationToken cancellationToken)
7791
{
78-
var simplifiedDoc = Simplifier.ReduceAsync(document, Simplifier.Annotation).Result;
79-
var root = simplifiedDoc.GetSyntaxRootAsync().Result;
80-
root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace);
81-
return root.GetText().ToString();
92+
var simplifiedDoc = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
93+
var formatted = await Formatter.FormatAsync(simplifiedDoc, Formatter.Annotation, cancellationToken: cancellationToken);
94+
var sourceText = await formatted.GetTextAsync(cancellationToken).ConfigureAwait(false);
95+
return sourceText.ToString();
8296
}
8397
}
8498
}
85-

OpenStackNetAnalyzers/OpenStackNetAnalyzers.Test/Helpers/DiagnosticResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public DiagnosticResultLocation(string path, int line, int column)
3030
}
3131

3232
/// <summary>
33-
/// Struct that stores information about a Diagnostic appearing in a source
33+
/// Structure that stores information about a <see cref="Diagnostic"/> appearing in a source.
3434
/// </summary>
3535
public struct DiagnosticResult
3636
{
@@ -72,7 +72,7 @@ public string Path
7272
{
7373
get
7474
{
75-
return this.Locations.Length > 0 ? this.Locations[0].Path : "";
75+
return this.Locations.Length > 0 ? this.Locations[0].Path : string.Empty;
7676
}
7777
}
7878

0 commit comments

Comments
 (0)