From 8642a4af622cdb72bb8def00f0b5da8b44706b70 Mon Sep 17 00:00:00 2001 From: Davyd McColl Date: Fri, 1 Nov 2024 14:58:46 +0200 Subject: [PATCH] :bug: when failing a test for .Orderd.Descending, message should say "descending", not "ascending" --- src/NExpect.Tests/Collections/Sequences.cs | 598 ++++++++++++++------- src/NExpect/CollectionOrderMatchers.cs | 6 +- 2 files changed, 404 insertions(+), 200 deletions(-) diff --git a/src/NExpect.Tests/Collections/Sequences.cs b/src/NExpect.Tests/Collections/Sequences.cs index c0542ca2..63a53259 100644 --- a/src/NExpect.Tests/Collections/Sequences.cs +++ b/src/NExpect.Tests/Collections/Sequences.cs @@ -15,18 +15,24 @@ public void ShouldThrowForEmptyCollection() { // Arrange // Act - Assert.That(() => - { - Expect(new int[0]) - .To.Be.Ordered.Ascending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); - Assert.That(() => - { - Expect(new int[0]) - .Not.To.Be.Ordered.Ascending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); + Assert.That( + () => + { + Expect(new int[0]) + .To.Be.Ordered.Ascending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); + Assert.That( + () => + { + Expect(new int[0]) + .Not.To.Be.Ordered.Ascending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); // Assert } @@ -34,20 +40,29 @@ public void ShouldThrowForEmptyCollection() public void ShouldAlwaysThrowForSingleItem() { // Arrange - var collection = new int[] { GetRandomInt() }; - // Act - Assert.That(() => + var collection = new int[] { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Ascending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); + GetRandomInt() + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Ordered.Ascending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); // Assert } @@ -55,21 +70,31 @@ public void ShouldAlwaysThrowForSingleItem() public void ShouldHandleOrderedCollection() { // Arrange - var collection = new[] { 1, 2 }; - // Act - Assert.That(() => + var collection = new[] { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); + 1, + 2 + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, + Throws.Nothing + ); var customMessage = GetRandomWords(2); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Ascending(() => customMessage); - }, Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage)); + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Ordered.Ascending(() => customMessage); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(customMessage) + ); // Assert } @@ -77,18 +102,29 @@ public void ShouldHandleOrderedCollection() public void ShouldHandleHomogenousCollection() { // Arrange - var collection = new[] { 1, 1, 1 }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); - Assert.That(() => + var collection = new[] { - Expect(collection) - .To.Be.Ordered.Descending(); - }, Throws.Nothing); + 1, + 1, + 1 + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, + Throws.Nothing + ); // Assert } @@ -96,13 +132,44 @@ public void ShouldHandleHomogenousCollection() public void ParamsOverload() { // Arrange - + // Act - Assert.That(() => - { - Expect(1, 2, 3, 4) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); + Assert.That( + () => + { + Expect(1, 2, 3, 4) + .To.Be.Ordered.Ascending(); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldHaveAscendingMessage() + { + // Arrange + + // Act + Expect( + () => Expect(3, 2, 1) + .To.Be.Ordered.Ascending() + ).To.Throw() + .With.Message.Containing("ascending"); + // Assert + } + + [Test] + public void ShouldHaveDescendingMessage() + { + // Arrange + + // Act + Expect( + () => Expect(1, 2, 3) + .To.Be.Ordered.Descending() + ).To.Throw() + .With.Message.Containing("descending"); // Assert } @@ -119,11 +186,14 @@ public void ShouldOrderCollectionOfStrings() }; // Act - Assert.That(() => - { - Expect(items) - .To.Be.Ordered.Ascending(); - }, Throws.Exception.InstanceOf()); + Assert.That( + () => + { + Expect(items) + .To.Be.Ordered.Ascending(); + }, + Throws.Exception.InstanceOf() + ); // Assert } @@ -131,15 +201,25 @@ public void ShouldOrderCollectionOfStrings() public void ShouldHandleIOrderedEnumerableToo() { // Arrange - var numbers = new[] { 4, 7, 3, 9, 2 }; - // Act - Assert.That(() => + var numbers = new[] { - Expect(numbers.OrderBy(x => x)) - .To.Be.Ordered.Ascending(); - Expect(numbers.OrderByDescending(x => x)) - .To.Be.Ordered.Descending(); - }, Throws.Nothing); + 4, + 7, + 3, + 9, + 2 + }; + // Act + Assert.That( + () => + { + Expect(numbers.OrderBy(x => x)) + .To.Be.Ordered.Ascending(); + Expect(numbers.OrderByDescending(x => x)) + .To.Be.Ordered.Descending(); + }, + Throws.Nothing + ); // Assert } @@ -147,21 +227,33 @@ public void ShouldHandleIOrderedEnumerableToo() public void ShouldHandleOrderedCollectionOfStrings() { // Arrange - var collection = new[] { "aardvark", "dingo", "hippo", "zebra" }; - // Act - Assert.That(() => + var collection = new[] { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); + "aardvark", + "dingo", + "hippo", + "zebra" + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, + Throws.Nothing + ); var customMessage = GetRandomWords(2); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Ascending(() => customMessage); - }, Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage)); + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Ordered.Ascending(() => customMessage); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(customMessage) + ); // Assert } @@ -170,13 +262,20 @@ public void ShouldHandleOrderedCollectionOfStrings() public void ShouldHandleAllEqualCollection() { // Arrange - var collection = new[] { 0, 0 }; - // Act - Assert.That(() => + var collection = new[] { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); + 0, + 0 + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, + Throws.Nothing + ); // Assert } @@ -184,18 +283,29 @@ public void ShouldHandleAllEqualCollection() public void ShouldHandleDescendingCollection() { // Arrange - var collection = new[] { 3, 2, 1 }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Exception.InstanceOf()); - Assert.That(() => + var collection = new[] { - Expect(collection) - .Not.To.Be.Ordered.Ascending(); - }, Throws.Nothing); + 3, + 2, + 1 + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Ordered.Ascending(); + }, + Throws.Nothing + ); // Assert } } @@ -208,18 +318,24 @@ public void ShouldThrowForEmptyCollection() { // Arrange // Act - Assert.That(() => - { - Expect(new int[0]) - .To.Be.Ordered.Descending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); - Assert.That(() => - { - Expect(new int[0]) - .Not.To.Be.Ordered.Descending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); + Assert.That( + () => + { + Expect(new int[0]) + .To.Be.Ordered.Descending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); + Assert.That( + () => + { + Expect(new int[0]) + .Not.To.Be.Ordered.Descending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); // Assert } @@ -227,20 +343,29 @@ public void ShouldThrowForEmptyCollection() public void ShouldAlwaysThrowForSingleItem() { // Arrange - var collection = new int[] { GetRandomInt() }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Descending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); - Assert.That(() => + var collection = new int[] { - Expect(collection) - .Not.To.Be.Ordered.Descending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); + GetRandomInt() + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Ordered.Descending(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items") + ); // Assert } @@ -248,21 +373,31 @@ public void ShouldAlwaysThrowForSingleItem() public void ShouldHandleOrderedCollection() { // Arrange - var collection = new[] { 2, 1 }; - // Act - Assert.That(() => + var collection = new[] { - Expect(collection) - .To.Be.Ordered.Descending(); - }, Throws.Nothing); + 2, + 1 + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, + Throws.Nothing + ); var customMessage = GetRandomWords(2); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Descending(() => customMessage); - }, Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage)); + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Ordered.Descending(() => customMessage); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(customMessage) + ); // Assert } @@ -270,13 +405,20 @@ public void ShouldHandleOrderedCollection() public void ShouldHandleAllEqualCollection() { // Arrange - var collection = new[] { 0, 0 }; - // Act - Assert.That(() => + var collection = new[] { - Expect(collection) - .To.Be.Ordered.Descending(); - }, Throws.Nothing); + 0, + 0 + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, + Throws.Nothing + ); // Assert } @@ -284,18 +426,29 @@ public void ShouldHandleAllEqualCollection() public void ShouldHandleDescendingCollection() { // Arrange - var collection = new[] { 1, 2, 3 }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Descending(); - }, Throws.Exception.InstanceOf()); - Assert.That(() => + var collection = new[] { - Expect(collection) - .Not.To.Be.Ordered.Descending(); - }, Throws.Nothing); + 1, + 2, + 3 + }; + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Ordered.Descending(); + }, + Throws.Nothing + ); // Assert } } @@ -314,52 +467,85 @@ public void ShouldEnforceOrderingOnCollectionOfTwo() // Arrange var collection = new[] { - new { Id = 1, Name = "Zebra" }, - new { Id = 2, Name = "Aardvark" } + new + { + Id = 1, + Name = "Zebra" + }, + new + { + Id = 2, + Name = "Aardvark" + } }; // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.By( - o => o.Id - ); - }, Throws.Nothing); - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.By( - o => o.Name, - Direction.Descending - ); - }, Throws.Nothing); - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.By( - o => o.Name - ); - }, Throws.Exception.InstanceOf()); + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.By( + o => o.Id + ); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.By( + o => o.Name, + Direction.Descending + ); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(collection) + .To.Be.Ordered.By( + o => o.Name + ); + }, + Throws.Exception.InstanceOf() + ); } [Test] public void ShouldEnforceOrderingOnCollectionOfThree() { // Arrange - var ordered = new[] { 1, 2, 3 }; - var unordered = new[] { 3, 1, 2 }; - // Act - Assert.That(() => + var ordered = new[] { - Expect(ordered) - .To.Be.Ordered.By(i => i); - }, Throws.Nothing); - Assert.That(() => + 1, + 2, + 3 + }; + var unordered = new[] { - Expect(unordered) - .To.Be.Ordered.By(i => i); - }, Throws.Exception.InstanceOf()); + 3, + 1, + 2 + }; + // Act + Assert.That( + () => + { + Expect(ordered) + .To.Be.Ordered.By(i => i); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(unordered) + .To.Be.Ordered.By(i => i); + }, + Throws.Exception.InstanceOf() + ); // Assert } @@ -367,21 +553,37 @@ public void ShouldEnforceOrderingOnCollectionOfThree() public void ShouldNegateCorrectly() { // Arrange - - var ordered = new[] { 1, 2, 3 }; - var unordered = new[] { 3, 1, 2 }; - // Act - Assert.That(() => + + var ordered = new[] { - Expect(unordered) - .Not.To.Be.Ordered.By(i => i); - }, Throws.Nothing); - Assert.That(() => + 1, + 2, + 3 + }; + var unordered = new[] { - Expect(ordered) - .Not.To.Be.Ordered.By(i => i); - }, Throws.Exception.InstanceOf()); + 3, + 1, + 2 + }; + // Act + Assert.That( + () => + { + Expect(unordered) + .Not.To.Be.Ordered.By(i => i); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(ordered) + .Not.To.Be.Ordered.By(i => i); + }, + Throws.Exception.InstanceOf() + ); // Assert } } -} +} \ No newline at end of file diff --git a/src/NExpect/CollectionOrderMatchers.cs b/src/NExpect/CollectionOrderMatchers.cs index 107bb30d..f1d7dd6e 100644 --- a/src/NExpect/CollectionOrderMatchers.cs +++ b/src/NExpect/CollectionOrderMatchers.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; using System.Reflection; using NExpect.Interfaces; using NExpect.MatcherLogic; @@ -154,6 +153,7 @@ Func customMessageGenerator return TestOrderingOf( actual, comparer, + "ascending", i => i > 0, customMessageGenerator ); @@ -284,6 +284,7 @@ Func customMessageGenerator return TestOrderingOf( actual, comparer, + "descending", i => i < 0, customMessageGenerator ); @@ -559,6 +560,7 @@ public int Compare(object left, object right) private static MatcherResult TestOrderingOf( IEnumerable actual, IComparer comparer, + string direction, Func failsWhen, Func customMessageGenerator ) @@ -580,7 +582,7 @@ Func customMessageGenerator return new MatcherResult( false, FinalMessageFor( - () => $"Expected collection {false.AsNot()}to be ordered ascending", + () => $"Expected collection {false.AsNot()}to be ordered {direction}", customMessageGenerator ) );