Skip to content

Commit fabec72

Browse files
committed
docs: add mstest docs
1 parent 6479db1 commit fabec72

File tree

4 files changed

+105
-25
lines changed

4 files changed

+105
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dotnet add package FluentAssertions.Analyzers
3131
## Docs
3232

3333
- [FluentAssertions Analyzer Docs](docs/FluentAssertionsAnalyzer.md)
34+
- [MsTest Analyzer Docs](docs/MsTestAnalyzer.md)
3435

3536
## Getting Started
3637

docs/MsTestAnalyzer.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!--
2+
This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator\DocsGenerator.cs to change the contents
3+
-->
4+
5+
# MsTest Analyzer Docs
6+
7+
- [BooleanAssertIsTrue](#scenario-booleanassertistrue) - `flag.Should().BeTrue();`
8+
9+
10+
## Scenarios
11+
12+
### scenario: BooleanAssertIsTrue
13+
14+
```cs
15+
// arrange
16+
var flag = true;
17+
18+
// old assertion:
19+
Assert.IsTrue(flag);
20+
21+
// new assertion:
22+
flag.Should().BeTrue();
23+
```
24+
25+
#### Failure messages
26+
27+
```cs
28+
// arrange
29+
var flag = false;
30+
31+
// old assertion:
32+
Assert.IsTrue(flag); // fail message: Assert.IsTrue failed.
33+
34+
// new assertion:
35+
flag.Should().BeTrue(); // fail message: Assert.IsTrue failed.
36+
```
37+
38+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentAssertions;
5+
using FluentAssertions.Execution;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs;
9+
10+
[TestClass]
11+
public class MsTestAnalyzerTests
12+
{
13+
[TestMethod]
14+
public void BooleanAssertIsTrue()
15+
{
16+
// arrange
17+
var flag = true;
18+
19+
// old assertion:
20+
Assert.IsTrue(flag);
21+
22+
// new assertion:
23+
flag.Should().BeTrue();
24+
}
25+
26+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
27+
public void BooleanAssertIsTrue_Failure()
28+
{
29+
using var scope = new AssertionScope();
30+
// arrange
31+
var flag = false;
32+
33+
// old assertion:
34+
Assert.IsTrue(flag);
35+
36+
// new assertion:
37+
flag.Should().BeTrue();
38+
}
39+
}

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator/DocsGenerator.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,29 @@ public async Task Execute()
2222
var compilation = await FluentAssertionAnalyzerDocsUtils.GetFluentAssertionAnalyzerDocsCompilation();
2323
var compilationWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzer));
2424

25-
var docs = new StringBuilder();
26-
var toc = new StringBuilder();
27-
var scenarios = new StringBuilder();
28-
29-
docs.AppendLine("<!--");
30-
docs.AppendLine("This is a generated file, please edit src\\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator\\DocsGenerator.cs to change the contents");
31-
docs.AppendLine("-->");
32-
docs.AppendLine();
33-
34-
docs.AppendLine("# FluentAssertions Analyzer Docs");
35-
docs.AppendLine();
36-
37-
scenarios.AppendLine("## Scenarios");
38-
scenarios.AppendLine();
39-
40-
var testAssembly = typeof(FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.FluentAssertionsAnalyzerTests).Assembly;
25+
var testAssembly = typeof(FluentAssertionAnalyzerDocs.FluentAssertionsAnalyzerTests).Assembly;
4126

4227
foreach (var tree in compilationWithAnalyzers.Compilation.SyntaxTrees.Where(t => t.FilePath.EndsWith("Tests.cs")))
4328
{
4429
Console.WriteLine($"File: {Path.GetFileName(tree.FilePath)}");
4530

31+
var docsName = Path.GetFileNameWithoutExtension(tree.FilePath).Replace("Tests", ".md");
32+
33+
var docs = new StringBuilder();
34+
var toc = new StringBuilder();
35+
var scenarios = new StringBuilder();
36+
37+
docs.AppendLine("<!--");
38+
docs.AppendLine("This is a generated file, please edit src\\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator\\DocsGenerator.cs to change the contents");
39+
docs.AppendLine("-->");
40+
docs.AppendLine();
41+
42+
var subject = Path.GetFileNameWithoutExtension(tree.FilePath).Replace("AnalyzerTests", string.Empty);
43+
docs.AppendLine($"# {subject} Analyzer Docs");
44+
docs.AppendLine();
45+
46+
scenarios.AppendLine("## Scenarios");
47+
scenarios.AppendLine();
4648

4749
var root = await tree.GetRootAsync();
4850
var classDef = root.DescendantNodes().OfType<ClassDeclarationSyntax>().First();
@@ -114,22 +116,22 @@ public async Task Execute()
114116
Console.WriteLine($"source: {root.FindNode(diagnostic.Location.SourceSpan)}");
115117
Console.WriteLine($" diagnostic: {diagnostic}");
116118
}
117-
}
118119

119-
docs.AppendLine(toc.ToString());
120-
docs.AppendLine();
121-
docs.AppendLine(scenarios.ToString());
120+
docs.AppendLine(toc.ToString());
121+
docs.AppendLine();
122+
docs.AppendLine(scenarios.ToString());
122123

123-
var docsPath = Path.Combine(Environment.CurrentDirectory, "..", "..", "docs", "FluentAssertionsAnalyzer.md");
124-
Directory.CreateDirectory(Path.GetDirectoryName(docsPath));
125-
await File.WriteAllTextAsync(docsPath, docs.ToString());
124+
var docsPath = Path.Combine(Environment.CurrentDirectory, "..", "..", "docs", docsName);
125+
Directory.CreateDirectory(Path.GetDirectoryName(docsPath));
126+
await File.WriteAllTextAsync(docsPath, docs.ToString());
127+
}
126128
}
127129

128-
private string[] GetMethodExceptionMessageLines(object instnace, MethodInfo method)
130+
private string[] GetMethodExceptionMessageLines(object instance, MethodInfo method)
129131
{
130132
try
131133
{
132-
method.Invoke(instnace, null);
134+
method.Invoke(instance, null);
133135
}
134136
catch (Exception ex) when (ex.InnerException is AssertFailedException exception)
135137
{

0 commit comments

Comments
 (0)