Skip to content

Commit fc4eef1

Browse files
committed
docs: fluent assertions string assertions
1 parent 6cd035f commit fc4eef1

File tree

6 files changed

+274
-48
lines changed

6 files changed

+274
-48
lines changed

docs/FluentAssertionsAnalyzer.md

Lines changed: 129 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ 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();`
711
- [CollectionShouldNotBeEmpty](#scenario-collectionshouldnotbeempty) - `collection.Should().NotBeEmpty();`
812
- [CollectionShouldBeEmpty](#scenario-collectionshouldbeempty) - `collection.Should().BeEmpty();`
913
- [CollectionShouldNotContainCondition](#scenario-collectionshouldnotcontaincondition) - `collection.Should().NotContain(i => i == 4);`
@@ -44,6 +48,118 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
4448

4549
## Scenarios
4650

51+
### scenario: StringShouldStartWith
52+
53+
```cs
54+
// arrange
55+
var actual = "actual";
56+
var expected = "act";
57+
58+
// old assertion:
59+
actual.StartsWith(expected).Should().BeTrue();
60+
61+
// new assertion:
62+
actual.Should().StartWith(expected);
63+
```
64+
65+
#### Failure messages
66+
67+
```cs
68+
// arrange
69+
var actual = "actual";
70+
var expected = "wrong";
71+
72+
// old assertion:
73+
actual.StartsWith(expected).Should().BeTrue(); // fail message: Expected actual.StartsWith(expected) to be True, but found False.
74+
75+
// new assertion:
76+
actual.Should().StartWith(expected); // fail message: Expected actual to start with "wrong", but "actual" differs near "act" (index 0).
77+
```
78+
79+
### scenario: StringShouldEndWith
80+
81+
```cs
82+
// arrange
83+
var actual = "actual";
84+
var expected = "ual";
85+
86+
// old assertion:
87+
actual.EndsWith(expected).Should().BeTrue();
88+
89+
// new assertion:
90+
actual.Should().EndWith(expected);
91+
```
92+
93+
#### Failure messages
94+
95+
```cs
96+
// arrange
97+
var actual = "actual";
98+
var expected = "wrong";
99+
100+
// old assertion:
101+
actual.EndsWith(expected).Should().BeTrue(); // fail message: Expected actual.EndsWith(expected) to be True, but found False.
102+
103+
// new assertion:
104+
actual.Should().EndWith(expected); // fail message: Expected actual "actual" to end with "wrong".
105+
```
106+
107+
### scenario: StringShouldNotBeNullOrEmpty
108+
109+
```cs
110+
// arrange
111+
var actual = "actual";
112+
113+
// old assertion:
114+
string.IsNullOrEmpty(actual).Should().BeFalse();
115+
actual.Should().NotBeNull().And.NotBeEmpty();
116+
actual.Should().NotBeEmpty().And.NotBeNull();
117+
118+
// new assertion:
119+
actual.Should().NotBeNullOrEmpty();
120+
```
121+
122+
#### Failure messages
123+
124+
```cs
125+
// arrange
126+
var actual = string.Empty;
127+
128+
// old assertion:
129+
string.IsNullOrEmpty(actual).Should().BeFalse(); // fail message: Expected string.IsNullOrEmpty(actual) to be False, but found True.
130+
actual.Should().NotBeNull().And.NotBeEmpty(); // fail message: Did not expect actual to be empty.
131+
actual.Should().NotBeEmpty().And.NotBeNull(); // fail message: Did not expect actual to be empty.
132+
133+
// new assertion:
134+
actual.Should().NotBeNullOrEmpty(); // fail message: Expected actual not to be <null> or empty, but found "".
135+
```
136+
137+
### scenario: StringShouldBeNullOrEmpty
138+
139+
```cs
140+
// arrange
141+
var actual = string.Empty;
142+
143+
// old assertion:
144+
string.IsNullOrEmpty(actual).Should().BeTrue();
145+
146+
// new assertion:
147+
actual.Should().BeNullOrEmpty();
148+
```
149+
150+
#### Failure messages
151+
152+
```cs
153+
// arrange
154+
var actual = "actual";
155+
156+
// old assertion:
157+
string.IsNullOrEmpty(actual).Should().BeTrue(); // fail message: Expected string.IsNullOrEmpty(actual) to be True, but found False.
158+
159+
// new assertion:
160+
actual.Should().BeNullOrEmpty(); // fail message: Expected actual to be <null> or empty, but found "actual".
161+
```
162+
47163
### scenario: CollectionShouldNotBeEmpty
48164

49165
```cs
@@ -64,7 +180,7 @@ collection.Should().NotBeEmpty();
64180
var collection = new List<int> { };
65181

66182
// old assertion:
67-
collection.Any().Should().BeTrue(); // fail message: Expected collection.Any() to be true, but found False.
183+
collection.Any().Should().BeTrue(); // fail message: Expected collection.Any() to be True, but found False.
68184
69185
// new assertion:
70186
collection.Should().NotBeEmpty(); // fail message: Expected collection not to be empty.
@@ -93,13 +209,13 @@ collection.Should().BeEmpty();
93209
var collection = new List<int> { 1, 2, 3 };
94210

95211
// old assertion:
96-
collection.Any().Should().BeFalse(); // fail message: Expected collection.Any() to be false, but found True.
212+
collection.Any().Should().BeFalse(); // fail message: Expected collection.Any() to be False, but found True.
97213
collection.Count().Should().Be(0); // fail message: Expected collection.Count() to be 0, but found 3 (difference of 3).
98214
collection.Count.Should().Be(0); // fail message: Expected collection.Count to be 0, but found 3 (difference of 3).
99215
collection.Should().HaveCount(0); // fail message: Expected collection to contain 0 item(s), but found 3: {1, 2, 3}.
100216
101217
// new assertion:
102-
collection.Should().BeEmpty(); // fail message: Expected collection to be empty, but found {1, 2, 3}.
218+
collection.Should().BeEmpty(); // fail message: Expected collection to be empty, but found at least one item {1}.
103219
```
104220

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

125241
// 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}.
242+
collection.Any(i => i == 4).Should().BeFalse(); // fail message: Expected collection.Any(i => i == 4) to be False, but found True.
243+
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}.
128244
129245
// new assertion:
130246
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 +266,7 @@ collection.Should().NotContain(4);
150266
var collection = new List<int> { 1, 2, 3, 4, 5 };
151267

152268
// old assertion:
153-
collection.Contains(4).Should().BeFalse(); // fail message: Expected collection.Contains(4) to be false, but found True.
269+
collection.Contains(4).Should().BeFalse(); // fail message: Expected collection.Contains(4) to be False, but found True.
154270
155271
// new assertion:
156272
collection.Should().NotContain(4); // fail message: Expected collection {1, 2, 3, 4, 5} to not contain 4.
@@ -176,7 +292,7 @@ collection.Should().OnlyContain(x => x > 0);
176292
var collection = new List<int> { 1, 2, 3, -1 };
177293

178294
// old assertion:
179-
collection.All(x => x > 0).Should().BeTrue(); // fail message: Expected collection.All(x => x > 0) to be true, but found False.
295+
collection.All(x => x > 0).Should().BeTrue(); // fail message: Expected collection.All(x => x > 0) to be True, but found False.
180296
181297
// new assertion:
182298
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 +318,7 @@ collection.Should().Contain(2);
202318
var collection = new List<int> { 1, 3, 4, 5 };
203319

204320
// old assertion:
205-
collection.Contains(2).Should().BeTrue(); // fail message: Expected collection.Contains(2) to be true, but found False.
321+
collection.Contains(2).Should().BeTrue(); // fail message: Expected collection.Contains(2) to be True, but found False.
206322
207323
// new assertion:
208324
collection.Should().Contain(2); // fail message: Expected collection {1, 3, 4, 5} to contain 2.
@@ -229,7 +345,7 @@ collection.Should().Contain(i => i == 2);
229345
var collection = new List<int> { 3, 4, 5 };
230346

231347
// old assertion:
232-
collection.Any(i => i == 2).Should().BeTrue(); // fail message: Expected collection.Any(i => i == 2) to be true, but found False.
348+
collection.Any(i => i == 2).Should().BeTrue(); // fail message: Expected collection.Any(i => i == 2) to be True, but found False.
233349
collection.Where(i => i == 2).Should().NotBeEmpty(); // fail message: Expected collection.Where(i => i == 2) not to be empty.
234350
235351
// new assertion:
@@ -580,7 +696,7 @@ dictionary.Should().ContainKey("two");
580696
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["three"] = 3 };
581697

582698
// old assertion:
583-
dictionary.ContainsKey("two").Should().BeTrue(); // fail message: Expected dictionary.ContainsKey("two") to be true, but found False.
699+
dictionary.ContainsKey("two").Should().BeTrue(); // fail message: Expected dictionary.ContainsKey("two") to be True, but found False.
584700
585701
// new assertion:
586702
dictionary.Should().ContainKey("two"); // fail message: Expected dictionary {["one"] = 1, ["three"] = 3} to contain key "two".
@@ -606,7 +722,7 @@ dictionary.Should().NotContainKey("four");
606722
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4 };
607723

608724
// old assertion:
609-
dictionary.ContainsKey("four").Should().BeFalse(); // fail message: Expected dictionary.ContainsKey("four") to be false, but found True.
725+
dictionary.ContainsKey("four").Should().BeFalse(); // fail message: Expected dictionary.ContainsKey("four") to be False, but found True.
610726
611727
// new assertion:
612728
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 +748,7 @@ dictionary.Should().ContainValue(2);
632748
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3 };
633749

634750
// old assertion:
635-
dictionary.ContainsValue(4).Should().BeTrue(); // fail message: Expected dictionary.ContainsValue(4) to be true, but found False.
751+
dictionary.ContainsValue(4).Should().BeTrue(); // fail message: Expected dictionary.ContainsValue(4) to be True, but found False.
636752
637753
// new assertion:
638754
dictionary.Should().ContainValue(4); // fail message: Expected dictionary {["one"] = 1, ["two"] = 2, ["three"] = 3} to contain value 4.
@@ -658,7 +774,7 @@ dictionary.Should().NotContainValue(4);
658774
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4 };
659775

660776
// old assertion:
661-
dictionary.ContainsValue(4).Should().BeFalse(); // fail message: Expected dictionary.ContainsValue(4) to be false, but found True.
777+
dictionary.ContainsValue(4).Should().BeFalse(); // fail message: Expected dictionary.ContainsValue(4) to be False, but found True.
662778
663779
// new assertion:
664780
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

docs/Nunit4Analyzer.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Assert.That(flag, Is.Not.False); /* fail message: Assert.That(flag, Is.Not.Fal
7777
*/
7878

7979
// new assertion:
80-
flag.Should().BeTrue(); /* fail message: Expected flag to be true, but found False. */
80+
flag.Should().BeTrue(); /* fail message: Expected flag to be True, but found False. */
8181
```
8282

8383
### scenario: AssertIsFalse
@@ -120,7 +120,7 @@ Assert.That(flag, Is.Not.True); /* fail message: Assert.That(flag, Is.Not.True
120120
*/
121121

122122
// new assertion:
123-
flag.Should().BeFalse(); /* fail message: Expected flag to be false, but found True. */
123+
flag.Should().BeFalse(); /* fail message: Expected flag to be False, but found True. */
124124
```
125125

126126
### scenario: AssertNull
@@ -244,7 +244,7 @@ CollectionAssert.IsEmpty(collection); /* fail message: Assert.That(collection,
244244
*/
245245

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

250250
### scenario: AssertIsNotEmpty

0 commit comments

Comments
 (0)