Skip to content

Commit e24e463

Browse files
authored
feat: Rewrite parameters order when handling constant values in assertions (#370)
* feat: Rewrite parameters order when handling constant values in assertions
1 parent 59a92c6 commit e24e463

File tree

5 files changed

+148
-24
lines changed

5 files changed

+148
-24
lines changed

docs/MsTestAnalyzer.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
1111
- [AssertIsInstanceOfType](#scenario-assertisinstanceoftype) - `obj.Should().BeOfType<List<object>>();`
1212
- [AssertIsNotInstanceOfType](#scenario-assertisnotinstanceoftype) - `obj.Should().NotBeOfType<List<object>>();`
1313
- [AssertObjectAreEqual](#scenario-assertobjectareequal) - `obj1.Should().Be(obj2);`
14+
- [AssertObjectAreEqual_LiteralValue](#scenario-assertobjectareequal_literalvalue) - `obj1.Should().Be("foo");`
1415
- [AssertOptionalIntegerAreEqual](#scenario-assertoptionalintegerareequal) - `number1.Should().Be(number2);`
1516
- [AssertOptionalIntegerAndNullAreEqual](#scenario-assertoptionalintegerandnullareequal) - `number.Should().BeNull();`
1617
- [AssertDoubleAreEqual](#scenario-assertdoubleareequal) - `number1.Should().BeApproximately(number2, delta);`
@@ -228,6 +229,31 @@ Assert.AreEqual(obj2, obj1); /* fail message: Assert.AreEqual failed. Expected:<
228229
obj1.Should().Be(obj2); /* fail message: Expected obj1 to be 42, but found "foo". */
229230
```
230231

232+
### scenario: AssertObjectAreEqual_LiteralValue
233+
234+
```cs
235+
// arrange
236+
object obj1 = "foo";
237+
238+
// old assertion:
239+
Assert.AreEqual(obj1, "foo");
240+
241+
// new assertion:
242+
obj1.Should().Be("foo");
243+
```
244+
245+
#### Failure messages
246+
247+
```cs
248+
object obj1 = "foo";
249+
250+
// old assertion:
251+
Assert.AreEqual(obj1, "bar"); /* fail message: Assert.AreEqual failed. Expected:<foo>. Actual:<bar>. */
252+
253+
// new assertion:
254+
obj1.Should().Be("bar"); /* fail message: Expected obj1 to be "bar", but found "foo". */
255+
```
256+
231257
### scenario: AssertOptionalIntegerAreEqual
232258

233259
```cs
@@ -950,7 +976,7 @@ Action action = ThrowException;
950976
// old assertion:
951977
Assert.ThrowsException<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
952978
Exception Message: Operation is not valid due to the current state of the object.
953-
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsException_Failure_OldAssertion>g__ThrowException|106_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1265
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
954980
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException[T](Action action, String message, Object[] parameters) */
955981

956982
// new assertion:
@@ -980,7 +1006,7 @@ Func<Task> action = ThrowExceptionAsync;
9801006
// old assertion:
9811007
await Assert.ThrowsExceptionAsync<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
9821008
Exception Message: Operation is not valid due to the current state of the object.
983-
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsExceptionAsync_Failure_OldAssertion>g__ThrowExceptionAsync|109_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1301
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
9841010
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync[T](Func`1 action, String message, Object[] parameters) */
9851011

9861012
// new assertion:

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ public void AssertObjectAreEqual_Failure_NewAssertion()
267267
obj1.Should().Be(obj2);
268268
}
269269

270+
[TestMethod]
271+
public void AssertObjectAreEqual_LiteralValue()
272+
{
273+
// arrange
274+
object obj1 = "foo";
275+
276+
// old assertion:
277+
Assert.AreEqual(obj1, "foo");
278+
279+
// new assertion:
280+
obj1.Should().Be("foo");
281+
}
282+
283+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
284+
public void AssertObjectAreEqual_LiteralValue_Failure_OldAssertion()
285+
{
286+
// arrange
287+
object obj1 = "foo";
288+
289+
// old assertion:
290+
Assert.AreEqual(obj1, "bar");
291+
}
292+
293+
[TestMethod, ExpectedException(typeof(AssertFailedException))]
294+
public void AssertObjectAreEqual_LiteralValue_Failure_NewAssertion()
295+
{
296+
// arrange
297+
object obj1 = "foo";
298+
299+
// new assertion:
300+
obj1.Should().Be("bar");
301+
}
302+
270303
[TestMethod]
271304
public void AssertOptionalIntegerAreEqual()
272305
{

src/FluentAssertions.Analyzers.Tests/Tips/MsTestTests.cs

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,33 +322,26 @@ public void AssertOptionalIntegerAreEqual_TestCodeFix(string oldAssertion, strin
322322

323323
[DataTestMethod]
324324
[AssertionDiagnostic("Assert.AreEqual(actual, null{0});")]
325+
[AssertionDiagnostic("Assert.AreEqual(null, actual{0});")]
325326
[Implemented]
326-
public void AssertOptionalIntegerAndNullAreEqual1_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);
327+
public void AssertOptionalIntegerAndNullAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);
327328

328329
[DataTestMethod]
329330
[AssertionCodeFix(
330331
oldAssertion: "Assert.AreEqual(actual, null{0});",
331332
newAssertion: "actual.Should().BeNull({0});")]
332-
[Implemented]
333-
public void AssertOptionalIntegerAndNullAreEqual1_TestCodeFix(string oldAssertion, string newAssertion)
334-
=> VerifyCSharpFix("int? actual", oldAssertion, newAssertion);
335-
336-
[DataTestMethod]
337-
[AssertionDiagnostic("Assert.AreEqual(null, actual{0});")]
338-
[Implemented]
339-
public void AssertOptionalIntegerAndNullAreEqual2_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);
340-
341-
[DataTestMethod]
342333
[AssertionCodeFix(
343334
oldAssertion: "Assert.AreEqual(null, actual{0});",
344335
newAssertion: "actual.Should().BeNull({0});")]
345336
[Implemented]
346-
public void AssertOptionalIntegerAndNullAreEqual2_TestCodeFix(string oldAssertion, string newAssertion)
337+
public void AssertOptionalIntegerAndNullAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
347338
=> VerifyCSharpFix("int? actual", oldAssertion, newAssertion);
348339

349340
[DataTestMethod]
350341
[AssertionDiagnostic("Assert.AreEqual(expected, actual, delta{0});")]
351342
[AssertionDiagnostic("Assert.AreEqual(expected, actual, 0.6{0});")]
343+
[AssertionDiagnostic("Assert.AreEqual(actual, 4.2d, 0.6{0});")]
344+
[AssertionDiagnostic("Assert.AreEqual(4.2d, actual, 0.6{0});")]
352345
[Implemented]
353346
public void AssertDoubleAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("double actual, double expected, double delta", assertion);
354347

@@ -359,13 +352,21 @@ public void AssertOptionalIntegerAndNullAreEqual2_TestCodeFix(string oldAssertio
359352
[AssertionCodeFix(
360353
oldAssertion: "Assert.AreEqual(expected, actual, 0.6{0});",
361354
newAssertion: "actual.Should().BeApproximately(expected, 0.6{0});")]
355+
[AssertionCodeFix(
356+
oldAssertion: "Assert.AreEqual(actual, 4.2d, 0.6{0});",
357+
newAssertion: "actual.Should().BeApproximately(4.2d, 0.6{0});")]
358+
[AssertionCodeFix(
359+
oldAssertion: "Assert.AreEqual(4.2d, actual, 0.6{0});",
360+
newAssertion: "actual.Should().BeApproximately(4.2d, 0.6{0});")]
362361
[Implemented]
363362
public void AssertDoubleAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
364363
=> VerifyCSharpFix("double actual, double expected, double delta", oldAssertion, newAssertion);
365364

366365
[DataTestMethod]
367366
[AssertionDiagnostic("Assert.AreEqual(expected, actual, delta{0});")]
368367
[AssertionDiagnostic("Assert.AreEqual(expected, actual, 0.6f{0});")]
368+
[AssertionDiagnostic("Assert.AreEqual(actual, 4.2f, 0.6f{0});")]
369+
[AssertionDiagnostic("Assert.AreEqual(4.2f, actual, 0.6f{0});")]
369370
[Implemented]
370371
public void AssertFloatAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("float actual, float expected, float delta", assertion);
371372

@@ -376,35 +377,81 @@ public void AssertDoubleAreEqual_TestCodeFix(string oldAssertion, string newAsse
376377
[AssertionCodeFix(
377378
oldAssertion: "Assert.AreEqual(expected, actual, 0.6f{0});",
378379
newAssertion: "actual.Should().BeApproximately(expected, 0.6f{0});")]
380+
[AssertionCodeFix(
381+
oldAssertion: "Assert.AreEqual(actual, 4.2f, 0.6f{0});",
382+
newAssertion: "actual.Should().BeApproximately(4.2f, 0.6f{0});")]
383+
[AssertionCodeFix(
384+
oldAssertion: "Assert.AreEqual(4.2f, actual, 0.6f{0});",
385+
newAssertion: "actual.Should().BeApproximately(4.2f, 0.6f{0});")]
379386
[Implemented]
380387
public void AssertFloatAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
381388
=> VerifyCSharpFix("float actual, float expected, float delta", oldAssertion, newAssertion);
382389

383390
[DataTestMethod]
384391
[AssertionDiagnostic("Assert.AreEqual(expected, actual{0});")]
392+
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\"{0});")]
393+
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual{0});")]
385394
[AssertionDiagnostic("Assert.AreEqual(expected, actual, false{0});")]
395+
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", false{0});")]
396+
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, false{0});")]
386397
[AssertionDiagnostic("Assert.AreEqual(expected, actual, true{0});")]
398+
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", true{0});")]
399+
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, true{0});")]
387400
[AssertionDiagnostic("Assert.AreEqual(expected, actual, false, System.Globalization.CultureInfo.CurrentCulture{0});")]
401+
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", false, System.Globalization.CultureInfo.CurrentCulture{0});")]
402+
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, false, System.Globalization.CultureInfo.CurrentCulture{0});")]
388403
[AssertionDiagnostic("Assert.AreEqual(expected, actual, true, System.Globalization.CultureInfo.CurrentCulture{0});")]
404+
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", true, System.Globalization.CultureInfo.CurrentCulture{0});")]
405+
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, true, System.Globalization.CultureInfo.CurrentCulture{0});")]
389406
[Implemented]
390407
public void AssertStringAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("string actual, string expected", assertion);
391408

392409
[DataTestMethod]
393410
[AssertionCodeFix(
394411
oldAssertion: "Assert.AreEqual(expected, actual{0});",
395412
newAssertion: "actual.Should().Be(expected{0});")]
413+
[AssertionCodeFix(
414+
oldAssertion: "Assert.AreEqual(actual, \"literal\"{0});",
415+
newAssertion: "actual.Should().Be(\"literal\"{0});")]
416+
[AssertionCodeFix(
417+
oldAssertion: "Assert.AreEqual(\"literal\", actual{0});",
418+
newAssertion: "actual.Should().Be(\"literal\"{0});")]
396419
[AssertionCodeFix(
397420
oldAssertion: "Assert.AreEqual(expected, actual, false{0});",
398421
newAssertion: "actual.Should().Be(expected{0});")]
422+
[AssertionCodeFix(
423+
oldAssertion: "Assert.AreEqual(actual, \"literal\", false{0});",
424+
newAssertion: "actual.Should().Be(\"literal\"{0});")]
425+
[AssertionCodeFix(
426+
oldAssertion: "Assert.AreEqual(\"literal\", actual, false{0});",
427+
newAssertion: "actual.Should().Be(\"literal\"{0});")]
399428
[AssertionCodeFix(
400429
oldAssertion: "Assert.AreEqual(expected, actual, true{0});",
401430
newAssertion: "actual.Should().BeEquivalentTo(expected{0});")]
431+
[AssertionCodeFix(
432+
oldAssertion: "Assert.AreEqual(actual, \"literal\", true{0});",
433+
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
434+
[AssertionCodeFix(
435+
oldAssertion: "Assert.AreEqual(\"literal\", actual, true{0});",
436+
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
402437
[AssertionCodeFix(
403438
oldAssertion: "Assert.AreEqual(expected, actual, false, System.Globalization.CultureInfo.CurrentCulture{0});",
404439
newAssertion: "actual.Should().Be(expected{0});")]
440+
[AssertionCodeFix(
441+
oldAssertion: "Assert.AreEqual(actual, \"literal\", false, System.Globalization.CultureInfo.CurrentCulture{0});",
442+
newAssertion: "actual.Should().Be(\"literal\"{0});")]
443+
[AssertionCodeFix(
444+
oldAssertion: "Assert.AreEqual(\"literal\", actual, false, System.Globalization.CultureInfo.CurrentCulture{0});",
445+
newAssertion: "actual.Should().Be(\"literal\"{0});")]
405446
[AssertionCodeFix(
406447
oldAssertion: "Assert.AreEqual(expected, actual, true, System.Globalization.CultureInfo.CurrentCulture{0});",
407448
newAssertion: "actual.Should().BeEquivalentTo(expected{0});")]
449+
[AssertionCodeFix(
450+
oldAssertion: "Assert.AreEqual(actual, \"literal\", true, System.Globalization.CultureInfo.CurrentCulture{0});",
451+
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
452+
[AssertionCodeFix(
453+
oldAssertion: "Assert.AreEqual(\"literal\", actual, true, System.Globalization.CultureInfo.CurrentCulture{0});",
454+
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
408455
[Implemented]
409456
public void AssertStringAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
410457
=> VerifyCSharpFix("string actual, string expected", oldAssertion, newAssertion);
@@ -455,20 +502,26 @@ public void AssertOptionalIntAreNotEqual_TestCodeFix(string oldAssertion, string
455502

456503
[DataTestMethod]
457504
[AssertionDiagnostic("Assert.AreNotEqual(actual, null{0});")]
505+
[AssertionDiagnostic("Assert.AreNotEqual(null, actual{0});")]
458506
[Implemented]
459507
public void AssertOptionalIntAndNullAreNotEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);
460508

461509
[DataTestMethod]
462510
[AssertionCodeFix(
463511
oldAssertion: "Assert.AreNotEqual(actual, null{0});",
464512
newAssertion: "actual.Should().NotBeNull({0});")]
513+
[AssertionCodeFix(
514+
oldAssertion: "Assert.AreNotEqual(null, actual{0});",
515+
newAssertion: "actual.Should().NotBeNull({0});")]
465516
[Implemented]
466517
public void AssertOptionalIntAndNullAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
467518
=> VerifyCSharpFix("int? actual", oldAssertion, newAssertion);
468519

469520
[DataTestMethod]
470521
[AssertionDiagnostic("Assert.AreNotEqual(expected, actual, delta{0});")]
471522
[AssertionDiagnostic("Assert.AreNotEqual(expected, actual, 0.6f{0});")]
523+
[AssertionDiagnostic("Assert.AreNotEqual(actual, 4.2f, 0.6f{0});")]
524+
[AssertionDiagnostic("Assert.AreNotEqual(4.2f, actual, 0.6f{0});")]
472525
[Implemented]
473526
public void AssertFloatAreNotEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("float actual, float expected, float delta", assertion);
474527

@@ -479,6 +532,12 @@ public void AssertOptionalIntAndNullAreNotEqual_TestCodeFix(string oldAssertion,
479532
[AssertionCodeFix(
480533
oldAssertion: "Assert.AreNotEqual(expected, actual, 0.6f{0});",
481534
newAssertion: "actual.Should().NotBeApproximately(expected, 0.6f{0});")]
535+
[AssertionCodeFix(
536+
oldAssertion: "Assert.AreNotEqual(actual, 4.2f, 0.6f{0});",
537+
newAssertion: "actual.Should().NotBeApproximately(4.2f, 0.6f{0});")]
538+
[AssertionCodeFix(
539+
oldAssertion: "Assert.AreNotEqual(4.2f, actual, 0.6f{0});",
540+
newAssertion: "actual.Should().NotBeApproximately(4.2f, 0.6f{0});")]
482541
[Implemented]
483542
public void AssertFloatAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
484543
=> VerifyCSharpFix("float actual, float expected, float delta", oldAssertion, newAssertion);

0 commit comments

Comments
 (0)