Skip to content

Commit 266f303

Browse files
authored
docs: fluent assertions string assertions (#396)
* docs: fluent assertions string assertions
1 parent 6cd035f commit 266f303

File tree

6 files changed

+440
-48
lines changed

6 files changed

+440
-48
lines changed

docs/FluentAssertionsAnalyzer.md

Lines changed: 212 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
44

55
# FluentAssertions Analyzer Docs
66

7+
- [StringShouldStartWith](#scenario-stringshouldstartwith) - `actual.Should().StartWith(expected);`
8+
- [StringShouldEndWith](#scenario-stringshouldendwith) - `actual.Should().EndWith(expected);`
9+
- [StringShouldNotBeNullOrEmpty](#scenario-stringshouldnotbenullorempty) - `actual.Should().NotBeNullOrEmpty();`
10+
- [StringShouldBeNullOrEmpty](#scenario-stringshouldbenullorempty) - `actual.Should().BeNullOrEmpty();`
11+
- [StringShouldBeNullOrWhiteSpace](#scenario-stringshouldbenullorwhitespace) - `actual.Should().BeNullOrWhiteSpace();`
12+
- [StringShouldNotBeNullOrWhiteSpace](#scenario-stringshouldnotbenullorwhitespace) - `actual.Should().NotBeNullOrWhiteSpace();`
13+
- [StringShouldHaveLength](#scenario-stringshouldhavelength) - `actual.Should().HaveLength(expected);`
714
- [CollectionShouldNotBeEmpty](#scenario-collectionshouldnotbeempty) - `collection.Should().NotBeEmpty();`
815
- [CollectionShouldBeEmpty](#scenario-collectionshouldbeempty) - `collection.Should().BeEmpty();`
916
- [CollectionShouldNotContainCondition](#scenario-collectionshouldnotcontaincondition) - `collection.Should().NotContain(i => i == 4);`
@@ -44,6 +51,198 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
4451

4552
## Scenarios
4653

54+
### scenario: StringShouldStartWith
55+
56+
```cs
57+
// arrange
58+
var actual = "actual";
59+
var expected = "act";
60+
61+
// old assertion:
62+
actual.StartsWith(expected).Should().BeTrue();
63+
64+
// new assertion:
65+
actual.Should().StartWith(expected);
66+
```
67+
68+
#### Failure messages
69+
70+
```cs
71+
// arrange
72+
var actual = "actual";
73+
var expected = "wrong";
74+
75+
// old assertion:
76+
actual.StartsWith(expected).Should().BeTrue(); // fail message: Expected actual.StartsWith(expected) to be True, but found False.
77+
78+
// new assertion:
79+
actual.Should().StartWith(expected); // fail message: Expected actual to start with "wrong", but "actual" differs near "act" (index 0).
80+
```
81+
82+
### scenario: StringShouldEndWith
83+
84+
```cs
85+
// arrange
86+
var actual = "actual";
87+
var expected = "ual";
88+
89+
// old assertion:
90+
actual.EndsWith(expected).Should().BeTrue();
91+
92+
// new assertion:
93+
actual.Should().EndWith(expected);
94+
```
95+
96+
#### Failure messages
97+
98+
```cs
99+
// arrange
100+
var actual = "actual";
101+
var expected = "wrong";
102+
103+
// old assertion:
104+
actual.EndsWith(expected).Should().BeTrue(); // fail message: Expected actual.EndsWith(expected) to be True, but found False.
105+
106+
// new assertion:
107+
actual.Should().EndWith(expected); // fail message: Expected actual "actual" to end with "wrong".
108+
```
109+
110+
### scenario: StringShouldNotBeNullOrEmpty
111+
112+
```cs
113+
// arrange
114+
var actual = "actual";
115+
116+
// old assertion:
117+
string.IsNullOrEmpty(actual).Should().BeFalse();
118+
actual.Should().NotBeNull().And.NotBeEmpty();
119+
actual.Should().NotBeEmpty().And.NotBeNull();
120+
121+
// new assertion:
122+
actual.Should().NotBeNullOrEmpty();
123+
```
124+
125+
#### Failure messages
126+
127+
```cs
128+
// arrange
129+
var actual = string.Empty;
130+
131+
// old assertion:
132+
string.IsNullOrEmpty(actual).Should().BeFalse(); // fail message: Expected string.IsNullOrEmpty(actual) to be False, but found True.
133+
actual.Should().NotBeNull().And.NotBeEmpty(); // fail message: Did not expect actual to be empty.
134+
actual.Should().NotBeEmpty().And.NotBeNull(); // fail message: Did not expect actual to be empty.
135+
136+
// new assertion:
137+
actual.Should().NotBeNullOrEmpty(); // fail message: Expected actual not to be <null> or empty, but found "".
138+
```
139+
140+
### scenario: StringShouldBeNullOrEmpty
141+
142+
```cs
143+
// arrange
144+
var actual = string.Empty;
145+
146+
// old assertion:
147+
string.IsNullOrEmpty(actual).Should().BeTrue();
148+
149+
// new assertion:
150+
actual.Should().BeNullOrEmpty();
151+
```
152+
153+
#### Failure messages
154+
155+
```cs
156+
// arrange
157+
var actual = "actual";
158+
159+
// old assertion:
160+
string.IsNullOrEmpty(actual).Should().BeTrue(); // fail message: Expected string.IsNullOrEmpty(actual) to be True, but found False.
161+
162+
// new assertion:
163+
actual.Should().BeNullOrEmpty(); // fail message: Expected actual to be <null> or empty, but found "actual".
164+
```
165+
166+
### scenario: StringShouldBeNullOrWhiteSpace
167+
168+
```cs
169+
// arrange
170+
var actual = string.Empty;
171+
172+
// old assertion:
173+
string.IsNullOrWhiteSpace(actual).Should().BeTrue();
174+
175+
// new assertion:
176+
actual.Should().BeNullOrWhiteSpace();
177+
```
178+
179+
#### Failure messages
180+
181+
```cs
182+
// arrange
183+
var actual = "actual";
184+
185+
// old assertion:
186+
string.IsNullOrWhiteSpace(actual).Should().BeTrue(); // fail message: Expected string.IsNullOrWhiteSpace(actual) to be True, but found False.
187+
188+
// new assertion:
189+
actual.Should().BeNullOrWhiteSpace(); // fail message: Expected actual to be <null> or whitespace, but found "actual".
190+
```
191+
192+
### scenario: StringShouldNotBeNullOrWhiteSpace
193+
194+
```cs
195+
// arrange
196+
var actual = "actual";
197+
198+
// old assertion:
199+
string.IsNullOrWhiteSpace(actual).Should().BeFalse();
200+
201+
// new assertion:
202+
actual.Should().NotBeNullOrWhiteSpace();
203+
```
204+
205+
#### Failure messages
206+
207+
```cs
208+
// arrange
209+
var actual = string.Empty;
210+
211+
// old assertion:
212+
string.IsNullOrWhiteSpace(actual).Should().BeFalse(); // fail message: Expected string.IsNullOrWhiteSpace(actual) to be False, but found True.
213+
214+
// new assertion:
215+
actual.Should().NotBeNullOrWhiteSpace(); // fail message: Expected actual not to be <null> or whitespace, but found "".
216+
```
217+
218+
### scenario: StringShouldHaveLength
219+
220+
```cs
221+
// arrange
222+
var actual = "actual";
223+
var expected = 6;
224+
225+
// old assertion:
226+
actual.Length.Should().Be(expected);
227+
228+
// new assertion:
229+
actual.Should().HaveLength(expected);
230+
```
231+
232+
#### Failure messages
233+
234+
```cs
235+
// arrange
236+
var actual = "actual";
237+
var expected = 5;
238+
239+
// old assertion:
240+
actual.Length.Should().Be(expected); // fail message: Expected actual.Length to be 5, but found 6.
241+
242+
// new assertion:
243+
actual.Should().HaveLength(expected); // fail message: Expected actual with length 5, but found string "actual" with length 6.
244+
```
245+
47246
### scenario: CollectionShouldNotBeEmpty
48247

49248
```cs
@@ -64,7 +263,7 @@ collection.Should().NotBeEmpty();
64263
var collection = new List<int> { };
65264

66265
// old assertion:
67-
collection.Any().Should().BeTrue(); // fail message: Expected collection.Any() to be true, but found False.
266+
collection.Any().Should().BeTrue(); // fail message: Expected collection.Any() to be True, but found False.
68267
69268
// new assertion:
70269
collection.Should().NotBeEmpty(); // fail message: Expected collection not to be empty.
@@ -93,13 +292,13 @@ collection.Should().BeEmpty();
93292
var collection = new List<int> { 1, 2, 3 };
94293

95294
// old assertion:
96-
collection.Any().Should().BeFalse(); // fail message: Expected collection.Any() to be false, but found True.
295+
collection.Any().Should().BeFalse(); // fail message: Expected collection.Any() to be False, but found True.
97296
collection.Count().Should().Be(0); // fail message: Expected collection.Count() to be 0, but found 3 (difference of 3).
98297
collection.Count.Should().Be(0); // fail message: Expected collection.Count to be 0, but found 3 (difference of 3).
99298
collection.Should().HaveCount(0); // fail message: Expected collection to contain 0 item(s), but found 3: {1, 2, 3}.
100299
101300
// new assertion:
102-
collection.Should().BeEmpty(); // fail message: Expected collection to be empty, but found {1, 2, 3}.
301+
collection.Should().BeEmpty(); // fail message: Expected collection to be empty, but found at least one item {1}.
103302
```
104303

105304
### scenario: CollectionShouldNotContainCondition
@@ -123,8 +322,8 @@ collection.Should().NotContain(i => i == 4);
123322
var collection = new List<int> { 1, 2, 3, 4, 5 };
124323

125324
// old assertion:
126-
collection.Any(i => i == 4).Should().BeFalse(); // fail message: Expected collection.Any(i => i == 4) to be false, but found True.
127-
collection.Where(i => i == 4).Should().BeEmpty(); // fail message: Expected collection.Where(i => i == 4) to be empty, but found {4}.
325+
collection.Any(i => i == 4).Should().BeFalse(); // fail message: Expected collection.Any(i => i == 4) to be False, but found True.
326+
collection.Where(i => i == 4).Should().BeEmpty(); // fail message: Expected collection.Where(i => i == 4) to be empty, but found at least one item {4}.
128327
129328
// new assertion:
130329
collection.Should().NotContain(i => i == 4); // fail message: Expected collection {1, 2, 3, 4, 5} to not have any items matching (i == 4), but found {4}.
@@ -150,7 +349,7 @@ collection.Should().NotContain(4);
150349
var collection = new List<int> { 1, 2, 3, 4, 5 };
151350

152351
// old assertion:
153-
collection.Contains(4).Should().BeFalse(); // fail message: Expected collection.Contains(4) to be false, but found True.
352+
collection.Contains(4).Should().BeFalse(); // fail message: Expected collection.Contains(4) to be False, but found True.
154353
155354
// new assertion:
156355
collection.Should().NotContain(4); // fail message: Expected collection {1, 2, 3, 4, 5} to not contain 4.
@@ -176,7 +375,7 @@ collection.Should().OnlyContain(x => x > 0);
176375
var collection = new List<int> { 1, 2, 3, -1 };
177376

178377
// old assertion:
179-
collection.All(x => x > 0).Should().BeTrue(); // fail message: Expected collection.All(x => x > 0) to be true, but found False.
378+
collection.All(x => x > 0).Should().BeTrue(); // fail message: Expected collection.All(x => x > 0) to be True, but found False.
180379
181380
// new assertion:
182381
collection.Should().OnlyContain(x => x > 0); // fail message: Expected collection to contain only items matching (x > 0), but {-1} do(es) not match.
@@ -202,7 +401,7 @@ collection.Should().Contain(2);
202401
var collection = new List<int> { 1, 3, 4, 5 };
203402

204403
// old assertion:
205-
collection.Contains(2).Should().BeTrue(); // fail message: Expected collection.Contains(2) to be true, but found False.
404+
collection.Contains(2).Should().BeTrue(); // fail message: Expected collection.Contains(2) to be True, but found False.
206405
207406
// new assertion:
208407
collection.Should().Contain(2); // fail message: Expected collection {1, 3, 4, 5} to contain 2.
@@ -229,7 +428,7 @@ collection.Should().Contain(i => i == 2);
229428
var collection = new List<int> { 3, 4, 5 };
230429

231430
// old assertion:
232-
collection.Any(i => i == 2).Should().BeTrue(); // fail message: Expected collection.Any(i => i == 2) to be true, but found False.
431+
collection.Any(i => i == 2).Should().BeTrue(); // fail message: Expected collection.Any(i => i == 2) to be True, but found False.
233432
collection.Where(i => i == 2).Should().NotBeEmpty(); // fail message: Expected collection.Where(i => i == 2) not to be empty.
234433
235434
// new assertion:
@@ -580,7 +779,7 @@ dictionary.Should().ContainKey("two");
580779
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["three"] = 3 };
581780

582781
// old assertion:
583-
dictionary.ContainsKey("two").Should().BeTrue(); // fail message: Expected dictionary.ContainsKey("two") to be true, but found False.
782+
dictionary.ContainsKey("two").Should().BeTrue(); // fail message: Expected dictionary.ContainsKey("two") to be True, but found False.
584783
585784
// new assertion:
586785
dictionary.Should().ContainKey("two"); // fail message: Expected dictionary {["one"] = 1, ["three"] = 3} to contain key "two".
@@ -606,7 +805,7 @@ dictionary.Should().NotContainKey("four");
606805
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4 };
607806

608807
// old assertion:
609-
dictionary.ContainsKey("four").Should().BeFalse(); // fail message: Expected dictionary.ContainsKey("four") to be false, but found True.
808+
dictionary.ContainsKey("four").Should().BeFalse(); // fail message: Expected dictionary.ContainsKey("four") to be False, but found True.
610809
611810
// new assertion:
612811
dictionary.Should().NotContainKey("four"); // fail message: Expected dictionary {["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4} not to contain key "four", but found it anyhow.
@@ -632,7 +831,7 @@ dictionary.Should().ContainValue(2);
632831
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3 };
633832

634833
// old assertion:
635-
dictionary.ContainsValue(4).Should().BeTrue(); // fail message: Expected dictionary.ContainsValue(4) to be true, but found False.
834+
dictionary.ContainsValue(4).Should().BeTrue(); // fail message: Expected dictionary.ContainsValue(4) to be True, but found False.
636835
637836
// new assertion:
638837
dictionary.Should().ContainValue(4); // fail message: Expected dictionary {["one"] = 1, ["two"] = 2, ["three"] = 3} to contain value 4.
@@ -658,7 +857,7 @@ dictionary.Should().NotContainValue(4);
658857
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4 };
659858

660859
// old assertion:
661-
dictionary.ContainsValue(4).Should().BeFalse(); // fail message: Expected dictionary.ContainsValue(4) to be false, but found True.
860+
dictionary.ContainsValue(4).Should().BeFalse(); // fail message: Expected dictionary.ContainsValue(4) to be False, but found True.
662861
663862
// new assertion:
664863
dictionary.Should().NotContainValue(4); // fail message: Expected dictionary {["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4} not to contain value 4, but found it anyhow.

docs/MsTestAnalyzer.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var flag = false;
7070
Assert.IsTrue(flag); /* fail message: Assert.IsTrue failed. */
7171

7272
// new assertion:
73-
flag.Should().BeTrue(); /* fail message: Expected flag to be true, but found False. */
73+
flag.Should().BeTrue(); /* fail message: Expected flag to be True, but found False. */
7474
```
7575

7676
### scenario: AssertIsFalse
@@ -95,7 +95,7 @@ var flag = true;
9595
Assert.IsFalse(flag); /* fail message: Assert.IsFalse failed. */
9696

9797
// new assertion:
98-
flag.Should().BeFalse(); /* fail message: Expected flag to be false, but found True. */
98+
flag.Should().BeFalse(); /* fail message: Expected flag to be False, but found True. */
9999
```
100100

101101
### scenario: AssertIsNull
@@ -974,10 +974,7 @@ static void ThrowException() => throw new InvalidOperationException();
974974
Action action = ThrowException;
975975

976976
// old assertion:
977-
Assert.ThrowsException<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
978-
Exception Message: Operation is not valid due to the current state of the object.
979-
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsException_Failure_OldAssertion>g__ThrowException|109_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1298
980-
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException[T](Action action, String message, Object[] parameters) */
977+
Assert.ThrowsException<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Expected exception type:<System.ArgumentException>. Actual exception type:<System.InvalidOperationException>. */
981978

982979
// new assertion:
983980
action.Should().ThrowExactly<ArgumentException>(); /* fail message: Expected type to be System.ArgumentException, but found System.InvalidOperationException. */
@@ -1004,10 +1001,7 @@ static Task ThrowExceptionAsync() => throw new InvalidOperationException();
10041001
Func<Task> action = ThrowExceptionAsync;
10051002

10061003
// old assertion:
1007-
await Assert.ThrowsExceptionAsync<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
1008-
Exception Message: Operation is not valid due to the current state of the object.
1009-
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsExceptionAsync_Failure_OldAssertion>g__ThrowExceptionAsync|112_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1334
1010-
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync[T](Func`1 action, String message, Object[] parameters) */
1004+
await Assert.ThrowsExceptionAsync<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Expected exception type:<System.ArgumentException>. Actual exception type:<System.InvalidOperationException>. */
10111005

10121006
// new assertion:
10131007
await action.Should().ThrowExactlyAsync<ArgumentException>(); /* fail message: Expected type to be System.ArgumentException, but found System.InvalidOperationException. */

docs/Nunit3Analyzer.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Assert.That(flag, Is.Not.False); /* fail message: Expected: not False
7272
*/
7373

7474
// new assertion:
75-
flag.Should().BeTrue(); /* fail message: Expected flag to be true, but found False. */
75+
flag.Should().BeTrue(); /* fail message: Expected flag to be True, but found False. */
7676
```
7777

7878
### scenario: AssertIsFalse
@@ -111,7 +111,7 @@ Assert.That(flag, Is.Not.True); /* fail message: Expected: not True
111111
*/
112112

113113
// new assertion:
114-
flag.Should().BeFalse(); /* fail message: Expected flag to be false, but found True. */
114+
flag.Should().BeFalse(); /* fail message: Expected flag to be False, but found True. */
115115
```
116116

117117
### scenario: AssertNull
@@ -224,7 +224,7 @@ CollectionAssert.IsEmpty(collection); /* fail message: Expected: <empty>
224224
*/
225225

226226
// new assertion:
227-
collection.Should().BeEmpty(); /* fail message: Expected collection to be empty, but found {1, 2, 3}. */
227+
collection.Should().BeEmpty(); /* fail message: Expected collection to be empty, but found at least one item {1}. */
228228
```
229229

230230
### scenario: AssertIsNotEmpty

0 commit comments

Comments
 (0)