Skip to content

Commit 13fca96

Browse files
Update version 1.0.8. (#16)
* Update version 1.0.8. * Refactor test framework.
1 parent 3ddfdcf commit 13fca96

File tree

33 files changed

+1046
-1113
lines changed

33 files changed

+1046
-1113
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ It is supposed to be used with StyleCop Analyzers.
2020
- Remove unnecessary using directives.
2121
- Use `T` as a type parameter name if the type parameter is single.
2222
- Move type parameters from the static class to its methods if possible.
23+
- Avoid thoughtless names for the identifer of local variables.
2324

2425
## Configuration
2526

StyleChecker/StyleChecker.Test/Cleaning/UnusedUsing/AnalyzerTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ namespace StyleChecker.Test.Cleaning.UnusedUsing
66
using Microsoft.CodeAnalysis.Diagnostics;
77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88
using StyleChecker.Cleaning.UnusedUsing;
9-
using TestHelper;
9+
using StyleChecker.Test.Framework;
1010

1111
[TestClass]
12-
public sealed class AnalyzerTest : CodeFixVerifier
12+
public sealed class AnalyzerTest : DiagnosticVerifier
1313
{
14-
protected override DiagnosticAnalyzer CSharpDiagnosticAnalyzer
14+
protected override DiagnosticAnalyzer DiagnosticAnalyzer
1515
=> new Analyzer();
1616

1717
protected override string BaseDir
1818
=> Path.Combine("Cleaning", "UnusedUsing");
1919

2020
[TestMethod]
2121
public void Empty()
22-
=> VerifyCSharpDiagnostic(@"", Environment.Default);
22+
=> VerifyDiagnostic(@"", Environment.Default);
2323

2424
[TestMethod]
2525
public void Code()
@@ -35,7 +35,7 @@ DiagnosticResult Expected(int row, int col)
3535
Locations = SingleLocation(startOffset + row, col),
3636
};
3737
var ignoreIds = ImmutableArray.Create("CS8019");
38-
VerifyCSharpDiagnostic(
38+
VerifyDiagnostic(
3939
code,
4040
Environment.Default.WithExcludeIds(ignoreIds),
4141
Expected(0, 5));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace StyleChecker.Test.Framework
2+
{
3+
/// <summary>
4+
/// Provides the utility methods for arrays.
5+
/// </summary>
6+
public static class Arrays
7+
{
8+
/// <summary>
9+
/// Returns a new array containing only the specified object.
10+
/// </summary>
11+
/// <typeparam name="T">
12+
/// The type of the element.
13+
/// </typeparam>
14+
/// <param name="element">
15+
/// An element that the array contains.
16+
/// </param>
17+
/// <returns>
18+
/// A new array containing only the specified object.
19+
/// </returns>
20+
public static T[] Singleton<T>(T element)
21+
{
22+
return new T[] { element };
23+
}
24+
}
25+
}

StyleChecker/StyleChecker.Test/Helpers/CodeChange.cs renamed to StyleChecker/StyleChecker.Test/Framework/CodeChange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TestHelper
1+
namespace StyleChecker.Test.Framework
22
{
33
/// <summary>
44
/// Represents two codes.

StyleChecker/StyleChecker.Test/Verifiers/CodeFixVerifier.cs renamed to StyleChecker/StyleChecker.Test/Framework/CodeFixVerifier.cs

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
namespace TestHelper
1+
namespace StyleChecker.Test.Framework
22
{
33
using System.Collections.Generic;
44
using System.Linq;
55
using Microsoft.CodeAnalysis;
66
using Microsoft.CodeAnalysis.CodeFixes;
77
using Microsoft.CodeAnalysis.Formatting;
8+
using Microsoft.CodeAnalysis.Simplification;
89
using Microsoft.VisualStudio.TestTools.UnitTesting;
910

1011
/// <summary>
1112
/// Superclass of all Unit tests made for diagnostics with CodeFixes.
1213
/// Contains methods used to verify correctness of CodeFixes.
1314
/// </summary>
14-
public abstract partial class CodeFixVerifier : DiagnosticVerifier
15+
public abstract class CodeFixVerifier : DiagnosticVerifier
1516
{
1617
/// <summary>
1718
/// Gets the CodeFix being tested (C#) - to be implemented in
@@ -20,24 +21,7 @@ public abstract partial class CodeFixVerifier : DiagnosticVerifier
2021
/// <returns>
2122
/// The CodeFixProvider to be used for CSharp code.
2223
/// </returns>
23-
protected virtual CodeFixProvider CSharpCodeFixProvider => null;
24-
25-
/// <summary>
26-
/// Returns a new array of <c>DiagnosticResultLocation</c> containing
27-
/// the single element representing the specified line and column.
28-
/// </summary>
29-
/// <param name="line">The line.</param>
30-
/// <param name="column">The column.</param>
31-
/// <returns>
32-
/// A new array of <c>DiagnosticResultLocation</c> containing
33-
/// the single element representing the specified line and column.
34-
/// </returns>
35-
protected static DiagnosticResultLocation[] SingleLocation(
36-
int line, int column)
37-
{
38-
return Singleton(
39-
new DiagnosticResultLocation("Test0.cs", line, column));
40-
}
24+
protected abstract CodeFixProvider CodeFixProvider { get; }
4125

4226
/// <summary>
4327
/// Creates a new <c>CodeChange</c> from entire texts representing for
@@ -72,62 +56,64 @@ protected CodeChange ReadCodeChange(string name)
7256
/// it.
7357
/// </param>
7458
/// <param name="allowNewCompilerDiagnostics">
75-
/// A bool controlling whether or not the test will fail if the CodeFix
76-
/// introduces other warnings after being applied.
59+
/// A <c>bool</c> controlling whether or not the test will fail if the
60+
/// CodeFix introduces other warnings after being applied.
7761
/// </param>
78-
protected void VerifyCSharpFix(
62+
protected void VerifyFix(
7963
string oldSource,
8064
string newSource,
8165
bool allowNewCompilerDiagnostics = false)
8266
{
83-
var codeChanges = Singleton(new CodeChange(oldSource, newSource));
67+
var codeChanges = Arrays.Singleton(
68+
new CodeChange(oldSource, newSource));
8469
VerifyFix(codeChanges, allowNewCompilerDiagnostics);
8570
}
8671

8772
/// <summary>
88-
/// Verifies the result of <c>CodeFix</c>es. Creates a <c>Document</c>s
73+
/// Verifies the result of CodeFixes. Creates a <c>Document</c>s
8974
/// from the <c>CodeChange</c>s, then gets diagnostics on it and
90-
/// applies the relevant <c>CodeFix</c>es. Then gets the string after
91-
/// the <c>CodeFix</c> is applied and compares it with the expected
92-
/// result. Note: If any <c>CodeFix</c> causes new diagnostics to show
75+
/// applies the relevant CodeFixes. Then gets the string after
76+
/// the CodeFix is applied and compares it with the expected
77+
/// result. Note: If any CodeFix causes new diagnostics to show
9378
/// up, the test fails unless
9479
/// <paramref name="allowNewCompilerDiagnostics"/> is set to true.
9580
/// </summary>
9681
/// <param name="codeChanges">
97-
/// The sources in the form of a string before/after the <c>CodeFix</c>
82+
/// The sources in the form of a string before/after the CodeFix
9883
/// was applied to it.
9984
/// </param>
10085
/// <param name="allowNewCompilerDiagnostics">
101-
/// A bool controlling whether or not the test will fail if the
102-
/// <c>CodeFix</c> introduces other warnings after being applied.
86+
/// A <c>bool</c> controlling whether or not the test will fail if the
87+
/// CodeFix introduces other warnings after being applied.
10388
/// </param>
10489
protected void VerifyFix(
10590
IEnumerable<CodeChange> codeChanges,
10691
bool allowNewCompilerDiagnostics = false)
10792
{
108-
var analyzer = CSharpDiagnosticAnalyzer;
109-
var codeFixProvider = CSharpCodeFixProvider;
110-
var verifier = new Verifier(analyzer, codeFixProvider);
93+
var analyzer = DiagnosticAnalyzer;
94+
var codeFixProvider = CodeFixProvider;
95+
var applier = new FixApplier(analyzer, codeFixProvider);
11196

11297
var codeChangeArray = codeChanges.ToArray();
11398
var expectedMap = new Dictionary<DocumentId, string>();
114-
var project = CreateProject(
99+
var project = Projects.Of(
115100
codeChanges,
116101
(id, c) => expectedMap[id] = c.After);
117102
var documents = project.Documents.ToArray();
118-
var firstVerifyContext = verifier.AnalyzeDocuments(documents);
119-
var maxTryCount = firstVerifyContext.AnalyzerDiagnostics.Length;
103+
var firstApplierContext = applier.Analyze(documents);
104+
var maxTryCount = firstApplierContext.AnalyzerDiagnostics.Length;
120105
Assert.IsTrue(maxTryCount > 0);
121106

122-
var verifyContext = firstVerifyContext;
123-
var newDocumentMap = verifier.ModifyDocuments(verifyContext);
107+
var verifyContext = firstApplierContext;
108+
var newDocumentMap = applier.Modify(verifyContext);
124109
for (var k = 0; k < maxTryCount; ++k)
125110
{
126111
var newDocuments = newDocumentMap.Values.ToArray();
127-
var newVerifyContext = verifier.AnalyzeDocuments(newDocuments);
128-
var newCompilerDiagnostics = newVerifyContext.CompilerDiagnostics;
129-
var diagnosticsDelta = GetNewDiagnostics(
130-
firstVerifyContext.CompilerDiagnostics,
112+
var newApplierContext = applier.Analyze(newDocuments);
113+
var newCompilerDiagnostics
114+
= newApplierContext.CompilerDiagnostics;
115+
var diagnosticsDelta = Diagnostics.GetNewDelta(
116+
firstApplierContext.CompilerDiagnostics,
131117
newCompilerDiagnostics);
132118

133119
// Checks if applying the code fix introduced any new compiler
@@ -138,44 +124,45 @@ protected void VerifyFix(
138124
newDocuments, newCompilerDiagnostics);
139125
}
140126

141-
verifyContext = newVerifyContext;
127+
verifyContext = newApplierContext;
142128
if (!verifyContext.AnalyzerDiagnostics.Any())
143129
{
144130
break;
145131
}
146-
newDocumentMap = verifier.ModifyDocuments(verifyContext);
132+
newDocumentMap = applier.Modify(verifyContext);
147133
}
148134
Assert.IsTrue(!verifyContext.AnalyzerDiagnostics.Any());
149135

150136
foreach (var id in project.DocumentIds)
151137
{
152-
var actual = GetStringFromDocument(newDocumentMap[id]);
138+
var actual = ToString(newDocumentMap[id]);
153139
var expected = expectedMap[id];
154140
Compare(id, actual, expected);
155141
}
156142
}
157143

158-
private void FailFixIntroducedNewCompilerDiagnostics(
159-
Document[] newDocuments, Diagnostic[] compilerDiagnostics)
144+
private static void FailFixIntroducedNewCompilerDiagnostics(
145+
IEnumerable<Document> newDocuments,
146+
IEnumerable<Diagnostic> compilerDiagnostics)
160147
{
161148
// Format and get the compiler diagnostics again so that
162149
// the locations make sense in the output.
163-
var formatteDocuments = newDocuments
150+
var formattedDocuments = newDocuments
164151
.Select(d => d.WithSyntaxRoot(Formatter.Format(
165152
d.GetSyntaxRootAsync().Result,
166153
Formatter.Annotation,
167154
d.Project.Solution.Workspace)))
168155
.ToArray();
169-
var newCompilerDiagnostics = formatteDocuments
170-
.SelectMany(d => Verifier.GetCompilerDiagnostics(d))
156+
var newCompilerDiagnostics = formattedDocuments
157+
.SelectMany(d => Documents.GetCompilerDiagnostics(d))
171158
.ToArray();
172-
var diagnosticsDelta = GetNewDiagnostics(
159+
var diagnosticsDelta = Diagnostics.GetNewDelta(
173160
compilerDiagnostics, newCompilerDiagnostics);
174161

175162
var diagnosticMessages = string.Join(
176163
"\r\n",
177164
diagnosticsDelta.Select(d => d.ToString()));
178-
var soucres = formatteDocuments
165+
var soucres = formattedDocuments
179166
.Select(d => d.GetSyntaxRootAsync().Result.ToFullString());
180167
Assert.Fail(
181168
"Fix introduced new compiler diagnostics:\r\n"
@@ -192,13 +179,14 @@ private void FailFixIntroducedNewCompilerDiagnostics(
192179
/// The <c>DocumentId</c> of the source to compare.
193180
/// </param>
194181
/// <param name="actual">
195-
/// The actual source that the <c>CodeFix</c> provider provides.
182+
/// The actual source that the CodeFix provider provides.
196183
/// </param>
197184
/// <param name="expected">
198-
/// The expected source that the <c>CodeFix</c> provider is supposed to
185+
/// The expected source that the CodeFix provider is supposed to
199186
/// provide.
200187
/// </param>
201-
private void Compare(DocumentId id, string actual, string expected)
188+
private static void Compare(
189+
DocumentId id, string actual, string expected)
202190
{
203191
var actualArray = actual.Split("\r\n");
204192
var expectedArray = expected.Split("\r\n");
@@ -215,5 +203,27 @@ private void Compare(DocumentId id, string actual, string expected)
215203
}
216204
Assert.AreEqual(expected, actual);
217205
}
206+
207+
/// <summary>
208+
/// Returns the string representing the specified document
209+
/// based on the syntax root.
210+
/// </summary>
211+
/// <param name="document">
212+
/// The <c>>Document</c> to be converted to a string.
213+
/// </param>
214+
/// <returns>
215+
/// A string containing the syntax of the Document after formatting.
216+
/// </returns>
217+
private static string ToString(Document document)
218+
{
219+
var simplifiedDoc = Simplifier.ReduceAsync(
220+
document, Simplifier.Annotation).Result;
221+
var root = simplifiedDoc.GetSyntaxRootAsync().Result;
222+
root = Formatter.Format(
223+
root,
224+
Formatter.Annotation,
225+
simplifiedDoc.Project.Solution.Workspace);
226+
return root.GetText().ToString();
227+
}
218228
}
219229
}

StyleChecker/StyleChecker.Test/Helpers/CompilationException.cs renamed to StyleChecker/StyleChecker.Test/Framework/CompilationException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TestHelper
1+
namespace StyleChecker.Test.Framework
22
{
33
using System;
44
using System.Collections.Immutable;

StyleChecker/StyleChecker.Test/Helpers/ConfigText.cs renamed to StyleChecker/StyleChecker.Test/Framework/ConfigText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TestHelper
1+
namespace StyleChecker.Test.Framework
22
{
33
using System.Collections.Immutable;
44
using System.Threading;

StyleChecker/StyleChecker.Test/Helpers/DiagnosticResult.cs renamed to StyleChecker/StyleChecker.Test/Framework/DiagnosticResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TestHelper
1+
namespace StyleChecker.Test.Framework
22
{
33
using System;
44
using Microsoft.CodeAnalysis;

StyleChecker/StyleChecker.Test/Helpers/DiagnosticResultLocation.cs renamed to StyleChecker/StyleChecker.Test/Framework/DiagnosticResultLocation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TestHelper
1+
namespace StyleChecker.Test.Framework
22
{
33
using System;
44

0 commit comments

Comments
 (0)