Skip to content

Commit 7260234

Browse files
committed
format mstest tests
1 parent ac9c2c9 commit 7260234

File tree

3 files changed

+116
-10
lines changed

3 files changed

+116
-10
lines changed

docs/MsTestAnalyzer.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
55
# MsTest Analyzer Docs
66

77
- [BooleanAssertIsTrue](#scenario-booleanassertistrue) - `flag.Should().BeTrue();`
8+
- [BooleanAssertIsFalse](#scenario-booleanassertisfalse) - `flag.Should().BeFalse();`
89

910

1011
## Scenarios
@@ -25,14 +26,38 @@ flag.Should().BeTrue();
2526
#### Failure messages
2627

2728
```cs
28-
// arrange
2929
var flag = false;
3030

3131
// old assertion:
3232
Assert.IsTrue(flag); // fail message: Assert.IsTrue failed.
3333
3434
// new assertion:
35-
flag.Should().BeTrue(); // fail message: Assert.IsTrue failed.
35+
flag.Should().BeTrue(); // fail message: Expected flag to be true, but found False.
36+
```
37+
38+
### scenario: BooleanAssertIsFalse
39+
40+
```cs
41+
// arrange
42+
var flag = false;
43+
44+
// old assertion:
45+
Assert.IsFalse(flag);
46+
47+
// new assertion:
48+
flag.Should().BeFalse();
49+
```
50+
51+
#### Failure messages
52+
53+
```cs
54+
var flag = true;
55+
56+
// old assertion:
57+
Assert.IsFalse(flag); // fail message: Assert.IsFalse failed.
58+
59+
// new assertion:
60+
flag.Should().BeFalse(); // fail message: Expected flag to be false, but found True.
3661
```
3762

3863

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,56 @@ public void BooleanAssertIsTrue()
2424
}
2525

2626
[TestMethod, ExpectedException(typeof(AssertFailedException))]
27-
public void BooleanAssertIsTrue_Failure()
27+
public void BooleanAssertIsTrue_Failure_OldAssertion()
2828
{
29-
using var scope = new AssertionScope();
3029
// arrange
3130
var flag = false;
3231

3332
// old assertion:
3433
Assert.IsTrue(flag);
34+
}
35+
36+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
37+
public void BooleanAssertIsTrue_Failure_NewAssertion()
38+
{
39+
// arrange
40+
var flag = false;
3541

3642
// new assertion:
3743
flag.Should().BeTrue();
3844
}
45+
46+
[TestMethod]
47+
public void BooleanAssertIsFalse()
48+
{
49+
// arrange
50+
var flag = false;
51+
52+
// old assertion:
53+
Assert.IsFalse(flag);
54+
55+
// new assertion:
56+
flag.Should().BeFalse();
57+
}
58+
59+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
60+
public void BooleanAssertIsFalse_Failure_OldAssertion()
61+
{
62+
// arrange
63+
var flag = true;
64+
65+
// old assertion:
66+
Assert.IsFalse(flag);
67+
}
68+
69+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
70+
public void BooleanAssertIsFalse_Failure_NewAssertion()
71+
{
72+
// arrange
73+
var flag = true;
74+
75+
// new assertion:
76+
flag.Should().BeFalse();
77+
}
78+
3979
}

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator/DocsGenerator.cs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task Execute()
3333
var docs = new StringBuilder();
3434
var toc = new StringBuilder();
3535
var scenarios = new StringBuilder();
36-
36+
3737
docs.AppendLine("<!--");
3838
docs.AppendLine("This is a generated file, please edit src\\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator\\DocsGenerator.cs to change the contents");
3939
docs.AppendLine("-->");
@@ -42,19 +42,21 @@ public async Task Execute()
4242
var subject = Path.GetFileNameWithoutExtension(tree.FilePath).Replace("AnalyzerTests", string.Empty);
4343
docs.AppendLine($"# {subject} Analyzer Docs");
4444
docs.AppendLine();
45-
45+
4646
scenarios.AppendLine("## Scenarios");
4747
scenarios.AppendLine();
4848

4949
var root = await tree.GetRootAsync();
5050
var classDef = root.DescendantNodes().OfType<ClassDeclarationSyntax>().First();
5151
var methods = root.DescendantNodes().OfType<MethodDeclarationSyntax>();
52+
var methodsMap = methods.ToDictionary(m => m.Identifier.Text);
5253

5354
var classType = testAssembly.GetType($"FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.{classDef.Identifier.Text}");
5455
var classInstance = Activator.CreateInstance(classType);
5556

5657
foreach (var method in methods.Where(m => m.AttributeLists.Any(list => list.Attributes.Count is 1 && list.Attributes[0].Name.ToString() is "TestMethod")))
5758
{
59+
// success scenario:
5860
{
5961
scenarios.AppendLine($"### scenario: {method.Identifier}");
6062
scenarios.AppendLine();
@@ -69,8 +71,10 @@ public async Task Execute()
6971

7072
toc.AppendLine($"- [{method.Identifier}](#scenario-{method.Identifier.Text.ToLower()}) - `{newAssertion}`");
7173
}
74+
75+
// FluentAssertion failures scenario:
76+
if (methodsMap.TryGetValue($"{method.Identifier.Text}_Failure", out var testWithFailure))
7277
{
73-
var testWithFailure = methods.FirstOrDefault(m => m.Identifier.Text == $"{method.Identifier.Text}_Failure");
7478
var testMethodWithFailure = classType.GetMethod(testWithFailure.Identifier.Text);
7579

7680
var exceptionMessageLines = GetMethodExceptionMessageLines(classInstance, testMethodWithFailure);
@@ -90,8 +94,6 @@ public async Task Execute()
9094
var arrange = bodyLines.TakeWhile(x => !string.IsNullOrEmpty(x))
9195
.Select(l => l.Length > paddingToRemove ? l.Substring(paddingToRemove) : l).Aggregate((a, b) => $"{a}{Environment.NewLine}{b}");
9296

93-
var methodBody = $"```cs{Environment.NewLine}{arrange}{Environment.NewLine}```";
94-
9597
scenarios.AppendLine($"#### Failure messages");
9698
scenarios.AppendLine();
9799
scenarios.AppendLine("```cs");
@@ -108,6 +110,43 @@ public async Task Execute()
108110
scenarios.AppendLine("```");
109111
scenarios.AppendLine();
110112
}
113+
114+
// Testing Libraries failures scenarios:
115+
if (methodsMap.TryGetValue($"{method.Identifier.Text}_Failure_OldAssertion", out var testWithFailureOldAssertion)
116+
&& methodsMap.TryGetValue($"{method.Identifier.Text}_Failure_NewAssertion", out var testWithFailureNewAssertion))
117+
{
118+
var testMethodWithFailureOldAssertion = classType.GetMethod(testWithFailureOldAssertion.Identifier.Text);
119+
var testMethodWithFailureNewAssertion = classType.GetMethod(testWithFailureNewAssertion.Identifier.Text);
120+
121+
var exceptionMessageLinesOldAssertion = GetMethodExceptionMessage(classInstance, testMethodWithFailureOldAssertion);
122+
var exceptionMessageLinesNewAssertion = GetMethodExceptionMessage(classInstance, testMethodWithFailureNewAssertion);
123+
124+
var oldAssertionComment = testWithFailureOldAssertion.DescendantTrivia().First(x => x.IsKind(SyntaxKind.SingleLineCommentTrivia) && x.ToString().Equals("// old assertion:"));
125+
var newAssertionComment = testWithFailureNewAssertion.DescendantTrivia().First(x => x.IsKind(SyntaxKind.SingleLineCommentTrivia) && x.ToString().Equals("// new assertion:"));
126+
127+
var bodyLines = testWithFailureNewAssertion.Body.ToFullString().Split(Environment.NewLine)[2..^2];
128+
var paddingToRemove = bodyLines[0].IndexOf(bodyLines[0].TrimStart());
129+
130+
var oldAssertion = testWithFailureOldAssertion.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(oldAssertionComment.Span) > 0).ToString().TrimStart() + " \t// fail message: " + exceptionMessageLinesOldAssertion;
131+
var newAssertion = testWithFailureNewAssertion.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(newAssertionComment.Span) > 0).ToString().TrimStart() + " \t// fail message: " + exceptionMessageLinesNewAssertion;
132+
133+
var arrange = bodyLines.TakeWhile(x => !string.IsNullOrEmpty(x))
134+
.Select(l => l.Length > paddingToRemove ? l.Substring(paddingToRemove) : l).Aggregate((a, b) => $"{a}{Environment.NewLine}{b}");
135+
136+
scenarios.AppendLine($"#### Failure messages");
137+
scenarios.AppendLine();
138+
scenarios.AppendLine("```cs");
139+
scenarios.AppendLine(arrange);
140+
scenarios.AppendLine();
141+
scenarios.AppendLine($"// old assertion:");
142+
scenarios.AppendLine(oldAssertion);
143+
scenarios.AppendLine();
144+
scenarios.AppendLine($"// new assertion:");
145+
scenarios.AppendLine(newAssertion);
146+
scenarios.AppendLine("```");
147+
scenarios.AppendLine();
148+
}
149+
111150
}
112151

113152
var diagnostics = await compilationWithAnalyzers.GetAllDiagnosticsAsync();
@@ -128,14 +167,16 @@ public async Task Execute()
128167
}
129168

130169
private string[] GetMethodExceptionMessageLines(object instance, MethodInfo method)
170+
=> GetMethodExceptionMessage(instance, method).Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
171+
private string GetMethodExceptionMessage(object instance, MethodInfo method)
131172
{
132173
try
133174
{
134175
method.Invoke(instance, null);
135176
}
136177
catch (Exception ex) when (ex.InnerException is AssertFailedException exception)
137178
{
138-
return exception.Message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
179+
return exception.Message;
139180
}
140181

141182
throw new InvalidOperationException("Method did not throw an exception");

0 commit comments

Comments
 (0)