Skip to content

Commit 59a92c6

Browse files
authored
feat: add xunit assert docs (#368)
1 parent 9f7a90c commit 59a92c6

File tree

3 files changed

+680
-2
lines changed

3 files changed

+680
-2
lines changed

docs/XunitAnalyzer.md

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

77
- [AssertTrue](#scenario-asserttrue) - `flag.Should().BeTrue();`
88
- [AssertFalse](#scenario-assertfalse) - `flag.Should().BeFalse();`
9+
- [AssertSame](#scenario-assertsame) - `obj1.Should().BeSameAs(obj2);`
10+
- [AssertNotSame](#scenario-assertnotsame) - `obj1.Should().NotBeSameAs(obj2);`
11+
- [AssertDoubleEqual](#scenario-assertdoubleequal) - `actual.Should().BeApproximately(expected, tolerance);`
12+
- [AssertDateTimeEqual](#scenario-assertdatetimeequal) - `actual.Should().BeCloseTo(expected, TimeSpan.FromDays(3));`
13+
- [AssertObjectEqual](#scenario-assertobjectequal) - `actual.Should().Be(expected);`
14+
- [AssertObjectEqualWithComparer](#scenario-assertobjectequalwithcomparer) - `actual.Should().BeEquivalentTo(expected, options => options.Using(EqualityComparer<object>.Default));`
15+
- [AssertObjectNotEqual](#scenario-assertobjectnotequal) - `actual.Should().NotBe(expected);`
16+
- [AssertObjectNotEqualWithComparer](#scenario-assertobjectnotequalwithcomparer) - `actual.Should().NotBeEquivalentTo(expected, options => options.Using(EqualityComparer<object>.Default));`
17+
- [AssertStrictEqual](#scenario-assertstrictequal) - `actual.Should().Be(expected);`
18+
- [AssertNotStrictEqual](#scenario-assertnotstrictequal) - `actual.Should().NotBe(expected);`
919

1020

1121
## Scenarios
@@ -64,4 +74,307 @@ Actual: True */
6474
flag.Should().BeFalse(); /* fail message: Expected flag to be false, but found True. */
6575
```
6676

77+
### scenario: AssertSame
78+
79+
```cs
80+
// arrange
81+
var obj1 = new object();
82+
var obj2 = obj1;
83+
84+
// old assertion:
85+
Assert.Same(obj1, obj2);
86+
87+
// new assertion:
88+
obj1.Should().BeSameAs(obj2);
89+
```
90+
91+
#### Failure messages
92+
93+
```cs
94+
object obj1 = 6;
95+
object obj2 = "foo";
96+
97+
// old assertion:
98+
Assert.Same(obj1, obj2); /* fail message: Assert.Same() Failure
99+
Expected: 6
100+
Actual: foo */
101+
102+
// new assertion:
103+
obj1.Should().BeSameAs(obj2); /* fail message: Expected obj1 to refer to "foo", but found 6. */
104+
```
105+
106+
### scenario: AssertNotSame
107+
108+
```cs
109+
// arrange
110+
object obj1 = 6;
111+
object obj2 = "foo";
112+
113+
// old assertion:
114+
Assert.NotSame(obj1, obj2);
115+
116+
// new assertion:
117+
obj1.Should().NotBeSameAs(obj2);
118+
```
119+
120+
#### Failure messages
121+
122+
```cs
123+
object obj1 = "foo";
124+
object obj2 = "foo";
125+
126+
// old assertion:
127+
Assert.NotSame(obj1, obj2); /* fail message: Assert.NotSame() Failure */
128+
129+
// new assertion:
130+
obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to "foo". */
131+
```
132+
133+
### scenario: AssertDoubleEqual
134+
135+
```cs
136+
// arrange
137+
double actual = 3.14;
138+
double expected = 3.141;
139+
double tolerance = 0.00159;
140+
141+
// old assertion:
142+
Assert.Equal(expected, actual, tolerance);
143+
144+
// new assertion:
145+
actual.Should().BeApproximately(expected, tolerance);
146+
```
147+
148+
#### Failure messages
149+
150+
```cs
151+
double actual = 3.14;
152+
double expected = 4.2;
153+
double tolerance = 0.0001;
154+
155+
// old assertion:
156+
Assert.Equal(expected, actual, tolerance); /* fail message: Assert.Equal() Failure
157+
Expected: 4.2000000000000002
158+
Actual: 3.1400000000000001 */
159+
160+
// new assertion:
161+
actual.Should().BeApproximately(expected, tolerance); /* fail message: Expected actual to approximate 4.2 +/- 0.0001, but 3.14 differed by 1.06. */
162+
```
163+
164+
### scenario: AssertDateTimeEqual
165+
166+
```cs
167+
// arrange
168+
var actual = new DateTime(2021, 1, 1);
169+
var expected = new DateTime(2021, 1, 2);
170+
171+
// old assertion:
172+
Assert.Equal(expected, actual, TimeSpan.FromDays(3));
173+
174+
// new assertion:
175+
actual.Should().BeCloseTo(expected, TimeSpan.FromDays(3));
176+
```
177+
178+
#### Failure messages
179+
180+
```cs
181+
var actual = new DateTime(2021, 1, 1);
182+
var expected = new DateTime(2021, 1, 2);
183+
184+
// old assertion:
185+
Assert.Equal(expected, actual, TimeSpan.FromHours(3)); /* fail message: Assert.Equal() Failure
186+
Expected: 1/2/2021 12:00:00 AM
187+
Actual: 1/1/2021 12:00:00 AM difference 1.00:00:00 is larger than 03:00:00 */
188+
189+
// new assertion:
190+
actual.Should().BeCloseTo(expected, TimeSpan.FromHours(3)); /* fail message: Expected actual to be within 3h from <2021-01-02>, but <2021-01-01> was off by 1d. */
191+
```
192+
193+
### scenario: AssertObjectEqual
194+
195+
```cs
196+
// arrange
197+
object actual = "foo";
198+
object expected = "foo";
199+
200+
// old assertion:
201+
Assert.Equal(expected, actual);
202+
203+
// new assertion:
204+
actual.Should().Be(expected);
205+
```
206+
207+
#### Failure messages
208+
209+
```cs
210+
object actual = "foo";
211+
object expected = 6;
212+
213+
// old assertion:
214+
Assert.Equal(expected, actual); /* fail message: Assert.Equal() Failure
215+
Expected: 6
216+
Actual: foo */
217+
218+
// new assertion:
219+
actual.Should().Be(expected); /* fail message: Expected actual to be 6, but found "foo". */
220+
```
221+
222+
### scenario: AssertObjectEqualWithComparer
223+
224+
```cs
225+
// arrange
226+
object actual = "foo";
227+
object expected = "foo";
228+
229+
// old assertion:
230+
Assert.Equal(expected, actual, EqualityComparer<object>.Default);
231+
232+
// new assertion:
233+
actual.Should().BeEquivalentTo(expected, options => options.Using(EqualityComparer<object>.Default));
234+
```
235+
236+
#### Failure messages
237+
238+
```cs
239+
object actual = "foo";
240+
object expected = 6;
241+
242+
// old assertion:
243+
Assert.Equal(expected, actual, EqualityComparer<object>.Default); /* fail message: Assert.Equal() Failure
244+
Expected: 6
245+
Actual: foo */
246+
247+
// new assertion:
248+
actual.Should().BeEquivalentTo(expected, options => options.Using(EqualityComparer<object>.Default)); /* fail message: Expected actual to be 6, but found "foo".
249+
250+
With configuration:
251+
- Use declared types and members
252+
- Compare enums by value
253+
- Compare tuples by their properties
254+
- Compare anonymous types by their properties
255+
- Compare records by their members
256+
- Include non-browsable members
257+
- Match member by name (or throw)
258+
- Use System.Collections.Generic.ObjectEqualityComparer`1[System.Object] for objects of type System.Object
259+
- Be strict about the order of items in byte arrays
260+
- Without automatic conversion.
261+
*/
262+
```
263+
264+
### scenario: AssertObjectNotEqual
265+
266+
```cs
267+
// arrange
268+
object actual = "foo";
269+
object expected = 6;
270+
271+
// old assertion:
272+
Assert.NotEqual(expected, actual);
273+
274+
// new assertion:
275+
actual.Should().NotBe(expected);
276+
```
277+
278+
#### Failure messages
279+
280+
```cs
281+
object actual = "foo";
282+
object expected = "foo";
283+
284+
// old assertion:
285+
Assert.NotEqual(expected, actual); /* fail message: Assert.NotEqual() Failure
286+
Expected: Not "foo"
287+
Actual: "foo" */
288+
289+
// new assertion:
290+
actual.Should().NotBe(expected); /* fail message: Did not expect actual to be equal to "foo". */
291+
```
292+
293+
### scenario: AssertObjectNotEqualWithComparer
294+
295+
```cs
296+
// arrange
297+
object actual = "foo";
298+
object expected = 6;
299+
300+
// old assertion:
301+
Assert.NotEqual(expected, actual, EqualityComparer<object>.Default);
302+
303+
// new assertion:
304+
actual.Should().NotBeEquivalentTo(expected, options => options.Using(EqualityComparer<object>.Default));
305+
```
306+
307+
#### Failure messages
308+
309+
```cs
310+
object actual = "foo";
311+
object expected = "foo";
312+
313+
// old assertion:
314+
Assert.NotEqual(expected, actual, EqualityComparer<object>.Default); /* fail message: Assert.NotEqual() Failure
315+
Expected: Not "foo"
316+
Actual: "foo" */
317+
318+
// new assertion:
319+
actual.Should().NotBeEquivalentTo(expected, options => options.Using(EqualityComparer<object>.Default)); /* fail message: Expected actual not to be equivalent to "foo", but they are. */
320+
```
321+
322+
### scenario: AssertStrictEqual
323+
324+
```cs
325+
// arrange
326+
object actual = "foo";
327+
object expected = "foo";
328+
329+
// old assertion:
330+
Assert.StrictEqual(expected, actual);
331+
332+
// new assertion:
333+
actual.Should().Be(expected);
334+
```
335+
336+
#### Failure messages
337+
338+
```cs
339+
object actual = "foo";
340+
object expected = 6;
341+
342+
// old assertion:
343+
Assert.StrictEqual(expected, actual); /* fail message: Assert.Equal() Failure
344+
Expected: 6
345+
Actual: foo */
346+
347+
// new assertion:
348+
actual.Should().Be(expected); /* fail message: Expected actual to be 6, but found "foo". */
349+
```
350+
351+
### scenario: AssertNotStrictEqual
352+
353+
```cs
354+
// arrange
355+
object actual = "foo";
356+
object expected = 6;
357+
358+
// old assertion:
359+
Assert.NotStrictEqual(expected, actual);
360+
361+
// new assertion:
362+
actual.Should().NotBe(expected);
363+
```
364+
365+
#### Failure messages
366+
367+
```cs
368+
object actual = "foo";
369+
object expected = "foo";
370+
371+
// old assertion:
372+
Assert.NotStrictEqual(expected, actual); /* fail message: Assert.NotEqual() Failure
373+
Expected: Not "foo"
374+
Actual: "foo" */
375+
376+
// new assertion:
377+
actual.Should().NotBe(expected); /* fail message: Did not expect actual to be equal to "foo". */
378+
```
379+
67380

0 commit comments

Comments
 (0)