Skip to content

Commit 4fafffc

Browse files
committed
docs: mstest support for IsNull, IsNotNull, IsInstanceOfType, IsNotInstanceOfType
1 parent aa84d3a commit 4fafffc

File tree

3 files changed

+273
-8
lines changed

3 files changed

+273
-8
lines changed

docs/MsTestAnalyzer.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
66

77
- [BooleanAssertIsTrue](#scenario-booleanassertistrue) - `flag.Should().BeTrue();`
88
- [BooleanAssertIsFalse](#scenario-booleanassertisfalse) - `flag.Should().BeFalse();`
9+
- [ObjectAssertIsNull](#scenario-objectassertisnull) - `obj.Should().BeNull();`
10+
- [ObjectAssertIsNotNull](#scenario-objectassertisnotnull) - `obj.Should().NotBeNull();`
11+
- [ReferenceTypeAssertIsInstanceOfType](#scenario-referencetypeassertisinstanceoftype) - `obj.Should().BeOfType<List<object>>();`
12+
- [ReferenceTypeAssertIsNotInstanceOfType](#scenario-referencetypeassertisnotinstanceoftype) - `obj.Should().NotBeOfType<List<object>>();`
913

1014

1115
## Scenarios
@@ -60,4 +64,108 @@ Assert.IsFalse(flag); // fail message: Assert.IsFalse failed.
6064
flag.Should().BeFalse(); // fail message: Expected flag to be false, but found True.
6165
```
6266

67+
### scenario: ObjectAssertIsNull
68+
69+
```cs
70+
// arrange
71+
object obj = null;
72+
73+
// old assertion:
74+
Assert.IsNull(obj);
75+
76+
// new assertion:
77+
obj.Should().BeNull();
78+
```
79+
80+
#### Failure messages
81+
82+
```cs
83+
var obj = new object();
84+
85+
// old assertion:
86+
Assert.IsNull(obj); // fail message: Assert.IsNull failed.
87+
88+
// new assertion:
89+
obj.Should().BeNull(); // fail message: Expected obj to be <null>, but found System.Object (HashCode=53299218).
90+
```
91+
92+
### scenario: ObjectAssertIsNotNull
93+
94+
```cs
95+
// arrange
96+
var obj = new object();
97+
98+
// old assertion:
99+
Assert.IsNotNull(obj);
100+
101+
// new assertion:
102+
obj.Should().NotBeNull();
103+
```
104+
105+
#### Failure messages
106+
107+
```cs
108+
object obj = null;
109+
110+
// old assertion:
111+
Assert.IsNotNull(obj); // fail message: Assert.IsNotNull failed.
112+
113+
// new assertion:
114+
obj.Should().NotBeNull(); // fail message: Expected obj not to be <null>.
115+
```
116+
117+
### scenario: ReferenceTypeAssertIsInstanceOfType
118+
119+
```cs
120+
// arrange
121+
var obj = new List<object>();
122+
123+
// old assertion:
124+
Assert.IsInstanceOfType(obj, typeof(List<object>));
125+
Assert.IsInstanceOfType<List<object>>(obj);
126+
127+
// new assertion:
128+
obj.Should().BeOfType<List<object>>();
129+
```
130+
131+
#### Failure messages
132+
133+
```cs
134+
var obj = new List<int>();
135+
136+
// old assertion:
137+
Assert.IsInstanceOfType(obj, typeof(List<object>)); // fail message: Assert.IsInstanceOfType failed. Expected type:<System.Collections.Generic.List`1[System.Object]>. Actual type:<System.Collections.Generic.List`1[System.Int32]>.
138+
Assert.IsInstanceOfType<List<object>>(obj); // fail message: Assert.IsInstanceOfType failed. Expected type:<System.Collections.Generic.List`1[System.Object]>. Actual type:<System.Collections.Generic.List`1[System.Int32]>.
139+
140+
// new assertion:
141+
obj.Should().BeOfType<List<object>>(); // fail message: Expected type to be System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], but found System.Collections.Generic.List`1[[System.Int32, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].
142+
```
143+
144+
### scenario: ReferenceTypeAssertIsNotInstanceOfType
145+
146+
```cs
147+
// arrange
148+
var obj = new List<int>();
149+
150+
// old assertion:
151+
Assert.IsNotInstanceOfType(obj, typeof(List<object>));
152+
Assert.IsNotInstanceOfType<List<object>>(obj);
153+
154+
// new assertion:
155+
obj.Should().NotBeOfType<List<object>>();
156+
```
157+
158+
#### Failure messages
159+
160+
```cs
161+
var obj = new List<object>();
162+
163+
// old assertion:
164+
Assert.IsNotInstanceOfType(obj, typeof(List<object>)); // fail message: Assert.IsNotInstanceOfType failed. Wrong Type:<System.Collections.Generic.List`1[System.Object]>. Actual type:<System.Collections.Generic.List`1[System.Object]>.
165+
Assert.IsNotInstanceOfType<List<object>>(obj); // fail message: Assert.IsNotInstanceOfType failed. Wrong Type:<System.Collections.Generic.List`1[System.Object]>. Actual type:<System.Collections.Generic.List`1[System.Object]>.
166+
167+
// new assertion:
168+
obj.Should().NotBeOfType<List<object>>(); // fail message: Expected type not to be [System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e], but it is.
169+
```
170+
63171

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,158 @@ public void BooleanAssertIsFalse_Failure_NewAssertion()
7575
// new assertion:
7676
flag.Should().BeFalse();
7777
}
78-
78+
79+
[TestMethod]
80+
public void ObjectAssertIsNull()
81+
{
82+
// arrange
83+
object obj = null;
84+
85+
// old assertion:
86+
Assert.IsNull(obj);
87+
88+
// new assertion:
89+
obj.Should().BeNull();
90+
}
91+
92+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
93+
public void ObjectAssertIsNull_Failure_OldAssertion()
94+
{
95+
// arrange
96+
var obj = new object();
97+
98+
// old assertion:
99+
Assert.IsNull(obj);
100+
}
101+
102+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
103+
public void ObjectAssertIsNull_Failure_NewAssertion()
104+
{
105+
// arrange
106+
var obj = new object();
107+
108+
// new assertion:
109+
obj.Should().BeNull();
110+
}
111+
112+
[TestMethod]
113+
public void ObjectAssertIsNotNull()
114+
{
115+
// arrange
116+
var obj = new object();
117+
118+
// old assertion:
119+
Assert.IsNotNull(obj);
120+
121+
// new assertion:
122+
obj.Should().NotBeNull();
123+
}
124+
125+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
126+
public void ObjectAssertIsNotNull_Failure_OldAssertion()
127+
{
128+
// arrange
129+
object obj = null;
130+
131+
// old assertion:
132+
Assert.IsNotNull(obj);
133+
}
134+
135+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
136+
public void ObjectAssertIsNotNull_Failure_NewAssertion()
137+
{
138+
// arrange
139+
object obj = null;
140+
141+
// new assertion:
142+
obj.Should().NotBeNull();
143+
}
144+
145+
[TestMethod]
146+
public void ReferenceTypeAssertIsInstanceOfType()
147+
{
148+
// arrange
149+
var obj = new List<object>();
150+
151+
// old assertion:
152+
Assert.IsInstanceOfType(obj, typeof(List<object>));
153+
Assert.IsInstanceOfType<List<object>>(obj);
154+
155+
// new assertion:
156+
obj.Should().BeOfType<List<object>>();
157+
}
158+
159+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
160+
public void ReferenceTypeAssertIsInstanceOfType_Failure_OldAssertion_0()
161+
{
162+
// arrange
163+
var obj = new List<int>();
164+
165+
// old assertion:
166+
Assert.IsInstanceOfType(obj, typeof(List<object>));
167+
}
168+
169+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
170+
public void ReferenceTypeAssertIsInstanceOfType_Failure_OldAssertion_1()
171+
{
172+
// arrange
173+
var obj = new List<int>();
174+
175+
// old assertion:
176+
Assert.IsInstanceOfType<List<object>>(obj);
177+
}
178+
179+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
180+
public void ReferenceTypeAssertIsInstanceOfType_Failure_NewAssertion()
181+
{
182+
// arrange
183+
var obj = new List<int>();
184+
185+
// new assertion:
186+
obj.Should().BeOfType<List<object>>();
187+
}
188+
189+
[TestMethod]
190+
public void ReferenceTypeAssertIsNotInstanceOfType()
191+
{
192+
// arrange
193+
var obj = new List<int>();
194+
195+
// old assertion:
196+
Assert.IsNotInstanceOfType(obj, typeof(List<object>));
197+
Assert.IsNotInstanceOfType<List<object>>(obj);
198+
199+
// new assertion:
200+
obj.Should().NotBeOfType<List<object>>();
201+
}
202+
203+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
204+
public void ReferenceTypeAssertIsNotInstanceOfType_Failure_OldAssertion_0()
205+
{
206+
// arrange
207+
var obj = new List<object>();
208+
209+
// old assertion:
210+
Assert.IsNotInstanceOfType(obj, typeof(List<object>));
211+
}
212+
213+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
214+
public void ReferenceTypeAssertIsNotInstanceOfType_Failure_OldAssertion_1()
215+
{
216+
// arrange
217+
var obj = new List<object>();
218+
219+
// old assertion:
220+
Assert.IsNotInstanceOfType<List<object>>(obj);
221+
}
222+
223+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
224+
public void ReferenceTypeAssertIsNotInstanceOfType_Failure_NewAssertion()
225+
{
226+
// arrange
227+
var obj = new List<object>();
228+
229+
// new assertion:
230+
obj.Should().NotBeOfType<List<object>>();
231+
}
79232
}

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator/DocsGenerator.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,23 @@ public async Task Execute()
113113
}
114114

115115
// Testing Libraries failures scenarios:
116-
if (methodsMap.TryGetValue($"{method.Identifier.Text}_Failure_OldAssertion", out var testWithFailureOldAssertion)
117-
&& methodsMap.TryGetValue($"{method.Identifier.Text}_Failure_NewAssertion", out var testWithFailureNewAssertion))
116+
if (methodsMap.TryGetValue($"{method.Identifier.Text}_Failure_NewAssertion", out var testWithFailureNewAssertion))
118117
{
119-
var testMethodWithFailureOldAssertion = classType.GetMethod(testWithFailureOldAssertion.Identifier.Text);
118+
var testWithFailureOldAssertions = methodsMap.Where(x => x.Key.StartsWith($"{method.Identifier.Text}_Failure_OldAssertion")).Select(x => x.Value);
119+
120+
var testMethodWithFailureOldAssertions = testWithFailureOldAssertions.Select(m => classType.GetMethod(m.Identifier.Text));
120121
var testMethodWithFailureNewAssertion = classType.GetMethod(testWithFailureNewAssertion.Identifier.Text);
121122

122-
var exceptionMessageLinesOldAssertion = GetMethodExceptionMessage(classInstance, testMethodWithFailureOldAssertion);
123+
var exceptionMessageLinesOldAssertions = testMethodWithFailureOldAssertions.Select(m => GetMethodExceptionMessage(classInstance, m)).ToArray();
123124
var exceptionMessageLinesNewAssertion = GetMethodExceptionMessage(classInstance, testMethodWithFailureNewAssertion);
124125

125-
var oldAssertionComment = testWithFailureOldAssertion.DescendantTrivia().First(x => x.IsKind(SyntaxKind.SingleLineCommentTrivia) && x.ToString().Equals("// old assertion:"));
126+
var oldAssertionComment = testWithFailureOldAssertions.Select(x => x.DescendantTrivia().First(x => x.IsKind(SyntaxKind.SingleLineCommentTrivia) && x.ToString().Equals("// old assertion:"))).ToArray();
126127
var newAssertionComment = testWithFailureNewAssertion.DescendantTrivia().First(x => x.IsKind(SyntaxKind.SingleLineCommentTrivia) && x.ToString().Equals("// new assertion:"));
127128

128129
var bodyLines = testWithFailureNewAssertion.Body.ToFullString().Split(Environment.NewLine)[2..^2];
129130
var paddingToRemove = bodyLines[0].IndexOf(bodyLines[0].TrimStart());
130131

131-
var oldAssertion = testWithFailureOldAssertion.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(oldAssertionComment.Span) > 0).ToString().TrimStart() + " \t// fail message: " + exceptionMessageLinesOldAssertion;
132+
var oldAssertions = testWithFailureOldAssertions.Select((x, i) => x.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(oldAssertionComment[i].Span) > 0).ToString().TrimStart() + " \t// fail message: " + exceptionMessageLinesOldAssertions[i]);
132133
var newAssertion = testWithFailureNewAssertion.Body.Statements.OfType<ExpressionStatementSyntax>().Single(x => x.Span.CompareTo(newAssertionComment.Span) > 0).ToString().TrimStart() + " \t// fail message: " + exceptionMessageLinesNewAssertion;
133134

134135
var arrange = bodyLines.TakeWhile(x => !string.IsNullOrEmpty(x))
@@ -140,7 +141,10 @@ public async Task Execute()
140141
scenarios.AppendLine(arrange);
141142
scenarios.AppendLine();
142143
scenarios.AppendLine($"// old assertion:");
143-
scenarios.AppendLine(oldAssertion);
144+
foreach (var oldAssertion in oldAssertions)
145+
{
146+
scenarios.AppendLine(oldAssertion);
147+
}
144148
scenarios.AppendLine();
145149
scenarios.AppendLine($"// new assertion:");
146150
scenarios.AppendLine(newAssertion);

0 commit comments

Comments
 (0)