@@ -33,7 +33,7 @@ public async Task Execute()
33
33
var docs = new StringBuilder ( ) ;
34
34
var toc = new StringBuilder ( ) ;
35
35
var scenarios = new StringBuilder ( ) ;
36
-
36
+
37
37
docs . AppendLine ( "<!--" ) ;
38
38
docs . AppendLine ( "This is a generated file, please edit src\\ FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator\\ DocsGenerator.cs to change the contents" ) ;
39
39
docs . AppendLine ( "-->" ) ;
@@ -42,19 +42,21 @@ public async Task Execute()
42
42
var subject = Path . GetFileNameWithoutExtension ( tree . FilePath ) . Replace ( "AnalyzerTests" , string . Empty ) ;
43
43
docs . AppendLine ( $ "# { subject } Analyzer Docs") ;
44
44
docs . AppendLine ( ) ;
45
-
45
+
46
46
scenarios . AppendLine ( "## Scenarios" ) ;
47
47
scenarios . AppendLine ( ) ;
48
48
49
49
var root = await tree . GetRootAsync ( ) ;
50
50
var classDef = root . DescendantNodes ( ) . OfType < ClassDeclarationSyntax > ( ) . First ( ) ;
51
51
var methods = root . DescendantNodes ( ) . OfType < MethodDeclarationSyntax > ( ) ;
52
+ var methodsMap = methods . ToDictionary ( m => m . Identifier . Text ) ;
52
53
53
54
var classType = testAssembly . GetType ( $ "FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.{ classDef . Identifier . Text } ") ;
54
55
var classInstance = Activator . CreateInstance ( classType ) ;
55
56
56
57
foreach ( var method in methods . Where ( m => m . AttributeLists . Any ( list => list . Attributes . Count is 1 && list . Attributes [ 0 ] . Name . ToString ( ) is "TestMethod" ) ) )
57
58
{
59
+ // success scenario:
58
60
{
59
61
scenarios . AppendLine ( $ "### scenario: { method . Identifier } ") ;
60
62
scenarios . AppendLine ( ) ;
@@ -69,8 +71,10 @@ public async Task Execute()
69
71
70
72
toc . AppendLine ( $ "- [{ method . Identifier } ](#scenario-{ method . Identifier . Text . ToLower ( ) } ) - `{ newAssertion } `") ;
71
73
}
74
+
75
+ // FluentAssertion failures scenario:
76
+ if ( methodsMap . TryGetValue ( $ "{ method . Identifier . Text } _Failure", out var testWithFailure ) )
72
77
{
73
- var testWithFailure = methods . FirstOrDefault ( m => m . Identifier . Text == $ "{ method . Identifier . Text } _Failure") ;
74
78
var testMethodWithFailure = classType . GetMethod ( testWithFailure . Identifier . Text ) ;
75
79
76
80
var exceptionMessageLines = GetMethodExceptionMessageLines ( classInstance , testMethodWithFailure ) ;
@@ -90,8 +94,6 @@ public async Task Execute()
90
94
var arrange = bodyLines . TakeWhile ( x => ! string . IsNullOrEmpty ( x ) )
91
95
. Select ( l => l . Length > paddingToRemove ? l . Substring ( paddingToRemove ) : l ) . Aggregate ( ( a , b ) => $ "{ a } { Environment . NewLine } { b } ") ;
92
96
93
- var methodBody = $ "```cs{ Environment . NewLine } { arrange } { Environment . NewLine } ```";
94
-
95
97
scenarios . AppendLine ( $ "#### Failure messages") ;
96
98
scenarios . AppendLine ( ) ;
97
99
scenarios . AppendLine ( "```cs" ) ;
@@ -108,6 +110,43 @@ public async Task Execute()
108
110
scenarios . AppendLine ( "```" ) ;
109
111
scenarios . AppendLine ( ) ;
110
112
}
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
+
111
150
}
112
151
113
152
var diagnostics = await compilationWithAnalyzers . GetAllDiagnosticsAsync ( ) ;
@@ -128,14 +167,16 @@ public async Task Execute()
128
167
}
129
168
130
169
private string [ ] GetMethodExceptionMessageLines ( object instance , MethodInfo method )
170
+ => GetMethodExceptionMessage ( instance , method ) . Split ( Environment . NewLine , StringSplitOptions . RemoveEmptyEntries ) ;
171
+ private string GetMethodExceptionMessage ( object instance , MethodInfo method )
131
172
{
132
173
try
133
174
{
134
175
method . Invoke ( instance , null ) ;
135
176
}
136
177
catch ( Exception ex ) when ( ex . InnerException is AssertFailedException exception )
137
178
{
138
- return exception . Message . Split ( Environment . NewLine , StringSplitOptions . RemoveEmptyEntries ) ;
179
+ return exception . Message ;
139
180
}
140
181
141
182
throw new InvalidOperationException ( "Method did not throw an exception" ) ;
0 commit comments