Skip to content

Commit

Permalink
🐛 when failing a test for .Orderd.Descending, message should say "des…
Browse files Browse the repository at this point in the history
…cending", not "ascending"
fluffynuts committed Nov 1, 2024
1 parent 709b8e8 commit 8642a4a
Showing 2 changed files with 404 additions and 200 deletions.
598 changes: 400 additions & 198 deletions src/NExpect.Tests/Collections/Sequences.cs
Original file line number Diff line number Diff line change
@@ -15,94 +15,161 @@ public void ShouldThrowForEmptyCollection()
{
// Arrange
// Act
Assert.That(() =>
{
Expect(new int[0])
.To.Be.Ordered.Ascending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
Assert.That(() =>
{
Expect(new int[0])
.Not.To.Be.Ordered.Ascending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
Assert.That(
() =>
{
Expect(new int[0])
.To.Be.Ordered.Ascending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
Assert.That(
() =>
{
Expect(new int[0])
.Not.To.Be.Ordered.Ascending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
// Assert
}

[Test]
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<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
Assert.That(() =>
{
Expect(collection)
.Not.To.Be.Ordered.Ascending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
GetRandomInt()
};
// Act
Assert.That(
() =>
{
Expect(collection)
.To.Be.Ordered.Ascending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
Assert.That(
() =>
{
Expect(collection)
.Not.To.Be.Ordered.Ascending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
// Assert
}

[Test]
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<UnmetExpectationException>()
.With.Message.Contains(customMessage));
Assert.That(
() =>
{
Expect(collection)
.Not.To.Be.Ordered.Ascending(() => customMessage);
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains(customMessage)
);
// Assert
}

[Test]
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
}

[Test]
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<UnmetExpectationException>()
.With.Message.Containing("ascending");
// Assert
}

[Test]
public void ShouldHaveDescendingMessage()
{
// Arrange

// Act
Expect(
() => Expect(1, 2, 3)
.To.Be.Ordered.Descending()
).To.Throw<UnmetExpectationException>()
.With.Message.Containing("descending");
// Assert
}

@@ -119,49 +186,74 @@ public void ShouldOrderCollectionOfStrings()
};

// Act
Assert.That(() =>
{
Expect(items)
.To.Be.Ordered.Ascending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>());
Assert.That(
() =>
{
Expect(items)
.To.Be.Ordered.Ascending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
);
// Assert
}

[Test]
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
}

[Test]
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<UnmetExpectationException>()
.With.Message.Contains(customMessage));
Assert.That(
() =>
{
Expect(collection)
.Not.To.Be.Ordered.Ascending(() => customMessage);
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains(customMessage)
);
// Assert
}

@@ -170,32 +262,50 @@ 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
}

[Test]
public void ShouldHandleDescendingCollection()
{
// Arrange
var collection = new[] { 3, 2, 1 };
// Act
Assert.That(() =>
{
Expect(collection)
.To.Be.Ordered.Ascending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>());
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<UnmetExpectationException>()
);
Assert.That(
() =>
{
Expect(collection)
.Not.To.Be.Ordered.Ascending();
},
Throws.Nothing
);
// Assert
}
}
@@ -208,94 +318,137 @@ public void ShouldThrowForEmptyCollection()
{
// Arrange
// Act
Assert.That(() =>
{
Expect(new int[0])
.To.Be.Ordered.Descending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
Assert.That(() =>
{
Expect(new int[0])
.Not.To.Be.Ordered.Descending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
Assert.That(
() =>
{
Expect(new int[0])
.To.Be.Ordered.Descending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
Assert.That(
() =>
{
Expect(new int[0])
.Not.To.Be.Ordered.Descending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
// Assert
}

[Test]
public void ShouldAlwaysThrowForSingleItem()
{
// Arrange
var collection = new int[] { GetRandomInt() };
// Act
Assert.That(() =>
{
Expect(collection)
.To.Be.Ordered.Descending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
Assert.That(() =>
var collection = new int[]
{
Expect(collection)
.Not.To.Be.Ordered.Descending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items"));
GetRandomInt()
};
// Act
Assert.That(
() =>
{
Expect(collection)
.To.Be.Ordered.Descending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
Assert.That(
() =>
{
Expect(collection)
.Not.To.Be.Ordered.Descending();
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("at least two items")
);
// Assert
}

[Test]
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<UnmetExpectationException>()
.With.Message.Contains(customMessage));
Assert.That(
() =>
{
Expect(collection)
.Not.To.Be.Ordered.Descending(() => customMessage);
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains(customMessage)
);
// Assert
}

[Test]
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
}

[Test]
public void ShouldHandleDescendingCollection()
{
// Arrange
var collection = new[] { 1, 2, 3 };
// Act
Assert.That(() =>
{
Expect(collection)
.To.Be.Ordered.Descending();
}, Throws.Exception.InstanceOf<UnmetExpectationException>());
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<UnmetExpectationException>()
);
Assert.That(
() =>
{
Expect(collection)
.Not.To.Be.Ordered.Descending();
},
Throws.Nothing
);
// Assert
}
}
@@ -314,74 +467,123 @@ 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<UnmetExpectationException>());
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<UnmetExpectationException>()
);
}

[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<UnmetExpectationException>());
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<UnmetExpectationException>()
);
// Assert
}

[Test]
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<UnmetExpectationException>());
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<UnmetExpectationException>()
);
// Assert
}
}
}
}
6 changes: 4 additions & 2 deletions src/NExpect/CollectionOrderMatchers.cs
Original file line number Diff line number Diff line change
@@ -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<string> customMessageGenerator
return TestOrderingOf(
actual,
comparer,
"ascending",
i => i > 0,
customMessageGenerator
);
@@ -284,6 +284,7 @@ Func<string> 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<T>(
IEnumerable<T> actual,
IComparer<T> comparer,
string direction,
Func<int, bool> failsWhen,
Func<string> customMessageGenerator
)
@@ -580,7 +582,7 @@ Func<string> customMessageGenerator
return new MatcherResult(
false,
FinalMessageFor(
() => $"Expected collection {false.AsNot()}to be ordered ascending",
() => $"Expected collection {false.AsNot()}to be ordered {direction}",
customMessageGenerator
)
);

0 comments on commit 8642a4a

Please sign in to comment.