Skip to content

Commit 1e6ed50

Browse files
authored
feat: add nunit assert AreNotSame (#350)
1 parent db3aeac commit 1e6ed50

File tree

5 files changed

+302
-8
lines changed

5 files changed

+302
-8
lines changed

docs/Nunit3Analyzer.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
1212
- [AssertIsNotEmpty](#scenario-assertisnotempty) - `collection.Should().NotBeEmpty();`
1313
- [AssertZero](#scenario-assertzero) - `number.Should().Be(0);`
1414
- [AssertNotZero](#scenario-assertnotzero) - `number.Should().NotBe(0);`
15+
- [AssertAreSame](#scenario-assertaresame) - `obj1.Should().BeSameAs(obj2);`
16+
- [AssertAreNotSame](#scenario-assertarenotsame) - `obj1.Should().NotBeSameAs(obj2);`
1517
- [CollectionAssertAreEqual](#scenario-collectionassertareequal) - `collection.Should().Equal(expected);`
1618
- [CollectionAssertAreNotEqual](#scenario-collectionassertarenotequal) - `collection.Should().NotEqual(expected);`
1719
- [CollectionAssertContains](#scenario-collectionassertcontains) - `collection.Should().Contain(2);`
@@ -308,6 +310,64 @@ Assert.That(number, Is.Not.Zero); /* fail message: Expected: not equal to 0
308310
number.Should().NotBe(0); /* fail message: Did not expect number to be 0. */
309311
```
310312

313+
### scenario: AssertAreSame
314+
315+
```cs
316+
// arrange
317+
var obj1 = new object();
318+
var obj2 = obj1;
319+
320+
// old assertion:
321+
Assert.AreSame(obj1, obj2);
322+
323+
// new assertion:
324+
obj1.Should().BeSameAs(obj2);
325+
```
326+
327+
#### Failure messages
328+
329+
```cs
330+
object obj1 = 6;
331+
object obj2 = "foo";
332+
333+
// old assertion:
334+
Assert.AreSame(obj1, obj2); /* fail message: Expected: same as 6
335+
But was: "foo"
336+
*/
337+
338+
// new assertion:
339+
obj1.Should().BeSameAs(obj2); /* fail message: Expected obj1 to refer to "foo", but found 6. */
340+
```
341+
342+
### scenario: AssertAreNotSame
343+
344+
```cs
345+
// arrange
346+
object obj1 = 6;
347+
object obj2 = "foo";
348+
349+
// old assertion:
350+
Assert.AreNotSame(obj1, obj2);
351+
352+
// new assertion:
353+
obj1.Should().NotBeSameAs(obj2);
354+
```
355+
356+
#### Failure messages
357+
358+
```cs
359+
var obj1 = "foo";
360+
var obj2 = "foo";
361+
362+
// old assertion:
363+
Assert.AreNotSame(obj1, obj2); /* fail message: Expected: not same as "foo"
364+
But was: "foo"
365+
*/
366+
367+
// new assertion:
368+
obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to "foo". */
369+
```
370+
311371
### scenario: CollectionAssertAreEqual
312372

313373
```cs

docs/Nunit4Analyzer.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
1212
- [AssertIsNotEmpty](#scenario-assertisnotempty) - `collection.Should().NotBeEmpty();`
1313
- [AssertZero](#scenario-assertzero) - `number.Should().Be(0);`
1414
- [AssertNotZero](#scenario-assertnotzero) - `number.Should().NotBe(0);`
15+
- [AssertAreSame](#scenario-assertaresame) - `obj1.Should().BeSameAs(obj2);`
16+
- [AssertAreNotSame](#scenario-assertarenotsame) - `obj1.Should().NotBeSameAs(obj2);`
1517
- [CollectionAssertAreEqual](#scenario-collectionassertareequal) - `collection.Should().Equal(expected);`
1618
- [CollectionAssertAreNotEqual](#scenario-collectionassertarenotequal) - `collection.Should().NotEqual(expected);`
1719
- [CollectionAssertContains](#scenario-collectionassertcontains) - `collection.Should().Contain(2);`
@@ -333,12 +335,72 @@ Assert.That(number, Is.Not.Zero); /* fail message: Assert.That(number, Is.Not.
333335
number.Should().NotBe(0); /* fail message: Did not expect number to be 0. */
334336
```
335337

338+
### scenario: AssertAreSame
339+
340+
```cs
341+
// arrange
342+
var obj1 = new object();
343+
var obj2 = obj1;
344+
345+
// old assertion:
346+
ClassicAssert.AreSame(obj1, obj2);
347+
348+
// new assertion:
349+
obj1.Should().BeSameAs(obj2);
350+
```
351+
352+
#### Failure messages
353+
354+
```cs
355+
object obj1 = 6;
356+
object obj2 = "foo";
357+
358+
// old assertion:
359+
ClassicAssert.AreSame(obj1, obj2); /* fail message: Assert.That(actual, Is.SameAs(expected))
360+
Expected: same as 6
361+
But was: "foo"
362+
*/
363+
364+
// new assertion:
365+
obj1.Should().BeSameAs(obj2); /* fail message: Expected obj1 to refer to "foo", but found 6. */
366+
```
367+
368+
### scenario: AssertAreNotSame
369+
370+
```cs
371+
// arrange
372+
object obj1 = 6;
373+
object obj2 = "foo";
374+
375+
// old assertion:
376+
ClassicAssert.AreNotSame(obj1, obj2);
377+
378+
// new assertion:
379+
obj1.Should().NotBeSameAs(obj2);
380+
```
381+
382+
#### Failure messages
383+
384+
```cs
385+
var obj1 = new object();
386+
var obj2 = obj1;
387+
388+
// old assertion:
389+
ClassicAssert.AreNotSame(obj1, obj2); /* fail message: Assert.That(actual, Is.Not.SameAs(expected))
390+
Expected: not same as <System.Object>
391+
But was: <System.Object>
392+
*/
393+
394+
// new assertion:
395+
obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to System.Object (HashCode=2766117). */
396+
```
397+
336398
### scenario: CollectionAssertAreEqual
337399

338400
```cs
339401
// arrange
340402
var collection = new[] { 1, 2, 3 };
341-
var expected = new [] { 1, 2, 3 };
403+
var expected = new[] { 1, 2, 3 };
342404

343405
// old assertion:
344406
CollectionAssert.AreEqual(expected, collection);

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public void AssertIsNotEmpty_Failure_OldAssertion_2()
358358

359359
// old assertion:
360360
CollectionAssert.IsNotEmpty(collection);
361-
}
361+
}
362362

363363
[Test, ExpectedAssertionException]
364364
public void AssertIsNotEmpty_Failure_NewAssertion()
@@ -458,12 +458,84 @@ public void AssertNotZero_Failure_NewAssertion()
458458
number.Should().NotBe(0);
459459
}
460460

461+
[Test]
462+
public void AssertAreSame()
463+
{
464+
// arrange
465+
var obj1 = new object();
466+
var obj2 = obj1;
467+
468+
// old assertion:
469+
ClassicAssert.AreSame(obj1, obj2);
470+
471+
// new assertion:
472+
obj1.Should().BeSameAs(obj2);
473+
}
474+
475+
[Test, ExpectedAssertionException]
476+
public void AssertAreSame_Failure_OldAssertion()
477+
{
478+
// arrange
479+
object obj1 = 6;
480+
object obj2 = "foo";
481+
482+
// old assertion:
483+
ClassicAssert.AreSame(obj1, obj2);
484+
}
485+
486+
[Test, ExpectedAssertionException]
487+
public void AssertAreSame_Failure_NewAssertion()
488+
{
489+
// arrange
490+
object obj1 = 6;
491+
object obj2 = "foo";
492+
493+
// new assertion:
494+
obj1.Should().BeSameAs(obj2);
495+
}
496+
497+
[Test]
498+
public void AssertAreNotSame()
499+
{
500+
// arrange
501+
object obj1 = 6;
502+
object obj2 = "foo";
503+
504+
// old assertion:
505+
ClassicAssert.AreNotSame(obj1, obj2);
506+
507+
// new assertion:
508+
obj1.Should().NotBeSameAs(obj2);
509+
}
510+
511+
[Test, ExpectedAssertionException]
512+
public void AssertAreNotSame_Failure_OldAssertion()
513+
{
514+
// arrange
515+
var obj1 = new object();
516+
var obj2 = obj1;
517+
518+
// old assertion:
519+
ClassicAssert.AreNotSame(obj1, obj2);
520+
}
521+
522+
[Test, ExpectedAssertionException]
523+
public void AssertAreNotSame_Failure_NewAssertion()
524+
{
525+
// arrange
526+
var obj1 = new object();
527+
var obj2 = obj1;
528+
529+
// new assertion:
530+
obj1.Should().NotBeSameAs(obj2);
531+
}
532+
461533
[Test]
462534
public void CollectionAssertAreEqual()
463535
{
464536
// arrange
465537
var collection = new[] { 1, 2, 3 };
466-
var expected = new [] { 1, 2, 3 };
538+
var expected = new[] { 1, 2, 3 };
467539

468540
// old assertion:
469541
CollectionAssert.AreEqual(expected, collection);

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public void AssertIsNotEmpty_Failure_OldAssertion_2()
361361

362362
// old assertion:
363363
CollectionAssert.IsNotEmpty(collection);
364-
}
364+
}
365365

366366
[TestMethod, ExpectedTestFrameworkException]
367367
public void AssertIsNotEmpty_Failure_NewAssertion()
@@ -461,6 +461,78 @@ public void AssertNotZero_Failure_NewAssertion()
461461
number.Should().NotBe(0);
462462
}
463463

464+
[TestMethod]
465+
public void AssertAreSame()
466+
{
467+
// arrange
468+
var obj1 = new object();
469+
var obj2 = obj1;
470+
471+
// old assertion:
472+
Assert.AreSame(obj1, obj2);
473+
474+
// new assertion:
475+
obj1.Should().BeSameAs(obj2);
476+
}
477+
478+
[TestMethod, ExpectedTestFrameworkException]
479+
public void AssertAreSame_Failure_OldAssertion()
480+
{
481+
// arrange
482+
object obj1 = 6;
483+
object obj2 = "foo";
484+
485+
// old assertion:
486+
Assert.AreSame(obj1, obj2);
487+
}
488+
489+
[TestMethod, ExpectedTestFrameworkException]
490+
public void AssertAreSame_Failure_NewAssertion()
491+
{
492+
// arrange
493+
object obj1 = 6;
494+
object obj2 = "foo";
495+
496+
// new assertion:
497+
obj1.Should().BeSameAs(obj2);
498+
}
499+
500+
[TestMethod]
501+
public void AssertAreNotSame()
502+
{
503+
// arrange
504+
object obj1 = 6;
505+
object obj2 = "foo";
506+
507+
// old assertion:
508+
Assert.AreNotSame(obj1, obj2);
509+
510+
// new assertion:
511+
obj1.Should().NotBeSameAs(obj2);
512+
}
513+
514+
[TestMethod, ExpectedTestFrameworkException]
515+
public void AssertAreNotSame_Failure_OldAssertion()
516+
{
517+
// arrange
518+
var obj1 = "foo";
519+
var obj2 = "foo";
520+
521+
// old assertion:
522+
Assert.AreNotSame(obj1, obj2);
523+
}
524+
525+
[TestMethod, ExpectedTestFrameworkException]
526+
public void AssertAreNotSame_Failure_NewAssertion()
527+
{
528+
// arrange
529+
var obj1 = "foo";
530+
var obj2 = "foo";
531+
532+
// new assertion:
533+
obj1.Should().NotBeSameAs(obj2);
534+
}
535+
464536
[TestMethod]
465537
public void CollectionAssertAreEqual()
466538
{

0 commit comments

Comments
 (0)