From 4c1d6e52f835dc709f0a5875e538ec63e9640209 Mon Sep 17 00:00:00 2001 From: Davyd McColl Date: Fri, 17 May 2024 14:03:42 +0200 Subject: [PATCH] :sparkles: add grammar extension .Require --- .../NExpect.Matchers.AspNetCore.csproj | 7 +- src/NExpect.Tests/ArbitraryBehaviorTests.cs | 1691 +++++----- src/NExpect.Tests/BetterStringErrors.cs | 73 +- src/NExpect.Tests/BetterStringHandling.cs | 167 +- src/NExpect.Tests/Collections/All.cs | 243 +- src/NExpect.Tests/Collections/Any.cs | 113 +- src/NExpect.Tests/Collections/AtLeast.cs | 873 +++-- src/NExpect.Tests/Collections/AtMost.cs | 113 +- ...ntMatchContinuationExtensionsForTesting.cs | 103 +- .../Collections/CustomEqualityTesting.cs | 155 +- .../Collections/CustomEquivalenceTesting.cs | 453 ++- .../Collections/DeepEqualityTesting.cs | 2289 +++++++------- .../Collections/DictionaryTesting.cs | 2231 +++++++------ .../DifferentTypesOfCollections.cs | 129 +- src/NExpect.Tests/Collections/Distinct.cs | 237 +- .../DriftingDateTimeEqualityComparer.cs | 25 +- src/NExpect.Tests/Collections/Emptiness.cs | 397 ++- src/NExpect.Tests/Collections/Equivalence.cs | 1721 +++++----- src/NExpect.Tests/Collections/Exactly.cs | 1467 +++++---- .../Collections/FirstLetterComparer.cs | 67 +- src/NExpect.Tests/Collections/Include.cs | 153 +- .../Collections/NameValueCollectionTesting.cs | 499 ++- .../Collections/NeverEqualEqualityComparer.cs | 23 +- src/NExpect.Tests/Collections/Only.cs | 823 +++-- src/NExpect.Tests/Collections/Sequences.cs | 701 +++-- src/NExpect.Tests/Collections/SetTesting.cs | 67 +- .../TestAgainstReadOnlyCollections.cs | 41 +- src/NExpect.Tests/ComposingExpectations.cs | 573 ++-- src/NExpect.Tests/DanglingPrepositions/A.cs | 121 +- src/NExpect.Tests/DanglingPrepositions/An.cs | 93 +- .../DanglingPrepositions/Contain.cs | 87 +- src/NExpect.Tests/DanglingPrepositions/For.cs | 187 +- .../DanglingPrepositions/Have.cs | 131 +- .../DanglingPrepositions/With.cs | 361 ++- src/NExpect.Tests/DeepEqualityTesting.cs | 217 +- src/NExpect.Tests/Defaults.cs | 283 +- src/NExpect.Tests/EnablingUserComposition.cs | 377 ++- .../Exceptions/EnforcingMessage.cs | 219 +- .../Exceptions/EnforcingThrownExceptions.cs | 2797 ++++++++--------- .../Exceptions/UserSpaceImplementations.cs | 147 +- src/NExpect.Tests/Issues.cs | 17 + .../ObjectEquality/ActingOnDateTimes.cs | 127 +- ...ingOnDecimalComparedWithNullableDecimal.cs | 271 +- .../ObjectEquality/ActingOnDecimals.cs | 271 +- .../ObjectEquality/ActingOnDoubles.cs | 237 +- .../ObjectEquality/ActingOnFloats.cs | 237 +- .../ObjectEquality/ActingOnInts.cs | 2475 ++++++++------- ...leDecimalComparedWithNonNullableDecimal.cs | 271 +- .../ActingOnNullableDecimals.cs | 429 ++- .../ObjectEquality/ActingOnTimeSpans.cs | 237 +- .../MultipleIComparableTypes.cs | 675 ++-- src/NExpect.Tests/ObjectEquality/Safety.cs | 113 +- .../ObjectEquality/Strings/Containing.cs | 419 ++- .../ObjectEquality/Strings/Empty.cs | 127 +- .../ObjectEquality/Strings/EndingWith.cs | 175 +- .../ObjectEquality/Strings/Equality.cs | 207 +- .../Strings/MoreStringExpectations.cs | 1085 ++++--- .../Strings/NullOrWhitespace.cs | 239 +- .../ObjectEquality/Strings/NullorEmpty.cs | 209 +- .../Strings/OrderedPartialContaining.cs | 257 +- .../ObjectEquality/Strings/RegExMatching.cs | 329 +- .../ObjectEquality/Strings/StartingWith.cs | 175 +- .../ObjectEquality/TestReflectiveMatchers.cs | 1639 +++++----- .../TestingApproximateEquality.cs | 1049 +++---- .../ObjectEquality/TestingBooleanValues.cs | 543 ++-- .../ObjectEquality/TestingForNull.cs | 229 +- .../ObjectEquality/TestingNullableTypes.cs | 91 +- src/NExpect.Tests/SetupFixture.cs | 29 +- src/NExpect.Tests/Shims/TestDictionaryShim.cs | 503 ++- src/NExpect.Tests/TestBadMatcherHandling.cs | 47 +- src/NExpect.Tests/TestEnumMatchers.cs | 185 +- src/NExpect.Tests/TestFuncFactory.cs | 45 +- src/NExpect.Tests/TestIntsAndEnumMatchers.cs | 353 ++- src/NExpect.Tests/TestRequireGrammar.cs | 101 + src/NExpect.Tests/TestStringify.cs | 207 +- src/NExpect.Tests/TestUtils.cs | 43 +- .../UsingCustomAssertionsGenerator.cs | 111 +- src/NExpect/Implementations/Find.cs | 4 + .../Implementations/Fluency/NotAfterTo.cs | 4 +- src/NExpect/Implementations/Fluency/To.cs | 1 + .../Implementations/Fluency/ToAfterNot.cs | 1 + src/NExpect/Implementations/Require.cs | 19 + src/NExpect/Interfaces/IExpectation.cs | 2 +- src/NExpect/Interfaces/INotAfterTo.cs | 21 +- src/NExpect/Interfaces/IRequire.cs | 10 + src/NExpect/Interfaces/ITo.cs | 5 + src/NExpect/Interfaces/IToAfterNot.cs | 5 + src/PeanutButter | 2 +- 88 files changed, 17196 insertions(+), 17089 deletions(-) create mode 100644 src/NExpect.Tests/TestRequireGrammar.cs create mode 100644 src/NExpect/Implementations/Require.cs create mode 100644 src/NExpect/Interfaces/IRequire.cs diff --git a/src/NExpect.Matchers.AspNetCore/NExpect.Matchers.AspNetCore.csproj b/src/NExpect.Matchers.AspNetCore/NExpect.Matchers.AspNetCore.csproj index 4a85318e..1e37a485 100644 --- a/src/NExpect.Matchers.AspNetCore/NExpect.Matchers.AspNetCore.csproj +++ b/src/NExpect.Matchers.AspNetCore/NExpect.Matchers.AspNetCore.csproj @@ -392,11 +392,8 @@ DateTimeExtensions.cs - - MIMEType.cs - - - Imported\MimeType.cs + + Imported\MimeTypes.cs Run.Until.cs diff --git a/src/NExpect.Tests/ArbitraryBehaviorTests.cs b/src/NExpect.Tests/ArbitraryBehaviorTests.cs index b05a4050..b6cd4981 100644 --- a/src/NExpect.Tests/ArbitraryBehaviorTests.cs +++ b/src/NExpect.Tests/ArbitraryBehaviorTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using NExpect.Exceptions; @@ -18,1046 +18,1045 @@ // ReSharper disable ReturnValueOfPureMethodIsNotUsed // ReSharper disable SuspiciousTypeConversion.Global -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class ArbitraryBehaviorTests { + [Test] + public void CountMatchContinuation_ShouldHaveActualPropertyExposingOriginalCollection() + { + // Arrange + using var _ = Assertions.SuspendTracking(); + var collection = GetRandomCollection(2) + .ToArray(); + // Pre-assert + // Act + var result = Expect(collection) + .To.Contain.Exactly(1); + // Assert + Expect(result.GetActual()) + .To.Be(collection); + } + + [Test] + public void WhenCustomMessageGeneratorThrows() + { + // Arrange + var collection = GetRandomCollection(2) + .ToArray(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Empty(() => throw new Exception("moo")); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("Unable to evaluate custom message expression") + ); + // Assert + } + + [Test] + public void MessageForNotContains() + { + // Arrange + var str = "moo, cow"; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(() => throw new Exception(str)) + .To.Throw() + .With.Message.Not.Containing("beef"); + }, + Throws.Nothing + ); + + Assert.That( + () => + { + Expect(() => throw new Exception(str)) + .To.Throw() + .With.Message.Not.Containing("cow"); + }, + Throws.Exception.InstanceOf() + .With.Message.Match("not\\sto\\scontain\\s\"cow\"") + ); + + Assert.That( + () => + { + Expect(() => throw new Exception(str)) + .To.Throw() + .With.Message.Containing("beef"); + }, + Throws.Exception.InstanceOf() + .With.Message.Match("to\\scontain\\s\"beef\"") + ); + // Assert + } + + [Test] + public void Inadvertent_Equals_InsteadOfEqual() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + Assert.That( + () => + { + Expect(1) + .To.Equals(1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("You probably intend to use .Equal(), not .Equals()") + ); + + Assert.That( + () => + { + Expect(1) + .Equals(1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("You probably intend to use .Equal(), not .Equals()") + ); + + Assert.That( + () => + { + Expect(1) + .Not.To.Equals(1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("You probably intend to use .Equal(), not .Equals()") + ); + // Assert + } + + [Test] + public void ExpectationContextHashingShouldThrow() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + Assert.That( + () => + { + Expect(1) + .GetHashCode(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("You probably shouldn't be hashing this") + ); + Assert.That( + () => + { + Expect(1) + .To.GetHashCode(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("You probably shouldn't be hashing this") + ); + Assert.That( + () => + { + Expect(1) + .To.Be.GetHashCode(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("You probably shouldn't be hashing this") + ); + // Assert + } + + [Test] + public void CollectionsOfNulls() + { + // Arrange + var left = new string[] { null }; + var right = new string[] { null }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(left) + .To.Be.Equivalent.To(right); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void CollectionsOfNullsDeep() + { + // Arrange + var left = new string[] { null }; + var right = new string[] { null }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(left) + .To.Be.Deep.Equivalent.To(right); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void CollectionsOfNullsDeepWhereOneHasNoNull() + { + // Arrange + var left = new string[] { null }; + var right = new[] { "cow" }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(left) + .Not.To.Be.Deep.Equivalent.To(right); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ContainInOrderContinuationAfterAnd() + { + // Arrange + var left = GetRandomString(72, 128); + var right = GetRandomString(72, 128); + // Pre-Assert + // Act + var ex = Assert.Throws( + () => Expect(left) + .To.Equal(right) + ); + // Assert + Expect(ex.Message) + .To.Start.With("Expected") + .And.To.Contain.In.Order( + "\n", + right, + "\n", + "but got", + "\n", + left + ); + } + + [Test] + public void AllowingContinuationForManyPreviouslyTerminatingExpectations() + { + // Arrange + var one = 1; + var two = 2; + var oneString = one.ToString(); + var twoString = two.ToString(); + // Act + Assert.That( + () => + { + Expect(oneString) + .Not.To.Be.Null() + .And.To.Not.Be.Null() + .And.To.Be(oneString) + .And.Not.To.Be(twoString) + .And.To.Not.Be(twoString) + .And.To.Equal(oneString); + Expect(null as string) + .To.Be.Null() + .And.Not.To.Equal(oneString); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(one) + .To.Equal(1) + .And.To.Equal(1); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(one) + .To.Equal(1) + .And.Not.To.Equal(2) + .And.To.Not.Equal(2) + .And.To.Equal(1) + .And.Not.To.Equal(2 as int?) + .And.To.Equal(1 as int?) + .And.To.Not.Equal(2 as int?) + .And.To.Equal(1 as int?) + .And.To.Be.Equal.To(1) + .And.Not.To.Be.Equal.To(2) + .And.To.Not.Be.Equal.To(2) + .And.To.Be.Greater.Than(0) + .And.To.Be.Greater.Than.Or.Equal.To(1) + .And.To.Be.Less.Than(2) + .And.To.Equal(1) + .And.To.Be.Less.Than.Or.Equal.To(1) + .And.To.Equal(1); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(one) + .To.Equal(1) + .And.To.Equal(2); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void MatcherWhichThrowsUnmetExpectationException_ShouldGetThatExactException() + { + // Arrange + // UnmetExpectationException can only be instatiated within NExpect; however, a little + // reflection can get to the internal Throw method + var method = typeof(Assertions).GetMethods(BindingFlags.Static | BindingFlags.NonPublic) + .FirstOrDefault( + mi => mi.Name == "Throw" && + mi.GetParameters() + .Length == + 1 + ); + Expect(method) + .Not.To.Be.Null(); + UnmetExpectationException expected = null; + try + { + method.Invoke(null, new object[] { GetRandomString(10) }); + } + catch (TargetInvocationException tex) + { + expected = tex.InnerException as UnmetExpectationException; + } + + // Pre-assert + // Act + try + { + Expect("moo") + .To.Moo(expected); + } + catch (Exception ex) + { + Expect(ex) + .To.Be(expected); + } + + // Assert + } + + [Test] + public void CustomCodeGettingToExpectedCountOnCountMatcher() + { + // Arrange + var expected = GetRandomInt(2, 7); + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + var continuation = Expect(new[] { 1, 2, 3 }) + .To.Contain.Exactly(expected); + var result = continuation.GetExpectedCount(); + // Assert + Expect(result) + .To.Equal(expected); + } + + [Test] + public void CustomCodeGettingToCountMatchMethod_Exactly() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + var continuation = Expect(new[] { "a", "b", "c" }) + .To.Contain.Exactly(123); + var result = continuation.GetCountMatchMethod(); + // Assert + Expect(result) + .To.Equal(CountMatchMethods.Exactly); + } + + [Test] + public void CustomCodeGettingToCountMatchMethod_AtLeast() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + var continuation = Expect(new[] { "a", "b", "c" }) + .To.Contain.At.Least(123); + var result = continuation.GetCountMatchMethod(); + // Assert + Expect(result) + .To.Equal(CountMatchMethods.Minimum); + } + + [Test] + public void CustomCodeGettingToCountMatchMethod_AtMost() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + var continuation = Expect(new[] { "a", "b", "c" }) + .To.Contain.At.Most(123); + var result = continuation.GetCountMatchMethod(); + // Assert + Expect(result) + .To.Equal(CountMatchMethods.Maximum); + } + + [Test] + public void CustomCodeGettingToCountMatchMethod_Any() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + var continuation = Expect(new[] { "a", "b", "c" }) + .To.Contain.Any; + var result = continuation.GetCountMatchMethod(); + // Assert + Expect(result) + .To.Equal(CountMatchMethods.Any); + } + + [Test] + public void CustomCodeGettingToCountMatchMethod_All() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + var continuation = Expect(new[] { "a", "b", "c" }) + .To.Contain.All; + var result = continuation.GetCountMatchMethod(); + // Assert + Expect(result) + .To.Equal(CountMatchMethods.All); + } + + [Test] + public void CustomCodeGettingToCountMatchMethod_Only() + { + // Arrange + // Pre-assert + // Act + using var _ = Assertions.SuspendTracking(); + var continuation = Expect(new[] { "a" }) + .To.Contain.Only(1); + var result = continuation.GetCountMatchMethod(); + // Assert + Expect(result) + .To.Equal(CountMatchMethods.Only); + } + + [TestCase(true)] + [TestCase(false)] + public void MatcherResultWithNoMessage(bool expected) + { + // Arrange + // Pre-assert + // Act + var result = new MatcherResult(expected); + // Assert + Expect(result.Passed) + .To.Equal(expected); + Expect(result.Message) + .To.Be.Empty(); + } + + [Test] + public void TryGetActual_ShouldThrowIf_ICanAddMatcher_HasNoActual() + { + // Arrange + // Pre-assert + // Act + Expect(() => (new SomeCanAddMatcher()).GetActual()) + .To.Throw(); + // Assert + } + + public class SomeCanAddMatcher : ICanAddMatcher + { + } + [TestFixture] - public class ArbitraryBehaviorTests + public class ShouldHaveActualProperty { [Test] - public void CountMatchContinuation_ShouldHaveActualPropertyExposingOriginalCollection() + public void CollectionEqual() { // Arrange using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(2) - .ToArray(); + var collection = GetRandomCollection(1); // Pre-assert // Act - var result = Expect(collection) - .To.Contain.Exactly(1); + var sut = Expect(collection) + .To.Be.Equal; // Assert - Expect(result.GetActual()) + Expect(sut.GetActual()) .To.Be(collection); } [Test] - public void WhenCustomMessageGeneratorThrows() + public void CollectionDeepEqual() { // Arrange - var collection = GetRandomCollection(2) - .ToArray(); + using var _ = Assertions.SuspendTracking(); + var collection = GetRandomCollection(1); // Pre-assert // Act - Assert.That( - () => - { - Expect(collection) - .To.Be.Empty(() => throw new Exception("moo")); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("Unable to evaluate custom message expression") - ); + var sut = Expect(collection) + .To.Be.Deep.Equal; // Assert + Expect(sut.GetActual()) + .To.Be(collection); } [Test] - public void MessageForNotContains() + public void CollectionEquivalent() { // Arrange - var str = "moo, cow"; + using var _ = Assertions.SuspendTracking(); + var collection = GetRandomCollection(1); // Pre-assert // Act - Assert.That( - () => - { - Expect(() => throw new Exception(str)) - .To.Throw() - .With.Message.Not.Containing("beef"); - }, - Throws.Nothing - ); - - Assert.That( - () => - { - Expect(() => throw new Exception(str)) - .To.Throw() - .With.Message.Not.Containing("cow"); - }, - Throws.Exception.InstanceOf() - .With.Message.Match("not\\sto\\scontain\\s\"cow\"") - ); - - Assert.That( - () => - { - Expect(() => throw new Exception(str)) - .To.Throw() - .With.Message.Containing("beef"); - }, - Throws.Exception.InstanceOf() - .With.Message.Match("to\\scontain\\s\"beef\"") - ); + var sut = Expect(collection) + .To.Be.Equivalent; // Assert + Expect(sut.GetActual()) + .To.Be(collection); } [Test] - public void Inadvertent_Equals_InsteadOfEqual() + public void CollectionDeepEquivalent() { // Arrange - // Pre-assert - // Act using var _ = Assertions.SuspendTracking(); - Assert.That( - () => - { - Expect(1) - .To.Equals(1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("You probably intend to use .Equal(), not .Equals()") - ); - - Assert.That( - () => - { - Expect(1) - .Equals(1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("You probably intend to use .Equal(), not .Equals()") - ); - - Assert.That( - () => - { - Expect(1) - .Not.To.Equals(1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("You probably intend to use .Equal(), not .Equals()") - ); - // Assert - } - - [Test] - public void ExpectationContextHashingShouldThrow() - { - // Arrange + var collection = GetRandomCollection(1); // Pre-assert // Act - using var _ = Assertions.SuspendTracking(); - Assert.That( - () => - { - Expect(1) - .GetHashCode(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("You probably shouldn't be hashing this") - ); - Assert.That( - () => - { - Expect(1) - .To.GetHashCode(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("You probably shouldn't be hashing this") - ); - Assert.That( - () => - { - Expect(1) - .To.Be.GetHashCode(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("You probably shouldn't be hashing this") - ); + var sut = Expect(collection) + .To.Be.Deep.Equivalent; // Assert + Expect(sut.GetActual()) + .To.Be(collection); } [Test] - public void CollectionsOfNulls() + public void CollectionIntersectionEquivalent() { // Arrange - var left = new string[] { null }; - var right = new string[] { null }; + using var _ = Assertions.SuspendTracking(); + var collection = GetRandomCollection(1); // Pre-assert // Act - Assert.That( - () => - { - Expect(left) - .To.Be.Equivalent.To(right); - }, - Throws.Nothing - ); + var sut = Expect(collection) + .To.Be.Intersection.Equivalent; // Assert + Expect(sut.GetActual()) + .To.Be(collection); } [Test] - public void CollectionsOfNullsDeep() + public void CollectionIntersectionEqual() { // Arrange - var left = new string[] { null }; - var right = new string[] { null }; + using var _ = Assertions.SuspendTracking(); + var collection = GetRandomCollection(1); // Pre-assert // Act - Assert.That( - () => - { - Expect(left) - .To.Be.Deep.Equivalent.To(right); - }, - Throws.Nothing - ); + var sut = Expect(collection) + .To.Be.Intersection.Equal; // Assert + Expect(sut.GetActual()) + .To.Be(collection); } [Test] - public void CollectionsOfNullsDeepWhereOneHasNoNull() + public void CollectionUnique() { // Arrange - var left = new string[] { null }; - var right = new[] { "cow" }; + using var _ = Assertions.SuspendTracking(); + var collection = GetRandomCollection(1); // Pre-assert // Act - Assert.That( - () => - { - Expect(left) - .Not.To.Be.Deep.Equivalent.To(right); - }, - Throws.Nothing - ); + var sut = Expect(collection) + .To.Have.Unique; // Assert + Expect(sut.GetActual()) + .To.Be(collection); } [Test] - public void ContainInOrderContinuationAfterAnd() + public void CollectionContainAt() { // Arrange - var left = GetRandomString(72, 128); - var right = GetRandomString(72, 128); - // Pre-Assert + using var _ = Assertions.SuspendTracking(); + var collection = GetRandomCollection(1); + // Pre-assert // Act - var ex = Assert.Throws( - () => Expect(left) - .To.Equal(right) - ); + var sut = Expect(collection) + .To.Contain.At; // Assert - Expect(ex.Message) - .To.Start.With("Expected") - .And.To.Contain.In.Order( - "\n", - right, - "\n", - "but got", - "\n", - left - ); + Expect(sut.GetActual()) + .To.Be(collection); } [Test] - public void AllowingContinuationForManyPreviouslyTerminatingExpectations() + public void Deep() { // Arrange - var one = 1; - var two = 2; - var oneString = one.ToString(); - var twoString = two.ToString(); + using var _ = Assertions.SuspendTracking(); + var src = new { }; + // Pre-assert // Act - Assert.That( - () => - { - Expect(oneString) - .Not.To.Be.Null() - .And.To.Not.Be.Null() - .And.To.Be(oneString) - .And.Not.To.Be(twoString) - .And.To.Not.Be(twoString) - .And.To.Equal(oneString); - Expect(null as string) - .To.Be.Null() - .And.Not.To.Equal(oneString); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(one) - .To.Equal(1) - .And.To.Equal(1); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(one) - .To.Equal(1) - .And.Not.To.Equal(2) - .And.To.Not.Equal(2) - .And.To.Equal(1) - .And.Not.To.Equal(2 as int?) - .And.To.Equal(1 as int?) - .And.To.Not.Equal(2 as int?) - .And.To.Equal(1 as int?) - .And.To.Be.Equal.To(1) - .And.Not.To.Be.Equal.To(2) - .And.To.Not.Be.Equal.To(2) - .And.To.Be.Greater.Than(0) - .And.To.Be.Greater.Than.Or.Equal.To(1) - .And.To.Be.Less.Than(2) - .And.To.Equal(1) - .And.To.Be.Less.Than.Or.Equal.To(1) - .And.To.Equal(1); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(one) - .To.Equal(1) - .And.To.Equal(2); - }, - Throws.Exception.InstanceOf() - ); + var sut = Expect(src) + .To.Deep; // Assert + Expect(sut.GetActual()) + .To.Be(src); } [Test] - public void MatcherWhichThrowsUnmetExpectationException_ShouldGetThatExactException() + public void DictionaryKeyWith() { // Arrange - // UnmetExpectationException can only be instatiated within NExpect; however, a little - // reflection can get to the internal Throw method - var method = typeof(Assertions).GetMethods(BindingFlags.Static | BindingFlags.NonPublic) - .FirstOrDefault( - mi => mi.Name == "Throw" && - mi.GetParameters() - .Length == - 1 - ); - Expect(method) - .Not.To.Be.Null(); - UnmetExpectationException expected = null; - try - { - method.Invoke(null, new object[] { GetRandomString(10) }); - } - catch (TargetInvocationException tex) + using var _ = Assertions.SuspendTracking(); + var dict = new Dictionary { - expected = tex.InnerException as UnmetExpectationException; - } - + ["key"] = "value" + }; // Pre-assert // Act - try - { - Expect("moo") - .To.Moo(expected); - } - catch (Exception ex) - { - Expect(ex) - .To.Be(expected); - } - + var sut = Expect(dict) + .To.Contain.Key("key") + .With; // Assert + Expect(sut.GetActual()) + .To.Equal("value"); } [Test] - public void CustomCodeGettingToExpectedCountOnCountMatcher() + public void BeEqual() { // Arrange - var expected = GetRandomInt(2, 7); + using var _ = Assertions.SuspendTracking(); + var src = GetRandomString(); // Pre-assert // Act - using var _ = Assertions.SuspendTracking(); - var continuation = Expect(new[] { 1, 2, 3 }) - .To.Contain.Exactly(expected); - var result = continuation.GetExpectedCount(); + var sut = Expect(src) + .To.Be.Equal; // Assert - Expect(result) - .To.Equal(expected); + Expect(sut.GetActual()) + .To.Be(src); } [Test] - public void CustomCodeGettingToCountMatchMethod_Exactly() + public void Intersection() { // Arrange + using var _ = Assertions.SuspendTracking(); + var expected = GetRandom(); // Pre-assert // Act - using var _ = Assertions.SuspendTracking(); - var continuation = Expect(new[] { "a", "b", "c" }) - .To.Contain.Exactly(123); - var result = continuation.GetCountMatchMethod(); + var sut = Expect(expected) + .To.Intersection; // Assert - Expect(result) - .To.Equal(CountMatchMethods.Exactly); + Expect(sut.GetActual()) + .To.Equal(expected); } [Test] - public void CustomCodeGettingToCountMatchMethod_AtLeast() + public void NotNullOr() { // Arrange + using var _ = Assertions.SuspendTracking(); + var expected = GetRandomString(10); // Pre-assert // Act - using var _ = Assertions.SuspendTracking(); - var continuation = Expect(new[] { "a", "b", "c" }) - .To.Contain.At.Least(123); - var result = continuation.GetCountMatchMethod(); + var sut = Expect(expected) + .Not.To.Be.Null.Or; // Assert - Expect(result) - .To.Equal(CountMatchMethods.Minimum); + Expect(sut.GetActual()) + .To.Be(expected); } [Test] - public void CustomCodeGettingToCountMatchMethod_AtMost() + public void LessContinuation() { // Arrange + using var _ = Assertions.SuspendTracking(); + var expected = GetRandomInt(); // Pre-assert // Act - using var _ = Assertions.SuspendTracking(); - var continuation = Expect(new[] { "a", "b", "c" }) - .To.Contain.At.Most(123); - var result = continuation.GetCountMatchMethod(); + var sut = Expect(expected) + .To.Be.Less; // Assert - Expect(result) - .To.Equal(CountMatchMethods.Maximum); + Expect(sut.GetActual()) + .To.Equal(expected); } [Test] - public void CustomCodeGettingToCountMatchMethod_Any() + public void ThrowContinuationOfT() { // Arrange + var expected = new InvalidOperationException(GetRandomString(10)); // Pre-assert // Act - using var _ = Assertions.SuspendTracking(); - var continuation = Expect(new[] { "a", "b", "c" }) - .To.Contain.Any; - var result = continuation.GetCountMatchMethod(); + var sut = Expect(() => throw expected) + .To.Throw(); // Assert - Expect(result) - .To.Equal(CountMatchMethods.Any); + Expect(sut.GetActual()) + .To.Equal(expected); } [Test] - public void CustomCodeGettingToCountMatchMethod_All() + public void ThrowContinuation() { // Arrange + var expected = new InvalidOperationException(GetRandomString(10)); // Pre-assert // Act - using var _ = Assertions.SuspendTracking(); - var continuation = Expect(new[] { "a", "b", "c" }) - .To.Contain.All; - var result = continuation.GetCountMatchMethod(); + var sut = Expect(() => throw expected) + .To.Throw(); // Assert - Expect(result) - .To.Equal(CountMatchMethods.All); + Expect(sut.GetActual()) + .To.Equal(expected); } + } - [Test] - public void CustomCodeGettingToCountMatchMethod_Only() - { - // Arrange - // Pre-assert - // Act - using var _ = Assertions.SuspendTracking(); - var continuation = Expect(new[] { "a" }) - .To.Contain.Only(1); - var result = continuation.GetCountMatchMethod(); - // Assert - Expect(result) - .To.Equal(CountMatchMethods.Only); - } - - [TestCase(true)] - [TestCase(false)] - public void MatcherResultWithNoMessage(bool expected) - { - // Arrange - // Pre-assert - // Act - var result = new MatcherResult(expected); - // Assert - Expect(result.Passed) - .To.Equal(expected); - Expect(result.Message) - .To.Be.Empty(); - } + [Test] + public void CountMatchDeepEqual_ShouldExposeOriginalContinuation() + { + // Arrange + using var _ = Assertions.SuspendTracking(); + // Pre-assert + // Act + var original = Expect(new[] { 1 }) + .To.Contain; + var sut = original.Exactly(1) + .Deep.Equal; + // Assert + Expect(sut.GetPropertyValue("Continuation")) + .To.Be(original); + } - [Test] - public void TryGetActual_ShouldThrowIf_ICanAddMatcher_HasNoActual() - { - // Arrange - // Pre-assert - // Act - Expect(() => (new SomeCanAddMatcher()).GetActual()) - .To.Throw(); - // Assert - } + [Test] + public void CountMatchIntersectionEqual_ShouldExposeOriginalContinuation() + { + // Arrange + using var _ = Assertions.SuspendTracking(); + // Pre-assert + // Act + var original = Expect(new[] { 1 }) + .To.Contain; + var sut = original.Exactly(1) + .Intersection.Equal; + // Assert + Expect(sut.GetPropertyValue("Continuation")) + .To.Be(original); + } - public class SomeCanAddMatcher : ICanAddMatcher - { - } + [Test] + public void DeepEquivalenceVsNull() + { + // Arrange + var src = new[] { "hello" }; + var test = new[] { null as string }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(src) + .Not.To.Be.Equivalent.To(test); + Expect(test) + .Not.To.Be.Equivalent.To(src); + }, + Throws.Nothing + ); + // Assert + } + [TestFixture] + public class DanglersForUserspaceExtension + { [TestFixture] - public class ShouldHaveActualProperty + public class AndExtension { [Test] - public void CollectionEqual() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Be.Equal; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void CollectionDeepEqual() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Be.Deep.Equal; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void CollectionEquivalent() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Be.Equivalent; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void CollectionDeepEquivalent() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Be.Deep.Equivalent; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void CollectionIntersectionEquivalent() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Be.Intersection.Equivalent; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void CollectionIntersectionEqual() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Be.Intersection.Equal; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void CollectionUnique() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Have.Unique; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void CollectionContainAt() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var collection = GetRandomCollection(1); - // Pre-assert - // Act - var sut = Expect(collection) - .To.Contain.At; - // Assert - Expect(sut.GetActual()) - .To.Be(collection); - } - - [Test] - public void Deep() + public void ShouldHave_HaveAndAn() { // Arrange - using var _ = Assertions.SuspendTracking(); - var src = new { }; // Pre-assert // Act - var sut = Expect(src) - .To.Deep; - // Assert - Expect(sut.GetActual()) - .To.Be(src); - } - - [Test] - public void DictionaryKeyWith() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var dict = new Dictionary - { - ["key"] = "value" - }; - // Pre-assert - // Act - var sut = Expect(dict) - .To.Contain.Key("key") - .With; - // Assert - Expect(sut.GetActual()) - .To.Equal("value"); - } - - [Test] - public void BeEqual() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var src = GetRandomString(); - // Pre-assert - // Act - var sut = Expect(src) - .To.Be.Equal; - // Assert - Expect(sut.GetActual()) - .To.Be(src); - } - - [Test] - public void Intersection() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var expected = GetRandom(); - // Pre-assert - // Act - var sut = Expect(expected) - .To.Intersection; - // Assert - Expect(sut.GetActual()) - .To.Equal(expected); - } - - [Test] - public void NotNullOr() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var expected = GetRandomString(10); - // Pre-assert - // Act - var sut = Expect(expected) - .Not.To.Be.Null.Or; - // Assert - Expect(sut.GetActual()) - .To.Be(expected); - } - - [Test] - public void LessContinuation() - { - // Arrange - using var _ = Assertions.SuspendTracking(); - var expected = GetRandomInt(); - // Pre-assert - // Act - var sut = Expect(expected) - .To.Be.Less; - // Assert - Expect(sut.GetActual()) - .To.Equal(expected); - } - - [Test] - public void ThrowContinuationOfT() - { - // Arrange - var expected = new InvalidOperationException(GetRandomString(10)); - // Pre-assert - // Act - var sut = Expect(() => throw expected) - .To.Throw(); - // Assert - Expect(sut.GetActual()) - .To.Equal(expected); - } - - [Test] - public void ThrowContinuation() - { - // Arrange - var expected = new InvalidOperationException(GetRandomString(10)); - // Pre-assert - // Act - var sut = Expect(() => throw expected) - .To.Throw(); + Assert.That( + () => + { + Expect("foo the ant") + .To.Have.A.Space() + .And.Have.A.Foo() + .And.An.Ant(); + }, + Throws.Nothing + ); // Assert - Expect(sut.GetActual()) - .To.Equal(expected); } } [Test] - public void CountMatchDeepEqual_ShouldExposeOriginalContinuation() + public void ExceptionPropertyCollectionEquivalenceTesting_Danglers() { // Arrange - using var _ = Assertions.SuspendTracking(); + var expected = new SomeNode() + { + Id = 1, + Name = "Moo" + }; // Pre-assert // Act - var original = Expect(new[] { 1 }) - .To.Contain; - var sut = original.Exactly(1) - .Deep.Equal; - // Assert - Expect(sut.GetPropertyValue("Continuation")) - .To.Be(original); + Assert.That( + () => + { + Expect(() => throw new ExceptionWithNode(expected)) + .To.Throw() + .With.CollectionProperty(e => e.Nodes) + .For.Moo(); + }, + Throws.Nothing + ); } [Test] - public void CountMatchIntersectionEqual_ShouldExposeOriginalContinuation() + public void ExceptionCollectionPropertyMatchingLikeOtherCollections() { // Arrange - using var _ = Assertions.SuspendTracking(); - // Pre-assert + var ex = new AggregateException("moo", new[] { new Exception("1"), new Exception("2") }); // Act - var original = Expect(new[] { 1 }) - .To.Contain; - var sut = original.Exactly(1) - .Intersection.Equal; + Assert.That( + () => + { + Expect(() => throw ex) + .To.Throw() + .With.CollectionProperty(e => e.InnerExceptions) + .Not.Containing.Exactly(1) + .Matched.By(e => e.Message == "1"); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(() => throw ex) + .To.Throw() + .With.CollectionProperty(e => e.InnerExceptions) + .Containing.Exactly(1) + .Matched.By(e => e.Message == "1"); + Expect(() => throw ex) + .To.Throw() + .With.CollectionProperty(e => e.InnerExceptions) + .Not.Containing.Any + .Matched.By(e => e.Message == "3"); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(() => throw ex) + .To.Throw() + .With.CollectionProperty(e => e.InnerExceptions) + .Not.Containing.Any + .Matched.By(e => e.Message == "2"); + }, + Throws.Exception.InstanceOf() + ); // Assert - Expect(sut.GetPropertyValue("Continuation")) - .To.Be(original); } [Test] - public void DeepEquivalenceVsNull() + public void InDangler() { // Arrange - var src = new[] { "hello" }; - var test = new[] { null as string }; - // Pre-assert + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize() + .JoinWith(" "); + // Pre-Assert // Act Assert.That( () => { - Expect(src) - .Not.To.Be.Equivalent.To(test); - Expect(test) - .Not.To.Be.Equivalent.To(src); + Expect( + () => throw new ArgumentNullException(message) + ) + .To.Throw() + .With.Message.Containing(e1); }, Throws.Nothing ); // Assert } - [TestFixture] - public class DanglersForUserspaceExtension + [TestCase("Negate")] + [TestCase("ResetNegation")] + public void NotInstanceDoesNotSupport_(string method) { - [TestFixture] - public class AndExtension + // Arrange + using var _ = Assertions.SuspendTracking(); + // Pre-assert + // Act + var continuation = Expect(new object()) + .Not.To.Be.An.Instance; + var mi = continuation.GetType() + .GetMethod(method); + Expect(mi) + .Not.To.Be.Null(); + Expect(() => mi.Invoke(continuation, new object[0])) + .To.Throw(); + // Assert + } + + [Test] + public void UnmetExpectationStackTraces_ShouldOmitTraversalThroughNExpect() + { + // Arrange + // Pre-assert + // Act + UnmetExpectationException captured = null; + try { - [Test] - public void ShouldHave_HaveAndAn() - { - // Arrange - // Pre-assert - // Act - Assert.That( - () => - { - Expect("foo the ant") - .To.Have.A.Space() - .And.Have.A.Foo() - .And.An.Ant(); - }, - Throws.Nothing - ); - // Assert - } + Expect(1) + .To.Be.Falsey(); } - - [Test] - public void ExceptionPropertyCollectionEquivalenceTesting_Danglers() + catch (UnmetExpectationException ex) { - // Arrange - var expected = new SomeNode() - { - Id = 1, - Name = "Moo" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new ExceptionWithNode(expected)) - .To.Throw() - .With.CollectionProperty(e => e.Nodes) - .For.Moo(); - }, - Throws.Nothing - ); + captured = ex; } - [Test] - public void ExceptionCollectionPropertyMatchingLikeOtherCollections() + // Assert + Expect(captured) + .Not.To.Be.Null(); + var lines = captured.StackTrace.Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); + Expect(lines) + .To.Contain.Only(1) + .Item(); + Expect(lines[0]) + .To.Contain(nameof(UnmetExpectationStackTraces_ShouldOmitTraversalThroughNExpect)); + } + } +} + +public static class MatcherThrowingUnmetExpectationException +{ + public static void CurrentFilePath( + this IStringContain contain, + [CallerFilePath] string path = null + ) + { + contain.AddMatcher( + actual => { - // Arrange - var ex = new AggregateException("moo", new[] { new Exception("1"), new Exception("2") }); - // Act - Assert.That( - () => - { - Expect(() => throw ex) - .To.Throw() - .With.CollectionProperty(e => e.InnerExceptions) - .Not.Containing.Exactly(1) - .Matched.By(e => e.Message == "1"); - }, - Throws.Exception.InstanceOf() + // ReSharper disable once AssignNullToNotNullAttribute + var passed = actual.Contains(path); + return new MatcherResult( + passed, + () => $"Expected {actual} to contain {path}" ); - Assert.That( - () => - { - Expect(() => throw ex) - .To.Throw() - .With.CollectionProperty(e => e.InnerExceptions) - .Containing.Exactly(1) - .Matched.By(e => e.Message == "1"); - Expect(() => throw ex) - .To.Throw() - .With.CollectionProperty(e => e.InnerExceptions) - .Not.Containing.Any - .Matched.By(e => e.Message == "3"); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(() => throw ex) - .To.Throw() - .With.CollectionProperty(e => e.InnerExceptions) - .Not.Containing.Any - .Matched.By(e => e.Message == "2"); - }, - Throws.Exception.InstanceOf() - ); - // Assert } + ); + } - [Test] - public void InDangler() + public static void Falsey(this IBe be) + { + be.AddMatcher( + actual => { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize() - .JoinWith(" "); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => throw new ArgumentNullException(message) - ) - .To.Throw() - .With.Message.Containing(e1); - }, - Throws.Nothing + var passed = actual == 0; + return new MatcherResult( + passed, + () => $"Expected {actual} {passed.AsNot()}to be falsey" ); - // Assert } + ); + } - [TestCase("Negate")] - [TestCase("ResetNegation")] - public void NotInstanceDoesNotSupport_(string method) + public static void Moo(this ICollectionFor continuation) + { + continuation.Compose( + actual => { - // Arrange - using var _ = Assertions.SuspendTracking(); - // Pre-assert - // Act - var continuation = Expect(new object()) - .Not.To.Be.An.Instance; - var mi = continuation.GetType() - .GetMethod(method); - Expect(mi) - .Not.To.Be.Null(); - Expect(() => mi.Invoke(continuation, new object[0])) - .To.Throw(); - // Assert + Expect(actual) + .To.Contain.Exactly(1) + .Matched.By(n => n.Name == "Moo"); } + ); + } - [Test] - public void UnmetExpectationStackTraces_ShouldOmitTraversalThroughNExpect() - { - // Arrange - // Pre-assert - // Act - UnmetExpectationException captured = null; - try - { - Expect(1) - .To.Be.Falsey(); - } - catch (UnmetExpectationException ex) - { - captured = ex; - } + public static void Moo( + this ITo to, + UnmetExpectationException ex + ) + { + to.AddMatcher(actual => throw ex); + } - // Assert - Expect(captured) - .Not.To.Be.Null(); - var lines = captured.StackTrace.Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); - Expect(lines) - .To.Contain.Only(1) - .Item(); - Expect(lines[0]) - .To.Contain(nameof(UnmetExpectationStackTraces_ShouldOmitTraversalThroughNExpect)); + public static IMore Space(this IA a) + { + return a.Compose( + actual => + { + Expect(actual) + .To.Contain(" "); } - } + ); } - public static class MatcherThrowingUnmetExpectationException + public static IMore Foo(this IA a) { - public static void CurrentFilePath( - this IStringContain contain, - [CallerFilePath] string path = null - ) - { - contain.AddMatcher( - actual => - { - // ReSharper disable once AssignNullToNotNullAttribute - var passed = actual.Contains(path); - return new MatcherResult( - passed, - () => $"Expected {actual} to contain {path}" - ); - } - ); - } - - public static void Falsey(this IBe be) - { - be.AddMatcher( - actual => - { - var passed = actual == 0; - return new MatcherResult( - passed, - () => $"Expected {actual} {passed.AsNot()}to be falsey" - ); - } - ); - } - - public static void Moo(this ICollectionFor continuation) - { - continuation.Compose( - actual => - { - Expect(actual) - .To.Contain.Exactly(1) - .Matched.By(n => n.Name == "Moo"); - } - ); - } - - public static void Moo( - this ITo to, - UnmetExpectationException ex - ) - { - to.AddMatcher(actual => throw ex); - } - - public static IMore Space(this IA a) - { - return a.Compose( - actual => - { - Expect(actual) - .To.Contain(" "); - } - ); - } - - public static IMore Foo(this IA a) - { - return a.Compose( - actual => - { - Expect(actual) - .To.Contain("foo"); - } - ); - } + return a.Compose( + actual => + { + Expect(actual) + .To.Contain("foo"); + } + ); + } - public static IMore Ant(this IAn an) - { - return an.Compose( - actual => - { - Expect(actual) - .To.Contain("ant"); - } - ); - } + public static IMore Ant(this IAn an) + { + return an.Compose( + actual => + { + Expect(actual) + .To.Contain("ant"); + } + ); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/BetterStringErrors.cs b/src/NExpect.Tests/BetterStringErrors.cs index aad8a99c..1817103f 100644 --- a/src/NExpect.Tests/BetterStringErrors.cs +++ b/src/NExpect.Tests/BetterStringErrors.cs @@ -1,43 +1,42 @@ -using NUnit.Framework; +using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class BetterStringErrors { - [TestFixture] - public class BetterStringErrors + [Test] + public void ShouldReportNewlineDifferences() { - [Test] - public void ShouldReportNewlineDifferences() - { - // Arrange - var a = "foo\nbar"; - var b = "foo\r\nbar"; - // Act - Assert.That(() => - { - Expect(a) - .To.Equal(b); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("values have different line endings") - ); - // Assert - } + // Arrange + var a = "foo\nbar"; + var b = "foo\r\nbar"; + // Act + Assert.That(() => + { + Expect(a) + .To.Equal(b); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("values have different line endings") + ); + // Assert + } - [Test] - public void ShouldReportPositionOfFirstDifference() - { - // Arrange - var a = "foobar"; - var b = "fooquux"; - // Act - Assert.That(() => - { - Expect(a) - .To.Equal(b); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("first difference found at character 3") - ); - // Assert - } + [Test] + public void ShouldReportPositionOfFirstDifference() + { + // Arrange + var a = "foobar"; + var b = "fooquux"; + // Act + Assert.That(() => + { + Expect(a) + .To.Equal(b); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("first difference found at character 3") + ); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/BetterStringHandling.cs b/src/NExpect.Tests/BetterStringHandling.cs index a6fefb64..fbf4be43 100644 --- a/src/NExpect.Tests/BetterStringHandling.cs +++ b/src/NExpect.Tests/BetterStringHandling.cs @@ -1,94 +1,93 @@ using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class BetterStringHandling { - [TestFixture] - public class BetterStringHandling + [Test] + public void ShouldReportWhenStringsOnlyDifferByWhitespace() { - [Test] - public void ShouldReportWhenStringsOnlyDifferByWhitespace() - { - // Arrange - var result = "foo bar"; - var expected = "foo\tbar"; - // Act - Assert.That(() => - { - Expect(result) - .To.Equal(expected); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("values have whitespace differences") - ); - // Assert - } + // Arrange + var result = "foo bar"; + var expected = "foo\tbar"; + // Act + Assert.That(() => + { + Expect(result) + .To.Equal(expected); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("values have whitespace differences") + ); + // Assert + } - [Test] - public void ShouldReportWhenStringsOnlyDifferByLineEndings() - { - // Arrange - var result = "foo\r\nbar\r\nquuz"; - var expected = "foo\nbar\nquuz"; - // Act - Assert.That(() => - { - Expect(result) - .To.Equal(expected); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("values have different line endings (CRLF vs LF)") - ); - // Assert - } + [Test] + public void ShouldReportWhenStringsOnlyDifferByLineEndings() + { + // Arrange + var result = "foo\r\nbar\r\nquuz"; + var expected = "foo\nbar\nquuz"; + // Act + Assert.That(() => + { + Expect(result) + .To.Equal(expected); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("values have different line endings (CRLF vs LF)") + ); + // Assert + } - [Test] - public void ShouldReportWhenOnlyDifferenceIsCasing() - { - // Arrange - var result = "Foo Bar"; - var expected = "foo bar"; - // Act - Assert.That(() => - { - Expect(result) - .To.Equal(expected); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("values have different casing") - ); - // Assert - } + [Test] + public void ShouldReportWhenOnlyDifferenceIsCasing() + { + // Arrange + var result = "Foo Bar"; + var expected = "foo bar"; + // Act + Assert.That(() => + { + Expect(result) + .To.Equal(expected); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("values have different casing") + ); + // Assert + } - [Test] - public void ShouldGiveAClueAboutWhereDifferencesStart() - { - // Arrange - var result = "foo bar"; - var expected = "foo quux"; - // Act - Assert.That(() => - { - Expect(result) - .To.Equal(expected); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("first difference found at character 4") - ); - // Assert - } + [Test] + public void ShouldGiveAClueAboutWhereDifferencesStart() + { + // Arrange + var result = "foo bar"; + var expected = "foo quux"; + // Act + Assert.That(() => + { + Expect(result) + .To.Equal(expected); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("first difference found at character 4") + ); + // Assert + } - [Test] - public void ShouldHandleWhenLeftIsTruncatedRight() - { - // Arrange - var result = "foo"; - var expected = "foo bar"; - // Act - Assert.That(() => - { - Expect(result) - .To.Equal(expected); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("character 3") - ); - // Assert - } + [Test] + public void ShouldHandleWhenLeftIsTruncatedRight() + { + // Arrange + var result = "foo"; + var expected = "foo bar"; + // Act + Assert.That(() => + { + Expect(result) + .To.Equal(expected); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("character 3") + ); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/All.cs b/src/NExpect.Tests/Collections/All.cs index 2f2f7b73..595bfb4a 100644 --- a/src/NExpect.Tests/Collections/All.cs +++ b/src/NExpect.Tests/Collections/All.cs @@ -1,147 +1,146 @@ -using System.Linq; +using System.Linq; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class All { - [TestFixture] - public class All + [Test] + public void WhenAllMatch_ShouldNotThrow() { - [Test] - public void WhenAllMatch_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(4); - var collection = PyLike.Range(GetRandomInt(3, 5)).Select(i => search); + // Arrange + var search = GetRandomString(4); + var collection = PyLike.Range(GetRandomInt(3, 5)).Select(i => search); - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection) - .To.Contain.All - .Equal.To(search); - }, - Throws.Nothing); - // Assert - } + // Act + Assert.That(() => + { + Expect(collection) + .To.Contain.All + .Equal.To(search); + }, + Throws.Nothing); + // Assert + } - [Test] - public void WhenNotAllMatch_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(4); - var collection = PyLike.Range(GetRandomInt(3, 5)) - .Select(i => search) - .Union(new[] {GetAnother(search)}); + [Test] + public void WhenNotAllMatch_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(4); + var collection = PyLike.Range(GetRandomInt(3, 5)) + .Select(i => search) + .Union(new[] {GetAnother(search)}); - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection) - .To.Contain.All - .Equal.To(search); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Act + Assert.That(() => + { + Expect(collection) + .To.Contain.All + .Equal.To(search); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void Negated_WhenAllMatch_ShouldThrow() - { - // Arrange - var search = GetRandomString(4); - var collection = PyLike.Range(GetRandomInt(3, 5)).Select(i => search); + [Test] + public void Negated_WhenAllMatch_ShouldThrow() + { + // Arrange + var search = GetRandomString(4); + var collection = PyLike.Range(GetRandomInt(3, 5)).Select(i => search); - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection) - .Not.To.Contain.All - .Equal.To(search); - }, - Throws.Exception.InstanceOf()); + // Act + Assert.That(() => + { + Expect(collection) + .Not.To.Contain.All + .Equal.To(search); + }, + Throws.Exception.InstanceOf()); - // Assert - } + // Assert + } + + [Test] + public void NegatedAlt_WhenAllMatch_ShouldThrow() + { + // Arrange + var search = GetRandomString(4); + var collection = PyLike.Range(GetRandomInt(3, 5)).Select(i => search); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection) + .To.Not.Contain.All + .Equal.To(search); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [TestFixture] + public class CollectionItemReferenceEquality + { [Test] - public void NegatedAlt_WhenAllMatch_ShouldThrow() + public void ShouldBeAbleToAssertCollectionItemsAreReferenceEqual() { // Arrange - var search = GetRandomString(4); - var collection = PyLike.Range(GetRandomInt(3, 5)).Select(i => search); - - // Pre-Assert - + var collection1 = new[] { new object(), new object() }; + var collection2 = collection1.ToArray(); + var outOfOrder = new[] { collection1[1], collection1[0] }; // Act + // this is the use-case: when the collection + // is cloned but the items are not + Assert.That(() => + { + Expect(collection1) + .To.Be(collection2); + }, Throws.Exception.InstanceOf()); + + // positive test + Assert.That(() => + { + Expect(collection1) + .Items.To.Be(collection2); + }, Throws.Nothing); + + // negative test + Assert.That(() => + { + Expect(collection1) + .Items.Not.To.Be(collection2); + }, Throws.Exception.InstanceOf()); + + // negative test + Assert.That(() => + { + Expect(collection1) + .Items.To.Not.Be(collection2); + }, Throws.Exception.InstanceOf()); + + // equality != equivalence Assert.That(() => - { - Expect(collection) - .To.Not.Contain.All - .Equal.To(search); - }, - Throws.Exception.InstanceOf()); + { + Expect(collection1) + .Items.To.Be(outOfOrder); + }, Throws.Exception.InstanceOf()); // Assert } - - [TestFixture] - public class CollectionItemReferenceEquality - { - [Test] - public void ShouldBeAbleToAssertCollectionItemsAreReferenceEqual() - { - // Arrange - var collection1 = new[] { new object(), new object() }; - var collection2 = collection1.ToArray(); - var outOfOrder = new[] { collection1[1], collection1[0] }; - // Act - // this is the use-case: when the collection - // is cloned but the items are not - Assert.That(() => - { - Expect(collection1) - .To.Be(collection2); - }, Throws.Exception.InstanceOf()); - - // positive test - Assert.That(() => - { - Expect(collection1) - .Items.To.Be(collection2); - }, Throws.Nothing); - - // negative test - Assert.That(() => - { - Expect(collection1) - .Items.Not.To.Be(collection2); - }, Throws.Exception.InstanceOf()); - - // negative test - Assert.That(() => - { - Expect(collection1) - .Items.To.Not.Be(collection2); - }, Throws.Exception.InstanceOf()); - - // equality != equivalence - Assert.That(() => - { - Expect(collection1) - .Items.To.Be(outOfOrder); - }, Throws.Exception.InstanceOf()); - - // Assert - } - } - } -} \ No newline at end of file + +} diff --git a/src/NExpect.Tests/Collections/Any.cs b/src/NExpect.Tests/Collections/Any.cs index d4457caa..cfdec6c3 100644 --- a/src/NExpect.Tests/Collections/Any.cs +++ b/src/NExpect.Tests/Collections/Any.cs @@ -1,77 +1,76 @@ -using System.Linq; +using System.Linq; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.Utils; // ReSharper disable PossibleMultipleEnumeration -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Any { - [TestFixture] - public class Any + [Test] + public void WhenHave1MatchInActual_ShouldNotThrow() { - [Test] - public void WhenHave1MatchInActual_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var actual = GetRandomCollection(2, 4).Union(search.InArray()); + // Arrange + var search = GetRandomString(); + var actual = GetRandomCollection(2, 4).Union(search.InArray()); - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual) - .To.Contain.Any - .Equal.To(search); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(actual) + .To.Contain.Any + .Equal.To(search); + }, + Throws.Nothing); - // Assert - } + // Assert + } - [Test] - public void WhenHave0MatchInActual_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var actual = GetRandomCollection(2, 4); + [Test] + public void WhenHave0MatchInActual_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(); + var actual = GetRandomCollection(2, 4); - // Pre-Assert - Assert.That(actual, Does.Not.Contain(search), "Should not find search before test"); + // Pre-Assert + Assert.That(actual, Does.Not.Contain(search), "Should not find search before test"); - // Act - Assert.That(() => - { - Expect(actual) - .To.Contain.Any - .Equal.To(search); - }, - Throws.Exception.InstanceOf()); + // Act + Assert.That(() => + { + Expect(actual) + .To.Contain.Any + .Equal.To(search); + }, + Throws.Exception.InstanceOf()); - // Assert - } + // Assert + } - [Test] - public void WhenHaveActualAllMatchingSearch_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var actual = PyLike.Range(GetRandomInt(2, 4)).Select(_ => search); + [Test] + public void WhenHaveActualAllMatchingSearch_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(); + var actual = PyLike.Range(GetRandomInt(2, 4)).Select(_ => search); - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual) - .To.Contain.Any - .Equal.To(search); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(actual) + .To.Contain.Any + .Equal.To(search); + }, + Throws.Nothing); - // Assert - } + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/AtLeast.cs b/src/NExpect.Tests/Collections/AtLeast.cs index 436654b3..4af391cd 100644 --- a/src/NExpect.Tests/Collections/AtLeast.cs +++ b/src/NExpect.Tests/Collections/AtLeast.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using NExpect.Exceptions; using NExpect.Implementations; using NUnit.Framework; @@ -8,534 +8,533 @@ // ReSharper disable InconsistentNaming // ReSharper disable RedundantAnonymousTypePropertyName -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class AtLeast_N { [TestFixture] - public class AtLeast_N + public class EqualTo { - [TestFixture] - public class EqualTo + [Test] + public void Contain_GivenAtLeast1_WhenCollectionHasNone_ShouldThrow() { - [Test] - public void Contain_GivenAtLeast1_WhenCollectionHasNone_ShouldThrow() - { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { item1, item2 }.Randomize(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.At.Least(1).Equal.To(search); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("at least 1") - ); - // Assert - } + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { item1, item2 }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.At.Least(1).Equal.To(search); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("at least 1") + ); + // Assert + } - [Test] - public void Contain_GivenAtLeast2Items_WhenHave16Items_ShouldNotThrow() - { - // Arrange - var src = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(src).To.Contain.At.Least(2).Items(); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void Contain_GivenAtLeast2Items_WhenHave16Items_ShouldNotThrow() + { + // Arrange + var src = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(src).To.Contain.At.Least(2).Items(); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void Contain_GivenAtLeast20Items_WhenHave16Items_ShouldThrowWithCustomMessage() - { - // Arrange - var src = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; - var expected = GetRandomString(10); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(src).To.Contain.At.Least(20).Items(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } + [Test] + public void Contain_GivenAtLeast20Items_WhenHave16Items_ShouldThrowWithCustomMessage() + { + // Arrange + var src = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + var expected = GetRandomString(10); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(src).To.Contain.At.Least(20).Items(expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + ); + // Assert + } - [Test] - public void Contain_GivenAtMost2Items_WhenHave1Item_ShouldNotThrow() - { - // Arrange - var src = new[] { 1 }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(src).To.Contain.At.Most(2).Items(); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void Contain_GivenAtMost2Items_WhenHave1Item_ShouldNotThrow() + { + // Arrange + var src = new[] { 1 }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(src).To.Contain.At.Most(2).Items(); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void Contain_Any_WhenCollectionHasNone_ShouldThrow() - { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { item1, item2 }.Randomize(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Any.Equal.To(search); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("Expected to find any match") - ); - // Assert - } + [Test] + public void Contain_Any_WhenCollectionHasNone_ShouldThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { item1, item2 }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Any.Equal.To(search); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("Expected to find any match") + ); + // Assert + } - [Test] - public void Contain_Any_WhenCollectionHas1_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { item1, item2, search }.Randomize(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Any.Equal.To(search); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void Contain_Any_WhenCollectionHas1_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { item1, item2, search }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Any.Equal.To(search); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void Contain_Any_Negated_WhenCollectionHas1_ShouldThrow() + [Test] + public void Contain_Any_Negated_WhenCollectionHas1_ShouldThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { item1, item2, search }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection) + .Not.To.Contain.Any + .Equal.To(search); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void Contain_Any_Negated_WhenCollectionHas1DeepEqual_ShouldThrow() + { + // Arrange + var search = new { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { item1, item2, search }.Randomize(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection) - .Not.To.Contain.Any - .Equal.To(search); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + Name = GetRandomString() + }; + var item1 = new + { + Name = search.Name + }; + var item2 = new + { + Name = GetAnother(new[] { search.Name, item1.Name }) + }; + var collection = new[] { item1, item2 }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection) + .Not.To.Contain.Any + .Deep.Equal.To(search); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } - [Test] - public void Contain_Any_Negated_WhenCollectionHas1DeepEqual_ShouldThrow() + [Test] + public void Contain_Exactly_WhenFails_ShouldProvideGoodMessage() + { + // Arrange + var search = new { id = 1 }; + var collection = new[] { new { id = 2 }, new { id = 3 } }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1) + .Deep.Equal.To(search); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain("to find exactly 1 occurrence of") + .And.Message.Contain(search.Stringify()) + .And.Message.Contain(" but found 0") + ); + // Assert + } + + [Test] + public void Contain_Any_Negated_WhenCollectionHas1IntersectionEqual_ShouldThrow() + { + // Arrange + var search = new { - // Arrange - var search = new + Name = GetRandomString() + }; + var item1 = new + { + Name = search.Name + }; + var item2 = new + { + Name = GetAnother(new[] { search.Name, item1.Name }) + }; + var collection = new[] { item1, item2 }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection) + .Not.To.Contain.Any + .Intersection.Equal.To(search); + }, + Throws.Exception + .InstanceOf() + .With.Message.Not.Contain("find 0 items") + ); + // Assert + } + + [Test] + public void Contain_All_WhenCollectionHasMismatches_ShouldThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { item1, item2, search }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => { - Name = GetRandomString() - }; - var item1 = new + Expect(collection) + .To.Contain.All + .Equal.To(search); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("Expected to find all matching") + ); + // Assert + } + + [Test] + public void Contain_All_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { item1, item2, search, null }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => { - Name = search.Name - }; - var item2 = new + Expect(collection) + .To.Contain.All + .Matched.By(s => s == null); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("Expected to find all matching but found 1") + ); + // Assert + } + + [Test] + public void Contain_Any_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { item1, item2, search }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => { - Name = GetAnother(new[] { search.Name, item1.Name }) - }; - var collection = new[] { item1, item2 }.Randomize(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection) - .Not.To.Contain.Any - .Deep.Equal.To(search); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + Expect(collection) + .To.Contain.Any + .Matched.By(s => s == null); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("Expected to find any matching but found none") + ); + // Assert + } - [Test] - public void Contain_Exactly_WhenFails_ShouldProvideGoodMessage() - { - // Arrange - var search = new { id = 1 }; - var collection = new[] { new { id = 2 }, new { id = 3 } }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Deep.Equal.To(search); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain("to find exactly 1 occurrence of") - .And.Message.Contain(search.Stringify()) - .And.Message.Contain(" but found 0") - ); - // Assert - } + [Test] + public void Contain_Any_MatchedByWithIndex_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() + { + // Arrange + var items = PyLike.Range(10).Select(i => i + 1).ToArray(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.Any + .Matched.By((idx, item) => item == idx); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("Expected to find any matching but found none") + ); + // Assert + } - [Test] - public void Contain_Any_Negated_WhenCollectionHas1IntersectionEqual_ShouldThrow() - { - // Arrange - var search = new + [Test] + public void Contain_All_MatchedByWithIndex_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() + { + // Arrange + var items = PyLike.Range(10).Select(i => i).ToArray(); + // Pre-Assert + // Act + Assert.That( + () => { - Name = GetRandomString() - }; - var item1 = new + Expect(items) + .To.Contain.All + .Matched.By((idx, item) => item == idx); + }, + Throws.Nothing + ); + // Assert + } + + + [Test] + public void Contain_All_WhenCollectionHasNoMismatches_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(); + var collection = PyLike.Range(2, 4).Select(i => search); + // Pre-Assert + // Act + Assert.That( + () => { - Name = search.Name - }; - var item2 = new + Expect(collection) + .To.Contain.All + .Equal.To(search); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void Contain_GivenAtLeast1_WhenCollectionHas1_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { search, item1, item2 }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => { - Name = GetAnother(new[] { search.Name, item1.Name }) - }; - var collection = new[] { item1, item2 }.Randomize(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection) - .Not.To.Contain.Any - .Intersection.Equal.To(search); - }, - Throws.Exception - .InstanceOf() - .With.Message.Not.Contain("find 0 items") - ); - // Assert - } + Expect(collection).To.Contain.At.Least(1).Equal.To(search); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void Contain_GivenAtLeast1_WhenCollectionHas2_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] { item1, search }); + var collection = new[] { search, item1, search, item2 }.Randomize(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.At.Least(1).Equal.To(search); + }, + Throws.Nothing + ); + // Assert + } + } + [TestFixture] + public class Matched + { + [TestFixture] + public class By + { [Test] - public void Contain_All_WhenCollectionHasMismatches_ShouldThrow() + public void OperatingOnCollection_WhenSeeking1Match_AndHas1Match_ShouldNotThrow() { // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { item1, item2, search }.Randomize(); + var collection = GetRandomCollection(3, 3).ToArray(); + var search = collection.Randomize().First(); // Pre-Assert + // Act Assert.That( () => { Expect(collection) - .To.Contain.All - .Equal.To(search); + .To.Contain + .At.Least(1) + .Matched.By(s => s == search); }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("Expected to find all matching") + Throws.Nothing ); + // Assert } [Test] - public void Contain_All_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() + public void OperatingOnCollection_WhenSeeking1Match_AndHas2Matches_ShouldNotThrow() { // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { item1, item2, search, null }.Randomize(); + var collection = GetRandomCollection(3, 3).ToArray(); + var search = collection.Randomize().First(); + collection = collection.And(search).Randomize().ToArray(); // Pre-Assert + Assert.That(collection.Count(s => s == search), Is.EqualTo(2)); // Act Assert.That( () => { Expect(collection) - .To.Contain.All - .Matched.By(s => s == null); + .To.Contain + .At.Least(1) + .Matched.By(s => s == search); }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("Expected to find all matching but found 1") + Throws.Nothing ); + // Assert } [Test] - public void Contain_Any_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() + public void OperatingOnCollection_WhenSeeking2Matches_AndHas1Match_ShouldThrow() { // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { item1, item2, search }.Randomize(); + var collection = GetRandomCollection(3, 3).ToArray(); + var search = collection.Randomize().First(); // Pre-Assert // Act Assert.That( () => { Expect(collection) - .To.Contain.Any - .Matched.By(s => s == null); + .To.Contain + .At.Least(2) + .Matched.By(s => s == search); }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("Expected to find any matching but found none") + Throws.Exception.InstanceOf() ); - // Assert - } - [Test] - public void Contain_Any_MatchedByWithIndex_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() - { - // Arrange - var items = PyLike.Range(10).Select(i => i + 1).ToArray(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.Any - .Matched.By((idx, item) => item == idx); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("Expected to find any matching but found none") - ); // Assert } [Test] - public void Contain_All_MatchedByWithIndex_WhenCollectionHasMismatchesWithMatcher_ShouldThrow() + public void Negated_OperatingOnCollection_WhenSeeking1Match_AndHas1Match_ShouldThrow() { // Arrange - var items = PyLike.Range(10).Select(i => i).ToArray(); + var collection = GetRandomCollection(3, 3).ToArray(); + var search = collection.Randomize().First(); // Pre-Assert - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.All - .Matched.By((idx, item) => item == idx); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void Contain_All_WhenCollectionHasNoMismatches_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var collection = PyLike.Range(2, 4).Select(i => search); - // Pre-Assert // Act Assert.That( () => { Expect(collection) - .To.Contain.All - .Equal.To(search); + .Not.To.Contain + .At.Least(1) + .Matched.By(s => s == search); }, - Throws.Nothing + Throws.Exception.InstanceOf() ); - // Assert - } - [Test] - public void Contain_GivenAtLeast1_WhenCollectionHas1_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { search, item1, item2 }.Randomize(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.At.Least(1).Equal.To(search); - }, - Throws.Nothing - ); // Assert } [Test] - public void Contain_GivenAtLeast1_WhenCollectionHas2_ShouldNotThrow() + public void NegatedAlt_OperatingOnCollection_WhenSeeking1Match_AndHas1Match_ShouldThrow() { // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] { item1, search }); - var collection = new[] { search, item1, search, item2 }.Randomize(); + var collection = GetRandomCollection(3, 3).ToArray(); + var search = collection.Randomize().First(); // Pre-Assert + // Act Assert.That( () => { - Expect(collection).To.Contain.At.Least(1).Equal.To(search); + Expect(collection) + .To.Not.Contain + .At.Least(1) + .Matched.By(s => s == search); }, - Throws.Nothing + Throws.Exception.InstanceOf() ); - // Assert - } - } - [TestFixture] - public class Matched - { - [TestFixture] - public class By - { - [Test] - public void OperatingOnCollection_WhenSeeking1Match_AndHas1Match_ShouldNotThrow() - { - // Arrange - var collection = GetRandomCollection(3, 3).ToArray(); - var search = collection.Randomize().First(); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(collection) - .To.Contain - .At.Least(1) - .Matched.By(s => s == search); - }, - Throws.Nothing - ); - - // Assert - } - - [Test] - public void OperatingOnCollection_WhenSeeking1Match_AndHas2Matches_ShouldNotThrow() - { - // Arrange - var collection = GetRandomCollection(3, 3).ToArray(); - var search = collection.Randomize().First(); - collection = collection.And(search).Randomize().ToArray(); - // Pre-Assert - Assert.That(collection.Count(s => s == search), Is.EqualTo(2)); - // Act - Assert.That( - () => - { - Expect(collection) - .To.Contain - .At.Least(1) - .Matched.By(s => s == search); - }, - Throws.Nothing - ); - - // Assert - } - - [Test] - public void OperatingOnCollection_WhenSeeking2Matches_AndHas1Match_ShouldThrow() - { - // Arrange - var collection = GetRandomCollection(3, 3).ToArray(); - var search = collection.Randomize().First(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection) - .To.Contain - .At.Least(2) - .Matched.By(s => s == search); - }, - Throws.Exception.InstanceOf() - ); - - // Assert - } - - [Test] - public void Negated_OperatingOnCollection_WhenSeeking1Match_AndHas1Match_ShouldThrow() - { - // Arrange - var collection = GetRandomCollection(3, 3).ToArray(); - var search = collection.Randomize().First(); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(collection) - .Not.To.Contain - .At.Least(1) - .Matched.By(s => s == search); - }, - Throws.Exception.InstanceOf() - ); - - // Assert - } - - [Test] - public void NegatedAlt_OperatingOnCollection_WhenSeeking1Match_AndHas1Match_ShouldThrow() - { - // Arrange - var collection = GetRandomCollection(3, 3).ToArray(); - var search = collection.Randomize().First(); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(collection) - .To.Not.Contain - .At.Least(1) - .Matched.By(s => s == search); - }, - Throws.Exception.InstanceOf() - ); - - // Assert - } + // Assert } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/AtMost.cs b/src/NExpect.Tests/Collections/AtMost.cs index f4023cea..736d42dc 100644 --- a/src/NExpect.Tests/Collections/AtMost.cs +++ b/src/NExpect.Tests/Collections/AtMost.cs @@ -1,65 +1,64 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.RandomGenerators; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +public class AtMost { - public class AtMost + [Test] + public void Contain_GivenAtMost1_WhenCollectionHasNone_ShouldNotThrow() { - [Test] - public void Contain_GivenAtMost1_WhenCollectionHasNone_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] {item1, search}); - var collection = new[] {item1, item2}.Randomize(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection).To.Contain.At.Most(1).Equal.To(search); - }, - Throws.Nothing); - // Assert - } + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] {item1, search}); + var collection = new[] {item1, item2}.Randomize(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(collection).To.Contain.At.Most(1).Equal.To(search); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Contain_GivenAtMost1_WhenCollectionHas1_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] {item1, search}); - var collection = new[] {search, item1, item2}.Randomize(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection).To.Contain.At.Most(1).Equal.To(search); - }, - Throws.Nothing); - // Assert - } + [Test] + public void Contain_GivenAtMost1_WhenCollectionHas1_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] {item1, search}); + var collection = new[] {search, item1, item2}.Randomize(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(collection).To.Contain.At.Most(1).Equal.To(search); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Contain_GivenAtMost1_WhenCollectionHas2_ShouldThrow() - { - // Arrange - var search = GetRandomString(); - var item1 = GetAnother(search); - var item2 = GetAnother(new[] {item1, search}); - var collection = new[] {search, item1, search, item2}.Randomize(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection).To.Contain.At.Most(1).Equal.To(search); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("at most 1")); - // Assert - } + [Test] + public void Contain_GivenAtMost1_WhenCollectionHas2_ShouldThrow() + { + // Arrange + var search = GetRandomString(); + var item1 = GetAnother(search); + var item2 = GetAnother(new[] {item1, search}); + var collection = new[] {search, item1, search, item2}.Randomize(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(collection).To.Contain.At.Most(1).Equal.To(search); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("at most 1")); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/CountMatchContinuationExtensionsForTesting.cs b/src/NExpect.Tests/Collections/CountMatchContinuationExtensionsForTesting.cs index 0185ee82..d609bf01 100644 --- a/src/NExpect.Tests/Collections/CountMatchContinuationExtensionsForTesting.cs +++ b/src/NExpect.Tests/Collections/CountMatchContinuationExtensionsForTesting.cs @@ -7,63 +7,62 @@ // ReSharper disable PossibleMultipleEnumeration -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +public static class CountMatchContinuationExtensionsForTesting { - public static class CountMatchContinuationExtensionsForTesting + public static IMore> Odds(this ICountMatchContinuation> continuation) { - public static IMore> Odds(this ICountMatchContinuation> continuation) - { - return continuation.AddMatcher(collection => - { - return TestCollection( - collection, - continuation.ExpectedCount, - continuation.GetCountMatchMethod(), - i => i % 2 == 1, - passed => $"Expected {collection.LimitedPrint()} {passed.AsNot()}to be only odd numbers" - ); - }); - } - - public static IMore> Evens( - this ICountMatchContinuation> continuation) + return continuation.AddMatcher(collection => { - return continuation.AddMatcher(collection => - { - return TestCollection( - collection, - continuation.ExpectedCount, - continuation.GetCountMatchMethod(), - i => i % 2 == 0, - passed => $"Expected {collection.LimitedPrint()} {passed.AsNot()}to be only even numbers" - ); - }); - } + return TestCollection( + collection, + continuation.ExpectedCount, + continuation.GetCountMatchMethod(), + i => i % 2 == 1, + passed => $"Expected {collection.LimitedPrint()} {passed.AsNot()}to be only odd numbers" + ); + }); + } - private static MatcherResult TestCollection( - IEnumerable collection, - int expectedCount, - CountMatchMethods method, - Func itemTester, - Func messageGenerator) + public static IMore> Evens( + this ICountMatchContinuation> continuation) + { + return continuation.AddMatcher(collection => { - var count = collection.Count(itemTester); - var total = collection.Count(); - var passed = _strategies[method](total, count, expectedCount); - return new MatcherResult( - passed, - () => messageGenerator(passed) + return TestCollection( + collection, + continuation.ExpectedCount, + continuation.GetCountMatchMethod(), + i => i % 2 == 0, + passed => $"Expected {collection.LimitedPrint()} {passed.AsNot()}to be only even numbers" ); - } + }); + } - private static Dictionary> _strategies = - new Dictionary>() - { - [CountMatchMethods.All] = (total, matched, expected) => total == matched, - [CountMatchMethods.Any] = (total, matched, expected) => matched > 0, - [CountMatchMethods.Exactly] = (total, matched, expected) => matched == expected, - [CountMatchMethods.Maximum] = (total, matched, expected) => matched <= expected, - [CountMatchMethods.Minimum] = (total, matched, expected) => matched >= expected - }; + private static MatcherResult TestCollection( + IEnumerable collection, + int expectedCount, + CountMatchMethods method, + Func itemTester, + Func messageGenerator) + { + var count = collection.Count(itemTester); + var total = collection.Count(); + var passed = _strategies[method](total, count, expectedCount); + return new MatcherResult( + passed, + () => messageGenerator(passed) + ); } -} \ No newline at end of file + + private static Dictionary> _strategies = + new Dictionary>() + { + [CountMatchMethods.All] = (total, matched, expected) => total == matched, + [CountMatchMethods.Any] = (total, matched, expected) => matched > 0, + [CountMatchMethods.Exactly] = (total, matched, expected) => matched == expected, + [CountMatchMethods.Maximum] = (total, matched, expected) => matched <= expected, + [CountMatchMethods.Minimum] = (total, matched, expected) => matched >= expected + }; +} diff --git a/src/NExpect.Tests/Collections/CustomEqualityTesting.cs b/src/NExpect.Tests/Collections/CustomEqualityTesting.cs index 3f6e2efb..d81c1b8d 100644 --- a/src/NExpect.Tests/Collections/CustomEqualityTesting.cs +++ b/src/NExpect.Tests/Collections/CustomEqualityTesting.cs @@ -1,102 +1,101 @@ -using NUnit.Framework; +using NUnit.Framework; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class CustomEqualityTesting { + // TODO: could have more tests + // -> the other implementations lean on the logic to + // accomplish this, so, techically, the other cases are covered... + [TestFixture] - public class CustomEqualityTesting + public class WithProvidedComparisonFunc { - // TODO: could have more tests - // -> the other implementations lean on the logic to - // accomplish this, so, techically, the other cases are covered... - - [TestFixture] - public class WithProvidedComparisonFunc + private static bool FirstLetterComparer(string x, string y) { - private static bool FirstLetterComparer(string x, string y) + if (x == null && + y == null) { - if (x == null && - y == null) - { - return true; - } - - if (x == null || - y == null) - { - return false; - } - - if (x.Length == 0 && - y.Length == 0) - { - return true; - } + return true; + } - if (x.Length == 0 || - y.Length == 0) - { - return false; - } + if (x == null || + y == null) + { + return false; + } - return x[0] == y[0]; + if (x.Length == 0 && + y.Length == 0) + { + return true; } - [Test] - public void PositiveResult_ShouldNotThrow() + if (x.Length == 0 || + y.Length == 0) { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + return false; + } + + return x[0] == y[0]; + } - // Pre-Assert + [Test] + public void PositiveResult_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - // Act - Assert.That(() => - { - Expect(left).To.Equal(right, FirstLetterComparer); - }, - Throws.Nothing); + // Pre-Assert - // Assert - } + // Act + Assert.That(() => + { + Expect(left).To.Equal(right, FirstLetterComparer); + }, + Throws.Nothing); - [Test] - public void NegativeNegatedResult_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; + // Assert + } - // Pre-Assert + [Test] + public void NegativeNegatedResult_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; - // Act - Assert.That(() => - { - Expect(left).Not.To.Equal(right, FirstLetterComparer); - }, - Throws.Nothing); + // Pre-Assert - // Assert - } + // Act + Assert.That(() => + { + Expect(left).Not.To.Equal(right, FirstLetterComparer); + }, + Throws.Nothing); - [Test] - public void NegativeNegatedResult_AltGrammar_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; + // Assert + } - // Pre-Assert + [Test] + public void NegativeNegatedResult_AltGrammar_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; - // Act - Assert.That(() => - { - Expect(left).To.Not.Equal(right, FirstLetterComparer); - }, - Throws.Nothing); + // Pre-Assert - // Assert - } + // Act + Assert.That(() => + { + Expect(left).To.Not.Equal(right, FirstLetterComparer); + }, + Throws.Nothing); + + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/CustomEquivalenceTesting.cs b/src/NExpect.Tests/Collections/CustomEquivalenceTesting.cs index ceb16c76..dcd1db4c 100644 --- a/src/NExpect.Tests/Collections/CustomEquivalenceTesting.cs +++ b/src/NExpect.Tests/Collections/CustomEquivalenceTesting.cs @@ -1,283 +1,282 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; // ReSharper disable ExpressionIsAlwaysNull -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class CustomEquivalenceTesting { [TestFixture] - public class CustomEquivalenceTesting + public class WithCustomEqualityComparer { - [TestFixture] - public class WithCustomEqualityComparer + [Test] + public void Positive_WhenMatches_ShouldNotThrow() { - [Test] - public void Positive_WhenMatches_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Equivalent.To(right, new FirstLetterComparer()); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(left).To.Be.Equivalent.To(right, new FirstLetterComparer()); + }, + Throws.Nothing); - // Assert - } + // Assert + } - [Test] - public void NegativeNegated_WhenNoMatches_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; + [Test] + public void NegativeNegated_WhenNoMatches_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).Not.To.Be.Equivalent.To(right, new FirstLetterComparer()); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(left).Not.To.Be.Equivalent.To(right, new FirstLetterComparer()); + }, + Throws.Nothing); - // Assert - } + // Assert + } - [Test] - public void ShouldFindTwoNullCollectionsEquivalent() + [Test] + public void ShouldFindTwoNullCollectionsEquivalent() + { + // Arrange + var left = null as int[]; + var right = null as int[]; + // Pre-assert + // Act + Assert.That(() => { - // Arrange - var left = null as int[]; - var right = null as int[]; - // Pre-assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Deep.Equivalent.To(right); - Expect(left).To.Be.Equivalent.To(right); - }, Throws.Nothing); - // Assert - } + Expect(left).To.Be.Deep.Equivalent.To(right); + Expect(left).To.Be.Equivalent.To(right); + }, Throws.Nothing); + // Assert + } - [Test] - public void ShouldNeverFindEquivalenceIfOneIsNull() + [Test] + public void ShouldNeverFindEquivalenceIfOneIsNull() + { + // Arrange + var left = new[] { 1 }; + var right = null as int[]; + // Pre-assert + // Act + Assert.That(() => { - // Arrange - var left = new[] { 1 }; - var right = null as int[]; - // Pre-assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Deep.Equivalent.To(right); - }, Throws.Exception.InstanceOf()); - Assert.That(() => - { - Expect(left).To.Be.Equivalent.To(right); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(left).To.Be.Deep.Equivalent.To(right); + }, Throws.Exception.InstanceOf()); + Assert.That(() => + { + Expect(left).To.Be.Equivalent.To(right); + }, Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void ShouldFindTwoCollectionsOfDifferentSizesNonEquivalent() + [Test] + public void ShouldFindTwoCollectionsOfDifferentSizesNonEquivalent() + { + // Arrange + var left = new[] { 1 }; + var right = new[] { 1, 2 }; + // Pre-assert + // Act + Assert.That(() => { - // Arrange - var left = new[] { 1 }; - var right = new[] { 1, 2 }; - // Pre-assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Deep.Equivalent.To(right); - }, Throws.Exception.InstanceOf()); - Assert.That(() => + Expect(left).To.Be.Deep.Equivalent.To(right); + }, Throws.Exception.InstanceOf()); + Assert.That(() => + { + Expect(left).To.Be.Equivalent.To(right); + }, Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void NegativeNegated_AltGrammar_WhenNoMatches_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; + + // Pre-Assert + + // Act + Assert.That(() => { - Expect(left).To.Be.Equivalent.To(right); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(left).To.Not.Be.Equivalent.To(right, new FirstLetterComparer()); + }, + Throws.Nothing); - [Test] - public void NegativeNegated_AltGrammar_WhenNoMatches_ShouldNotThrow() + // Assert + } + } + + [TestFixture] + public class WithCustomEqualityComparisonFunc + { + private static bool FirstLetterComparer(string x, string y) + { + if (x == null && + y == null) { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; + return true; + } - // Pre-Assert + if (x == null || + y == null) + { + return false; + } - // Act - Assert.That(() => - { - Expect(left).To.Not.Be.Equivalent.To(right, new FirstLetterComparer()); - }, - Throws.Nothing); + if (x.Length == 0 && + y.Length == 0) + { + return true; + } - // Assert + if (x.Length == 0 || + y.Length == 0) + { + return false; } + + return x[0] == y[0]; } - [TestFixture] - public class WithCustomEqualityComparisonFunc + [Test] + public void Positive_WhenMatches_ShouldNotThrow() { - private static bool FirstLetterComparer(string x, string y) - { - if (x == null && - y == null) - { - return true; - } + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; - if (x == null || - y == null) - { - return false; - } + // Pre-Assert - if (x.Length == 0 && - y.Length == 0) + // Act + Assert.That(() => { - return true; - } + Expect(left).To.Be.Equivalent.To(right, FirstLetterComparer); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void NegativeNegated_WhenNoMatches_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; - if (x.Length == 0 || - y.Length == 0) + // Pre-Assert + + // Act + Assert.That(() => { - return false; - } + Expect(left).Not.To.Be.Equivalent.To(right, FirstLetterComparer); + }, + Throws.Nothing); - return x[0] == y[0]; - } + // Assert + } - [Test] - public void Positive_WhenMatches_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; + [Test] + public void NegativeNegated_AltGrammar_WhenNoMatches_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Equivalent.To(right, FirstLetterComparer); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(left).To.Not.Be.Equivalent.To(right, FirstLetterComparer); + }, + Throws.Nothing); - // Assert - } + // Assert + } + } - [Test] - public void NegativeNegated_WhenNoMatches_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; + [TestFixture] + public class WithProvidedComparer + { + [Test] + public void PositiveResult_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var withComparer = new FirstLetterComparer(); + Assert.That(withComparer.Equals(left[0], right[0]), Is.True); - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).Not.To.Be.Equivalent.To(right, FirstLetterComparer); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(left).To.Equal(right, withComparer); + }, + Throws.Nothing); - // Assert - } + // Assert + } - [Test] - public void NegativeNegated_AltGrammar_WhenNoMatches_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"C{GetRandomString()}", $"A{GetRandomString()}"}; + [Test] + public void NegativeNegatedResult_ShouldNotThrow() + { + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; + var withComparer = new FirstLetterComparer(); + Assert.That(withComparer.Equals(left[0], right[0]), Is.False); - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Not.Be.Equivalent.To(right, FirstLetterComparer); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(left).Not.To.Equal(right, withComparer); + }, + Throws.Nothing); - // Assert - } + // Assert } - [TestFixture] - public class WithProvidedComparer + [Test] + public void NegativeNegatedResult_AltGrammar_ShouldNotThrow() { - [Test] - public void PositiveResult_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var withComparer = new FirstLetterComparer(); - Assert.That(withComparer.Equals(left[0], right[0]), Is.True); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(left).To.Equal(right, withComparer); - }, - Throws.Nothing); - - // Assert - } + // Arrange + var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; + var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; + var withComparer = new FirstLetterComparer(); + Assert.That(withComparer.Equals(left[0], right[0]), Is.False); - [Test] - public void NegativeNegatedResult_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; - var withComparer = new FirstLetterComparer(); - Assert.That(withComparer.Equals(left[0], right[0]), Is.False); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(left).Not.To.Equal(right, withComparer); - }, - Throws.Nothing); - - // Assert - } + // Pre-Assert - [Test] - public void NegativeNegatedResult_AltGrammar_ShouldNotThrow() - { - // Arrange - var left = new[] {$"A{GetRandomString()}", $"B{GetRandomString()}"}; - var right = new[] {$"B{GetRandomString()}", $"A{GetRandomString()}"}; - var withComparer = new FirstLetterComparer(); - Assert.That(withComparer.Equals(left[0], right[0]), Is.False); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(left).To.Not.Equal(right, withComparer); - }, - Throws.Nothing); - - // Assert - } + // Act + Assert.That(() => + { + Expect(left).To.Not.Equal(right, withComparer); + }, + Throws.Nothing); + + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/DeepEqualityTesting.cs b/src/NExpect.Tests/Collections/DeepEqualityTesting.cs index bfe7264b..e817d93d 100644 --- a/src/NExpect.Tests/Collections/DeepEqualityTesting.cs +++ b/src/NExpect.Tests/Collections/DeepEqualityTesting.cs @@ -1,4 +1,4 @@ -using System; +using System; using NUnit.Framework; using NExpect.Exceptions; using PeanutButter.DuckTyping.Extensions; @@ -10,421 +10,54 @@ // ReSharper disable UnusedMember.Global // ReSharper disable InconsistentNaming -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class DeepEqualityTesting { [TestFixture] - public class DeepEqualityTesting + public class Expect_Collection_To { [TestFixture] - public class Expect_Collection_To + public class Deep { [TestFixture] - public class Deep + public class Equal { - [TestFixture] - public class Equal + public class IdentifierAndName1 { - public class IdentifierAndName1 - { - public int Id { get; } - public string Name { get; } - - public IdentifierAndName1(int id, string name) - { - Id = id; - Name = name; - } - } - - public class IdentifierAndName2 - { - public int Id { get; } - public string Name { get; } - - public IdentifierAndName2(int id, string name) - { - Id = id; - Name = name; - } - } - - private static IdentifierAndName1 o1(int id, string name) - { - return new IdentifierAndName1(id, name); - } - - private static IdentifierAndName2 o2(int id, string name) - { - return new IdentifierAndName2(id, name); - } - - [Test] - public void PositiveExpectation_WhenCollectionsMatch_ShouldNotThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o2(1, "bob"), o2(2, "janet") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).As.Objects.To.Deep.Equal(second); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenCollectionsMatch_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o1(1, "bob"), o1(2, "janet") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).Not.To.Deep.Equal(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltGrammar_WhenCollectionsMatch_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o1(1, "bob"), o1(2, "janet") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).To.Not.Deep.Equal(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void PositiveExpectation_AltGrammer_WhenCollectionsMatch_ShouldNotThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o1(1, "bob"), o1(2, "janet") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).To.Be.Deep.Equal.To(second); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void WithCustomComparer() - { - // Arrange - var first = new[] { new { Date = DateTime.Now } }; - var second = new[] { new { Date = DateTime.Now.AddSeconds(-1) } }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(first).To.Be.Deep.Equal.To( - second, - new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_AltGrammar_WhenCollectionsMatch_ShouldNotThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o1(1, "bob"), o1(2, "janet") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).Not.To.Be.Deep.Equal.To(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void PositiveExpectation_WhenCollectionsDontMatchInFirstRecord_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "paddy") }; - var second = new[] { o1(1, "bobby"), o1(2, "janet"), o1(3, "paddy") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).To.Deep.Equal(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } + public int Id { get; } + public string Name { get; } - [Test] - public void PositiveExpectation_WhenCollectionsDontMatchInLastRecord_ShouldThrow() + public IdentifierAndName1(int id, string name) { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "paddy") }; - var second = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "mcgee") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).To.Deep.Equal(second); - }, - Throws.Exception.InstanceOf()); - // Assert + Id = id; + Name = name; } + } - [Test] - public void ShouldNotTreatDateTimesWithDifferentKindsAsEqual() - { - // Arrange - var src = GetRandomDate(); - var local = new - { - Date = new DateTime( - src.Year, - src.Month, - src.Day, - src.Hour, - src.Minute, - src.Second, - src.Millisecond, - DateTimeKind.Local) - }; - var utc = new - { - Date = new DateTime( - src.Year, - src.Month, - src.Day, - src.Hour, - src.Minute, - src.Second, - src.Millisecond, - DateTimeKind.Utc) - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(local).To.Deep.Equal(utc); - }, - Throws.Exception.InstanceOf()); - // Assert - } + public class IdentifierAndName2 + { + public int Id { get; } + public string Name { get; } - [TestFixture] - public class UsingCustomEqualityComparers + public IdentifierAndName2(int id, string name) { - [Test] - public void AllowingDateTimeDrift() - { - // Arrange - var left = new { Date = DateTime.Now }; - var right = new { Date = left.Date.AddSeconds(1) }; - - // Pre-assert - // Act - Assert.That( - () => - { - Expect(left).To.Deep.Equal( - right, - new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void MessageWhenCustomEqualityComparerSaysNotEqual() - { - // Arrange - var left = new { Date = DateTime.Now }; - var right = new { Date = left.Date.AddSeconds(1) }; - - // Pre-assert - // Act - Assert.That( - () => - { - Expect(left).To.Deep.Equal( - right, - new NeverEqualEqualityComparer()); - }, - Throws.Exception.InstanceOf() - .With.Message.Contain(nameof(NeverEqualEqualityComparer))); - // Assert - } + Id = id; + Name = name; } } - } - } - - [TestFixture] - public class Intersection - { - public class IdentifierAndName - { - public int Id { get; } - public string Name { get; } - - public IdentifierAndName(int id, string name) - { - Id = id; - Name = name; - } - } - - public class OtherIdentifierAndName - { - public int Id { get; } - public string Name { get; } - public string Type => GetType().Name; - public OtherIdentifierAndName(int id, string name) + private static IdentifierAndName1 o1(int id, string name) { - Id = id; - Name = name; + return new IdentifierAndName1(id, name); } - } - private static IdentifierAndName o1(int id, string name) - { - return new IdentifierAndName(id, name); - } - - private static OtherIdentifierAndName o2(int id, string name) - { - return new OtherIdentifierAndName(id, name); - } - - [TestFixture] - public class Equivalent - { - [TestFixture] - public class To + private static IdentifierAndName2 o2(int id, string name) { - [Test] - public void PositiveExpectation_WhenHaveEquivalence_ShouldNotThrow() - { - // Arrange - var first = new[] { o1(1, "moo"), o1(2, "cow") }; - var second = new[] { o2(2, "cow"), o2(1, "moo") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).As.Objects.To.Be.Intersection.Equivalent.To(second); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenHaveEquivalence_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "moo"), o1(2, "cow") }; - var second = new[] { o2(2, "cow"), o2(1, "moo") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).As.Objects.Not.To.Be.Intersection.Equivalent.To(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltGrammar_WhenHaveEquivalence_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "moo"), o1(2, "cow") }; - var second = new[] { o2(2, "cow"), o2(1, "moo") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).As.Objects.To.Not.Be.Intersection.Equivalent.To(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void PositiveExpectation_WhenCollectionsDontMatch_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o2(1, "bobby"), o2(2, "janet") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).As.Objects.To.Be.Intersection.Equivalent.To(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [TestFixture] - public class UsingCustomEqualityComparers - { - [Test] - public void AllowingDateTimeDrift() - { - // Arrange - var left = new { Date = DateTime.Now }.InArray(); - var right = new { Date = left[0].Date.AddSeconds(1) }.InArray(); - - // Pre-assert - // Act - Assert.That( - () => - { - Expect(left).As.Objects.To.Be.Intersection.Equivalent.To( - right, - new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - // Assert - } - } + return new IdentifierAndName2(id, name); } - } - [TestFixture] - public class Equal - { [Test] public void PositiveExpectation_WhenCollectionsMatch_ShouldNotThrow() { @@ -436,7 +69,7 @@ public void PositiveExpectation_WhenCollectionsMatch_ShouldNotThrow() Assert.That( () => { - Expect(first).As.Objects.To.Intersection.Equal(second); + Expect(first).As.Objects.To.Deep.Equal(second); }, Throws.Nothing); // Assert @@ -447,13 +80,13 @@ public void NegativeExpectation_WhenCollectionsMatch_ShouldThrow() { // Arrange var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o2(1, "bob"), o2(2, "janet") }; + var second = new[] { o1(1, "bob"), o1(2, "janet") }; // Pre-Assert // Act Assert.That( () => { - Expect(first).As.Objects.Not.To.Intersection.Equal(second); + Expect(first).Not.To.Deep.Equal(second); }, Throws.Exception.InstanceOf()); // Assert @@ -464,13 +97,13 @@ public void NegativeExpectation_AltGrammar_WhenCollectionsMatch_ShouldThrow() { // Arrange var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o2(1, "bob"), o2(2, "janet") }; + var second = new[] { o1(1, "bob"), o1(2, "janet") }; // Pre-Assert // Act Assert.That( () => { - Expect(first).As.Objects.To.Not.Intersection.Equal(second); + Expect(first).To.Not.Deep.Equal(second); }, Throws.Exception.InstanceOf()); // Assert @@ -481,883 +114,1249 @@ public void PositiveExpectation_AltGrammer_WhenCollectionsMatch_ShouldNotThrow() { // Arrange var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o2(1, "bob"), o2(2, "janet") }; + var second = new[] { o1(1, "bob"), o1(2, "janet") }; // Pre-Assert // Act Assert.That( () => { - Expect(first).As.Objects.To.Be.Intersection.Equal.To(second); + Expect(first).To.Be.Deep.Equal.To(second); }, Throws.Nothing); // Assert } [Test] - public void NegativeExpectation_AltGrammar_WhenCollectionsMatch_ShouldNotThrow() + public void WithCustomComparer() { // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o2(1, "bob"), o2(2, "janet") }; - // Pre-Assert + var first = new[] { new { Date = DateTime.Now } }; + var second = new[] { new { Date = DateTime.Now.AddSeconds(-1) } }; + // Pre-assert // Act Assert.That( () => { - Expect(first).As.Objects.Not.To.Be.Intersection.Equal.To(second); + Expect(first).To.Be.Deep.Equal.To( + second, + new DriftingDateTimeEqualityComparer()); }, - Throws.Exception.InstanceOf()); + Throws.Nothing); // Assert } [Test] - public void PositiveExpectation_WhenCollectionsDontMatchInFirstRecord_ShouldThrow() + public void NegativeExpectation_AltGrammar_WhenCollectionsMatch_ShouldNotThrow() { // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "paddy") }; - var second = new[] { o2(1, "bobby"), o2(2, "janet"), o2(3, "paddy") }; + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o1(1, "bob"), o1(2, "janet") }; // Pre-Assert // Act Assert.That( () => { - Expect(first).As.Objects.To.Intersection.Equal(second); + Expect(first).Not.To.Be.Deep.Equal.To(second); }, Throws.Exception.InstanceOf()); // Assert } [Test] - public void PositiveExpectation_WhenCollectionsDontMatchInLastRecord_ShouldThrow() + public void PositiveExpectation_WhenCollectionsDontMatchInFirstRecord_ShouldThrow() { // Arrange var first = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "paddy") }; - var second = new[] { o2(1, "bob"), o2(2, "janet"), o2(3, "mcgee") }; + var second = new[] { o1(1, "bobby"), o1(2, "janet"), o1(3, "paddy") }; // Pre-Assert // Act Assert.That( () => { - Expect(first).As.Objects.To.Intersection.Equal(second); + Expect(first).To.Deep.Equal(second); }, Throws.Exception.InstanceOf()); // Assert } [Test] - public void AllowingDateTimeDrift() + public void PositiveExpectation_WhenCollectionsDontMatchInLastRecord_ShouldThrow() { // Arrange - var left = new { Date = DateTime.Now }.InArray(); - var right = new { Date = left[0].Date.AddSeconds(1) }.InArray(); - - // Pre-assert + var first = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "paddy") }; + var second = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "mcgee") }; + // Pre-Assert // Act Assert.That( () => { - Expect(left).As.Objects.To.Intersection.Equal( - right, - new DriftingDateTimeEqualityComparer()); - Expect(left).As.Objects.To.Be.Intersection.Equal.To( - right, - new DriftingDateTimeEqualityComparer()); + Expect(first).To.Deep.Equal(second); }, - Throws.Nothing); + Throws.Exception.InstanceOf()); // Assert } - } - } - [TestFixture] - public class Expect_Collection_To_Contain - { - [TestFixture] - public class Exactly_N - { - [TestFixture] - public class Intersection + [Test] + public void ShouldNotTreatDateTimesWithDifferentKindsAsEqual() { - [TestFixture] - public class Equal + // Arrange + var src = GetRandomDate(); + var local = new + { + Date = new DateTime( + src.Year, + src.Month, + src.Day, + src.Hour, + src.Minute, + src.Second, + src.Millisecond, + DateTimeKind.Local) + }; + var utc = new { - [TestFixture] - public class To + Date = new DateTime( + src.Year, + src.Month, + src.Day, + src.Hour, + src.Minute, + src.Second, + src.Millisecond, + DateTimeKind.Utc) + }; + // Pre-Assert + // Act + Assert.That( + () => { - public class Item1 - { - public int Id { get; set; } - public string Name { get; set; } - public DateTime DateTime => DateTime.Now; - } - - public class Item2 - { - public int Id { get; set; } - public string Name { get; set; } - } - - [Test] - public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() - { - // Arrange - var src = new[] - { - new Item1 - { - Id = 1, - Name = "moo" - }, - new Item1 - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - // FIXME: this is lazy -- should have a separate test fixture - // for At.Least and At.Most; I'm just in a bit of a hurry and - // need to prove the syntax - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Intersection.Equal.To( - new Item2 - { - Id = 1, - Name = "moo" - } - ); - }, - Throws.Nothing); - Assert.That( - () => - { - Expect(src.And(new Item1 - { - Id = 1, - Name = "moo" - })) - .To.Contain.At.Least(1) - .Intersection.Equal.To( - new Item2 - { - Id = 1, - Name = "moo" - } - ); - }, - Throws.Nothing); - Assert.That( - () => - { - Expect(src) - .To.Contain.At.Most(1) - .Intersection.Equal.To( - new Item2 - { - Id = 2, - Name = "Cake" - } - ); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeAssertion_WhenShouldFail_ShouldThrow() - { - // Arrange - var src = new[] - { - new Item1 - { - Id = 1, - Name = "moo" - }, - new Item1 - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .Not.To.Contain.Exactly(1) - .Intersection.Equal.To( - new Item2 - { - Id = 1, - Name = "moo" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void CountMatchWithCustomComparer() - { - // Arrange - var left = new[] { new { Date = DateTime.Now } }; - var search = new { Date = DateTime.Now.AddSeconds(-1) }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(left).To.Contain.Only(1) - .Intersection.Equal.To( - search, - new DriftingDateTimeEqualityComparer() - ); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void GivenInvalidComparers_ShouldThrowArgumentException() - { - // Arrange - var left = new[] { new { Date = DateTime.Now } }; - var search = new { Date = DateTime.Now.AddSeconds(-1) }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(left).To.Contain.Only(1) - .Intersection.Equal.To( - search, - new NotAnEqualityComparer() - ); - }, - Throws.Exception.InstanceOf() - .With.InnerException.InstanceOf() - .With.Message.Contains("must implement IEqualityComparer")); - // Assert - } - - public class NotAnEqualityComparer : IDisposable - { - public void Dispose() - { - } - } - - [Test] - public void NegativeAssertion_AltGrammer_WhenShouldFail_ShouldThrow() - { - // Arrange - var src = new[] - { - new Item1 - { - Id = 1, - Name = "moo" - }, - new Item1 - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Not.Contain.Exactly(1) - .Intersection.Equal.To( - new Item2 - { - Id = 1, - Name = "moo" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void PositiveAssertion_WhenShouldFailForObjectMisMatch_ShouldThrow() - { - // Arrange - var src = new[] - { - new Item1 - { - Id = 1, - Name = "moo" - }, - new Item1 - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Intersection.Equal.To( - new Item2 - { - Id = 1, - Name = "bar" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void PositiveAssertion_WhenShouldFailForCountMismatch_ShouldThrow() - { - // Arrange - var src = new[] - { - new Item1 - { - Id = 1, - Name = "moo" - }, - new Item1 - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.At.Least(2) - .Intersection.Equal.To( - new Item2 - { - Id = 1, - Name = "Cake" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } - } - } + Expect(local).To.Deep.Equal(utc); + }, + Throws.Exception.InstanceOf()); + // Assert } [TestFixture] - public class Deep + public class UsingCustomEqualityComparers { - [TestFixture] - public class Equal + [Test] + public void AllowingDateTimeDrift() { - [TestFixture] - public class To - { - [Test] - public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() - { - // Arrange - var src = new[] - { - new - { - Id = 1, - Name = "moo" - }, - new - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Deep.Equal.To( - new - { - Id = 1, - Name = "moo" - } - ) - .And - .To.Contain.Exactly(1) - .Deep.Equal.To( - new - { - Id = 2, - Name = "Cake" - }); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeAssertion_WhenShouldFail_ShouldThrow() - { - // Arrange - var src = new[] - { - new - { - Id = 1, - Name = "moo" - }, - new - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .Not.To.Contain.Exactly(1) - .Deep.Equal.To( - new - { - Id = 1, - Name = "moo" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var left = new { Date = DateTime.Now }; + var right = new { Date = left.Date.AddSeconds(1) }; - [Test] - public void NegativeAssertion_AltGrammer_WhenShouldFail_ShouldThrow() + // Pre-assert + // Act + Assert.That( + () => { - // Arrange - var src = new[] - { - new - { - Id = 1, - Name = "moo" - }, - new - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Not.Contain.Exactly(1) - .Deep.Equal.To( - new - { - Id = 1, - Name = "moo" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } + Expect(left).To.Deep.Equal( + right, + new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + // Assert + } - [Test] - public void PositiveAssertion_WhenShouldFailForObjectMisMatch_ShouldThrow() - { - // Arrange - var src = new[] - { - new - { - Id = 1, - Name = "moo" - }, - new - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Deep.Equal.To( - new - { - Id = 1, - Name = "bar" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void MessageWhenCustomEqualityComparerSaysNotEqual() + { + // Arrange + var left = new { Date = DateTime.Now }; + var right = new { Date = left.Date.AddSeconds(1) }; - [Test] - public void PositiveAssertion_WhenShouldFailForCountMismatch_ShouldThrow() + // Pre-assert + // Act + Assert.That( + () => { - // Arrange - var src = new[] - { - new - { - Id = 1, - Name = "moo" - }, - new - { - Id = 2, - Name = "Cake" - } - }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(2) - .Deep.Equal.To( - new - { - Id = 1, - Name = "Cake" - } - ); - }, - Throws.Exception.InstanceOf()); - // Assert - } + Expect(left).To.Deep.Equal( + right, + new NeverEqualEqualityComparer()); + }, + Throws.Exception.InstanceOf() + .With.Message.Contain(nameof(NeverEqualEqualityComparer))); + // Assert + } + } + } + } + } - [Test] - public void UsingCustomEqualityComparer_ForDateTime() - { - // Arrange - var src = new[] { new { Date = DateTime.Now } }; - var search = new { Date = DateTime.Now.AddSeconds(-1) }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Deep.Equal.To(search, new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Intersection.Equal.To(search, new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - // Assert - } + [TestFixture] + public class Intersection + { + public class IdentifierAndName + { + public int Id { get; } + public string Name { get; } - [Test] - public void UsingCustomEqualityComparer_ForDouble() - { - // Arrange - var src = new[] { new { Date = DateTime.Now } }; - var search = new { Date = DateTime.Now.AddSeconds(-1) }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Deep.Equal.To(search, new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - Assert.That( - () => - { - Expect(src) - .To.Contain.Exactly(1) - .Intersection.Equal.To(search, new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - // Assert - } - } - } + public IdentifierAndName(int id, string name) + { + Id = id; + Name = name; + } + } - [TestFixture] - public class Equivalent - { - [TestFixture] - public class To - { - public class IdentifierAndName1 - { - public int Id { get; } - public string Name { get; } + public class OtherIdentifierAndName + { + public int Id { get; } + public string Name { get; } + public string Type => GetType().Name; - public IdentifierAndName1(int id, string name) - { - Id = id; - Name = name; - } - } + public OtherIdentifierAndName(int id, string name) + { + Id = id; + Name = name; + } + } - public class IdentifierAndName2 - { - public int Id { get; } - public string Name { get; } + private static IdentifierAndName o1(int id, string name) + { + return new IdentifierAndName(id, name); + } - public IdentifierAndName2(int id, string name) - { - Id = id; - Name = name; - } - } + private static OtherIdentifierAndName o2(int id, string name) + { + return new OtherIdentifierAndName(id, name); + } - private static IdentifierAndName1 o1(int id, string name) - { - return new IdentifierAndName1(id, name); - } + [TestFixture] + public class Equivalent + { + [TestFixture] + public class To + { + [Test] + public void PositiveExpectation_WhenHaveEquivalence_ShouldNotThrow() + { + // Arrange + var first = new[] { o1(1, "moo"), o1(2, "cow") }; + var second = new[] { o2(2, "cow"), o2(1, "moo") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).As.Objects.To.Be.Intersection.Equivalent.To(second); + }, + Throws.Nothing); + // Assert + } - [Test] - public void PositiveExpectation_WhenHaveEquivalence_ShouldNotThrow() - { - // Arrange - var first = new[] { o1(1, "moo"), o1(2, "cow") }; - var second = new[] { o1(2, "cow"), o1(1, "moo") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).To.Be.Deep.Equivalent.To(second); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegativeExpectation_WhenHaveEquivalence_ShouldThrow() + { + // Arrange + var first = new[] { o1(1, "moo"), o1(2, "cow") }; + var second = new[] { o2(2, "cow"), o2(1, "moo") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).As.Objects.Not.To.Be.Intersection.Equivalent.To(second); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void NegativeExpectation_WhenHaveEquivalence_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "moo"), o1(2, "cow") }; - var second = new[] { o1(2, "cow"), o1(1, "moo") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).Not.To.Be.Deep.Equivalent.To(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void NegativeExpectation_AltGrammar_WhenHaveEquivalence_ShouldThrow() + { + // Arrange + var first = new[] { o1(1, "moo"), o1(2, "cow") }; + var second = new[] { o2(2, "cow"), o2(1, "moo") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).As.Objects.To.Not.Be.Intersection.Equivalent.To(second); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void NegativeExpectation_AltGrammar_WhenHaveEquivalence_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "moo"), o1(2, "cow") }; - var second = new[] { o1(2, "cow"), o1(1, "moo") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).To.Not.Be.Deep.Equivalent.To(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void PositiveExpectation_WhenCollectionsDontMatch_ShouldThrow() + { + // Arrange + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o2(1, "bobby"), o2(2, "janet") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).As.Objects.To.Be.Intersection.Equivalent.To(second); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void PositiveExpectation_WhenCollectionsDontMatch_ShouldThrow() - { - // Arrange - var first = new[] { o1(1, "bob"), o1(2, "janet") }; - var second = new[] { o1(1, "bobby"), o1(2, "janet") }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(first).To.Be.Deep.Equivalent.To(second); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [TestFixture] + public class UsingCustomEqualityComparers + { + [Test] + public void AllowingDateTimeDrift() + { + // Arrange + var left = new { Date = DateTime.Now }.InArray(); + var right = new { Date = left[0].Date.AddSeconds(1) }.InArray(); - [Test] - public void WithCustomEqualityComparer() + // Pre-assert + // Act + Assert.That( + () => { - // Arrange - var first = new[] { new { Date = DateTime.Now } }; - var second = new[] { new { Date = DateTime.Now.AddSeconds(-1) } }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(first).To.Be.Deep.Equivalent.To( - second, - new DriftingDateTimeEqualityComparer()); - }, - Throws.Nothing); - // Assert - } - } + Expect(left).As.Objects.To.Be.Intersection.Equivalent.To( + right, + new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + // Assert } } } } [TestFixture] - public class TestingIssuesFromTheWild + public class Equal { - public class ChangeRequest - { - public int Id { get; set; } - public int ChangeRequestNumber { get; set; } - public Guid Uuid { get; set; } - public string Title { get; set; } - public string Detail { get; set; } - public string Motivation { get; set; } - public int CategoryId { get; set; } - public int CountryId { get; set; } - public int DistributionCenterId { get; set; } - public int StatusId { get; set; } - public int NumberOfStoresId { get; set; } - public string SubmittedBy { get; set; } - public int SubmittedByUserId { get; set; } - public DateTime SubmittedDate { get; set; } - public string AssignedTo { get; set; } - public int AssignedToUserId { get; set; } - public string ReleaseNumber { get; set; } - public int UpdatedDate { get; set; } - public int CompletedDate { get; set; } - public string AttachmentGuidsCsv { get; set; } - } - [Test] - public void RegularDeepEquality() + public void PositiveExpectation_WhenCollectionsMatch_ShouldNotThrow() { // Arrange - var left = GetRandom(); - var right = new ChangeRequest(); - left.CopyPropertiesTo(right); + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o2(1, "bob"), o2(2, "janet") }; + // Pre-Assert // Act Assert.That( - () => Expect(left).To.Deep.Equal(right), Throws.Nothing); + () => + { + Expect(first).As.Objects.To.Intersection.Equal(second); + }, + Throws.Nothing); // Assert } - public interface IWrapped + [Test] + public void NegativeExpectation_WhenCollectionsMatch_ShouldThrow() { - int Id { get; set; } - int ChangeRequestNumber { get; set; } + // Arrange + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o2(1, "bob"), o2(2, "janet") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).As.Objects.Not.To.Intersection.Equal(second); + }, + Throws.Exception.InstanceOf()); + // Assert } [Test] - public void WrappedDeepEquality() + public void NegativeExpectation_AltGrammar_WhenCollectionsMatch_ShouldThrow() { - if (RunningUnderMono()) - { - Assert.Ignore("This test fails under mono"); - return; - } - // Arrange - var left = GetRandom(); - var right = new ChangeRequest(); - left.CopyPropertiesTo(right); - left.Uuid = Guid.NewGuid(); // will end up being available through a private field! - var wrappedLeft = left.DuckAs(); - var wrappedRight = right.DuckAs(); + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o2(1, "bob"), o2(2, "janet") }; + // Pre-Assert // Act Assert.That( - () => Expect(wrappedLeft).To.Deep.Equal(wrappedRight), Throws.Nothing); + () => + { + Expect(first).As.Objects.To.Not.Intersection.Equal(second); + }, + Throws.Exception.InstanceOf()); // Assert } - public bool RunningUnderMono() + [Test] + public void PositiveExpectation_AltGrammer_WhenCollectionsMatch_ShouldNotThrow() { - var type = Type.GetType("Mono.Runtime"); - return type != null; + // Arrange + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o2(1, "bob"), o2(2, "janet") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).As.Objects.To.Be.Intersection.Equal.To(second); + }, + Throws.Nothing); + // Assert } - public class HasPrivates + [Test] + public void NegativeExpectation_AltGrammar_WhenCollectionsMatch_ShouldNotThrow() { - public int Id { get; set; } - private string Name { get; set; } - private string _color; - - public HasPrivates(int id, string name, string color) - { - Id = id; - Name = name; - _color = color; - } + // Arrange + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o2(1, "bob"), o2(2, "janet") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).As.Objects.Not.To.Be.Intersection.Equal.To(second); + }, + Throws.Exception.InstanceOf()); + // Assert } [Test] - public void ShouldNotComparePrivateFieldsOrProperties() + public void PositiveExpectation_WhenCollectionsDontMatchInFirstRecord_ShouldThrow() { // Arrange - var left = new HasPrivates(1, "Bob", "red"); - var right = new HasPrivates(1, "Mary", "blue"); + var first = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "paddy") }; + var second = new[] { o2(1, "bobby"), o2(2, "janet"), o2(3, "paddy") }; + // Pre-Assert // Act - Assert.That(() => Expect(left).To.Deep.Equal(right), Throws.Nothing); + Assert.That( + () => + { + Expect(first).As.Objects.To.Intersection.Equal(second); + }, + Throws.Exception.InstanceOf()); // Assert } [Test] - public void ShouldIncludeDebugInfoInError() + public void PositiveExpectation_WhenCollectionsDontMatchInLastRecord_ShouldThrow() { // Arrange - var left = new HasPrivates(1, "Bob", "red"); - var right = new HasPrivates(2, "Mary", "blue"); + var first = new[] { o1(1, "bob"), o1(2, "janet"), o1(3, "paddy") }; + var second = new[] { o2(1, "bob"), o2(2, "janet"), o2(3, "mcgee") }; + // Pre-Assert // Act Assert.That( - () => Expect(left).To.Deep.Equal(right), - Throws.Exception.InstanceOf() - .With.Message.Contains("Property value mismatch for Id")); + () => + { + Expect(first).As.Objects.To.Intersection.Equal(second); + }, + Throws.Exception.InstanceOf()); // Assert } + + [Test] + public void AllowingDateTimeDrift() + { + // Arrange + var left = new { Date = DateTime.Now }.InArray(); + var right = new { Date = left[0].Date.AddSeconds(1) }.InArray(); + + // Pre-assert + // Act + Assert.That( + () => + { + Expect(left).As.Objects.To.Intersection.Equal( + right, + new DriftingDateTimeEqualityComparer()); + Expect(left).As.Objects.To.Be.Intersection.Equal.To( + right, + new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + // Assert + } + } + } + + [TestFixture] + public class Expect_Collection_To_Contain + { + [TestFixture] + public class Exactly_N + { + [TestFixture] + public class Intersection + { + [TestFixture] + public class Equal + { + [TestFixture] + public class To + { + public class Item1 + { + public int Id { get; set; } + public string Name { get; set; } + public DateTime DateTime => DateTime.Now; + } + + public class Item2 + { + public int Id { get; set; } + public string Name { get; set; } + } + + [Test] + public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var src = new[] + { + new Item1 + { + Id = 1, + Name = "moo" + }, + new Item1 + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + // FIXME: this is lazy -- should have a separate test fixture + // for At.Least and At.Most; I'm just in a bit of a hurry and + // need to prove the syntax + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Intersection.Equal.To( + new Item2 + { + Id = 1, + Name = "moo" + } + ); + }, + Throws.Nothing); + Assert.That( + () => + { + Expect(src.And(new Item1 + { + Id = 1, + Name = "moo" + })) + .To.Contain.At.Least(1) + .Intersection.Equal.To( + new Item2 + { + Id = 1, + Name = "moo" + } + ); + }, + Throws.Nothing); + Assert.That( + () => + { + Expect(src) + .To.Contain.At.Most(1) + .Intersection.Equal.To( + new Item2 + { + Id = 2, + Name = "Cake" + } + ); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void NegativeAssertion_WhenShouldFail_ShouldThrow() + { + // Arrange + var src = new[] + { + new Item1 + { + Id = 1, + Name = "moo" + }, + new Item1 + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .Not.To.Contain.Exactly(1) + .Intersection.Equal.To( + new Item2 + { + Id = 1, + Name = "moo" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void CountMatchWithCustomComparer() + { + // Arrange + var left = new[] { new { Date = DateTime.Now } }; + var search = new { Date = DateTime.Now.AddSeconds(-1) }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(left).To.Contain.Only(1) + .Intersection.Equal.To( + search, + new DriftingDateTimeEqualityComparer() + ); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GivenInvalidComparers_ShouldThrowArgumentException() + { + // Arrange + var left = new[] { new { Date = DateTime.Now } }; + var search = new { Date = DateTime.Now.AddSeconds(-1) }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(left).To.Contain.Only(1) + .Intersection.Equal.To( + search, + new NotAnEqualityComparer() + ); + }, + Throws.Exception.InstanceOf() + .With.InnerException.InstanceOf() + .With.Message.Contains("must implement IEqualityComparer")); + // Assert + } + + public class NotAnEqualityComparer : IDisposable + { + public void Dispose() + { + } + } + + [Test] + public void NegativeAssertion_AltGrammer_WhenShouldFail_ShouldThrow() + { + // Arrange + var src = new[] + { + new Item1 + { + Id = 1, + Name = "moo" + }, + new Item1 + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Not.Contain.Exactly(1) + .Intersection.Equal.To( + new Item2 + { + Id = 1, + Name = "moo" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void PositiveAssertion_WhenShouldFailForObjectMisMatch_ShouldThrow() + { + // Arrange + var src = new[] + { + new Item1 + { + Id = 1, + Name = "moo" + }, + new Item1 + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Intersection.Equal.To( + new Item2 + { + Id = 1, + Name = "bar" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void PositiveAssertion_WhenShouldFailForCountMismatch_ShouldThrow() + { + // Arrange + var src = new[] + { + new Item1 + { + Id = 1, + Name = "moo" + }, + new Item1 + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.At.Least(2) + .Intersection.Equal.To( + new Item2 + { + Id = 1, + Name = "Cake" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } + } + } + + [TestFixture] + public class Deep + { + [TestFixture] + public class Equal + { + [TestFixture] + public class To + { + [Test] + public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var src = new[] + { + new + { + Id = 1, + Name = "moo" + }, + new + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Deep.Equal.To( + new + { + Id = 1, + Name = "moo" + } + ) + .And + .To.Contain.Exactly(1) + .Deep.Equal.To( + new + { + Id = 2, + Name = "Cake" + }); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void NegativeAssertion_WhenShouldFail_ShouldThrow() + { + // Arrange + var src = new[] + { + new + { + Id = 1, + Name = "moo" + }, + new + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .Not.To.Contain.Exactly(1) + .Deep.Equal.To( + new + { + Id = 1, + Name = "moo" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void NegativeAssertion_AltGrammer_WhenShouldFail_ShouldThrow() + { + // Arrange + var src = new[] + { + new + { + Id = 1, + Name = "moo" + }, + new + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Not.Contain.Exactly(1) + .Deep.Equal.To( + new + { + Id = 1, + Name = "moo" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void PositiveAssertion_WhenShouldFailForObjectMisMatch_ShouldThrow() + { + // Arrange + var src = new[] + { + new + { + Id = 1, + Name = "moo" + }, + new + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Deep.Equal.To( + new + { + Id = 1, + Name = "bar" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void PositiveAssertion_WhenShouldFailForCountMismatch_ShouldThrow() + { + // Arrange + var src = new[] + { + new + { + Id = 1, + Name = "moo" + }, + new + { + Id = 2, + Name = "Cake" + } + }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(2) + .Deep.Equal.To( + new + { + Id = 1, + Name = "Cake" + } + ); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void UsingCustomEqualityComparer_ForDateTime() + { + // Arrange + var src = new[] { new { Date = DateTime.Now } }; + var search = new { Date = DateTime.Now.AddSeconds(-1) }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Deep.Equal.To(search, new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Intersection.Equal.To(search, new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void UsingCustomEqualityComparer_ForDouble() + { + // Arrange + var src = new[] { new { Date = DateTime.Now } }; + var search = new { Date = DateTime.Now.AddSeconds(-1) }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Deep.Equal.To(search, new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + Assert.That( + () => + { + Expect(src) + .To.Contain.Exactly(1) + .Intersection.Equal.To(search, new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + // Assert + } + } + } + + [TestFixture] + public class Equivalent + { + [TestFixture] + public class To + { + public class IdentifierAndName1 + { + public int Id { get; } + public string Name { get; } + + public IdentifierAndName1(int id, string name) + { + Id = id; + Name = name; + } + } + + public class IdentifierAndName2 + { + public int Id { get; } + public string Name { get; } + + public IdentifierAndName2(int id, string name) + { + Id = id; + Name = name; + } + } + + private static IdentifierAndName1 o1(int id, string name) + { + return new IdentifierAndName1(id, name); + } + + [Test] + public void PositiveExpectation_WhenHaveEquivalence_ShouldNotThrow() + { + // Arrange + var first = new[] { o1(1, "moo"), o1(2, "cow") }; + var second = new[] { o1(2, "cow"), o1(1, "moo") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).To.Be.Deep.Equivalent.To(second); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void NegativeExpectation_WhenHaveEquivalence_ShouldThrow() + { + // Arrange + var first = new[] { o1(1, "moo"), o1(2, "cow") }; + var second = new[] { o1(2, "cow"), o1(1, "moo") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).Not.To.Be.Deep.Equivalent.To(second); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void NegativeExpectation_AltGrammar_WhenHaveEquivalence_ShouldThrow() + { + // Arrange + var first = new[] { o1(1, "moo"), o1(2, "cow") }; + var second = new[] { o1(2, "cow"), o1(1, "moo") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).To.Not.Be.Deep.Equivalent.To(second); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void PositiveExpectation_WhenCollectionsDontMatch_ShouldThrow() + { + // Arrange + var first = new[] { o1(1, "bob"), o1(2, "janet") }; + var second = new[] { o1(1, "bobby"), o1(2, "janet") }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(first).To.Be.Deep.Equivalent.To(second); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void WithCustomEqualityComparer() + { + // Arrange + var first = new[] { new { Date = DateTime.Now } }; + var second = new[] { new { Date = DateTime.Now.AddSeconds(-1) } }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(first).To.Be.Deep.Equivalent.To( + second, + new DriftingDateTimeEqualityComparer()); + }, + Throws.Nothing); + // Assert + } + } + } + } + } + } + + [TestFixture] + public class TestingIssuesFromTheWild + { + public class ChangeRequest + { + public int Id { get; set; } + public int ChangeRequestNumber { get; set; } + public Guid Uuid { get; set; } + public string Title { get; set; } + public string Detail { get; set; } + public string Motivation { get; set; } + public int CategoryId { get; set; } + public int CountryId { get; set; } + public int DistributionCenterId { get; set; } + public int StatusId { get; set; } + public int NumberOfStoresId { get; set; } + public string SubmittedBy { get; set; } + public int SubmittedByUserId { get; set; } + public DateTime SubmittedDate { get; set; } + public string AssignedTo { get; set; } + public int AssignedToUserId { get; set; } + public string ReleaseNumber { get; set; } + public int UpdatedDate { get; set; } + public int CompletedDate { get; set; } + public string AttachmentGuidsCsv { get; set; } + } + + [Test] + public void RegularDeepEquality() + { + // Arrange + var left = GetRandom(); + var right = new ChangeRequest(); + left.CopyPropertiesTo(right); + // Act + Assert.That( + () => Expect(left).To.Deep.Equal(right), Throws.Nothing); + // Assert + } + + public interface IWrapped + { + int Id { get; set; } + int ChangeRequestNumber { get; set; } + } + + [Test] + public void WrappedDeepEquality() + { + if (RunningUnderMono()) + { + Assert.Ignore("This test fails under mono"); + return; + } + + // Arrange + var left = GetRandom(); + var right = new ChangeRequest(); + left.CopyPropertiesTo(right); + left.Uuid = Guid.NewGuid(); // will end up being available through a private field! + var wrappedLeft = left.DuckAs(); + var wrappedRight = right.DuckAs(); + // Act + Assert.That( + () => Expect(wrappedLeft).To.Deep.Equal(wrappedRight), Throws.Nothing); + // Assert + } + + public bool RunningUnderMono() + { + var type = Type.GetType("Mono.Runtime"); + return type != null; + } + + public class HasPrivates + { + public int Id { get; set; } + private string Name { get; set; } + private string _color; + + public HasPrivates(int id, string name, string color) + { + Id = id; + Name = name; + _color = color; + } + } + + [Test] + public void ShouldNotComparePrivateFieldsOrProperties() + { + // Arrange + var left = new HasPrivates(1, "Bob", "red"); + var right = new HasPrivates(1, "Mary", "blue"); + // Act + Assert.That(() => Expect(left).To.Deep.Equal(right), Throws.Nothing); + // Assert + } + + [Test] + public void ShouldIncludeDebugInfoInError() + { + // Arrange + var left = new HasPrivates(1, "Bob", "red"); + var right = new HasPrivates(2, "Mary", "blue"); + // Act + Assert.That( + () => Expect(left).To.Deep.Equal(right), + Throws.Exception.InstanceOf() + .With.Message.Contains("Property value mismatch for Id")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/DictionaryTesting.cs b/src/NExpect.Tests/Collections/DictionaryTesting.cs index 9ac85acc..767d04ec 100644 --- a/src/NExpect.Tests/Collections/DictionaryTesting.cs +++ b/src/NExpect.Tests/Collections/DictionaryTesting.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using NUnit.Framework; using NExpect.Exceptions; @@ -8,1279 +8,1278 @@ // ReSharper disable InconsistentNaming // ReSharper disable ExpressionIsAlwaysNull -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class DictionaryTesting { [TestFixture] - public class DictionaryTesting + public class Issues { - [TestFixture] - public class Issues + [Test] + public void NullDictionaryAssertion() { - [Test] - public void NullDictionaryAssertion() - { - // Arrange - var dict = null as Dictionary; - // Pre-assert - // Act - Assert.That( - () => Expect(dict).To.Be.Null(), - Throws.Nothing - ); - Assert.That( - () => Expect(dict).Not.To.Be.Null(), - Throws.Exception.InstanceOf() - ); - // Assert - } + // Arrange + var dict = null as Dictionary; + // Pre-assert + // Act + Assert.That( + () => Expect(dict).To.Be.Null(), + Throws.Nothing + ); + Assert.That( + () => Expect(dict).Not.To.Be.Null(), + Throws.Exception.InstanceOf() + ); + // Assert } + } - [TestFixture] - public class ShortContain + [TestFixture] + public class ShortContain + { + [Test] + public void WhenDoesContainSoughtValue_ShouldNotThrow() { - [Test] - public void WhenDoesContainSoughtValue_ShouldNotThrow() + // Arrange + var kvp = GetRandom>(); + var src = new Dictionary() { - // Arrange - var kvp = GetRandom>(); - var src = new Dictionary() - { - [kvp.Key] = kvp.Value - }; - var dict = src as IDictionary; + [kvp.Key] = kvp.Value + }; + var dict = src as IDictionary; - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(dict).To.Contain(kvp); - foreach (var item in dict) - Expect(dict).To.Contain(item); - }, - Throws.Nothing - ); - - // Assert - } + // Act + Assert.That( + () => + { + Expect(dict).To.Contain(kvp); + foreach (var item in dict) + Expect(dict).To.Contain(item); + }, + Throws.Nothing + ); + + // Assert + } - [Test] - public void PositiveAssertion_WhenDoesNotContainSoughtValue_ShouldThrow() + [Test] + public void PositiveAssertion_WhenDoesNotContainSoughtValue_ShouldThrow() + { + // Arrange + var missed = GetRandom>(); + var have = GetAnother(missed); + var dict = new Dictionary() { - // Arrange - var missed = GetRandom>(); - var have = GetAnother(missed); - var dict = new Dictionary() - { - [have.Key] = have.Value - }; + [have.Key] = have.Value + }; - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(dict).To.Contain(missed); - }, - Throws.Exception - .InstanceOf() - .With.Message.Matches( - (string actual) => actual.Compact() - .Equals($"Expected {dict.Stringify()} to contain {missed.Stringify()}".Compact()) - ) - ); - - // Assert - } + // Act + Assert.That( + () => + { + Expect(dict).To.Contain(missed); + }, + Throws.Exception + .InstanceOf() + .With.Message.Matches( + (string actual) => actual.Compact() + .Equals($"Expected {dict.Stringify()} to contain {missed.Stringify()}".Compact()) + ) + ); + + // Assert + } - [Test] - public void NegativeAssertion_WhenDoesContainSoughtValue_ShouldThrow() + [Test] + public void NegativeAssertion_WhenDoesContainSoughtValue_ShouldThrow() + { + // Arrange + var have = GetRandom>(); + var dict = new Dictionary() { - // Arrange - var have = GetRandom>(); - var dict = new Dictionary() - { - [have.Key] = have.Value - }; + [have.Key] = have.Value + }; - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(dict).Not.To.Contain(have); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"Expected\n{dict.Stringify()}\nnot to contain\n{have.Stringify()}") - ); - // Assert - } + // Act + Assert.That( + () => + { + Expect(dict).Not.To.Contain(have); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"Expected\n{dict.Stringify()}\nnot to contain\n{have.Stringify()}") + ); + // Assert } + } + [TestFixture] + public class Expect_Dictionary_To_Contain + { [TestFixture] - public class Expect_Dictionary_To_Contain + public class Key { [TestFixture] - public class Key + public class OperatingOnPlainDictionary { - [TestFixture] - public class OperatingOnPlainDictionary + [Test] + public void WhenDictionaryHasKey_ShouldNotThrow() { - [Test] - public void WhenDictionaryHasKey_ShouldNotThrow() + // Arrange + var key = GetRandomString(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var src = new Dictionary() + [key] = GetRandomInt() + }; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = GetRandomInt() - }; - // Pre-Assert + Expect(src).To.Contain.Key(key); + }, + Throws.Nothing + ); - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key); - }, - Throws.Nothing - ); + // Assert + } - // Assert + public static IEnumerable CaseInsensitiveComparers() + { + return new[] + { + StringComparer.OrdinalIgnoreCase, + StringComparer.CurrentCultureIgnoreCase, + StringComparer.InvariantCultureIgnoreCase + }; + } + + [TestCaseSource(nameof(CaseInsensitiveComparers))] + public void WhenDictionaryIsCaseInsentive_ShouldNotThrowForIncorrectCasing(StringComparer comparer) + { + // Arrange + var key = GetRandomAlphaString(2); + var recased = key.ToUpperInvariant(); + if (recased == key) + { + recased = key.ToLowerInvariant(); } - public static IEnumerable CaseInsensitiveComparers() + var src = new Dictionary(comparer) { - return new[] + [key] = GetRandomInt() + }; + // Pre-assert + // Act + Assert.That( + () => { - StringComparer.OrdinalIgnoreCase, - StringComparer.CurrentCultureIgnoreCase, - StringComparer.InvariantCultureIgnoreCase - }; + Expect(src).To.Contain.Key(recased); + }, + Throws.Nothing + ); + // Assert + } + + public static IEnumerable CaseSensitiveComparers() + { + return new[] + { + StringComparer.CurrentCulture, + StringComparer.InvariantCulture, + StringComparer.Ordinal + }; + } + + [TestCaseSource(nameof(CaseSensitiveComparers))] + public void WhenDictionaryIsCaseSentive_ShouldThrowForIncorrectCasing(StringComparer comparer) + { + // Arrange + var key = GetRandomAlphaString(2); + var recased = key.ToUpperInvariant(); + if (recased == key) + { + recased = key.ToLowerInvariant(); } - [TestCaseSource(nameof(CaseInsensitiveComparers))] - public void WhenDictionaryIsCaseInsentive_ShouldNotThrowForIncorrectCasing(StringComparer comparer) + var src = new Dictionary(comparer) { - // Arrange - var key = GetRandomAlphaString(2); - var recased = key.ToUpperInvariant(); - if (recased == key) + [key] = GetRandomInt() + }; + // Pre-assert + // Act + Assert.That( + () => { - recased = key.ToLowerInvariant(); - } + Expect(src).To.Contain.Key(recased); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void Negated_WhenDictionaryHasKey_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var src = new Dictionary() + { + [key] = GetRandomInt() + }; + // Pre-Assert - var src = new Dictionary(comparer) + // Act + Assert.That( + () => { - [key] = GetRandomInt() - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(recased); - }, - Throws.Nothing - ); - // Assert - } + Expect(src).Not.To.Contain.Key(key); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + $"not to contain key\n{key.Stringify()}" + ) + ); + + // Assert + } - public static IEnumerable CaseSensitiveComparers() + [Test] + public void Negated_Alt_WhenDictionaryHasKey_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var src = new Dictionary() { - return new[] + [key] = GetRandomInt() + }; + // Pre-Assert + + // Act + Assert.That( + () => { - StringComparer.CurrentCulture, - StringComparer.InvariantCulture, - StringComparer.Ordinal - }; - } + Expect(src).To.Not.Contain.Key(key); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + $"not to contain key\n{key.Stringify()}" + ) + ); + + // Assert + } - [TestCaseSource(nameof(CaseSensitiveComparers))] - public void WhenDictionaryIsCaseSentive_ShouldThrowForIncorrectCasing(StringComparer comparer) + [TestFixture] + public class WithValue + { + [TestFixture] + public class OperatingOnPrimitiveTypes { - // Arrange - var key = GetRandomAlphaString(2); - var recased = key.ToUpperInvariant(); - if (recased == key) + [Test] + public void WhenDictionaryHasKey_AndValueMatches_ShouldNotThrow() { - recased = key.ToLowerInvariant(); + // Arrange + var key = GetRandomString(2); + var value = GetRandomInt(2); + var src = new Dictionary() + { + [key] = value + }; + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(src).To.Contain.Key(key).With.Value(value); + }, + Throws.Nothing + ); + + // Assert } - var src = new Dictionary(comparer) + [Test] + public void WhenCaseInsensitiveDictionaryHasKey_AndValueMatches_ShouldNotThrow() { - [key] = GetRandomInt() - }; - // Pre-assert - // Act - Assert.That( - () => + // Arrange + var key = GetRandomString(2); + var value = GetRandomInt(2); + var src = new Dictionary(StringComparer.OrdinalIgnoreCase) { - Expect(src).To.Contain.Key(recased); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + [key] = value + }; + var recased = key.ToUpperInvariant(); + if (recased == key) + { + recased = key.ToLowerInvariant(); + } + // Pre-Assert - [Test] - public void Negated_WhenDictionaryHasKey_ShouldThrow() - { - // Arrange - var key = GetRandomString(2); - var src = new Dictionary() - { - [key] = GetRandomInt() - }; - // Pre-Assert + // Act + Assert.That( + () => + { + Expect(src).To.Contain.Key(recased).With.Value(value); + }, + Throws.Nothing + ); - // Act - Assert.That( - () => - { - Expect(src).Not.To.Contain.Key(key); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"not to contain key\n{key.Stringify()}" - ) - ); - - // Assert - } + // Assert + } - [Test] - public void Negated_Alt_WhenDictionaryHasKey_ShouldThrow() - { - // Arrange - var key = GetRandomString(2); - var src = new Dictionary() + [Test] + public void WhenDictionaryDoesNotHaveKey_ShouldThrowForThatReason() { - [key] = GetRandomInt() - }; - // Pre-Assert + // Arrange + var key = GetRandomString(2); + var value = GetRandomInt(2); + var src = new Dictionary() + { + [key] = value + }; + var testingValue = GetAnother(key); + // Pre-Assert + + // Act + TestUtils.WithNoLineBreaks( + () => + { + Assert.That( + () => + { + Expect(src).To.Contain.Key(testingValue).With.Value(value); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"to contain key\n\"{testingValue}\"") + ); + } + ); + + // Assert + } - // Act - Assert.That( - () => + [Test] + public void WhenDictionaryHasKey_AndValueDoesNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = GetRandomInt(2); + var src = new Dictionary() { - Expect(src).To.Not.Contain.Key(key); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"not to contain key\n{key.Stringify()}" - ) - ); - - // Assert + [key] = value + }; + var testingValue = GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(src).To.Contain.Key(key).With.Value(testingValue); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"Expected\n{testingValue}\nbut got\n{value}") + ); + + // Assert + } } [TestFixture] - public class WithValue + public class OperatingOnNullable { - [TestFixture] - public class OperatingOnPrimitiveTypes + [Test] + public void WhenDictionaryHasKey_AndValueMatches_ShouldNotThrow() { - [Test] - public void WhenDictionaryHasKey_AndValueMatches_ShouldNotThrow() + // Arrange + var key = GetRandomString(2); + int? value = GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(value); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(value); + }, + Throws.Nothing + ); - [Test] - public void WhenCaseInsensitiveDictionaryHasKey_AndValueMatches_ShouldNotThrow() + // Assert + } + + [Test] + public void WhenDictionaryHasKey_AndValueDoesNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + int? value = GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomInt(2); - var src = new Dictionary(StringComparer.OrdinalIgnoreCase) + [key] = value + }; + var testingValue = GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var recased = key.ToUpperInvariant(); - if (recased == key) + Expect(src).To.Contain.Key(key).With.Value(testingValue); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"Expected\n{testingValue}\nbut got\n{value}") + ); + + // Assert + } + + [Test] + public void WhenDictionaryHasKey_AndValueIsNull_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + int? value = null; + var src = new Dictionary() + { + [key] = value + }; + var testingValue = GetRandomInt(); + // Pre-Assert + + // Act + TestUtils.WithNoLineBreaks( + () => { - recased = key.ToLowerInvariant(); + Assert.That( + () => + { + Expect(src).To.Contain.Key(key).With.Value(testingValue); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"Expected\n{testingValue}\nbut got\n(null)") + ); } - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(recased).With.Value(value); - }, - Throws.Nothing - ); - - // Assert - } + ); + // Assert + } - [Test] - public void WhenDictionaryDoesNotHaveKey_ShouldThrowForThatReason() + [Test] + public void WhenDictionaryHasKey_AndMatchingValueIsNull_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + int? value = GetRandomInt(); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + int? testingValue = null; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var testingValue = GetAnother(key); - // Pre-Assert - - // Act - TestUtils.WithNoLineBreaks( - () => - { - Assert.That( - () => - { - Expect(src).To.Contain.Key(testingValue).With.Value(value); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"to contain key\n\"{testingValue}\"") - ); - } - ); + Expect(src).To.Contain.Key(key).With.Value(testingValue); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"Expected\n(null)\nbut got\n{value}") + ); - // Assert - } + // Assert + } + } - [Test] - public void WhenDictionaryHasKey_AndValueDoesNotMatch_ShouldThrow() + [TestFixture] + public class OperatingOnEnumerable + { + [Test] + public void WhenDictionaryHasKey_AndValueMatches_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = new List + { + GetRandomInt(2, 9), + GetRandomInt(2, 9), + GetRandomInt(2, 9) + }; + var test = new List(value); + var src = new Dictionary>() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var testingValue = GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(testingValue); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"Expected\n{testingValue}\nbut got\n{value}") - ); - - // Assert - } + Expect(src) + .To.Contain.Key(key) + .With.Value(test); + }, + Throws.Nothing + ); + + // Assert } - [TestFixture] - public class OperatingOnNullable + [Test] + public void WhenTestAndMatchAreNull_ShouldNotThrow() { - [Test] - public void WhenDictionaryHasKey_AndValueMatches_ShouldNotThrow() + // Arrange + var key = GetRandomString(2); + var src = new Dictionary>() { - // Arrange - var key = GetRandomString(2); - int? value = GetRandomInt(2); - var src = new Dictionary() + [key] = null + }; + // Act + Assert.That( + () => { - [key] = value - }; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(value); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src) + .To.Contain.Key(key) + .With.Value(null); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void WhenDictionaryHasKey_AndValueDoesNotMatch_ShouldThrow() - { - // Arrange - var key = GetRandomString(2); - int? value = GetRandomInt(2); - var src = new Dictionary() - { - [key] = value - }; - var testingValue = GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(testingValue); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"Expected\n{testingValue}\nbut got\n{value}") - ); - - // Assert - } + [Test] + public void TestingDeepEqualityOnDateTimeValues() + { + // Arrange + var date1 = DateTime.Now; + var date2 = new DateTime(date1.Ticks, date1.Kind); + var tester = new DeepEqualityTester( + date1, + date2 + ); + Expect(date1.Equals(date2)) + .To.Be.True("test values don't actually match..."); + Expect(date1.Kind).To.Equal( + date2.Kind, + () => "Not really equal if their kinds are different, eh" + ); + // Act + var result = tester.AreDeepEqual(); + // Assert + Expect(result).To.Be.True(); + } + + [Test] + public void TestingDeepEqualityOnNulls() + { + // Arrange + var left = null as string; + var right = null as string; + var tester = new DeepEqualityTester(left, right); + // Act + var result = tester.AreDeepEqual(); + // Assert + Expect(result).To.Be.True(); + } - [Test] - public void WhenDictionaryHasKey_AndValueIsNull_ShouldThrow() + [Test] + public void WhenDictionaryHasKey_AndValueDoesNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = new List + { + GetRandomInt(2, 9), + GetRandomInt(2, 9), + GetRandomInt(2, 9) + }; + var src = new Dictionary>() { - // Arrange - var key = GetRandomString(2); - int? value = null; - var src = new Dictionary() + [key] = value + }; + var testingValue = new List + { + GetRandomInt(10, 19), + GetRandomInt(10, 19), + GetRandomInt(10, 19) + }; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var testingValue = GetRandomInt(); - // Pre-Assert - - // Act - TestUtils.WithNoLineBreaks( - () => - { - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(testingValue); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"Expected\n{testingValue}\nbut got\n(null)") - ); - } - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(testingValue); + }, + Throws.Exception.TypeOf() + .With.Message + .Contains( + $"Expected\n{testingValue.Stringify()}\nbut got\n{value.Stringify()}" + ) + ); + + // Assert + } + } - [Test] - public void WhenDictionaryHasKey_AndMatchingValueIsNull_ShouldThrow() + [TestFixture] + public class DeepEquality + { + [Test] + public void ShouldPassWhenMatched() + { + // Arrange + var data = new { foo = "bar" }; + var copy = new { foo = "bar" }; + var key = GetRandomString(1); + var dictionary = new Dictionary() { - // Arrange - var key = GetRandomString(2); - int? value = GetRandomInt(); - var src = new Dictionary() + [key] = data + }; + // Act + Assert.That( + () => { - [key] = value - }; - int? testingValue = null; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(testingValue); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"Expected\n(null)\nbut got\n{value}") - ); - - // Assert - } + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Deep.Equal.To(copy); + }, + Throws.Nothing + ); + // Assert } - [TestFixture] - public class OperatingOnEnumerable + [Test] + public void ShouldFailWhenUnMatched() { - [Test] - public void WhenDictionaryHasKey_AndValueMatches_ShouldNotThrow() + // Arrange + var data = new { foo = "bar" }; + var other = new { foo1 = "bar" }; + var another = new { foo = "qux" }; + var key = GetRandomString(1); + var dictionary = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = new List + [key] = data + }; + // Act + Assert.That( + () => { - GetRandomInt(2, 9), - GetRandomInt(2, 9), - GetRandomInt(2, 9) - }; - var test = new List(value); - var src = new Dictionary>() + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Deep.Equal.To(other); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(data.Stringify()) + .And.Message.Contains(other.Stringify()) + ); + + Assert.That( + () => { - [key] = value - }; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Key(key) - .With.Value(test); - }, - Throws.Nothing - ); - - // Assert - } + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Deep.Equal.To(another); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(data.Stringify()) + .And.Message.Contains(another.Stringify()) + ); + // Assert + } - [Test] - public void WhenTestAndMatchAreNull_ShouldNotThrow() + [Test] + public void ShouldIncludeCustomMessagesWhenFailing() + { + // Arrange + var data = new { foo = "bar" }; + var other = new { foo1 = "bar" }; + var another = new { foo = "qux" }; + var key = GetRandomString(1); + var message1 = GetRandomString(10); + var message2 = GetRandomString(10); + var dictionary = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var src = new Dictionary>() + [key] = data + }; + // Act + Assert.That( + () => { - [key] = null - }; - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Key(key) - .With.Value(null); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void TestingDeepEqualityOnDateTimeValues() - { - // Arrange - var date1 = DateTime.Now; - var date2 = new DateTime(date1.Ticks, date1.Kind); - var tester = new DeepEqualityTester( - date1, - date2 - ); - Expect(date1.Equals(date2)) - .To.Be.True("test values don't actually match..."); - Expect(date1.Kind).To.Equal( - date2.Kind, - () => "Not really equal if their kinds are different, eh" - ); - // Act - var result = tester.AreDeepEqual(); - // Assert - Expect(result).To.Be.True(); - } + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Deep.Equal.To(other, message1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message1) + .And.Message.Contains(data.Stringify()) + .And.Message.Contains(other.Stringify()) + ); + + Assert.That( + () => + { + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Deep.Equal.To(another, () => message2); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(data.Stringify()) + .And.Message.Contains(another.Stringify()) + .And.Message.Contains(message2) + ); + // Assert + } + } - [Test] - public void TestingDeepEqualityOnNulls() + [TestFixture] + public class IntersectionEquality + { + [Test] + public void ShouldPassWhenMatched() + { + // Arrange + var data = new { foo = "bar", other = "moo" }; + var copy = new { foo = "bar", quuz = "wibble" }; + var key = GetRandomString(1); + var dictionary = new Dictionary() { - // Arrange - var left = null as string; - var right = null as string; - var tester = new DeepEqualityTester(left, right); - // Act - var result = tester.AreDeepEqual(); - // Assert - Expect(result).To.Be.True(); - } + [key] = data + }; + // Act + Assert.That( + () => + { + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Intersection.Equal.To(copy); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void WhenDictionaryHasKey_AndValueDoesNotMatch_ShouldThrow() + [Test] + public void ShouldFailWhenUnMatched() + { + // Arrange + var data = new { foo = "bar" }; + var other = new { foo1 = "bar" }; + var another = new { foo = "qux" }; + var key = GetRandomString(1); + var dictionary = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = new List - { - GetRandomInt(2, 9), - GetRandomInt(2, 9), - GetRandomInt(2, 9) - }; - var src = new Dictionary>() + [key] = data + }; + // Act + Assert.That( + () => { - [key] = value - }; - var testingValue = new List + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Intersection.Equal.To(other); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(data.Stringify()) + .And.Message.Contains(other.Stringify()) + ); + + Assert.That( + () => { - GetRandomInt(10, 19), - GetRandomInt(10, 19), - GetRandomInt(10, 19) - }; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(testingValue); - }, - Throws.Exception.TypeOf() - .With.Message - .Contains( - $"Expected\n{testingValue.Stringify()}\nbut got\n{value.Stringify()}" - ) - ); - - // Assert - } + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Deep.Equal.To(another); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(data.Stringify()) + .And.Message.Contains(another.Stringify()) + ); + // Assert } - [TestFixture] - public class DeepEquality + [Test] + public void ShouldIncludeCustomMessagesWhenFailing() { - [Test] - public void ShouldPassWhenMatched() + // Arrange + var data = new { foo = "bar" }; + var other = new { foo1 = "bar" }; + var another = new { foo = "qux" }; + var key = GetRandomString(1); + var message1 = GetRandomString(10); + var message2 = GetRandomString(10); + var dictionary = new Dictionary() { - // Arrange - var data = new { foo = "bar" }; - var copy = new { foo = "bar" }; - var key = GetRandomString(1); - var dictionary = new Dictionary() + [key] = data + }; + // Act + Assert.That( + () => { - [key] = data - }; - // Act - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Deep.Equal.To(copy); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldFailWhenUnMatched() - { - // Arrange - var data = new { foo = "bar" }; - var other = new { foo1 = "bar" }; - var another = new { foo = "qux" }; - var key = GetRandomString(1); - var dictionary = new Dictionary() + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Intersection.Equal.To(other, message1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message1) + .And.Message.Contains(data.Stringify()) + .And.Message.Contains(other.Stringify()) + ); + + Assert.That( + () => { - [key] = data - }; - // Act - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Deep.Equal.To(other); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(data.Stringify()) - .And.Message.Contains(other.Stringify()) - ); - - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Deep.Equal.To(another); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(data.Stringify()) - .And.Message.Contains(another.Stringify()) - ); - // Assert - } + Expect(dictionary) + .To.Contain.Key(key) + .With.Value.Deep.Equal.To(another, () => message2); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(data.Stringify()) + .And.Message.Contains(another.Stringify()) + .And.Message.Contains(message2) + ); + // Assert + } + } - [Test] - public void ShouldIncludeCustomMessagesWhenFailing() + [TestFixture] + public class OperatingOnMisMatchedTypes + { + [Test] + public void WhenDictionaryValueIsSByte_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (sbyte) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var data = new { foo = "bar" }; - var other = new { foo1 = "bar" }; - var another = new { foo = "qux" }; - var key = GetRandomString(1); - var message1 = GetRandomString(10); - var message2 = GetRandomString(10); - var dictionary = new Dictionary() + [key] = value + }; + var expected = (int) value; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = data - }; - // Act - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Deep.Equal.To(other, message1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message1) - .And.Message.Contains(data.Stringify()) - .And.Message.Contains(other.Stringify()) - ); - - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Deep.Equal.To(another, () => message2); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(data.Stringify()) - .And.Message.Contains(another.Stringify()) - .And.Message.Contains(message2) - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Nothing + ); + + // Assert } - [TestFixture] - public class IntersectionEquality + [Test] + public void WhenDictionaryValueIsSByte_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() { - [Test] - public void ShouldPassWhenMatched() + // Arrange + var key = GetRandomString(2); + var value = (sbyte) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var data = new { foo = "bar", other = "moo" }; - var copy = new { foo = "bar", quuz = "wibble" }; - var key = GetRandomString(1); - var dictionary = new Dictionary() + [key] = value + }; + var expected = (int) GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = data - }; - // Act - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Intersection.Equal.To(copy); - }, - Throws.Nothing - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Exception.TypeOf() + ); + // Assert + } - [Test] - public void ShouldFailWhenUnMatched() + [Test] + public void WhenDictionaryValueIsShort_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (short) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var data = new { foo = "bar" }; - var other = new { foo1 = "bar" }; - var another = new { foo = "qux" }; - var key = GetRandomString(1); - var dictionary = new Dictionary() + [key] = value + }; + var expected = (int) value; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = data - }; - // Act - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Intersection.Equal.To(other); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(data.Stringify()) - .And.Message.Contains(other.Stringify()) - ); - - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Deep.Equal.To(another); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(data.Stringify()) - .And.Message.Contains(another.Stringify()) - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Nothing + ); - [Test] - public void ShouldIncludeCustomMessagesWhenFailing() - { - // Arrange - var data = new { foo = "bar" }; - var other = new { foo1 = "bar" }; - var another = new { foo = "qux" }; - var key = GetRandomString(1); - var message1 = GetRandomString(10); - var message2 = GetRandomString(10); - var dictionary = new Dictionary() - { - [key] = data - }; - // Act - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Intersection.Equal.To(other, message1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message1) - .And.Message.Contains(data.Stringify()) - .And.Message.Contains(other.Stringify()) - ); - - Assert.That( - () => - { - Expect(dictionary) - .To.Contain.Key(key) - .With.Value.Deep.Equal.To(another, () => message2); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(data.Stringify()) - .And.Message.Contains(another.Stringify()) - .And.Message.Contains(message2) - ); - // Assert - } + // Assert } - [TestFixture] - public class OperatingOnMisMatchedTypes + [Test] + public void WhenDictionaryValueIsShort_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() { - [Test] - public void WhenDictionaryValueIsSByte_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() + // Arrange + var key = GetRandomString(2); + var value = (short) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (sbyte) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (int) GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (int) value; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Exception.TypeOf() + ); + // Assert + } - [Test] - public void WhenDictionaryValueIsSByte_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() + [Test] + public void WhenDictionaryValueIsInt_AndExpectedIsLong_ValuesMatch_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (sbyte) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (long) value; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (int) GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Exception.TypeOf() - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Nothing + ); - [Test] - public void WhenDictionaryValueIsShort_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() - { - // Arrange - var key = GetRandomString(2); - var value = (short) GetRandomInt(2); - var src = new Dictionary() - { - [key] = value - }; - var expected = (int) value; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Nothing - ); - - // Assert - } + // Assert + } - [Test] - public void WhenDictionaryValueIsShort_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() + [Test] + public void WhenDictionaryValueIsInt_AndExpectedIsLong_ValuesDoNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (short) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (long) GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (int) GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Exception.TypeOf() - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Exception.TypeOf() + ); + // Assert + } - [Test] - public void WhenDictionaryValueIsInt_AndExpectedIsLong_ValuesMatch_ShouldNotThrow() + [Test] + public void WhenDictionaryValueIsByte_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (byte) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (int) value; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (long) value; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Nothing + ); - [Test] - public void WhenDictionaryValueIsInt_AndExpectedIsLong_ValuesDoNotMatch_ShouldThrow() - { - // Arrange - var key = GetRandomString(2); - var value = GetRandomInt(2); - var src = new Dictionary() - { - [key] = value - }; - var expected = (long) GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Exception.TypeOf() - ); - // Assert - } + // Assert + } - [Test] - public void WhenDictionaryValueIsByte_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() + [Test] + public void WhenDictionaryValueIsByte_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (byte) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (byte) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (int) GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (int) value; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Exception.TypeOf() + ); + // Assert + } - [Test] - public void WhenDictionaryValueIsByte_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() + [Test] + public void WhenDictionaryValueIsUShort_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (ushort) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (byte) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (int) value; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (int) GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Exception.TypeOf() - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Nothing + ); - [Test] - public void WhenDictionaryValueIsUShort_AndExpectedIsInt_ValuesMatch_ShouldNotThrow() + // Assert + } + + [Test] + public void WhenDictionaryValueIsUShort_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (ushort) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (ushort) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (int) GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (int) value; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Exception.TypeOf() + ); + // Assert + } - [Test] - public void WhenDictionaryValueIsUShort_AndExpectedIsInt_ValuesDoNotMatch_ShouldThrow() + [Test] + public void WhenDictionaryValueIsUInt_AndExpectedIsLong_ValuesMatch_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (uint) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (ushort) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (long) value; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (int) GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Exception.TypeOf() - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Nothing + ); - [Test] - public void WhenDictionaryValueIsUInt_AndExpectedIsLong_ValuesMatch_ShouldNotThrow() + // Assert + } + + [Test] + public void WhenDictionaryValueIsUInt_AndExpectedIsLong_ValuesDoNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = (uint) GetRandomInt(2); + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (uint) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = (long) GetAnother(value); + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (long) value; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Exception.TypeOf() + ); + // Assert + } - [Test] - public void WhenDictionaryValueIsUInt_AndExpectedIsLong_ValuesDoNotMatch_ShouldThrow() + [Test] + public void WhenDictionaryValueIsFloat_AndExpectedIsDouble_ValuesMatch_ShouldNotThrow() + { + // Arrange + var key = GetRandomString(2); + var value = 123f; + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = (uint) GetRandomInt(2); - var src = new Dictionary() + [key] = value + }; + var expected = 123d; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = (long) GetAnother(value); - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Exception.TypeOf() - ); - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Nothing + ); - [Test] - public void WhenDictionaryValueIsFloat_AndExpectedIsDouble_ValuesMatch_ShouldNotThrow() + // Assert + } + + [Test] + public void WhenDictionaryValueIsFloat_AndExpectedIsDouble_ValuesDoNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = 123.1f; + var src = new Dictionary() { - // Arrange - var key = GetRandomString(2); - var value = 123f; - var src = new Dictionary() + [key] = value + }; + var expected = 54d; + // Pre-Assert + + // Act + Assert.That( + () => { - [key] = value - }; - var expected = 123d; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Nothing - ); - - // Assert - } + Expect(src).To.Contain.Key(key).With.Value(expected); + }, + Throws.Exception.TypeOf() + ); + // Assert + } + } - [Test] - public void WhenDictionaryValueIsFloat_AndExpectedIsDouble_ValuesDoNotMatch_ShouldThrow() - { - // Arrange - var key = GetRandomString(2); - var value = 123.1f; - var src = new Dictionary() + [TestFixture] + public class MatchingValueWithLambda + { + [Test] + public void ShouldBeAbleToMatchValueWithLambda() + { + // Arrange + var dict = new Dictionary(); + var key = GetRandomString(1); + var value = GetRandom(); + dict[key] = value; + // Act + Assert.That( + () => { - [key] = value - }; - var expected = 54d; - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(expected); - }, - Throws.Exception.TypeOf() - ); - // Assert - } + Expect(dict) + .To.Contain.Key(key) + .With.Value.Matched.By( + o => o.Name == value.Name && + o.Id == value.Id + ); + }, + Throws.Nothing + ); + var customMessage = GetRandomWords(); + Assert.That( + () => + { + Expect(dict) + .To.Contain.Key(key) + .With.Value.Matched.By( + o => o.Name != value.Name, + () => customMessage + ); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(customMessage) + ); + // Assert } - [TestFixture] - public class MatchingValueWithLambda + [Test] + public void ValueMismatchOnFoundKeyShouldOutputValueOfValue() { - [Test] - public void ShouldBeAbleToMatchValueWithLambda() - { - // Arrange - var dict = new Dictionary(); - var key = GetRandomString(1); - var value = GetRandom(); - dict[key] = value; - // Act - Assert.That( - () => - { - Expect(dict) - .To.Contain.Key(key) - .With.Value.Matched.By( - o => o.Name == value.Name && - o.Id == value.Id - ); - }, - Throws.Nothing - ); - var customMessage = GetRandomWords(); - Assert.That( - () => - { - Expect(dict) - .To.Contain.Key(key) - .With.Value.Matched.By( - o => o.Name != value.Name, - () => customMessage - ); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage) - ); - // Assert - } + // Arrange + var dict = new Dictionary(); + var key = GetRandomString(1); + var value = GetRandom(); + dict[key] = value; + // Act + Assert.That( + () => + { + Expect(dict) + .To.Contain.Key(key) + .With.Value.Matched.By( + o => o.Name != value.Name + ); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(value.Name) + .And.Message.Contains(value.Id.ToString()) + ); + // Assert + } - [Test] - public void ValueMismatchOnFoundKeyShouldOutputValueOfValue() - { - // Arrange - var dict = new Dictionary(); - var key = GetRandomString(1); - var value = GetRandom(); - dict[key] = value; - // Act - Assert.That( - () => - { - Expect(dict) - .To.Contain.Key(key) - .With.Value.Matched.By( - o => o.Name != value.Name - ); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(value.Name) - .And.Message.Contains(value.Id.ToString()) - ); - // Assert - } + public class IdentifierAndName + { + public int Id { get; } + public string Name { get; } - public class IdentifierAndName + public IdentifierAndName(int id, string name) { - public int Id { get; } - public string Name { get; } - - public IdentifierAndName(int id, string name) - { - Id = id; - Name = name; - } + Id = id; + Name = name; } } } @@ -1288,12 +1287,12 @@ public IdentifierAndName(int id, string name) } } } +} - public static class TestingStringExtensions +public static class TestingStringExtensions +{ + public static string Compact(this string str) { - public static string Compact(this string str) - { - return str.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", ""); - } + return str.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", ""); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/DifferentTypesOfCollections.cs b/src/NExpect.Tests/Collections/DifferentTypesOfCollections.cs index 815ce4ad..86de6088 100644 --- a/src/NExpect.Tests/Collections/DifferentTypesOfCollections.cs +++ b/src/NExpect.Tests/Collections/DifferentTypesOfCollections.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using NUnit.Framework; // ReSharper disable UnusedAutoPropertyAccessor.Global @@ -8,82 +8,81 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable PossibleMultipleEnumeration -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class DifferentTypesOfCollections { [TestFixture] - public class DifferentTypesOfCollections + public class Containing { - [TestFixture] - public class Containing + [Test] + public void ShouldBeAbleToOperateOnOtherCollections() { - [Test] - public void ShouldBeAbleToOperateOnOtherCollections() - { - // Arrange - var collection = new List(new[] {"a", "b", "c"}); - // Pre-Assert + // Arrange + var collection = new List(new[] {"a", "b", "c"}); + // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection).To.Contain.Exactly(1).Equal.To("a"); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(collection).To.Contain.Exactly(1).Equal.To("a"); + }, + Throws.Nothing); - Assert.That(() => - { - Expect(new Queue(collection)).To.Contain.Exactly(1).Equal.To("a"); - }, - Throws.Nothing); + Assert.That(() => + { + Expect(new Queue(collection)).To.Contain.Exactly(1).Equal.To("a"); + }, + Throws.Nothing); - Assert.That(() => - { - Expect(collection as IList).To.Contain.Exactly(1).Equal.To("a"); - }, - Throws.Nothing); + Assert.That(() => + { + Expect(collection as IList).To.Contain.Exactly(1).Equal.To("a"); + }, + Throws.Nothing); - Assert.That(() => - { - Expect(collection as ICollection).To.Contain.Exactly(1).Equal.To("a"); - }, - Throws.Nothing); + Assert.That(() => + { + Expect(collection as ICollection).To.Contain.Exactly(1).Equal.To("a"); + }, + Throws.Nothing); - Assert.That(() => - { - Expect(new Stack(collection)).To.Contain.Exactly(1).Equal.To("a"); - }, - Throws.Nothing); + Assert.That(() => + { + Expect(new Stack(collection)).To.Contain.Exactly(1).Equal.To("a"); + }, + Throws.Nothing); - Assert.That(() => - { - Expect(new HashSet(collection)).To.Contain.Exactly(1).Equal.To("a"); - }, - Throws.Nothing); + Assert.That(() => + { + Expect(new HashSet(collection)).To.Contain.Exactly(1).Equal.To("a"); + }, + Throws.Nothing); - Assert.That(() => - { - Expect(new Dictionary() - { - ["a"] = "aye" - }.Keys) - .To.Contain.Exactly(1) - .Equal.To("a"); - }, - Throws.Nothing); + Assert.That(() => + { + Expect(new Dictionary() + { + ["a"] = "aye" + }.Keys) + .To.Contain.Exactly(1) + .Equal.To("a"); + }, + Throws.Nothing); - Assert.That(() => - { - Expect(new Dictionary() - { - ["a"] = "aye" - }.Values) - .To.Contain.Exactly(1) - .Equal.To("aye"); - }, - Throws.Nothing); + Assert.That(() => + { + Expect(new Dictionary() + { + ["a"] = "aye" + }.Values) + .To.Contain.Exactly(1) + .Equal.To("aye"); + }, + Throws.Nothing); - // Assert - } + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/Distinct.cs b/src/NExpect.Tests/Collections/Distinct.cs index 78aff3bd..c3df1669 100644 --- a/src/NExpect.Tests/Collections/Distinct.cs +++ b/src/NExpect.Tests/Collections/Distinct.cs @@ -2,16 +2,37 @@ using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Distinct { [TestFixture] - public class Distinct + public class OperatingOnEmptyCollection { + [Test] + public void ShouldNotThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Be.Distinct(); + }, + Throws.Nothing); + + // Assert + } + [TestFixture] - public class OperatingOnEmptyCollection + public class WhenNegated { [Test] - public void ShouldNotThrow() + public void ShouldThrow() { // Arrange var collection = new List(); @@ -21,65 +42,86 @@ public void ShouldNotThrow() // Act Assert.That(() => { - Expect(collection).To.Be.Distinct(); + Expect(collection).Not.To.Be.Distinct(); }, - Throws.Nothing); + Throws.Exception.TypeOf() + ); // Assert } + } + } - [TestFixture] - public class WhenNegated - { - [Test] - public void ShouldThrow() + [TestFixture] + public class OperatingOnNullCollection + { + [Test] + public void ShouldThrow() + { + // Arrange + List collection = null; + + // Pre-Assert + + // Act + Assert.That(() => { - // Arrange - var collection = new List(); + // ReSharper disable once ExpressionIsAlwaysNull + Expect(collection).To.Be.Distinct(); + }, + Throws.Exception.TypeOf()); - // Pre-Assert + // Assert + } + } - // Act - Assert.That(() => - { - Expect(collection).Not.To.Be.Distinct(); - }, - Throws.Exception.TypeOf() - ); + [TestFixture] + public class WhenCollectionHasRepeatedItems + { + [Test] + public void ShouldThrow() + { + // Arrange + var collection = new List { 1, 1 }; - // Assert - } - } + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Be.Distinct(); + }, + Throws.Exception.TypeOf()); + + // Assert } [TestFixture] - public class OperatingOnNullCollection + public class WhenNegated { [Test] - public void ShouldThrow() + public void ShouldNotThrow() { // Arrange - List collection = null; + + var collection = new List { 1, 1 }; // Pre-Assert // Act Assert.That(() => { - // ReSharper disable once ExpressionIsAlwaysNull - Expect(collection).To.Be.Distinct(); + Expect(collection) + .Not.To.Be.Distinct(); }, - Throws.Exception.TypeOf()); + Throws.Nothing + ); // Assert } - } - [TestFixture] - public class WhenCollectionHasRepeatedItems - { [Test] - public void ShouldThrow() + public void AltGrammar_ShouldNotThrow() { // Arrange var collection = new List { 1, 1 }; @@ -89,64 +131,43 @@ public void ShouldThrow() // Act Assert.That(() => { - Expect(collection).To.Be.Distinct(); + Expect(collection) + .To.Not.Be.Distinct(); }, - Throws.Exception.TypeOf()); + Throws.Nothing + ); // Assert } + } + } - [TestFixture] - public class WhenNegated - { - [Test] - public void ShouldNotThrow() - { - // Arrange - - var collection = new List { 1, 1 }; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Distinct(); - }, - Throws.Nothing - ); + [TestFixture] + public class WhenCollectionHasUniqueItems + { + [Test] + public void ShouldNotThrow() + { + // Arrange + var collection = new List { 1, 2, 3 }; - // Assert - } + // Pre-Assert - [Test] - public void AltGrammar_ShouldNotThrow() + // Act + Assert.That(() => { - // Arrange - var collection = new List { 1, 1 }; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection) - .To.Not.Be.Distinct(); - }, - Throws.Nothing - ); - - // Assert - } - } + Expect(collection).To.Be.Distinct(); + }, + Throws.Nothing); + + // Assert } [TestFixture] - public class WhenCollectionHasUniqueItems + public class AndIsNegated { [Test] - public void ShouldNotThrow() + public void ShouldThrow() { // Arrange var collection = new List { 1, 2, 3 }; @@ -156,54 +177,32 @@ public void ShouldNotThrow() // Act Assert.That(() => { - Expect(collection).To.Be.Distinct(); + Expect(collection) + .Not.To.Be.Distinct(); }, - Throws.Nothing); + Throws.Exception.InstanceOf() + ); // Assert } - [TestFixture] - public class AndIsNegated + [Test] + public void AltGrammar_ShouldThrow() { - [Test] - public void ShouldThrow() - { - // Arrange - var collection = new List { 1, 2, 3 }; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Distinct(); - }, - Throws.Exception.InstanceOf() - ); - - // Assert - } - - [Test] - public void AltGrammar_ShouldThrow() - { - // Arrange - var collection = new List { 1, 2, 3 }; + // Arrange + var collection = new List { 1, 2, 3 }; - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection).To.Not.Be.Distinct(); - }, - Throws.Exception.TypeOf()); + // Act + Assert.That(() => + { + Expect(collection).To.Not.Be.Distinct(); + }, + Throws.Exception.TypeOf()); - // Assert - } + // Assert } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/DriftingDateTimeEqualityComparer.cs b/src/NExpect.Tests/Collections/DriftingDateTimeEqualityComparer.cs index 131db9e4..81c42d9e 100644 --- a/src/NExpect.Tests/Collections/DriftingDateTimeEqualityComparer.cs +++ b/src/NExpect.Tests/Collections/DriftingDateTimeEqualityComparer.cs @@ -1,19 +1,18 @@ -using System; +using System; using System.Collections.Generic; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +public class DriftingDateTimeEqualityComparer : IEqualityComparer { - public class DriftingDateTimeEqualityComparer : IEqualityComparer + public bool Equals(DateTime x, DateTime y) { - public bool Equals(DateTime x, DateTime y) - { - var delta = x - y; - return Math.Abs(delta.TotalSeconds) < 2; - } + var delta = x - y; + return Math.Abs(delta.TotalSeconds) < 2; + } - public int GetHashCode(DateTime obj) - { - return 0; - } + public int GetHashCode(DateTime obj) + { + return 0; } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/Emptiness.cs b/src/NExpect.Tests/Collections/Emptiness.cs index 6eabfcf0..dd17d997 100644 --- a/src/NExpect.Tests/Collections/Emptiness.cs +++ b/src/NExpect.Tests/Collections/Emptiness.cs @@ -1,212 +1,211 @@ -using System.Collections.Concurrent; +using System.Collections.Concurrent; using System.Collections.Generic; using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Emptiness { [TestFixture] - public class Emptiness + public class OperatingOnCollection { - [TestFixture] - public class OperatingOnCollection + [Test] + public void WhenIsEmpty_ShouldNotThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Be.Empty(); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void WhenIsEmpty_WhenNegated_ShouldThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).Not.To.Be.Empty(); + }, + Throws.Exception + .InstanceOf() + .With.Message.EqualTo("Expected\n[ ]\nnot to be an empty collection")); + + // Assert + } + + [Test] + public void WhenIsNotEmpty_ShouldThrow() + { + // Arrange + var collection = GetRandomCollection(2); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Be.Empty(); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("]\nto be an empty collection")); + + // Assert + } + + [Test] + public void WhenIsNotEmpty_WhenNegated_ShouldNotThrow() { - [Test] - public void WhenIsEmpty_ShouldNotThrow() - { - // Arrange - var collection = new List(); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Be.Empty(); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void WhenIsEmpty_WhenNegated_ShouldThrow() - { - // Arrange - var collection = new List(); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Be.Empty(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo("Expected\n[ ]\nnot to be an empty collection")); - - // Assert - } - - [Test] - public void WhenIsNotEmpty_ShouldThrow() - { - // Arrange - var collection = GetRandomCollection(2); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Be.Empty(); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("]\nto be an empty collection")); - - // Assert - } - - [Test] - public void WhenIsNotEmpty_WhenNegated_ShouldNotThrow() - { - // Arrange - var collection = GetRandomCollection(2); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Be.Empty(); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void WhenIsNotEmpty_WhenNegatedAlt_ShouldNotThrow() - { - // Arrange - var collection = GetRandomCollection(2); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Not.Be.Empty(); - }, - Throws.Nothing); - - // Assert - } + // Arrange + var collection = GetRandomCollection(2); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).Not.To.Be.Empty(); + }, + Throws.Nothing); + + // Assert } - [TestFixture] - public class OperatingOnDictionary + [Test] + public void WhenIsNotEmpty_WhenNegatedAlt_ShouldNotThrow() { - [Test] - public void WhenIsEmpty_ShouldNotThrow() - { - // Arrange - var collection = new Dictionary(); - var sorted = new SortedDictionary(); - var concurrent = new ConcurrentDictionary(); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection as IDictionary).To.Be.Empty(); - Expect(collection).To.Be.Empty(); - Expect(sorted).To.Be.Empty(); - Expect(concurrent).To.Be.Empty(); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void WhenIsEmpty_WhenNegated_ShouldThrow() - { - // Arrange - var collection = new List(); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Be.Empty(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo("Expected\n[ ]\nnot to be an empty collection")); - - // Assert - } - - [Test] - public void WhenIsNotEmpty_ShouldThrow() - { - // Arrange - var collection = GetRandomCollection(2); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Be.Empty(); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("]\nto be an empty collection")); - - // Assert - } - - [Test] - public void WhenIsNotEmpty_WhenNegated_ShouldNotThrow() - { - // Arrange - var collection = GetRandomCollection(2); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Be.Empty(); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void WhenIsNotEmpty_WhenNegatedAlt_ShouldNotThrow() - { - // Arrange - var collection = GetRandomCollection(2); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Not.Be.Empty(); - }, - Throws.Nothing); - - // Assert - } + // Arrange + var collection = GetRandomCollection(2); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Not.Be.Empty(); + }, + Throws.Nothing); + + // Assert + } + } + + [TestFixture] + public class OperatingOnDictionary + { + [Test] + public void WhenIsEmpty_ShouldNotThrow() + { + // Arrange + var collection = new Dictionary(); + var sorted = new SortedDictionary(); + var concurrent = new ConcurrentDictionary(); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection as IDictionary).To.Be.Empty(); + Expect(collection).To.Be.Empty(); + Expect(sorted).To.Be.Empty(); + Expect(concurrent).To.Be.Empty(); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void WhenIsEmpty_WhenNegated_ShouldThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).Not.To.Be.Empty(); + }, + Throws.Exception + .InstanceOf() + .With.Message.EqualTo("Expected\n[ ]\nnot to be an empty collection")); + + // Assert + } + + [Test] + public void WhenIsNotEmpty_ShouldThrow() + { + // Arrange + var collection = GetRandomCollection(2); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Be.Empty(); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("]\nto be an empty collection")); + + // Assert + } + + [Test] + public void WhenIsNotEmpty_WhenNegated_ShouldNotThrow() + { + // Arrange + var collection = GetRandomCollection(2); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).Not.To.Be.Empty(); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void WhenIsNotEmpty_WhenNegatedAlt_ShouldNotThrow() + { + // Arrange + var collection = GetRandomCollection(2); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Not.Be.Empty(); + }, + Throws.Nothing); + + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/Equivalence.cs b/src/NExpect.Tests/Collections/Equivalence.cs index 3b9abf37..6d2c0f6a 100644 --- a/src/NExpect.Tests/Collections/Equivalence.cs +++ b/src/NExpect.Tests/Collections/Equivalence.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using NExpect.Exceptions; using NUnit.Framework; @@ -6,367 +6,535 @@ // ReSharper disable InconsistentNaming -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Equivalence { [TestFixture] - public class Equivalence + public class OperatingOnEmptyCollection { - [TestFixture] - public class OperatingOnEmptyCollection - { - [Test] - public void ComparingWithEmptyCollection_ShouldNotThrow() - { - // Arrange - var collection = new List(); - var compare = new int[0]; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Be.Equivalent.To(compare); - }, - Throws.Nothing); - - // Assert - } - } - - [TestFixture] - public class OperatingOnIdenticalCOllections - { - [Test] - public void ShouldNotThrow() - { - // Arrange - var start = GetRandomCollection(4, 6).ToArray(); - var other = start.Select(i => i).ToArray(); - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(start).To.Be.Equivalent.To(other); - }, - Throws.Nothing); - - // Assert - } - } - - [TestFixture] - public class OperatingOnCollectionsWithSameItemsOutOfOrder - { - [Test] - public void OperatingOnTwoEquivalentCollections_ShouldNotThrow() - { - // Arrange - var start = GetRandomCollection(4, 6).ToArray(); - var other = start.Randomize(); - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(start).To.Be.Equivalent.To(other); - }, - Throws.Nothing); - - // Assert - } - } - - [Test] - public void OperatingOnTwoInequivalentCollectionsOfSameSize_ShouldThrow() + public void ComparingWithEmptyCollection_ShouldNotThrow() { // Arrange - var test = GetRandomArray(4, 6); - var other = GetRandomCollection(test.Length, test.Length); + var collection = new List(); + var compare = new int[0]; + // Pre-Assert // Act Assert.That(() => { - Expect(test).To.Be.Equivalent.To(other); + Expect(collection).To.Be.Equivalent.To(compare); }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("]\nto be equivalent to\n[")); + Throws.Nothing); // Assert } + } + [TestFixture] + public class OperatingOnIdenticalCOllections + { [Test] - public void OperatingOnTwoInequivalentCollectionsOfSameSize_Negated_ShouldNotThrow() + public void ShouldNotThrow() { // Arrange - var test = GetRandomArray(4, 6); - var other = GetRandomCollection(test.Length, test.Length); + var start = GetRandomCollection(4, 6).ToArray(); + var other = start.Select(i => i).ToArray(); // Pre-Assert // Act Assert.That(() => { - Expect(test).Not.To.Be.Equivalent.To(other); + Expect(start).To.Be.Equivalent.To(other); }, Throws.Nothing); // Assert } + } + [TestFixture] + public class OperatingOnCollectionsWithSameItemsOutOfOrder + { [Test] - public void OperatingOnTwoInequivalentCollectionsOfSameSize_NegatedAlt_ShouldNotThrow() + public void OperatingOnTwoEquivalentCollections_ShouldNotThrow() { // Arrange - var test = GetRandomArray(4, 6); - var other = GetRandomCollection(test.Length, test.Length); + var start = GetRandomCollection(4, 6).ToArray(); + var other = start.Randomize(); // Pre-Assert // Act Assert.That(() => { - Expect(test).To.Not.Be.Equivalent.To(other); + Expect(start).To.Be.Equivalent.To(other); }, Throws.Nothing); // Assert } + } - [Test] - public void - OperatingOnTwoEquivalentCollectionsOfSameSizeWithSameRepeatedElements_ShouldNotThrow() - { - // Arrange - var test = new[] {1, 1, 2, 3}; - var other = new[] {1, 2, 3, 1}; - // Pre-Assert + [Test] + public void OperatingOnTwoInequivalentCollectionsOfSameSize_ShouldThrow() + { + // Arrange + var test = GetRandomArray(4, 6); + var other = GetRandomCollection(test.Length, test.Length); + // Pre-Assert - // Act - Assert.That(() => - { - Expect(test).To.Be.Equivalent.To(other); - }, - Throws.Nothing); + // Act + Assert.That(() => + { + Expect(test).To.Be.Equivalent.To(other); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("]\nto be equivalent to\n[")); - // Assert - } + // Assert + } - [Test] - public void - OperatingOnTwoEquivalentCollectionsOfSameSizeWithDifferentRepeatedElements_ShouldThrow() - { - // Arrange - var test = new[] {1, 1, 2, 3}; - var other = new[] {1, 2, 3, 2}; + [Test] + public void OperatingOnTwoInequivalentCollectionsOfSameSize_Negated_ShouldNotThrow() + { + // Arrange + var test = GetRandomArray(4, 6); + var other = GetRandomCollection(test.Length, test.Length); + // Pre-Assert - // Pre-Assert + // Act + Assert.That(() => + { + Expect(test).Not.To.Be.Equivalent.To(other); + }, + Throws.Nothing); - // Act - Assert.That(() => - { - Expect(test).To.Be.Equivalent.To(other); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("]\nto be equivalent to\n[")); + // Assert + } - // Assert - } + [Test] + public void OperatingOnTwoInequivalentCollectionsOfSameSize_NegatedAlt_ShouldNotThrow() + { + // Arrange + var test = GetRandomArray(4, 6); + var other = GetRandomCollection(test.Length, test.Length); + // Pre-Assert - [Test] - public void - OperatingOnTwoEquivalentCollectionsOfSameSizeWithSameRepeatedElements_WhenNegated_ShouldThrow() - { - // Arrange - var test = new[] {1, 1, 2, 3}; - var other = new[] {1, 2, 3, 1}; + // Act + Assert.That(() => + { + Expect(test).To.Not.Be.Equivalent.To(other); + }, + Throws.Nothing); - // Pre-Assert + // Assert + } - // Act - Assert.That(() => - { - Expect(test).Not.To.Be.Equivalent.To(other); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("]\nnot to be equivalent to\n[")); + [Test] + public void + OperatingOnTwoEquivalentCollectionsOfSameSizeWithSameRepeatedElements_ShouldNotThrow() + { + // Arrange + var test = new[] {1, 1, 2, 3}; + var other = new[] {1, 2, 3, 1}; - // Assert - } + // Pre-Assert - [Test] - public void Extending_CountMatchContinuation() - { - // Arrange - var evens = new[] {2, 4, 6}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(evens) - .Not.To.Contain.Any.Odds() - .And.To.Contain.All.Evens(); - }, - Throws.Nothing); - // Assert - } + // Act + Assert.That(() => + { + Expect(test).To.Be.Equivalent.To(other); + }, + Throws.Nothing); - [Test] - public void Extending_CountMatchContinuationNegated() - { - // Arrange - var evens = new[] {2, 4, 6}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(evens).To.Contain.Any.Odds(); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Assert } - [TestFixture] - public class Null + [Test] + public void + OperatingOnTwoEquivalentCollectionsOfSameSizeWithDifferentRepeatedElements_ShouldThrow() { - [Test] - public void OperatingOnNull_ShouldNotThrow() - { - // Arrange - List collection = null; - // Pre-Assert - // Act - Assert.That(() => - { - // ReSharper disable once ExpressionIsAlwaysNull - Expect(collection).To.Be.Null(); - }, - Throws.Nothing); - // Assert - } + // Arrange + var test = new[] {1, 1, 2, 3}; + var other = new[] {1, 2, 3, 2}; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(test).To.Be.Equivalent.To(other); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("]\nto be equivalent to\n[")); + + // Assert + } + + [Test] + public void + OperatingOnTwoEquivalentCollectionsOfSameSizeWithSameRepeatedElements_WhenNegated_ShouldThrow() + { + // Arrange + var test = new[] {1, 1, 2, 3}; + var other = new[] {1, 2, 3, 1}; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(test).Not.To.Be.Equivalent.To(other); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("]\nnot to be equivalent to\n[")); + + // Assert + } + + [Test] + public void Extending_CountMatchContinuation() + { + // Arrange + var evens = new[] {2, 4, 6}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(evens) + .Not.To.Contain.Any.Odds() + .And.To.Contain.All.Evens(); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void Extending_CountMatchContinuationNegated() + { + // Arrange + var evens = new[] {2, 4, 6}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(evens).To.Contain.Any.Odds(); + }, + Throws.Exception.InstanceOf()); + // Assert + } +} + +[TestFixture] +public class Null +{ + [Test] + public void OperatingOnNull_ShouldNotThrow() + { + // Arrange + List collection = null; + // Pre-Assert + // Act + Assert.That(() => + { + // ReSharper disable once ExpressionIsAlwaysNull + Expect(collection).To.Be.Null(); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void OperatingOnNull_Negated_ShouldThrow() + { + // Arrange + List collection = null; + // Pre-Assert + // Act + Assert.That(() => + { + // ReSharper disable once ExpressionIsAlwaysNull + Expect(collection).Not.To.Be.Null(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("not to be null")); + // Assert + } + + [Test] + public void OperatingOnNotNull_Negated_ShouldNotThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + // Act + Assert.That(() => + { + Expect(collection).Not.To.Be.Null(); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void OperatingOnNotNull_ShouldThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + // Act + Assert.That(() => + { + Expect(collection).To.Be.Null(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("]\nto be null")); + // Assert + } + [TestFixture] + public class WithCustomMessage + { [Test] public void OperatingOnNull_Negated_ShouldThrow() { // Arrange + var expectedMessage = "My Message"; List collection = null; // Pre-Assert // Act Assert.That(() => { // ReSharper disable once ExpressionIsAlwaysNull - Expect(collection).Not.To.Be.Null(); + Expect(collection).Not.To.Be.Null(expectedMessage); }, Throws.Exception.InstanceOf() - .With.Message.Contains("not to be null")); + .With.Message.Contains(expectedMessage)); // Assert } [Test] - public void OperatingOnNotNull_Negated_ShouldNotThrow() + public void OperatingOnNotNull_WithCustomMessage_ShouldThrow_IncludingCustomMessage() { // Arrange var collection = new List(); - + var expected = GetRandomString(); // Pre-Assert // Act Assert.That(() => { - Expect(collection).Not.To.Be.Null(); + Expect(collection).To.Be.Null(expected); }, - Throws.Nothing); + Throws.Exception.InstanceOf() + .With.Message.Contains(expected)); // Assert } [Test] - public void OperatingOnNotNull_ShouldThrow() + public void OperatingOnNotNullAlt_WithCustomMessage_ShouldThrow_IncludingCustomMessage() { // Arrange - var collection = new List(); - + List collection = null; + var expected = GetRandomString(); // Pre-Assert // Act Assert.That(() => { - Expect(collection).To.Be.Null(); + // ReSharper disable once ExpressionIsAlwaysNull + Expect(collection).To.Not.Be.Null(expected); }, Throws.Exception.InstanceOf() - .With.Message.Contains("]\nto be null")); + .With.Message.Contains(expected)); // Assert } + } +} - [TestFixture] - public class WithCustomMessage - { - [Test] - public void OperatingOnNull_Negated_ShouldThrow() +[TestFixture] +public class HaveUniqueItems +{ + [Test] + public void OperatingOnEmptyCollection_ShouldNotThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Have.Unique.Items(); + }, + Throws.Nothing + ); + + // Assert + } + + [Test] + public void OperatingOnNullCollection_ShouldThrow() + { + // Arrange + List collection = null; + + // Pre-Assert + + // Act + Assert.That(() => + { + // ReSharper disable once ExpressionIsAlwaysNull + Expect(collection).To.Have.Unique.Items(); + }, + Throws.Exception.TypeOf()); + + // Assert + } + + [Test] + public void Negated_OperatingOnEmptyCollection_ShouldThrow() + { + // Arrange + var collection = new List(); + + // Pre-Assert + + // Act + Assert.That(() => { - // Arrange - var expectedMessage = "My Message"; - List collection = null; - // Pre-Assert - // Act - Assert.That(() => - { - // ReSharper disable once ExpressionIsAlwaysNull - Expect(collection).Not.To.Be.Null(expectedMessage); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expectedMessage)); - // Assert - } - - [Test] - public void OperatingOnNotNull_WithCustomMessage_ShouldThrow_IncludingCustomMessage() + Expect(collection).Not.To.Have.Unique.Items(); + }, + Throws.Exception.TypeOf() + ); + + // Assert + } + + [Test] + public void OperatingOnCollectionWithSameItems_ShouldThrow() + { + // Arrange + var collection = new List {1, 1}; + + // Pre-Assert + + // Act + Assert.That(() => { - // Arrange - var collection = new List(); - var expected = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(collection).To.Be.Null(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected)); - // Assert - } - - [Test] - public void OperatingOnNotNullAlt_WithCustomMessage_ShouldThrow_IncludingCustomMessage() + Expect(collection).To.Have.Unique.Items(); + }, + Throws.Exception.TypeOf()); + + // Assert + } + + [Test] + public void OperatingOnCollectionWithUniqueItems_ShouldNotThrow() + { + // Arrange + var collection = new List {1, 2, 3}; + + // Pre-Assert + + // Act + Assert.That(() => { - // Arrange - List collection = null; - var expected = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - // ReSharper disable once ExpressionIsAlwaysNull - Expect(collection).To.Not.Be.Null(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected)); - // Assert - } - } + Expect(collection).To.Have.Unique.Items(); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void Negated_OperatingOnCollectionWithUniqueItems_ShouldThrow() + { + // Arrange + var collection = new List {1, 2, 3}; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).Not.To.Have.Unique.Items(); + }, + Throws.Exception.TypeOf()); + + // Assert + } + + [Test] + public void Negated_AltGrammar_OperatingOnCollectionWithUniqueItems_ShouldThrow() + { + // Arrange + var collection = new List {1, 2, 3}; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Not.Have.Unique.Items(); + }, + Throws.Exception.TypeOf()); + + // Assert + } + + [Test] + public void Negated_OperatingOnCollectionWithSameItems_ShouldNotThrow() + { + // Arrange + var collection = new List {1, 1}; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).Not.To.Have.Unique.Items(); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void Negated_AltGrammar_OperatingOnCollectionWithSameItems_ShouldNotThrow() + { + // Arrange + var collection = new List {1, 1}; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(collection).To.Not.Have.Unique.Items(); + }, + Throws.Nothing); + + // Assert } [TestFixture] - public class HaveUniqueItems + public class UnmetMessage { [Test] - public void OperatingOnEmptyCollection_ShouldNotThrow() + public void OperatingOnCollectionWithSameItems() { // Arrange - var collection = new List(); + var collection = new List {1, 1, 1}; // Pre-Assert @@ -375,33 +543,33 @@ public void OperatingOnEmptyCollection_ShouldNotThrow() { Expect(collection).To.Have.Unique.Items(); }, - Throws.Nothing + Throws.Exception.TypeOf() + .With.Message.EqualTo("Expected [ 1, 1, 1 ] to only contain unique items") ); - // Assert } [Test] - public void OperatingOnNullCollection_ShouldThrow() + public void OperatingOnCollectionWithUniqueItems() { // Arrange - List collection = null; + var collection = new List {1, 2, 3}; // Pre-Assert // Act Assert.That(() => { - // ReSharper disable once ExpressionIsAlwaysNull - Expect(collection).To.Have.Unique.Items(); + Expect(collection).Not.To.Have.Unique.Items(); }, - Throws.Exception.TypeOf()); - + Throws.Exception.TypeOf() + .With.Message.EqualTo("Expected [ 1, 2, 3 ] to contain duplicate items") + ); // Assert } [Test] - public void Negated_OperatingOnEmptyCollection_ShouldThrow() + public void OperatingOnEmptyCollection() { // Arrange var collection = new List(); @@ -414,714 +582,545 @@ public void Negated_OperatingOnEmptyCollection_ShouldThrow() Expect(collection).Not.To.Have.Unique.Items(); }, Throws.Exception.TypeOf() + .With.Message + .EqualTo("Expected [ ] to contain duplicate items, but found empty collection") ); - // Assert } [Test] - public void OperatingOnCollectionWithSameItems_ShouldThrow() + public void OperatingOnNullCollection() { // Arrange - var collection = new List {1, 1}; + List collection = null; // Pre-Assert // Act Assert.That(() => { + // ReSharper disable once ExpressionIsAlwaysNull Expect(collection).To.Have.Unique.Items(); }, - Throws.Exception.TypeOf()); - + Throws.Exception.TypeOf() + .With.Message.Contains("Expected IEnumerable, but found (null)") + ); // Assert } + } +} - [Test] - public void OperatingOnCollectionWithUniqueItems_ShouldNotThrow() - { - // Arrange - var collection = new List {1, 2, 3}; +[TestFixture] +public class ItemCountTesting +{ + [Test] + public void WhenCollectionHasExpectedCount_ShouldNotThrow() + { + // Arrange + var expected = GetRandomInt(1); + var input = new int[expected]; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Contain.Exactly(expected).Items(); + }, + Throws.Nothing); + // Assert + } - // Pre-Assert + [Test] + public void WhenCollectionDoesNotHaveExpectedCount_ShouldThrow() + { + // Arrange + var expected = GetRandomInt(10); + var delta = GetRandomInt(1, 3); + var actual = GetRandomBoolean() + ? expected + delta + : expected - delta; + var input = new int[actual]; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Contain.Exactly(expected).Items(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected to find exactly {expected} occurrences of any int but found {actual}")); + // Assert + } - // Act - Assert.That(() => - { - Expect(collection).To.Have.Unique.Items(); - }, - Throws.Nothing); + [Test] + public void Negated_WhenCollectionDoesNotHaveExpectedCount_ShouldNotThrow() + { + // Arrange + var expected = GetRandomInt(10); + var delta = GetRandomInt(1, 3); + var actual = GetRandomBoolean() + ? expected + delta + : expected - delta; + var input = new int[actual]; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).Not.To.Contain.Exactly(expected).Items(); + }, + Throws.Nothing); + // Assert + } - // Assert - } - - [Test] - public void Negated_OperatingOnCollectionWithUniqueItems_ShouldThrow() - { - // Arrange - var collection = new List {1, 2, 3}; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Have.Unique.Items(); - }, - Throws.Exception.TypeOf()); - - // Assert - } - - [Test] - public void Negated_AltGrammar_OperatingOnCollectionWithUniqueItems_ShouldThrow() - { - // Arrange - var collection = new List {1, 2, 3}; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Not.Have.Unique.Items(); - }, - Throws.Exception.TypeOf()); - - // Assert - } - - [Test] - public void Negated_OperatingOnCollectionWithSameItems_ShouldNotThrow() - { - // Arrange - var collection = new List {1, 1}; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Have.Unique.Items(); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void Negated_AltGrammar_OperatingOnCollectionWithSameItems_ShouldNotThrow() - { - // Arrange - var collection = new List {1, 1}; + [Test] + public void Negated_WhenCollectionDoesHaveExpectedCount_ShouldThrow() + { + // Arrange + var expected = GetRandomInt(10); + var delta = GetRandomInt(1, 3); + var actual = GetRandomBoolean() + ? expected + delta + : expected - delta; + var input = new int[actual]; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).Not.To.Contain.Exactly(actual).Items(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected not to find exactly {actual} occurrences of any int but found {actual}")); + // Assert + } - // Pre-Assert + [Test] + public void Negated_AltGrammar_WhenCollectionDoesNotHaveExpectedCount_ShouldNotThrow() + { + // Arrange + var expected = GetRandomInt(10); + var delta = GetRandomInt(1, 3); + var actual = GetRandomBoolean() + ? expected + delta + : expected - delta; + var input = new int[actual]; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Contain.Exactly(expected).Items(); + }, + Throws.Nothing); + // Assert + } - // Act - Assert.That(() => - { - Expect(collection).To.Not.Have.Unique.Items(); - }, - Throws.Nothing); + [Test] + public void Item_Alias() + { + // Arrange + var collection = new[] {GetRandomInt()}; - // Assert - } + // Pre-Assert - [TestFixture] - public class UnmetMessage - { - [Test] - public void OperatingOnCollectionWithSameItems() + // Act + Assert.That(() => { - // Arrange - var collection = new List {1, 1, 1}; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).To.Have.Unique.Items(); - }, - Throws.Exception.TypeOf() - .With.Message.EqualTo("Expected [ 1, 1, 1 ] to only contain unique items") - ); - // Assert - } - - [Test] - public void OperatingOnCollectionWithUniqueItems() - { - // Arrange - var collection = new List {1, 2, 3}; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Have.Unique.Items(); - }, - Throws.Exception.TypeOf() - .With.Message.EqualTo("Expected [ 1, 2, 3 ] to contain duplicate items") - ); - // Assert - } - - [Test] - public void OperatingOnEmptyCollection() + Expect(collection).To.Contain.Exactly(1).Item(); + }, + Throws.Nothing); + + Assert.That(() => { - // Arrange - var collection = new List(); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(collection).Not.To.Have.Unique.Items(); - }, - Throws.Exception.TypeOf() - .With.Message - .EqualTo("Expected [ ] to contain duplicate items, but found empty collection") - ); - // Assert - } - - [Test] - public void OperatingOnNullCollection() + Expect(collection).Not.To.Contain.Exactly(1).Item(); + }, + Throws.Exception.InstanceOf()); + + Assert.That(() => { - // Arrange - List collection = null; - - // Pre-Assert - - // Act - Assert.That(() => - { - // ReSharper disable once ExpressionIsAlwaysNull - Expect(collection).To.Have.Unique.Items(); - }, - Throws.Exception.TypeOf() - .With.Message.Contains("Expected IEnumerable, but found (null)") - ); - // Assert - } - } + Expect(collection).To.Not.Contain.Exactly(1).Item(); + }, + Throws.Exception.InstanceOf()); + + // Assert } [TestFixture] - public class ItemCountTesting + public class No { [Test] - public void WhenCollectionHasExpectedCount_ShouldNotThrow() + public void WhenCollectionHasNoItems_ShouldThrow() { // Arrange - var expected = GetRandomInt(1); - var input = new int[expected]; + var input = new int[0]; // Pre-Assert // Act Assert.That(() => { - Expect(input).To.Contain.Exactly(expected).Items(); + Expect(input).To.Contain.No.Items(); }, Throws.Nothing); // Assert } [Test] - public void WhenCollectionDoesNotHaveExpectedCount_ShouldThrow() + public void WhenCollectionHasItems_ShouldThrow() { // Arrange - var expected = GetRandomInt(10); - var delta = GetRandomInt(1, 3); - var actual = GetRandomBoolean() - ? expected + delta - : expected - delta; - var input = new int[actual]; + var expected = GetRandomInt(1); + var input = new int[expected]; // Pre-Assert // Act Assert.That(() => { - Expect(input).To.Contain.Exactly(expected).Items(); + Expect(input).To.Contain.No.Items(); }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected to find exactly {expected} occurrences of any int but found {actual}")); + Throws.Exception.InstanceOf()); // Assert } [Test] - public void Negated_WhenCollectionDoesNotHaveExpectedCount_ShouldNotThrow() + public void Negated_WhenCollectionHasItems_ShouldNotThrow() { // Arrange - var expected = GetRandomInt(10); - var delta = GetRandomInt(1, 3); - var actual = GetRandomBoolean() - ? expected + delta - : expected - delta; - var input = new int[actual]; + var expected = GetRandomInt(1); + var input = new int[expected]; // Pre-Assert // Act Assert.That(() => { - Expect(input).Not.To.Contain.Exactly(expected).Items(); + Expect(input).Not.To.Contain.No.Items(); }, Throws.Nothing); // Assert } + } + [TestFixture] + public class None + { [Test] - public void Negated_WhenCollectionDoesHaveExpectedCount_ShouldThrow() + public void ShouldNotThrowWhenNoMatches() { // Arrange - var expected = GetRandomInt(10); - var delta = GetRandomInt(1, 3); - var actual = GetRandomBoolean() - ? expected + delta - : expected - delta; - var input = new int[actual]; - // Pre-Assert // Act Assert.That(() => - { - Expect(input).Not.To.Contain.Exactly(actual).Items(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected not to find exactly {actual} occurrences of any int but found {actual}")); - // Assert - } - - [Test] - public void Negated_AltGrammar_WhenCollectionDoesNotHaveExpectedCount_ShouldNotThrow() - { - // Arrange - var expected = GetRandomInt(10); - var delta = GetRandomInt(1, 3); - var actual = GetRandomBoolean() - ? expected + delta - : expected - delta; - var input = new int[actual]; - // Pre-Assert - // Act + { + Expect(new[] { 1, 2, 3 }) + .To.Contain.None + .Matched.By(i => i > 3); + }, Throws.Nothing); + Assert.That(() => - { - Expect(input).To.Not.Contain.Exactly(expected).Items(); - }, - Throws.Nothing); + { + Expect(new[] { 1, 2, 3 }) + .Not.To.Contain.None + .Matched.By(i => i > 3); + }, Throws.Exception.InstanceOf()); // Assert } [Test] - public void Item_Alias() + public void ShouldThrowWhenHaveMatches() { // Arrange - var collection = new[] {GetRandomInt()}; - - // Pre-Assert - // Act Assert.That(() => - { - Expect(collection).To.Contain.Exactly(1).Item(); - }, - Throws.Nothing); - - Assert.That(() => - { - Expect(collection).Not.To.Contain.Exactly(1).Item(); - }, - Throws.Exception.InstanceOf()); - + { + Expect(new[] { 1, 2, 3 }) + .To.Contain.None + .Equal.To(3); + }, Throws.Exception.InstanceOf()); + Assert.That(() => - { - Expect(collection).To.Not.Contain.Exactly(1).Item(); - }, - Throws.Exception.InstanceOf()); - + { + Expect(new[] { 1, 2, 3 }) + .To.Contain.None + .Equal.To(4); + }, Throws.Nothing); // Assert } + } +} - [TestFixture] - public class No - { - [Test] - public void WhenCollectionHasNoItems_ShouldThrow() - { - // Arrange - var input = new int[0]; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Contain.No.Items(); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void WhenCollectionHasItems_ShouldThrow() - { - // Arrange - var expected = GetRandomInt(1); - var input = new int[expected]; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Contain.No.Items(); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void Negated_WhenCollectionHasItems_ShouldNotThrow() +[TestFixture] +public class Equal_WithNoCustomMessage +{ + [Test] + public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var expected = GetRandomInt(1); - var input = new int[expected]; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).Not.To.Contain.No.Items(); - }, - Throws.Nothing); - // Assert - } - } + Expect(left).To.Equal(right); + }, + Throws.Nothing); + // Assert + } - [TestFixture] - public class None - { - [Test] - public void ShouldNotThrowWhenNoMatches() - { - // Arrange - // Act - Assert.That(() => - { - Expect(new[] { 1, 2, 3 }) - .To.Contain.None - .Matched.By(i => i > 3); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(new[] { 1, 2, 3 }) - .Not.To.Contain.None - .Matched.By(i => i > 3); - }, Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void ShouldThrowWhenHaveMatches() + [Test] + public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - // Act - Assert.That(() => - { - Expect(new[] { 1, 2, 3 }) - .To.Contain.None - .Equal.To(3); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(new[] { 1, 2, 3 }) - .To.Contain.None - .Equal.To(4); - }, Throws.Nothing); - // Assert - } - } + Expect(left).Not.To.Equal(right); + }, + Throws.Exception.InstanceOf()); + // Assert } - [TestFixture] - public class Equal_WithNoCustomMessage + [Test] + public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() { - [Test] - public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Equal(right); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).Not.To.Equal(right); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Not.Equal(right); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Not.Equal(right); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {2, 1}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Equal(right); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {2, 1}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Equal(right); + }, + Throws.Exception.InstanceOf()); + // Assert } +} - [TestFixture] - public class Equal_NoCustomMessage_LongerSyntax +[TestFixture] +public class Equal_NoCustomMessage_LongerSyntax +{ + [Test] + public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() { - [Test] - public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Equal.To(right); - }, - Throws.Nothing); - // Assert - } + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Be.Equal.To(right); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).Not.To.Be.Equal.To(right); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).Not.To.Be.Equal.To(right); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Not.Be.Equal.To(right); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Not.Be.Equal.To(right); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {2, 1}; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Equal.To(right); - }, - Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {2, 1}; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Be.Equal.To(right); + }, + Throws.Exception.InstanceOf()); + // Assert } +} - [TestFixture] - public class Equal_WithCustomMessage +[TestFixture] +public class Equal_WithCustomMessage +{ + [Test] + public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() { - [Test] - public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Equal(right, message); - }, - Throws.Nothing); - // Assert - } + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Equal(right, message); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).Not.To.Equal(right, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - // Assert - } + [Test] + public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).Not.To.Equal(right, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + // Assert + } - [Test] - public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Not.Equal(right, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - // Assert - } + [Test] + public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Not.Equal(right, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + // Assert + } - [Test] - public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {2, 1}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Equal(right, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - // Assert - } + [Test] + public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {2, 1}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Equal(right, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + // Assert } +} - [TestFixture] - public class Equal_WithCustomMessageLongerSyntax +[TestFixture] +public class Equal_WithCustomMessageLongerSyntax +{ + [Test] + public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() { - [Test] - public void GivenTwoCollectionsWithSameItemsInSameOrder_ShouldNotThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Equal.To(right, message); - }, - Throws.Nothing); - // Assert - } + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Be.Equal.To(right, message); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).Not.To.Be.Equal.To(right, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - // Assert - } + [Test] + public void Negated_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).Not.To.Be.Equal.To(right, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + // Assert + } - [Test] - public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {1, 2}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Not.Be.Equal.To(right, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - // Assert - } + [Test] + public void Negated_AltGrammar_GivenTwoCollectionsWithSameItemsInSameOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {1, 2}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Not.Be.Equal.To(right, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + // Assert + } - [Test] - public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() - { - // Arrange - var left = new[] {1, 2}; - var right = new[] {2, 1}; - var message = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(left).To.Be.Equal.To(right, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - // Assert - } + [Test] + public void GivenTwoCollectionsWithSameItemsOutOfOrder_ShouldThrow() + { + // Arrange + var left = new[] {1, 2}; + var right = new[] {2, 1}; + var message = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(left).To.Be.Equal.To(right, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/Exactly.cs b/src/NExpect.Tests/Collections/Exactly.cs index d55a486d..acea1cf0 100644 --- a/src/NExpect.Tests/Collections/Exactly.cs +++ b/src/NExpect.Tests/Collections/Exactly.cs @@ -1,22 +1,58 @@ -using System; +using System; using System.Collections.Generic; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.RandomGenerators; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Exactly { [TestFixture] - public class Exactly + public class Equal { [TestFixture] - public class Equal + public class To { [TestFixture] - public class To + public class OperatingOnCollectionOfStrings { + [Test] + public void WhenDoesContain_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var collection = new[] + { + search, + other1, + other2 + }.Randomize(); + + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1).Equal.To(search); + }, + Throws.Nothing + ); + + // Assert + } + [TestFixture] - public class OperatingOnCollectionOfStrings + public class ShortContain { [Test] public void WhenDoesContain_ShouldNotThrow() @@ -43,7 +79,7 @@ public void WhenDoesContain_ShouldNotThrow() Assert.That( () => { - Expect(collection).To.Contain.Exactly(1).Equal.To(search); + Expect(collection).To.Contain(search); }, Throws.Nothing ); @@ -51,635 +87,8 @@ public void WhenDoesContain_ShouldNotThrow() // Assert } - [TestFixture] - public class ShortContain - { - [Test] - public void WhenDoesContain_ShouldNotThrow() - { - // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( - new[] - { - search, - other1 - } - ); - var collection = new[] - { - search, - other1, - other2 - }.Randomize(); - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain(search); - }, - Throws.Nothing - ); - - // Assert - } - - [Test] - public void Negated_WhenDoesContain_ShouldThrow() - { - // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( - new[] - { - search, - other1 - } - ); - var collection = new[] - { - search, - other1, - other2 - }.Randomize(); - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).Not.To.Contain(search); - }, - Throws.Exception.InstanceOf() - ); - - // Assert - } - - [Test] - public void - Negated_AltGrammar_WhenDoesContain_ShouldThrow() - { - // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( - new[] - { - search, - other1 - } - ); - var collection = new[] - { - search, - other1, - other2 - }.Randomize(); - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Not.Contain(search); - }, - Throws.Exception.InstanceOf() - ); - - // Assert - } - - [Test] - public void ShouldBeAbleToChain() - { - // Arrange - var ints = new[] - { - 1, - 2, - 3 - }; - // Act - Assert.That( - () => - { - Expect(ints) - .To.Contain(1) - .And - .To.Contain(3); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(ints) - .To.Contain(1) - .And - .Not.To.Contain(4); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(ints) - .To.Contain(1) - .And - .Not.To.Contain(3); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } - - [Test] - public void ShouldBeAbleToChainForHashSet() - { - // Arrange - var ints = new HashSet( - new[] - { - 1, - 2, - 3 - } - ); - // Act - Assert.That( - () => - { - Expect(ints) - .To.Contain(1) - .And - .To.Contain(3); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(ints) - .To.Contain(1) - .And - .Not.To.Contain(4); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(ints) - .To.Contain(1) - .And - .Not.To.Contain(3); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } - } - - [TestFixture] - public class StringCollectionContainingStringContaining - { - [Test] - public void WhenCollectionContains1StringContainingTestValueWith1Required_ShouldNotThrow() - { - // Arrange - var collection = new[] - { - "moo cows", - "bovine", - "beef-witted" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1).Containing("cow"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void WhenCollectionContains2StringsContainingTestValueWith2Required_ShouldNotThrow() - { - // Arrange - var collection = new[] - { - "moo cows", - "bovine", - "beef-witted", - "cows moo" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection) - .To.Contain.Exactly(2).Containing("cow"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void WhenCollectionContainsTooManyMatchingStrings_ShouldThrow() - { - // Arrange - var collection = new[] - { - "moo cows", - "bovine", - "beef-witted", - "cows moo" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1).Containing("cow"); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } - - [Test] - public void WhenThrowing_ShouldIncludeCustomMessageIfSet() - { - // Arrange - var collection = new[] - { - "moo cows", - "bovine", - "beef-witted", - "cows moo" - }; - var expected = GetRandomString(10); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1).Containing("cow", expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } - - [Test] - public void ShouldUseProvidedStringComparison() - { - // Arrange - var collection = new[] - { - "bovine", - "beef-witted", - "cows moo" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Containing("Cow", StringComparison.OrdinalIgnoreCase); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldUseProvidedStringComparisonAndCustomMessage() - { - // Arrange - var collection = new[] - { - "bovine", - "beef-witted", - "Cows moo" - }; - var expected = GetRandomString(10); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1).Containing( - "cow", - StringComparison.Ordinal, - expected - ); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } - } - - [TestFixture] - public class StringCollectionContainingItemEndingWith - { - [Test] - public void WhenShouldPassWithOneOrdinalMatch_ShouldNotThrow() - { - // Arrange - var collection = new[] - { - "cow says moo", - "foo bar" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Ending.With("moo"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldUseProvidedComparer() - { - // Arrange - var collection = new[] - { - "cow says Moo", - "foo bar" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Ending.With("moo", StringComparison.OrdinalIgnoreCase); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldUseProvidedCustomMessageForFailure() - { - // Arrange - var collection = new[] - { - "cow says Moo", - "foo bar" - }; - var expected = GetRandomString(10); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Ending.With("moo", expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } - - [Test] - public void ShouldUseProvidedStringComparerAndCustomMessageForFailure() - { - // Arrange - var collection = new[] - { - "cow says Moo", - "foo bar" - }; - var expected = GetRandomString(10); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Ending.With("moo", StringComparison.InvariantCulture, expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } - } - - [TestFixture] - public class StringCollectionContainingItemStartingWith - { - [Test] - public void WhenShouldPassWithOneOrdinalMatch_ShouldNotThrow() - { - // Arrange - var collection = new[] - { - "moo, said the cow", - "foo bar" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Starting.With("moo"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldUseProvidedComparer() - { - // Arrange - var collection = new[] - { - "Moo said the cow", - "foo bar" - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Starting.With("moo", StringComparison.OrdinalIgnoreCase); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldUseProvidedCustomMessageForFailure() - { - // Arrange - var collection = new[] - { - "Moo said the cow", - "foo bar" - }; - var expected = GetRandomString(10); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Starting.With("moo", expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - .And.Message.Contains(collection[0]) - ); - // Assert - } - - [Test] - public void ShouldUseProvidedStringComparerAndCustomMessageForFailure() - { - // Arrange - var collection = new[] - { - "Moo, said the cow", - "foo bar" - }; - var expected = GetRandomString(10); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Exactly(1) - .Starting.With("moo", StringComparison.InvariantCulture, expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } - } - - [TestFixture] - public class StringCollectionContainingItemMatchedByRegex - { - [Test] - public void ShouldFindSingleMatch() - { - // Arrange - var data = new[] - { - "foo", - "bar", - "quux" - }; - // Act - Assert.That( - () => - { - Expect(data) - .To.Contain.Exactly(1) - .Matched.By("^foo$"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldFailOnNoMatch() - { - // Arrange - var data = new[] - { - "bar", - "quux" - }; - // Act - Assert.That( - () => - { - Expect(data) - .To.Contain.Exactly(1) - .Matched.By("^foo$"); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } - - [Test] - public void ShouldFailOnCountMisMatch() - { - // Arrange - var data = new[] - { - "foobar", - "fooquux", - "barfoo" - }; - // Act - Assert.That( - () => - { - Expect(data) - .To.Contain.Exactly(1) - .Matched.By("foo"); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(data) - .To.Contain.Exactly(1) - .Matched.By("^foo"); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(data) - .To.Contain.Exactly(1) - .Matched.By("foo$"); - }, - Throws.Nothing - ); - // Assert - } - } - - [Test] - public void WhenSeeking2AndDoesContain2_ShouldNotThrow() + public void Negated_WhenDoesContain_ShouldThrow() { // Arrange var search = GetRandomString(3); @@ -695,8 +104,7 @@ public void WhenSeeking2AndDoesContain2_ShouldNotThrow() { search, other1, - other2, - search + other2 }.Randomize(); // Pre-Assert @@ -704,16 +112,17 @@ public void WhenSeeking2AndDoesContain2_ShouldNotThrow() Assert.That( () => { - Expect(collection).To.Contain.Exactly(2).Equal.To(search); + Expect(collection).Not.To.Contain(search); }, - Throws.Nothing + Throws.Exception.InstanceOf() ); // Assert } [Test] - public void WhenSeeking1AndDoesContain2_ShouldThrow() + public void + Negated_AltGrammar_WhenDoesContain_ShouldThrow() { // Arrange var search = GetRandomString(3); @@ -725,207 +134,797 @@ public void WhenSeeking1AndDoesContain2_ShouldThrow() other1 } ); - var customMessage = GetRandomString(2); var collection = new[] { search, other1, - other2, - search + other2 }.Randomize(); - var standardMessage = $"to find exactly 1 occurrence of \"{search}\" but found 2"; // Pre-Assert // Act Assert.That( () => { - Expect(collection).To.Contain.Exactly(1).Equal.To(search, customMessage); + Expect(collection).To.Not.Contain(search); }, - Throws.Exception - .InstanceOf() - .With.Message.Matches($"{customMessage}.*\\s*.*{standardMessage}") + Throws.Exception.InstanceOf() ); + // Assert } [Test] - public void WhenDoesNoContain_ShouldThrow() + public void ShouldBeAbleToChain() { // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( + var ints = new[] + { + 1, + 2, + 3 + }; + // Act + Assert.That( + () => + { + Expect(ints) + .To.Contain(1) + .And + .To.Contain(3); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(ints) + .To.Contain(1) + .And + .Not.To.Contain(4); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(ints) + .To.Contain(1) + .And + .Not.To.Contain(3); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void ShouldBeAbleToChainForHashSet() + { + // Arrange + var ints = new HashSet( new[] { - search, - other1 + 1, + 2, + 3 } ); + // Act + Assert.That( + () => + { + Expect(ints) + .To.Contain(1) + .And + .To.Contain(3); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(ints) + .To.Contain(1) + .And + .Not.To.Contain(4); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(ints) + .To.Contain(1) + .And + .Not.To.Contain(3); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + } + + [TestFixture] + public class StringCollectionContainingStringContaining + { + [Test] + public void WhenCollectionContains1StringContainingTestValueWith1Required_ShouldNotThrow() + { + // Arrange var collection = new[] { - other1, - other2 - }.Randomize(); - - // Pre-Assert + "moo cows", + "bovine", + "beef-witted" + }; + // Pre-assert // Act Assert.That( () => { - Expect(collection).To.Contain.Exactly(1).Equal.To(search); + Expect(collection).To.Contain.Exactly(1).Containing("cow"); }, - Throws.Exception - .InstanceOf() - .With.Message.Contains("find exactly 1 occurrence of") + Throws.Nothing ); - // Assert } [Test] - public void Negated_WhenDoesContain_ShouldThrow() + public void WhenCollectionContains2StringsContainingTestValueWith2Required_ShouldNotThrow() { // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( - new[] + var collection = new[] + { + "moo cows", + "bovine", + "beef-witted", + "cows moo" + }; + // Pre-assert + // Act + Assert.That( + () => { - search, - other1 - } + Expect(collection) + .To.Contain.Exactly(2).Containing("cow"); + }, + Throws.Nothing ); + // Assert + } + + [Test] + public void WhenCollectionContainsTooManyMatchingStrings_ShouldThrow() + { + // Arrange var collection = new[] { - search, - other1, - other2 - }.Randomize(); - - // Pre-Assert - + "moo cows", + "bovine", + "beef-witted", + "cows moo" + }; + // Pre-assert // Act Assert.That( () => { - Expect(collection).Not.To.Contain.Exactly(1).Equal.To(search); + Expect(collection).To.Contain.Exactly(1).Containing("cow"); }, - Throws - .Exception - .InstanceOf() - .With.Message - .Contains($"not to find exactly 1 occurrence of \"{search}\" but found 1") + Throws.Exception.InstanceOf() ); - // Assert } [Test] - public void Negated_WhenDoesNotContain_ShouldNotThrow() + public void WhenThrowing_ShouldIncludeCustomMessageIfSet() { // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( - new[] + var collection = new[] + { + "moo cows", + "bovine", + "beef-witted", + "cows moo" + }; + var expected = GetRandomString(10); + // Pre-assert + // Act + Assert.That( + () => { - search, - other1 - } + Expect(collection).To.Contain.Exactly(1).Containing("cow", expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) ); + // Assert + } + + [Test] + public void ShouldUseProvidedStringComparison() + { + // Arrange var collection = new[] { - other1, - other2 - }.Randomize(); - - // Pre-Assert - + "bovine", + "beef-witted", + "cows moo" + }; + // Pre-assert // Act Assert.That( () => { - Expect(collection).Not.To.Contain.Exactly(1).Equal.To(search); + Expect(collection).To.Contain.Exactly(1) + .Containing("Cow", StringComparison.OrdinalIgnoreCase); }, Throws.Nothing ); + // Assert + } + [Test] + public void ShouldUseProvidedStringComparisonAndCustomMessage() + { + // Arrange + var collection = new[] + { + "bovine", + "beef-witted", + "Cows moo" + }; + var expected = GetRandomString(10); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1).Containing( + "cow", + StringComparison.Ordinal, + expected + ); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + ); // Assert } + } + [TestFixture] + public class StringCollectionContainingItemEndingWith + { [Test] - public void Negated_Alt_WhenDoesContain_ShouldThrow() + public void WhenShouldPassWithOneOrdinalMatch_ShouldNotThrow() { // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( - new[] + var collection = new[] + { + "cow says moo", + "foo bar" + }; + // Pre-assert + // Act + Assert.That( + () => { - search, - other1 - } + Expect(collection).To.Contain.Exactly(1) + .Ending.With("moo"); + }, + Throws.Nothing ); + // Assert + } + + [Test] + public void ShouldUseProvidedComparer() + { + // Arrange var collection = new[] { - search, - other1, - other2 - }.Randomize(); + "cow says Moo", + "foo bar" + }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1) + .Ending.With("moo", StringComparison.OrdinalIgnoreCase); + }, + Throws.Nothing + ); + // Assert + } - // Pre-Assert + [Test] + public void ShouldUseProvidedCustomMessageForFailure() + { + // Arrange + var collection = new[] + { + "cow says Moo", + "foo bar" + }; + var expected = GetRandomString(10); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1) + .Ending.With("moo", expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + ); + // Assert + } + [Test] + public void ShouldUseProvidedStringComparerAndCustomMessageForFailure() + { + // Arrange + var collection = new[] + { + "cow says Moo", + "foo bar" + }; + var expected = GetRandomString(10); + // Pre-assert // Act Assert.That( () => { - Expect(collection).To.Not.Contain.Exactly(1).Equal.To(search); + Expect(collection).To.Contain.Exactly(1) + .Ending.With("moo", StringComparison.InvariantCulture, expected); }, - Throws - .Exception - .InstanceOf() - .With.Message - .Contains($"not to find exactly 1 occurrence of \"{search}\" but found 1") + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) ); + // Assert + } + } + [TestFixture] + public class StringCollectionContainingItemStartingWith + { + [Test] + public void WhenShouldPassWithOneOrdinalMatch_ShouldNotThrow() + { + // Arrange + var collection = new[] + { + "moo, said the cow", + "foo bar" + }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1) + .Starting.With("moo"); + }, + Throws.Nothing + ); // Assert } [Test] - public void Negated_Alt_WhenDoesNotContain_ShouldNotThrow() + public void ShouldUseProvidedComparer() { // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother( - new[] + var collection = new[] + { + "Moo said the cow", + "foo bar" + }; + // Pre-assert + // Act + Assert.That( + () => { - search, - other1 - } + Expect(collection).To.Contain.Exactly(1) + .Starting.With("moo", StringComparison.OrdinalIgnoreCase); + }, + Throws.Nothing ); + // Assert + } + + [Test] + public void ShouldUseProvidedCustomMessageForFailure() + { + // Arrange var collection = new[] { - other1, - other2 - }.Randomize(); + "Moo said the cow", + "foo bar" + }; + var expected = GetRandomString(10); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1) + .Starting.With("moo", expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + .And.Message.Contains(collection[0]) + ); + // Assert + } - // Pre-Assert + [Test] + public void ShouldUseProvidedStringComparerAndCustomMessageForFailure() + { + // Arrange + var collection = new[] + { + "Moo, said the cow", + "foo bar" + }; + var expected = GetRandomString(10); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1) + .Starting.With("moo", StringComparison.InvariantCulture, expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + ); + // Assert + } + } + [TestFixture] + public class StringCollectionContainingItemMatchedByRegex + { + [Test] + public void ShouldFindSingleMatch() + { + // Arrange + var data = new[] + { + "foo", + "bar", + "quux" + }; // Act Assert.That( () => { - Expect(collection).To.Not.Contain.Exactly(1).Equal.To(search); + Expect(data) + .To.Contain.Exactly(1) + .Matched.By("^foo$"); }, Throws.Nothing ); + // Assert + } + [Test] + public void ShouldFailOnNoMatch() + { + // Arrange + var data = new[] + { + "bar", + "quux" + }; + // Act + Assert.That( + () => + { + Expect(data) + .To.Contain.Exactly(1) + .Matched.By("^foo$"); + }, + Throws.Exception.InstanceOf() + ); // Assert } + + [Test] + public void ShouldFailOnCountMisMatch() + { + // Arrange + var data = new[] + { + "foobar", + "fooquux", + "barfoo" + }; + // Act + Assert.That( + () => + { + Expect(data) + .To.Contain.Exactly(1) + .Matched.By("foo"); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(data) + .To.Contain.Exactly(1) + .Matched.By("^foo"); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(data) + .To.Contain.Exactly(1) + .Matched.By("foo$"); + }, + Throws.Nothing + ); + // Assert + } + } + + + [Test] + public void WhenSeeking2AndDoesContain2_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var collection = new[] + { + search, + other1, + other2, + search + }.Randomize(); + + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(2).Equal.To(search); + }, + Throws.Nothing + ); + + // Assert + } + + [Test] + public void WhenSeeking1AndDoesContain2_ShouldThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var customMessage = GetRandomString(2); + var collection = new[] + { + search, + other1, + other2, + search + }.Randomize(); + var standardMessage = $"to find exactly 1 occurrence of \"{search}\" but found 2"; + + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1).Equal.To(search, customMessage); + }, + Throws.Exception + .InstanceOf() + .With.Message.Matches($"{customMessage}.*\\s*.*{standardMessage}") + ); + // Assert + } + + [Test] + public void WhenDoesNoContain_ShouldThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var collection = new[] + { + other1, + other2 + }.Randomize(); + + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Exactly(1).Equal.To(search); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains("find exactly 1 occurrence of") + ); + + // Assert + } + + [Test] + public void Negated_WhenDoesContain_ShouldThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var collection = new[] + { + search, + other1, + other2 + }.Randomize(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(collection).Not.To.Contain.Exactly(1).Equal.To(search); + }, + Throws + .Exception + .InstanceOf() + .With.Message + .Contains($"not to find exactly 1 occurrence of \"{search}\" but found 1") + ); + + // Assert + } + + [Test] + public void Negated_WhenDoesNotContain_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var collection = new[] + { + other1, + other2 + }.Randomize(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(collection).Not.To.Contain.Exactly(1).Equal.To(search); + }, + Throws.Nothing + ); + + // Assert + } + + [Test] + public void Negated_Alt_WhenDoesContain_ShouldThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var collection = new[] + { + search, + other1, + other2 + }.Randomize(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(collection).To.Not.Contain.Exactly(1).Equal.To(search); + }, + Throws + .Exception + .InstanceOf() + .With.Message + .Contains($"not to find exactly 1 occurrence of \"{search}\" but found 1") + ); + + // Assert + } + + [Test] + public void Negated_Alt_WhenDoesNotContain_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother( + new[] + { + search, + other1 + } + ); + var collection = new[] + { + other1, + other2 + }.Randomize(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(collection).To.Not.Contain.Exactly(1).Equal.To(search); + }, + Throws.Nothing + ); + + // Assert } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/FirstLetterComparer.cs b/src/NExpect.Tests/Collections/FirstLetterComparer.cs index a373485c..dd293386 100644 --- a/src/NExpect.Tests/Collections/FirstLetterComparer.cs +++ b/src/NExpect.Tests/Collections/FirstLetterComparer.cs @@ -1,46 +1,45 @@ using System.Collections.Generic; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +public class FirstLetterComparer : IEqualityComparer { - public class FirstLetterComparer : IEqualityComparer + public bool Equals(string x, string y) { - public bool Equals(string x, string y) + if (x == null && + y == null) { - if (x == null && - y == null) - { - return true; - } - - if (x == null || - y == null) - { - return false; - } - - if (x.Length == 0 && - y.Length == 0) - { - return true; - } + return true; + } - if (x.Length == 0 || - y.Length == 0) - { - return false; - } + if (x == null || + y == null) + { + return false; + } - return x[0] == y[0]; + if (x.Length == 0 && + y.Length == 0) + { + return true; } - public int GetHashCode(string obj) + if (x.Length == 0 || + y.Length == 0) { - var chr = string.IsNullOrEmpty(obj) - ? null - : obj[0] as char?; - return chr == null - ? 0 - : chr.GetHashCode(); + return false; } + + return x[0] == y[0]; + } + + public int GetHashCode(string obj) + { + var chr = string.IsNullOrEmpty(obj) + ? null + : obj[0] as char?; + return chr == null + ? 0 + : chr.GetHashCode(); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/Include.cs b/src/NExpect.Tests/Collections/Include.cs index f672f34a..62c088be 100644 --- a/src/NExpect.Tests/Collections/Include.cs +++ b/src/NExpect.Tests/Collections/Include.cs @@ -1,90 +1,89 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Include { - [TestFixture] - public class Include + [Test] + public void ShouldBeAbleToQuicklyTestIfCollectionIncludesValue() { - [Test] - public void ShouldBeAbleToQuicklyTestIfCollectionIncludesValue() + // Arrange + var collection = new[] { - // Arrange - var collection = new[] + 1, + 2, + 3 + }; + // Act + Assert.That( + () => { - 1, - 2, - 3 - }; - // Act - Assert.That( - () => - { - Expect(collection) - .To.Include(1); - }, - Throws.Nothing - ); + Expect(collection) + .To.Include(1); + }, + Throws.Nothing + ); - Assert.That( - () => - { - Expect(collection) - .Not.To.Include(1); - }, - Throws.Exception.InstanceOf() - ); + Assert.That( + () => + { + Expect(collection) + .Not.To.Include(1); + }, + Throws.Exception.InstanceOf() + ); - Assert.That( - () => - { - Expect(collection) - .To.Not.Include(1); - }, - Throws.Exception.InstanceOf() - ); + Assert.That( + () => + { + Expect(collection) + .To.Not.Include(1); + }, + Throws.Exception.InstanceOf() + ); - Assert.That( - () => - { - Expect(collection) - .To.Include(-1); - }, - Throws.Exception.InstanceOf() - ); + Assert.That( + () => + { + Expect(collection) + .To.Include(-1); + }, + Throws.Exception.InstanceOf() + ); - // Assert - } + // Assert + } - [Test] - public void ShouldEmitUsefulAndGrammaticallyCorrectErrors() - { - // Arrange - // Act - Assert.That( - () => - { - Expect(3, 4, 5) - .To.Include(1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - "Expected to find value 1 in collection" - ) - ); + [Test] + public void ShouldEmitUsefulAndGrammaticallyCorrectErrors() + { + // Arrange + // Act + Assert.That( + () => + { + Expect(3, 4, 5) + .To.Include(1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + "Expected to find value 1 in collection" + ) + ); - Assert.That( - () => - { - Expect(1, 2, 3) - .Not.To.Include(1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - "Expected not to find value 1 in collection" - ) - ); - // Assert - } + Assert.That( + () => + { + Expect(1, 2, 3) + .Not.To.Include(1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + "Expected not to find value 1 in collection" + ) + ); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/NameValueCollectionTesting.cs b/src/NExpect.Tests/Collections/NameValueCollectionTesting.cs index 2f01597e..85e989c0 100644 --- a/src/NExpect.Tests/Collections/NameValueCollectionTesting.cs +++ b/src/NExpect.Tests/Collections/NameValueCollectionTesting.cs @@ -1,4 +1,4 @@ -using System.Collections.Specialized; +using System.Collections.Specialized; using NUnit.Framework; using NExpect.Exceptions; using NExpect.Implementations; @@ -6,299 +6,298 @@ // ReSharper disable InconsistentNaming // ReSharper disable ExpressionIsAlwaysNull -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class NameValueCollectionTesting { - [TestFixture] - public class NameValueCollectionTesting + [Test] + public void ShouldBeAbleToAssertEmpty() { - [Test] - public void ShouldBeAbleToAssertEmpty() - { - // Arrange - var collection = new NameValueCollection(); - // Act - Assert.That( - () => - { - Expect(collection) - .To.Be.Empty(); - }, - Throws.Nothing - ); - collection["a"] = "b"; - Assert.That( - () => - { - Expect(collection) - .Not.To.Be.Empty(); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(collection) - .To.Be.Empty(); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + // Arrange + var collection = new NameValueCollection(); + // Act + Assert.That( + () => + { + Expect(collection) + .To.Be.Empty(); + }, + Throws.Nothing + ); + collection["a"] = "b"; + Assert.That( + () => + { + Expect(collection) + .Not.To.Be.Empty(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(collection) + .To.Be.Empty(); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } - [Test] - public void ShouldBeAbleToAssertEmptyKeys() - { - // Arrange - var collection = new NameValueCollection(); - // Act - Assert.That( - () => - { - Expect(collection.Keys) - .To.Be.Empty(); - }, - Throws.Nothing - ); - collection["a"] = "b"; - Assert.That( - () => - { - Expect(collection.Keys) - .Not.To.Be.Empty(); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(collection.Keys) - .To.Be.Empty(); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + [Test] + public void ShouldBeAbleToAssertEmptyKeys() + { + // Arrange + var collection = new NameValueCollection(); + // Act + Assert.That( + () => + { + Expect(collection.Keys) + .To.Be.Empty(); + }, + Throws.Nothing + ); + collection["a"] = "b"; + Assert.That( + () => + { + Expect(collection.Keys) + .Not.To.Be.Empty(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(collection.Keys) + .To.Be.Empty(); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + [TestFixture] + public class ContainingAssertions + { [TestFixture] - public class ContainingAssertions + public class Key { [TestFixture] - public class Key + public class OperatingOnPlainNameValueCollection { - [TestFixture] - public class OperatingOnPlainNameValueCollection + [Test] + public void WhenHasKey_ShouldNotThrow() { - [Test] - public void WhenHasKey_ShouldNotThrow() - { - // Arrange - var key = GetRandomString(2); - var src = new NameValueCollection(); - src[key] = GetRandomString(); - // Pre-Assert + // Arrange + var key = GetRandomString(2); + var src = new NameValueCollection(); + src[key] = GetRandomString(); + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key); - }, - Throws.Nothing - ); + // Act + Assert.That( + () => + { + Expect(src).To.Contain.Key(key); + }, + Throws.Nothing + ); - // Assert - } + // Assert + } - [Test] - public void Negated_WhenHasKey_ShouldThrow() + [Test] + public void Negated_WhenHasKey_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var src = new NameValueCollection() { - // Arrange - var key = GetRandomString(2); - var src = new NameValueCollection() - { - [key] = GetRandomString() - }; - // Pre-Assert + [key] = GetRandomString() + }; + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src).Not.To.Contain.Key(key); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"not to contain key\n{key.Stringify()}" - ) - ); + // Act + Assert.That( + () => + { + Expect(src).Not.To.Contain.Key(key); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + $"not to contain key\n{key.Stringify()}" + ) + ); - // Assert - } + // Assert + } - [Test] - public void Negated_WhenDoesNotHaveKey_ShouldNotThrow() - { - // Arrange - var src = new NameValueCollection(); - var key = GetRandomString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(src).Not.To.Contain.Key(key); - }, - Throws.Nothing - ); + [Test] + public void Negated_WhenDoesNotHaveKey_ShouldNotThrow() + { + // Arrange + var src = new NameValueCollection(); + var key = GetRandomString(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(src).Not.To.Contain.Key(key); + }, + Throws.Nothing + ); - Assert.That( - () => - { - Expect(src).To.Not.Contain.Key(key); - }, - Throws.Nothing - ); - // Assert - } + Assert.That( + () => + { + Expect(src).To.Not.Contain.Key(key); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void Negated_Alt_WhenHasKey_ShouldThrow() + [Test] + public void Negated_Alt_WhenHasKey_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var src = new NameValueCollection() { - // Arrange - var key = GetRandomString(2); - var src = new NameValueCollection() - { - [key] = GetRandomString() - }; - // Pre-Assert + [key] = GetRandomString() + }; + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src).To.Not.Contain.Key(key); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"not to contain key\n{key.Stringify()}" - ) - ); + // Act + Assert.That( + () => + { + Expect(src).To.Not.Contain.Key(key); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + $"not to contain key\n{key.Stringify()}" + ) + ); - // Assert - } + // Assert + } + [TestFixture] + public class WithValue + { [TestFixture] - public class WithValue + public class OperatingOnPrimitiveTypes { - [TestFixture] - public class OperatingOnPrimitiveTypes + [Test] + public void WhenHasKey_AndValueMatches_ShouldNotThrow() { - [Test] - public void WhenHasKey_AndValueMatches_ShouldNotThrow() + // Arrange + var key = GetRandomString(2); + var value = GetRandomString(2); + var src = new NameValueCollection() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomString(2); - var src = new NameValueCollection() - { - [key] = value - }; - // Pre-Assert + [key] = value + }; + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(key).With.Value(value); - }, - Throws.Nothing - ); + // Act + Assert.That( + () => + { + Expect(src).To.Contain.Key(key).With.Value(value); + }, + Throws.Nothing + ); - // Assert - } + // Assert + } - [Test] - public void WhenDoesNotHaveKey_ShouldThrowForThatReason() + [Test] + public void WhenDoesNotHaveKey_ShouldThrowForThatReason() + { + // Arrange + var key = GetRandomString(2); + var value = GetRandomString(2); + var src = new NameValueCollection() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomString(2); - var src = new NameValueCollection() - { - [key] = value - }; - var testingValue = GetAnother(key); - // Pre-Assert + [key] = value + }; + var testingValue = GetAnother(key); + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src).To.Contain.Key(testingValue).With.Value(value); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"to contain key\n\"{testingValue}\"") - ); + // Act + Assert.That( + () => + { + Expect(src).To.Contain.Key(testingValue).With.Value(value); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"to contain key\n\"{testingValue}\"") + ); - // Assert - } + // Assert + } - [Test] - public void WhenHasKey_AndValueDoesNotMatch_ShouldThrow() + [Test] + public void WhenHasKey_AndValueDoesNotMatch_ShouldThrow() + { + // Arrange + var key = GetRandomString(2); + var value = GetRandomString(2); + var src = new NameValueCollection() { - // Arrange - var key = GetRandomString(2); - var value = GetRandomString(2); - var src = new NameValueCollection() - { - [key] = value - }; - var testingValue = GetAnother(value); - // Pre-Assert + [key] = value + }; + var testingValue = GetAnother(value); + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(src) - .To.Contain.Key(key) - .With.Value(testingValue); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"Expected\n\"{testingValue}\"\nbut got\n\"{value}\"") - ); + // Act + Assert.That( + () => + { + Expect(src) + .To.Contain.Key(key) + .With.Value(testingValue); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"Expected\n\"{testingValue}\"\nbut got\n\"{value}\"") + ); - // Assert - } + // Assert } } } } } + } - [TestFixture] - public class Emptiness + [TestFixture] + public class Emptiness + { + [Test] + public void ShouldBeAbleToAssertEmptiness() { - [Test] - public void ShouldBeAbleToAssertEmptiness() + // Arrange + var empty = new NameValueCollection(); + var notEmpty = new NameValueCollection { - // Arrange - var empty = new NameValueCollection(); - var notEmpty = new NameValueCollection + [GetRandomString(1)] = GetRandomString() + }; + // Act + Assert.That( + () => { - [GetRandomString(1)] = GetRandomString() - }; - // Act - Assert.That( - () => - { - Expect(empty) - .To.Be.Empty(); - }, - Throws.Nothing - ); - // Assert - } + Expect(empty) + .To.Be.Empty(); + }, + Throws.Nothing + ); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/NeverEqualEqualityComparer.cs b/src/NExpect.Tests/Collections/NeverEqualEqualityComparer.cs index 83bd3bb6..7a27bcf8 100644 --- a/src/NExpect.Tests/Collections/NeverEqualEqualityComparer.cs +++ b/src/NExpect.Tests/Collections/NeverEqualEqualityComparer.cs @@ -1,18 +1,17 @@ -using System; +using System; using System.Collections.Generic; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +public class NeverEqualEqualityComparer : IEqualityComparer { - public class NeverEqualEqualityComparer : IEqualityComparer + public bool Equals(DateTime x, DateTime y) { - public bool Equals(DateTime x, DateTime y) - { - return false; - } + return false; + } - public int GetHashCode(DateTime obj) - { - return 0; - } + public int GetHashCode(DateTime obj) + { + return 0; } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/Only.cs b/src/NExpect.Tests/Collections/Only.cs index bdd1da77..922abf95 100644 --- a/src/NExpect.Tests/Collections/Only.cs +++ b/src/NExpect.Tests/Collections/Only.cs @@ -1,449 +1,448 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.RandomGenerators; // ReSharper disable InconsistentNaming -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Only_N { - [TestFixture] - public class Only_N + [Test] + public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding1_ShouldNotThrow() { - [Test] - public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding1_ShouldNotThrow() + // Arrange + var search = GetRandomString(3); + var collection = new[] { - // Arrange - var search = GetRandomString(3); - var collection = new[] + search + }; + + // Pre-Assert + // Act + Assert.That( + () => { - search - }; - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Only(1).Equal.To(search); - }, - Throws.Nothing - ); - - // Assert - } + Expect(collection).To.Contain.Only(1).Equal.To(search); + }, + Throws.Nothing + ); - [Test] - public void OperatingOnCollectionOfStrings_WhenExpecting2Items_AndFinding2_ShouldNotThrow() + // Assert + } + + [Test] + public void OperatingOnCollectionOfStrings_WhenExpecting2Items_AndFinding2_ShouldNotThrow() + { + // Arrange + var search = GetRandomString(3); + var collection = new[] { - // Arrange - var search = GetRandomString(3); - var collection = new[] + search, + search + }; + + // Pre-Assert + // Act + Assert.That( + () => { - search, - search - }; - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Only(2).Equal.To(search); - }, - Throws.Nothing - ); - - // Assert - } + Expect(collection).To.Contain.Only(2).Equal.To(search); + }, + Throws.Nothing + ); + + // Assert + } - [Test] - public void OperatingOnCollectionOfStrings_WhenContainsCorrectNumber_AndNotEqual_ShouldThrow() + [Test] + public void OperatingOnCollectionOfStrings_WhenContainsCorrectNumber_AndNotEqual_ShouldThrow() + { + // Arrange + var search = GetRandomString(3); + var collection = new[] { - // Arrange - var search = GetRandomString(3); - var collection = new[] + GetAnother(search) + }; + + // Pre-Assert + // Act + Assert.That( + () => { - GetAnother(search) - }; - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Only(1).Equal.To(search); - }, - Throws.Exception.TypeOf() - .With.Message.Contains($"Expected to find only 1 occurrence of \"{search}\" but found 0") - ); - - // Assert - } + Expect(collection).To.Contain.Only(1).Equal.To(search); + }, + Throws.Exception.TypeOf() + .With.Message.Contains($"Expected to find only 1 occurrence of \"{search}\" but found 0") + ); - [Test] - public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding0_ShouldThrow() - { - // Arrange - var collection = new string[] { }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Only(1).Items(); - }, - Throws.Exception.TypeOf() - .With.Message.Contains( - "Expected to find only 1 occurrence of any string in collection but found a total of 0" - ) - ); - - // Assert - } + // Assert + } - [Test] - public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding0_ShouldThrowWithCustomMessage() - { - // Arrange - var collection = new string[] { }; - var expected = GetRandomString(10); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Only(1).Items(expected); - }, - Throws.Exception.TypeOf() - .With.Message.Contains(expected) - ); - - Assert.That( - () => - { - Expect(collection).To.Contain.Only(1).Items(() => expected); - }, - Throws.Exception.TypeOf() - .With.Message.Contains(expected) - ); - - // Assert - } + [Test] + public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding0_ShouldThrow() + { + // Arrange + var collection = new string[] { }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Only(1).Items(); + }, + Throws.Exception.TypeOf() + .With.Message.Contains( + "Expected to find only 1 occurrence of any string in collection but found a total of 0" + ) + ); + + // Assert + } - [Test] - public void ShouldBreakOnCountMisMatch() - { - // Arrange - var items = new[] { 1, 2, 3 }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.Only(1) - .Matched.By(i => i == 1); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(items) - .Not.To.Contain.Only(1) - .Matched.By(i => i == 1); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding0_ShouldThrowWithCustomMessage() + { + // Arrange + var collection = new string[] { }; + var expected = GetRandomString(10); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(collection).To.Contain.Only(1).Items(expected); + }, + Throws.Exception.TypeOf() + .With.Message.Contains(expected) + ); + + Assert.That( + () => + { + Expect(collection).To.Contain.Only(1).Items(() => expected); + }, + Throws.Exception.TypeOf() + .With.Message.Contains(expected) + ); - [Test] - public void AnyShouldPassForOneMatch() + // Assert + } + + [Test] + public void ShouldBreakOnCountMisMatch() + { + // Arrange + var items = new[] { 1, 2, 3 }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.Only(1) + .Matched.By(i => i == 1); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(items) + .Not.To.Contain.Only(1) + .Matched.By(i => i == 1); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void AnyShouldPassForOneMatch() + { + // Arrange + var items = new[] { 1, 2, 3 }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.Any + .Matched.By(i => i == 1); + }, + Throws.Nothing + ); + // Assert + } + + [TestFixture] + public class QuickSubstringTestingForStringCollections + { + [TestFixture] + public class PositiveTests { - // Arrange - var items = new[] { 1, 2, 3 }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.Any - .Matched.By(i => i == 1); - }, - Throws.Nothing - ); - // Assert + [Test] + public void ShouldFindSingleOnlyHit() + { + // Arrange + var items = new[] { "foo" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.Only(1) + .Containing("oo"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldFindMultipleOnlyHits() + { + // Arrange + var items = new[] { "foo", "book" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.Only(2) + .Containing("oo"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldFindSingleExactlyHit() + { + // Arrange + var items = new[] { "bar", "foo" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.Exactly(1) + .Containing("oo"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldFindMultipleExactlyHit() + { + // Arrange + var items = new[] { "book", "bar", "foo" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.Exactly(2) + .Containing("oo"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldFindMultipleAtLeastHits() + { + // Arrange + var items = new[] { "book", "bar", "foo" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.At.Least(2) + .Containing("oo"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldFindMultipleAtMostHits() + { + // Arrange + var items = new[] { "book", "bar", "foo" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.At.Most(2) + .Containing("oo"); + }, + Throws.Nothing + ); + // Assert + } } [TestFixture] - public class QuickSubstringTestingForStringCollections + public class NegativeTests { - [TestFixture] - public class PositiveTests + [Test] + public void ShouldFailOnInvalidOnlyHit() { - [Test] - public void ShouldFindSingleOnlyHit() - { - // Arrange - var items = new[] { "foo" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.Only(1) - .Containing("oo"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldFindMultipleOnlyHits() - { - // Arrange - var items = new[] { "foo", "book" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.Only(2) - .Containing("oo"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldFindSingleExactlyHit() - { - // Arrange - var items = new[] { "bar", "foo" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.Exactly(1) - .Containing("oo"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldFindMultipleExactlyHit() - { - // Arrange - var items = new[] { "book", "bar", "foo" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.Exactly(2) - .Containing("oo"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldFindMultipleAtLeastHits() - { - // Arrange - var items = new[] { "book", "bar", "foo" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.At.Least(2) - .Containing("oo"); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldFindMultipleAtMostHits() - { - // Arrange - var items = new[] { "book", "bar", "foo" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.At.Most(2) - .Containing("oo"); - }, - Throws.Nothing - ); - // Assert - } + // Arrange + // Act + Assert.That( + () => + { + Expect(new[] { "foo" }) + .To.Contain.Only(1) + .Containing("aa"); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(new[] { "foo", "aardvark" }) + .To.Contain.Only(1) + .Containing("aa"); + }, + Throws.Exception.InstanceOf() + ); + // Assert } - [TestFixture] - public class NegativeTests + [Test] + public void ShouldFailWhenMissingSinglExactHit() { - [Test] - public void ShouldFailOnInvalidOnlyHit() - { - // Arrange - // Act - Assert.That( - () => - { - Expect(new[] { "foo" }) - .To.Contain.Only(1) - .Containing("aa"); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(new[] { "foo", "aardvark" }) - .To.Contain.Only(1) - .Containing("aa"); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } - - [Test] - public void ShouldFailWhenMissingSinglExactHit() - { - // Arrange - // Act - Assert.That( - () => - { - Expect(new[] { "bar", "foo", "book" }) - .To.Contain.Exactly(1) - .Containing("oo"); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(new[] { "bar", "faa", }) - .To.Contain.Exactly(1) - .Containing("oo"); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } - - [Test] - public void ShouldFailOnMissedAtLeastHits() - { - // Arrange - var items = new[] { "book", "bar" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.At.Least(2) - .Containing("oo"); - }, - Throws.Exception.InstanceOf() - - ); - // Assert - } - - [Test] - public void ShouldFailOnTooManyAtMostHits() - { - // Arrange - var items = new[] { "book", "bar", "foo" }; - // Act - Assert.That( - () => - { - Expect(items) - .To.Contain.At.Most(1) - .Containing("oo"); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + // Arrange + // Act + Assert.That( + () => + { + Expect(new[] { "bar", "foo", "book" }) + .To.Contain.Exactly(1) + .Containing("oo"); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(new[] { "bar", "faa", }) + .To.Contain.Exactly(1) + .Containing("oo"); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void ShouldFailOnMissedAtLeastHits() + { + // Arrange + var items = new[] { "book", "bar" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.At.Least(2) + .Containing("oo"); + }, + Throws.Exception.InstanceOf() + + ); + // Assert + } + + [Test] + public void ShouldFailOnTooManyAtMostHits() + { + // Arrange + var items = new[] { "book", "bar", "foo" }; + // Act + Assert.That( + () => + { + Expect(items) + .To.Contain.At.Most(1) + .Containing("oo"); + }, + Throws.Exception.InstanceOf() + ); + // Assert } } + } - [Test] - public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding3_ShouldThrow() + [Test] + public void OperatingOnCollectionOfStrings_WhenExpecting1Item_AndFinding3_ShouldThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother(new[] { search, other1 }); + var other3 = GetAnother(new[] { search, other1, other2 }); + var collection = new[] { - // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother(new[] { search, other1 }); - var other3 = GetAnother(new[] { search, other1, other2 }); - var collection = new[] + other1, + other2, + other3 + }.Randomize(); + + // Pre-Assert + // Act + Assert.That( + () => { - other1, - other2, - other3 - }.Randomize(); - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Only(1).Equal.To(search); - }, - Throws.Exception.TypeOf() - .With.Message.Contains( - $"Expected to find only 1 occurrence of {search} in collection but found a total of 3 items" - ) - ); - - // Assert - } + Expect(collection).To.Contain.Only(1).Equal.To(search); + }, + Throws.Exception.TypeOf() + .With.Message.Contains( + $"Expected to find only 1 occurrence of {search} in collection but found a total of 3 items" + ) + ); + + // Assert + } - [Test] - public void OperatingOnCollectionOfStrings_WhenExpecting2Items_AndFinding3_ShouldThrow() + [Test] + public void OperatingOnCollectionOfStrings_WhenExpecting2Items_AndFinding3_ShouldThrow() + { + // Arrange + var search = GetRandomString(3); + var other1 = GetAnother(search); + var other2 = GetAnother(new[] { search, other1 }); + var other3 = GetAnother(new[] { search, other1, other2 }); + var collection = new[] { - // Arrange - var search = GetRandomString(3); - var other1 = GetAnother(search); - var other2 = GetAnother(new[] { search, other1 }); - var other3 = GetAnother(new[] { search, other1, other2 }); - var collection = new[] + other1, + other2, + other3 + }.Randomize(); + + // Pre-Assert + // Act + Assert.That( + () => { - other1, - other2, - other3 - }.Randomize(); - - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(collection).To.Contain.Only(2).Equal.To(search); - }, - Throws.Exception.TypeOf() - .With.Message.Contains( - $"Expected to find only 2 occurrences of {search} in collection but found a total of 3 items" - ) - ); - - // Assert - } + Expect(collection).To.Contain.Only(2).Equal.To(search); + }, + Throws.Exception.TypeOf() + .With.Message.Contains( + $"Expected to find only 2 occurrences of {search} in collection but found a total of 3 items" + ) + ); + + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/Sequences.cs b/src/NExpect.Tests/Collections/Sequences.cs index bacb4727..c0542ca2 100644 --- a/src/NExpect.Tests/Collections/Sequences.cs +++ b/src/NExpect.Tests/Collections/Sequences.cs @@ -1,388 +1,387 @@ -using System.Linq; +using System.Linq; using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class Sequences { [TestFixture] - public class Sequences + public class AscendingOrder { - [TestFixture] - public class AscendingOrder + [Test] + public void ShouldThrowForEmptyCollection() { - [Test] - 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 - } - - [Test] - public void ShouldAlwaysThrowForSingleItem() - { - // Arrange - var collection = new int[] { 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 - } + // 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 + } - [Test] - public void ShouldHandleOrderedCollection() - { - // Arrange - var collection = new[] { 1, 2 }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); + [Test] + public void ShouldAlwaysThrowForSingleItem() + { + // Arrange + var collection = new int[] { 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 + } - var customMessage = GetRandomWords(2); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Ascending(() => customMessage); - }, Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage)); - // Assert - } + [Test] + public void ShouldHandleOrderedCollection() + { + // Arrange + var collection = new[] { 1, 2 }; + // Act + Assert.That(() => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, Throws.Nothing); - [Test] - public void ShouldHandleHomogenousCollection() - { - // Arrange - var collection = new[] { 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 - } + var customMessage = GetRandomWords(2); + Assert.That(() => + { + Expect(collection) + .Not.To.Be.Ordered.Ascending(() => customMessage); + }, Throws.Exception.InstanceOf() + .With.Message.Contains(customMessage)); + // Assert + } - [Test] - public void ParamsOverload() - { - // Arrange - - // Act - Assert.That(() => - { - Expect(1, 2, 3, 4) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); - // 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(() => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, Throws.Nothing); + // Assert + } - [Test] - public void ShouldOrderCollectionOfStrings() - { - // Arrange - var items = new[] - { - "'Murican", - "Queen's English", - "Boerie", - "English" - }; + [Test] + public void ParamsOverload() + { + // Arrange + + // Act + Assert.That(() => + { + Expect(1, 2, 3, 4) + .To.Be.Ordered.Ascending(); + }, Throws.Nothing); + // Assert + } - // Act - Assert.That(() => - { - Expect(items) - .To.Be.Ordered.Ascending(); - }, Throws.Exception.InstanceOf()); - // Assert - } + [Test] + public void ShouldOrderCollectionOfStrings() + { + // Arrange + var items = new[] + { + "'Murican", + "Queen's English", + "Boerie", + "English" + }; - [Test] - public void ShouldHandleIOrderedEnumerableToo() - { - // Arrange - var numbers = new[] { 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 - } + // Act + Assert.That(() => + { + Expect(items) + .To.Be.Ordered.Ascending(); + }, Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void ShouldHandleOrderedCollectionOfStrings() - { - // Arrange - var collection = new[] { "aardvark", "dingo", "hippo", "zebra" }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Ascending(); - }, Throws.Nothing); + [Test] + public void ShouldHandleIOrderedEnumerableToo() + { + // Arrange + var numbers = new[] { 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 + } - var customMessage = GetRandomWords(2); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Ascending(() => customMessage); - }, Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage)); - // Assert - } + [Test] + public void ShouldHandleOrderedCollectionOfStrings() + { + // Arrange + var collection = new[] { "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 + } - [Test] - public void ShouldHandleAllEqualCollection() - { - // Arrange - var collection = new[] { 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()); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Ascending(); - }, Throws.Nothing); - // Assert - } + [Test] + public void ShouldHandleAllEqualCollection() + { + // Arrange + var collection = new[] { 0, 0 }; + // Act + Assert.That(() => + { + Expect(collection) + .To.Be.Ordered.Ascending(); + }, Throws.Nothing); + // Assert } - [TestFixture] - public class DescendingOrder + [Test] + public void ShouldHandleDescendingCollection() { - [Test] - 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 - } + // Arrange + var collection = new[] { 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 + } + } - [Test] - 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(() => - { - Expect(collection) - .Not.To.Be.Ordered.Descending(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("at least two items")); - // Assert - } + [TestFixture] + public class DescendingOrder + { + [Test] + 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 + } - [Test] - public void ShouldHandleOrderedCollection() - { - // Arrange - var collection = new[] { 2, 1 }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Descending(); - }, Throws.Nothing); + [Test] + 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(() => + { + Expect(collection) + .Not.To.Be.Ordered.Descending(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("at least two items")); + // Assert + } - var customMessage = GetRandomWords(2); - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Ordered.Descending(() => customMessage); - }, Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage)); - // Assert - } + [Test] + public void ShouldHandleOrderedCollection() + { + // Arrange + var collection = new[] { 2, 1 }; + // Act + Assert.That(() => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, Throws.Nothing); - [Test] - public void ShouldHandleAllEqualCollection() - { - // Arrange - var collection = new[] { 0, 0 }; - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Ordered.Descending(); - }, Throws.Nothing); - // Assert - } + var customMessage = GetRandomWords(2); + Assert.That(() => + { + Expect(collection) + .Not.To.Be.Ordered.Descending(() => customMessage); + }, Throws.Exception.InstanceOf() + .With.Message.Contains(customMessage)); + // Assert + } - [Test] - public void ShouldHandleDescendingCollection() - { - // Arrange - var collection = new[] { 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 - } + [Test] + public void ShouldHandleAllEqualCollection() + { + // Arrange + var collection = new[] { 0, 0 }; + // Act + Assert.That(() => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, Throws.Nothing); + // Assert } - [TestFixture] - public class GivenOrderingLambda + [Test] + public void ShouldHandleDescendingCollection() { - public class HasId + // Arrange + var collection = new[] { 1, 2, 3 }; + // Act + Assert.That(() => + { + Expect(collection) + .To.Be.Ordered.Descending(); + }, Throws.Exception.InstanceOf()); + Assert.That(() => { - public int Id { get; set; } - } + Expect(collection) + .Not.To.Be.Ordered.Descending(); + }, Throws.Nothing); + // Assert + } + } - [Test] - public void ShouldEnforceOrderingOnCollectionOfTwo() - { - // Arrange - var collection = new[] - { - new { Id = 1, Name = "Zebra" }, - new { Id = 2, Name = "Aardvark" } - }; + [TestFixture] + public class GivenOrderingLambda + { + public class HasId + { + public int Id { get; set; } + } - // 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()); - } + [Test] + public void ShouldEnforceOrderingOnCollectionOfTwo() + { + // Arrange + var collection = new[] + { + 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()); + } - [Test] - public void ShouldEnforceOrderingOnCollectionOfThree() - { - // Arrange - var ordered = new[] { 1, 2, 3 }; - var unordered = new[] { 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 - } + [Test] + public void ShouldEnforceOrderingOnCollectionOfThree() + { + // Arrange + var ordered = new[] { 1, 2, 3 }; + var unordered = new[] { 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 + } - [Test] - public void ShouldNegateCorrectly() - { - // Arrange - - var ordered = new[] { 1, 2, 3 }; - var unordered = new[] { 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 - } + [Test] + public void ShouldNegateCorrectly() + { + // Arrange + + var ordered = new[] { 1, 2, 3 }; + var unordered = new[] { 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.Tests/Collections/SetTesting.cs b/src/NExpect.Tests/Collections/SetTesting.cs index 8c876112..cd20e771 100644 --- a/src/NExpect.Tests/Collections/SetTesting.cs +++ b/src/NExpect.Tests/Collections/SetTesting.cs @@ -2,41 +2,40 @@ using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +[TestFixture] +public class SetTesting { - [TestFixture] - public class SetTesting + [Test] + public void ShouldBeAbleToAssertHashSetIsEmpty() { - [Test] - public void ShouldBeAbleToAssertHashSetIsEmpty() + // Arrange + var empty = new HashSet() as ISet; + var notEmpty = new HashSet(new[] { 1 }) as ISet; + // Act + Assert.That(() => + { + Expect(empty) + .To.Be.Empty(); + }, Throws.Nothing); + + Assert.That(() => + { + Expect(empty) + .Not.To.Be.Empty(); + }, Throws.Exception.InstanceOf()); + + Assert.That(() => + { + Expect(notEmpty) + .To.Be.Empty(); + }, Throws.Exception.InstanceOf()); + Assert.That(() => { - // Arrange - var empty = new HashSet() as ISet; - var notEmpty = new HashSet(new[] { 1 }) as ISet; - // Act - Assert.That(() => - { - Expect(empty) - .To.Be.Empty(); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(empty) - .Not.To.Be.Empty(); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(notEmpty) - .To.Be.Empty(); - }, Throws.Exception.InstanceOf()); - Assert.That(() => - { - Expect(notEmpty) - .Not.To.Be.Empty(); - }, Throws.Nothing); - // Assert - } + Expect(notEmpty) + .Not.To.Be.Empty(); + }, Throws.Nothing); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Collections/TestAgainstReadOnlyCollections.cs b/src/NExpect.Tests/Collections/TestAgainstReadOnlyCollections.cs index 83f384aa..bd20fdae 100644 --- a/src/NExpect.Tests/Collections/TestAgainstReadOnlyCollections.cs +++ b/src/NExpect.Tests/Collections/TestAgainstReadOnlyCollections.cs @@ -2,28 +2,27 @@ using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests.Collections +namespace NExpect.Tests.Collections; + +public class TestAgainstReadOnlyCollections { - public class TestAgainstReadOnlyCollections + [Test] + public void ShouldBeAbleToAssertEmpty() { - [Test] - public void ShouldBeAbleToAssertEmpty() + // Arrange + var collection = new ReadOnlyCollection(new string[0]); + // Act + Assert.That(() => + { + Expect(collection) + .To.Be.Empty(); + }, Throws.Nothing); + + Assert.That(() => { - // Arrange - var collection = new ReadOnlyCollection(new string[0]); - // Act - Assert.That(() => - { - Expect(collection) - .To.Be.Empty(); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(collection) - .Not.To.Be.Empty(); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(collection) + .Not.To.Be.Empty(); + }, Throws.Exception.InstanceOf()); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ComposingExpectations.cs b/src/NExpect.Tests/ComposingExpectations.cs index 06f65b0c..98ee5f42 100644 --- a/src/NExpect.Tests/ComposingExpectations.cs +++ b/src/NExpect.Tests/ComposingExpectations.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using NExpect.Exceptions; using NExpect.Interfaces; using NExpect.Implementations; @@ -6,345 +6,344 @@ using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class ComposingExpectations { [TestFixture] - public class ComposingExpectations + public class SingleItems { - [TestFixture] - public class SingleItems + [Test] + public void PositiveComposition_WhenPasses_ShouldNotThrow() { - [Test] - public void PositiveComposition_WhenPasses_ShouldNotThrow() + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person).To.Be.A.Benny(); - }, Throws.Nothing); - // Assert - } - - [Test] - public void PositiveComposition_WhenFails_ShouldThrow() + Name = "Benny", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var person = new Person() - { - Name = "Sally", - Gender = Genders.Female - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person).To.Be.A.Benny(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected {person.Stringify()} to be a Benny") - ); - // Assert - } + Expect(person).To.Be.A.Benny(); + }, Throws.Nothing); + // Assert + } - [Test] - public void NegativeComposition_WhenFails_ShouldThrow() + [Test] + public void PositiveComposition_WhenFails_ShouldThrow() + { + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => + Name = "Sally", + Gender = Genders.Female + }; + // Pre-Assert + // Act + Assert.That(() => { - Expect(person).Not.To.Be.A.Benny(); + Expect(person).To.Be.A.Benny(); }, Throws.Exception.InstanceOf() - .With.Message.Contains( - $"Expected {person.Stringify()} not to be a Benny" - )); - // Assert - } + .With.Message.Contains($"Expected {person.Stringify()} to be a Benny") + ); + // Assert + } - [Test] - public void NegativeComposition_WhenPasseds_ShouldNotThrow() + [Test] + public void NegativeComposition_WhenFails_ShouldThrow() + { + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Andrew", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person).Not.To.Be.A.Benny(); - }, Throws.Nothing); - // Assert - } + Name = "Benny", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person).Not.To.Be.A.Benny(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains( + $"Expected {person.Stringify()} not to be a Benny" + )); + // Assert } - [TestFixture] - public class Collections + [Test] + public void NegativeComposition_WhenPasseds_ShouldNotThrow() { - [Test] - public void PositiveComposition_WhenPasses_ShouldNotThrow() + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person.InArray()).To.Be.Bennies(); - }, Throws.Nothing); - // Assert - } + Name = "Andrew", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person).Not.To.Be.A.Benny(); + }, Throws.Nothing); + // Assert + } + } - [Test] - public void PositiveComposition_Multiple_WhenPasses_ShouldNotThrow() + [TestFixture] + public class Collections + { + [Test] + public void PositiveComposition_WhenPasses_ShouldNotThrow() + { + // Arrange + var person = new Person() { - // Arrange - var person1 = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - var person2 = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(new[] { person1, person2 }).To.Be.Bennies(); - }, Throws.Nothing); - // Assert - } + Name = "Benny", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person.InArray()).To.Be.Bennies(); + }, Throws.Nothing); + // Assert + } - [Test] - public void PositiveComposition_WhenFails_ShouldThrow() + [Test] + public void PositiveComposition_Multiple_WhenPasses_ShouldNotThrow() + { + // Arrange + var person1 = new Person() { - // Arrange - var person = new Person() - { - Name = "Sally", - Gender = Genders.Female - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person.InArray()).To.Be.Bennies(); - }, Throws.Exception.InstanceOf() - .With.Message.Not.Contains("not")); - // Assert - } + Name = "Benny", + Gender = Genders.Male + }; + var person2 = new Person() + { + Name = "Benny", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(new[] { person1, person2 }).To.Be.Bennies(); + }, Throws.Nothing); + // Assert + } - [Test] - public void NegativeComposition_WhenFails_ShouldThrow() + [Test] + public void PositiveComposition_WhenFails_ShouldThrow() + { + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person.InArray()).Not.To.Be.Bennies(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains( - "not to all be Bennies" - )); - // Assert - } + Name = "Sally", + Gender = Genders.Female + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person.InArray()).To.Be.Bennies(); + }, Throws.Exception.InstanceOf() + .With.Message.Not.Contains("not")); + // Assert + } - [Test] - public void NegativeComposition_WhenFails_WithNoCustomMessage_ShouldThrow() + [Test] + public void NegativeComposition_WhenFails_ShouldThrow() + { + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person.InArray()).Not.To.Be.Bennies(); - }, Throws.Exception.InstanceOf()); - // Assert - } + Name = "Benny", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person.InArray()).Not.To.Be.Bennies(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains( + "not to all be Bennies" + )); + // Assert + } - [Test] - public void NegativeComposition_WhenPasses_ShouldNotThrow() + [Test] + public void NegativeComposition_WhenFails_WithNoCustomMessage_ShouldThrow() + { + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Andrew", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person.InArray()).Not.To.Be.Bennies2(); - }, Throws.Nothing); - // Assert - } + Name = "Benny", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person.InArray()).Not.To.Be.Bennies(); + }, Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void PositiveComposition_WhenFails_OnCompositionWithoutMessageGenerator_ShouldThrow() + [Test] + public void NegativeComposition_WhenPasses_ShouldNotThrow() + { + // Arrange + var person = new Person() { - // Arrange - var person = new Person() - { - Name = "Andrew", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person.InArray()).To.Be.Bennies2(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("Bennies2")); - // Assert - } + Name = "Andrew", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person.InArray()).Not.To.Be.Bennies2(); + }, Throws.Nothing); + // Assert } - [TestFixture] - public class More + [Test] + public void PositiveComposition_WhenFails_OnCompositionWithoutMessageGenerator_ShouldThrow() { - [Test] - public void ObjectCompositionsShouldAllowQuickMore() + // Arrange + var person = new Person() + { + Name = "Andrew", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var src = GetRandomString(); - var have = Expect(src).To.Have; - // Pre-Assert + Expect(person.InArray()).To.Be.Bennies2(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("Bennies2")); + // Assert + } + } - // Act - var result = have.Compose(actual => Expect(actual).Not.To.Be.Null()); + [TestFixture] + public class More + { + [Test] + public void ObjectCompositionsShouldAllowQuickMore() + { + // Arrange + var src = GetRandomString(); + var have = Expect(src).To.Have; + // Pre-Assert - // Assert - Assert.That(result, Is.InstanceOf>()); - } + // Act + var result = have.Compose(actual => Expect(actual).Not.To.Be.Null()); - [Test] - public void CollectionCompositionsShouldAllowQuickMore() - { - // Arrange - var src = GetRandomCollection(); - var have = Expect(src).To.Have; - // Pre-Assert + // Assert + Assert.That(result, Is.InstanceOf>()); + } + + [Test] + public void CollectionCompositionsShouldAllowQuickMore() + { + // Arrange + var src = GetRandomCollection(); + var have = Expect(src).To.Have; + // Pre-Assert - // Act - var result = have.Compose(actual => Expect(actual).Not.To.Be.Null()); + // Act + var result = have.Compose(actual => Expect(actual).Not.To.Be.Null()); - // Assert - Assert.That(result, Is.InstanceOf>>()); - } + // Assert + Assert.That(result, Is.InstanceOf>>()); } + } - [TestFixture] - public class Negation + [TestFixture] + public class Negation + { + [Test] + public void ShouldPassWhenNegatingAFailingComposition() { - [Test] - public void ShouldPassWhenNegatingAFailingComposition() + // Arrange + using var _ = new AutoResetter( + () => Assertions.RegisterAssertionsFactory( + s => new AssertionException(s) + ), + Assertions.UseDefaultAssertionsFactory + ); + var person = new Person() { - // Arrange - using var _ = new AutoResetter( - () => Assertions.RegisterAssertionsFactory( - s => new AssertionException(s) - ), - Assertions.UseDefaultAssertionsFactory - ); - var person = new Person() - { - Gender = Genders.Unknown, - Name = "Blergschootz" - }; - // Act - Assert.That(() => - { - Expect(person) - .Not.To.Be.A.Benny(); - }, Throws.Nothing); - // Assert - } + Gender = Genders.Unknown, + Name = "Blergschootz" + }; + // Act + Assert.That(() => + { + Expect(person) + .Not.To.Be.A.Benny(); + }, Throws.Nothing); + // Assert } } +} - public enum Genders - { - Unknown, - Male, - Female, - Other - } +public enum Genders +{ + Unknown, + Male, + Female, + Other +} - public class Person - { - public int Id { get; set; } - public string Name { get; set; } - public Genders Gender { get; set; } - public Person Partner { get; set; } - } +public class Person +{ + public int Id { get; set; } + public string Name { get; set; } + public Genders Gender { get; set; } + public Person Partner { get; set; } +} - internal static class PersonMatchers +internal static class PersonMatchers +{ + internal static void Benny(this IA a) { - internal static void Benny(this IA a) + a.Compose(person => { - a.Compose(person => - { - Expect(person).Not.To.Be.Null("Null person"); - Expect(person.Gender).To.Equal(Genders.Male); - Expect(person.Name).To.Equal("Benny"); - }, (person, passed) => - $"Expected {person.Stringify()} {passed.AsNot()}to be a Benny"); - } + Expect(person).Not.To.Be.Null("Null person"); + Expect(person.Gender).To.Equal(Genders.Male); + Expect(person.Name).To.Equal("Benny"); + }, (person, passed) => + $"Expected {person.Stringify()} {passed.AsNot()}to be a Benny"); + } - internal static void Bennies(this ICollectionBe be) + internal static void Bennies(this ICollectionBe be) + { + be.Compose(persons => { - be.Compose(persons => + persons.ForEach(p => { - persons.ForEach(p => - { - Expect(p).To.Be.A.Benny(); - }); - }, (collection, passed) => - $"Expected {MessageHelpers.Stringify(collection)} {passed.AsNot()}to all be Bennies"); - } + Expect(p).To.Be.A.Benny(); + }); + }, (collection, passed) => + $"Expected {MessageHelpers.Stringify(collection)} {passed.AsNot()}to all be Bennies"); + } - internal static void Bennies2(this ICollectionBe be) + internal static void Bennies2(this ICollectionBe be) + { + be.Compose(persons => { - be.Compose(persons => + persons.ForEach(p => { - persons.ForEach(p => - { - Expect(p).To.Be.A.Benny(); - }); + Expect(p).To.Be.A.Benny(); }); - } + }); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/DanglingPrepositions/A.cs b/src/NExpect.Tests/DanglingPrepositions/A.cs index 9072cec3..2466e1f8 100644 --- a/src/NExpect.Tests/DanglingPrepositions/A.cs +++ b/src/NExpect.Tests/DanglingPrepositions/A.cs @@ -1,84 +1,83 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NExpect.Implementations; using NExpect.MatcherLogic; using NUnit.Framework; using NExpect.Interfaces; -namespace NExpect.Tests.DanglingPrepositions +namespace NExpect.Tests.DanglingPrepositions; + +[TestFixture] +public class A { - [TestFixture] - public class A + [Test] + public void ShouldProvideExtensionPoint() { - [Test] - public void ShouldProvideExtensionPoint() - { - // Arrange + // Arrange - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(new Frog() as object).To.Be.A.Frog(); - }, - Throws.Nothing); - Assert.That( - () => - { - Expect(new Frog() as object).Not.To.Be.A.Frog(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("Expected not to get a frog")); + // Act + Assert.That( + () => + { + Expect(new Frog() as object).To.Be.A.Frog(); + }, + Throws.Nothing); + Assert.That( + () => + { + Expect(new Frog() as object).Not.To.Be.A.Frog(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("Expected not to get a frog")); - // Assert - } + // Assert } +} - [TestFixture] - public class MoreFromAddMatcher +[TestFixture] +public class MoreFromAddMatcher +{ + [Test] + public void ShouldBeAbleToContinueWithMore() { - [Test] - public void ShouldBeAbleToContinueWithMore() + // Arrange + // Act + Assert.DoesNotThrow(() => { - // Arrange - // Act - Assert.DoesNotThrow(() => - { - Expect(new Frog() as object) - .To.Be.A.Frog() - .And.Not.To.Be.A.Dog(); - }); - // Assert - } + Expect(new Frog() as object) + .To.Be.A.Frog() + .And.Not.To.Be.A.Dog(); + }); + // Assert } +} - public static class ExtensionsForTestingA +public static class ExtensionsForTestingA +{ + public static IMore Frog(this IA continuation) { - public static IMore Frog(this IA continuation) - { - return continuation.AddMatcher( - o => - { - var passed = o is Frog; - return new MatcherResult( - passed, - () => $"Expected {passed.AsNot()}to get a frog" - ); - }); - } - - public static IMore Dog( - this IA continuation) - { - return continuation.AddMatcher(o => + return continuation.AddMatcher( + o => { - var passed = o is Dog; + var passed = o is Frog; return new MatcherResult( passed, - () => $"Expected {passed.AsNot()}to get a dog" + () => $"Expected {passed.AsNot()}to get a frog" ); }); - } } -} \ No newline at end of file + + public static IMore Dog( + this IA continuation) + { + return continuation.AddMatcher(o => + { + var passed = o is Dog; + return new MatcherResult( + passed, + () => $"Expected {passed.AsNot()}to get a dog" + ); + }); + } +} diff --git a/src/NExpect.Tests/DanglingPrepositions/An.cs b/src/NExpect.Tests/DanglingPrepositions/An.cs index 267f56b8..3f485d71 100644 --- a/src/NExpect.Tests/DanglingPrepositions/An.cs +++ b/src/NExpect.Tests/DanglingPrepositions/An.cs @@ -1,58 +1,57 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NExpect.MatcherLogic; using NUnit.Framework; using NExpect.Interfaces; -namespace NExpect.Tests.DanglingPrepositions +namespace NExpect.Tests.DanglingPrepositions; + +[TestFixture] +public class An { - [TestFixture] - public class An + [Test] + public void ShouldProvideExtensionPoint() { - [Test] - public void ShouldProvideExtensionPoint() - { - // Arrange - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(new Ostrich() as object).To.Be.An.Ostrich(); - }, - Throws.Nothing); - - Assert.That( - () => - { - Expect(new Ostrich() as object).Not.To.Be.An.Ostrich(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("Expected not to get an Ostrich")); - - // Assert - } - } + // Arrange - public class Ostrich - { + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(new Ostrich() as object).To.Be.An.Ostrich(); + }, + Throws.Nothing); + + Assert.That( + () => + { + Expect(new Ostrich() as object).Not.To.Be.An.Ostrich(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("Expected not to get an Ostrich")); + + // Assert } +} + +public class Ostrich +{ +} - public static class ExtensionsForTestingAn +public static class ExtensionsForTestingAn +{ + public static void Ostrich(this IAn continuation) { - public static void Ostrich(this IAn continuation) - { - continuation.AddMatcher( - o => - { - var passed = o is Ostrich; - return new MatcherResult( - passed, - () => passed - ? "Expected not to get an Ostrich" - : "Expected to get an Ostrich"); - }); - } + continuation.AddMatcher( + o => + { + var passed = o is Ostrich; + return new MatcherResult( + passed, + () => passed + ? "Expected not to get an Ostrich" + : "Expected to get an Ostrich"); + }); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/DanglingPrepositions/Contain.cs b/src/NExpect.Tests/DanglingPrepositions/Contain.cs index 3985c9df..efbf87df 100644 --- a/src/NExpect.Tests/DanglingPrepositions/Contain.cs +++ b/src/NExpect.Tests/DanglingPrepositions/Contain.cs @@ -1,58 +1,57 @@ -using System.Drawing; +using System.Drawing; using NExpect.Exceptions; using NUnit.Framework; using NExpect.Interfaces; using NExpect.MatcherLogic; using NExpect.Implementations; -namespace NExpect.Tests.DanglingPrepositions +namespace NExpect.Tests.DanglingPrepositions; + +[TestFixture] +public class Contain { - [TestFixture] - public class Contain + [Test] + public void DanglingContain() { - [Test] - public void DanglingContain() + // Arrange + var outer = new Rectangle(0, 0, 100, 100); + var inner = new Rectangle(10, 10, 10, 10); + // Pre-assert + // Act + Assert.That(() => + { + Expect(outer).Not.To.Contain.Shape(inner); + }, Throws.Exception.InstanceOf()); + + Assert.That(() => { - // Arrange - var outer = new Rectangle(0, 0, 100, 100); - var inner = new Rectangle(10, 10, 10, 10); - // Pre-assert - // Act - Assert.That(() => - { - Expect(outer).Not.To.Contain.Shape(inner); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(outer).To.Not.Contain.Shape(inner); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(outer).To.Contain.Shape(inner); - }, Throws.Nothing); - // Assert - } + Expect(outer).To.Not.Contain.Shape(inner); + }, Throws.Exception.InstanceOf()); + + Assert.That(() => + { + Expect(outer).To.Contain.Shape(inner); + }, Throws.Nothing); + // Assert } +} - public static class ShapeMatchers +public static class ShapeMatchers +{ + public static void Shape( + this IContain contain, + Rectangle other + ) { - public static void Shape( - this IContain contain, - Rectangle other - ) + contain.AddMatcher(actual => { - contain.AddMatcher(actual => - { - var passed = actual.Left <= other.Left && - actual.Right >= other.Right && - actual.Top <= other.Top && - actual.Bottom >= other.Bottom; - return new MatcherResult( - passed, - () => $"Expected {actual.Stringify()} {passed.AsNot()}to contain {other.Stringify()}"); - }); - } + var passed = actual.Left <= other.Left && + actual.Right >= other.Right && + actual.Top <= other.Top && + actual.Bottom >= other.Bottom; + return new MatcherResult( + passed, + () => $"Expected {actual.Stringify()} {passed.AsNot()}to contain {other.Stringify()}"); + }); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/DanglingPrepositions/For.cs b/src/NExpect.Tests/DanglingPrepositions/For.cs index 35075490..d0cdccf6 100644 --- a/src/NExpect.Tests/DanglingPrepositions/For.cs +++ b/src/NExpect.Tests/DanglingPrepositions/For.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using NExpect.Exceptions; using NExpect.Interfaces; @@ -9,122 +9,121 @@ // ReSharper disable ClassNeverInstantiated.Global // ReSharper disable UnusedAutoPropertyAccessor.Global -namespace NExpect.Tests.DanglingPrepositions +namespace NExpect.Tests.DanglingPrepositions; + +[TestFixture] +public class For { [TestFixture] - public class For + public class ForObjects { - [TestFixture] - public class ForObjects + [Test] + public void ShouldProvideExtensionPoint() { - [Test] - public void ShouldProvideExtensionPoint() + // Arrange + var pet = GetRandom(); + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var pet = GetRandom(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(pet).To.Be.For.Owner(pet.Owner); - }, Throws.Nothing); - // Assert - } + Expect(pet).To.Be.For.Owner(pet.Owner); + }, Throws.Nothing); + // Assert + } - [Test] - public void ShouldProvideExtensionPoint_Negative() + [Test] + public void ShouldProvideExtensionPoint_Negative() + { + // Arrange + var pet = GetRandom(); + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var pet = GetRandom(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(pet).Not.To.Be.For.Owner(pet.Owner); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(pet).Not.To.Be.For.Owner(pet.Owner); + }, Throws.Exception.InstanceOf()); + // Assert } + } - [TestFixture] - public class ForCollections + [TestFixture] + public class ForCollections + { + [Test] + public void ShouldProvideExtensionPoint() { - [Test] - public void ShouldProvideExtensionPoint() + // Arrange + var pet1 = GetRandom(); + var pet2 = GetRandom(); + pet2.Owner = pet1.Owner; + var pets = new[] { pet1, pet2 }; + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var pet1 = GetRandom(); - var pet2 = GetRandom(); - pet2.Owner = pet1.Owner; - var pets = new[] { pet1, pet2 }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(pets).To.Be.For.Owner(pet1.Owner); - }, Throws.Nothing); - // Assert - } + Expect(pets).To.Be.For.Owner(pet1.Owner); + }, Throws.Nothing); + // Assert + } - [Test] - public void ShouldProvideExtensionPoint_Negative() + [Test] + public void ShouldProvideExtensionPoint_Negative() + { + // Arrange + var pet1 = GetRandom(); + var pet2 = GetRandom(); + pet2.Owner = pet1.Owner; + var pets = new[] { pet1, pet2 }; + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var pet1 = GetRandom(); - var pet2 = GetRandom(); - pet2.Owner = pet1.Owner; - var pets = new[] { pet1, pet2 }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(pets).Not.To.Be.For.Owner(pet1.Owner); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(pets).Not.To.Be.For.Owner(pet1.Owner); + }, Throws.Exception.InstanceOf()); + // Assert } + } - [TestFixture] - public class ForMore + [TestFixture] + public class ForMore + { + [Test] + public void ShouldProvideExtensionPointOnMore() { - [Test] - public void ShouldProvideExtensionPointOnMore() + // Arrange + var pet = GetRandom(); + + // Act + Assert.That(() => { - // Arrange - var pet = GetRandom(); - - // Act - Assert.That(() => - { - Expect(pet) - .To.Be.For.Owner(pet.Owner) - // yes, the syntax here doesn't make sense - I just - // need a quick 'n dirty test to enforce that .For is in - .For.Owner(pet.Owner); - }, Throws.Nothing); - // Assert - } + Expect(pet) + .To.Be.For.Owner(pet.Owner) + // yes, the syntax here doesn't make sense - I just + // need a quick 'n dirty test to enforce that .For is in + .For.Owner(pet.Owner); + }, Throws.Nothing); + // Assert } } +} + +public class Pet +{ + public Person Owner { get; set; } +} - public class Pet +internal static class ExtendingFor +{ + internal static IMore Owner(this IFor petFor, Person expectedOwner) { - public Person Owner { get; set; } + return petFor.Compose(pet => Expect(pet.Owner).To.Deep.Equal(expectedOwner)); } - internal static class ExtendingFor + internal static IMore> Owner(this ICollectionFor petFor, Person expectedOwner) { - internal static IMore Owner(this IFor petFor, Person expectedOwner) + return petFor.Compose(pets => { - return petFor.Compose(pet => Expect(pet.Owner).To.Deep.Equal(expectedOwner)); - } - - internal static IMore> Owner(this ICollectionFor petFor, Person expectedOwner) - { - return petFor.Compose(pets => - { - Expect(pets.All(p => p.Owner.DeepEquals(expectedOwner))).To.Be.True(); - }); - } + Expect(pets.All(p => p.Owner.DeepEquals(expectedOwner))).To.Be.True(); + }); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/DanglingPrepositions/Have.cs b/src/NExpect.Tests/DanglingPrepositions/Have.cs index 4c5ca4bf..a057bdfb 100644 --- a/src/NExpect.Tests/DanglingPrepositions/Have.cs +++ b/src/NExpect.Tests/DanglingPrepositions/Have.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using NExpect.Exceptions; using NExpect.Implementations; using NExpect.Interfaces; @@ -6,80 +6,79 @@ using NUnit.Framework; using PeanutButter.RandomGenerators; -namespace NExpect.Tests.DanglingPrepositions +namespace NExpect.Tests.DanglingPrepositions; + +[TestFixture] +public class Have { - [TestFixture] - public class Have + [Test] + public void DanglingHave() { - [Test] - public void DanglingHave() + // Arrange + var parent = new Container() { - // Arrange - var parent = new Container() - { - Subs = RandomValueGen.GetRandomArray(2) - }; - var search = RandomValueGen.GetRandomFrom(parent.Subs); - // Pre-assert - // Act - Assert.That(() => - { - Expect(parent).Not.To.Have.Child(search); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(parent).To.Not.Have.Child(search); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(parent).To.Have.Child(search); - }, Throws.Nothing); - // Assert - } - } + Subs = RandomValueGen.GetRandomArray(2) + }; + var search = RandomValueGen.GetRandomFrom(parent.Subs); + // Pre-assert + // Act + Assert.That(() => + { + Expect(parent).Not.To.Have.Child(search); + }, Throws.Exception.InstanceOf()); - public class Container - { - public Sub[] Subs { get; set; } + Assert.That(() => + { + Expect(parent).To.Not.Have.Child(search); + }, Throws.Exception.InstanceOf()); + + Assert.That(() => + { + Expect(parent).To.Have.Child(search); + }, Throws.Nothing); + // Assert } +} + +public class Container +{ + public Sub[] Subs { get; set; } +} - public class Sub - { - public string Name { get; set; } - } +public class Sub +{ + public string Name { get; set; } +} - public static class ObjectContainMatchers +public static class ObjectContainMatchers +{ + public static void Child( + this IContain contain, + Sub sub) { - public static void Child( - this IContain contain, - Sub sub) + contain.AddMatcher(actual => { - contain.AddMatcher(actual => - { - var passed = (actual?.Subs ?? new Sub[0]) - .Any(c => c.Name == sub.Name); - return new MatcherResult( - passed, - $"Expected {actual.Stringify()} {passed.AsNot()}to contain sub {sub.Name}"); - }); - } - } - public static class ObjectHaveMatchers + var passed = (actual?.Subs ?? new Sub[0]) + .Any(c => c.Name == sub.Name); + return new MatcherResult( + passed, + $"Expected {actual.Stringify()} {passed.AsNot()}to contain sub {sub.Name}"); + }); + } +} +public static class ObjectHaveMatchers +{ + public static void Child( + this IHave contain, + Sub sub) { - public static void Child( - this IHave contain, - Sub sub) + contain.AddMatcher(actual => { - contain.AddMatcher(actual => - { - var passed = (actual?.Subs ?? new Sub[0]) - .Any(c => c.Name == sub.Name); - return new MatcherResult( - passed, - $"Expected {actual.Stringify()} {passed.AsNot()}to contain sub {sub.Name}"); - }); - } + var passed = (actual?.Subs ?? new Sub[0]) + .Any(c => c.Name == sub.Name); + return new MatcherResult( + passed, + $"Expected {actual.Stringify()} {passed.AsNot()}to contain sub {sub.Name}"); + }); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/DanglingPrepositions/With.cs b/src/NExpect.Tests/DanglingPrepositions/With.cs index 20b755ed..4b1fb412 100644 --- a/src/NExpect.Tests/DanglingPrepositions/With.cs +++ b/src/NExpect.Tests/DanglingPrepositions/With.cs @@ -4,219 +4,218 @@ using NExpect.Interfaces; using NExpect.MatcherLogic; -namespace NExpect.Tests.DanglingPrepositions +namespace NExpect.Tests.DanglingPrepositions; + +[TestFixture] +public class Matched { - [TestFixture] - public class Matched + [Test] + public void ShouldAllowArbitraryMatchingForPropertyFetcher() { - [Test] - public void ShouldAllowArbitraryMatchingForPropertyFetcher() + // Arrange + var dog = new Dog() { Name = "Fluffy" }; + + // Act + Assert.That(() => { - // Arrange - var dog = new Dog() { Name = "Fluffy" }; - - // Act - Assert.That(() => - { - Expect(dog) - .To.Be.Matched.By(o => o.Name == "Fluffy"); - }, Throws.Nothing); - Assert.That(() => - { - Expect(dog) - .To.Be.Matched.By(o => o.Name == "Rex"); - }, Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void ShouldAllowMatchingOnExceptionPropertyGrabbers() + Expect(dog) + .To.Be.Matched.By(o => o.Name == "Fluffy"); + }, Throws.Nothing); + Assert.That(() => { - // Arrange - // Act - Assert.That(() => - { - Expect(() => throw new Exception("moo")) - .To.Throw() - .With.Property(o => o) - .Matched.By(e => e.Message == "moo"); - }, Throws.Nothing); - Assert.That(() => - { - Expect(() => throw new Exception("moo")) - .To.Throw() - .With.Property(o => o) - .Matched.By(e => e.Message == "cow"); - }, Throws.Exception.InstanceOf()); - // Assert - } - + Expect(dog) + .To.Be.Matched.By(o => o.Name == "Rex"); + }, Throws.Exception.InstanceOf()); + // Assert } - [TestFixture] - public class With + + [Test] + public void ShouldAllowMatchingOnExceptionPropertyGrabbers() { - [Test] - public void ShouldBeAbleToDangleWith() + // Arrange + // Act + Assert.That(() => { - // Arrange - var dog = new Dog() { Name = "Rex" }; - // Act - Assert.That(() => - { - Expect(dog) - .To.Be.An.Instance.Of() - .With.Name("Spot"); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("to have name 'Spot'") - ); + Expect(() => throw new Exception("moo")) + .To.Throw() + .With.Property(o => o) + .Matched.By(e => e.Message == "moo"); + }, Throws.Nothing); + Assert.That(() => + { + Expect(() => throw new Exception("moo")) + .To.Throw() + .With.Property(o => o) + .Matched.By(e => e.Message == "cow"); + }, Throws.Exception.InstanceOf()); + // Assert + } - Assert.That(() => +} +[TestFixture] +public class With +{ + [Test] + public void ShouldBeAbleToDangleWith() + { + // Arrange + var dog = new Dog() { Name = "Rex" }; + // Act + Assert.That(() => { Expect(dog) .To.Be.An.Instance.Of() - .With.Name("Rex"); - }, Throws.Nothing); - - // Assert - } + .With.Name("Spot"); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("to have name 'Spot'") + ); - [Test] - public void ShouldHaveGeneralMatchedBy() + Assert.That(() => { - // Arrange - var dog = new Dog() { Name = "Rex" }; - // Act - Assert.That(() => - { - Expect(dog) - .To.Be.An.Instance.Of() - .Matched.By(o => o.Name == "Spot"); - }, Throws.Exception.InstanceOf() - ); + Expect(dog) + .To.Be.An.Instance.Of() + .With.Name("Rex"); + }, Throws.Nothing); - Assert.That(() => + // Assert + } + + [Test] + public void ShouldHaveGeneralMatchedBy() + { + // Arrange + var dog = new Dog() { Name = "Rex" }; + // Act + Assert.That(() => { Expect(dog) .To.Be.An.Instance.Of() - .Matched.By(o => o.Name == "Rex"); - }, Throws.Nothing); + .Matched.By(o => o.Name == "Spot"); + }, Throws.Exception.InstanceOf() + ); + + Assert.That(() => + { + Expect(dog) + .To.Be.An.Instance.Of() + .Matched.By(o => o.Name == "Rex"); + }, Throws.Nothing); - // Assert - } + // Assert + } - [Test] - public void ShouldHaveGenericWithPropertyAvailable() + [Test] + public void ShouldHaveGenericWithPropertyAvailable() + { + // Arrange + var dog = new Dog() { Name = "Fluffy" }; + // Act + Assert.That(() => { - // Arrange - var dog = new Dog() { Name = "Fluffy" }; - // Act - Assert.That(() => - { - Expect(dog) - .To.Be.An.Instance.Of() - .With.Property(d => d.Name) - .Equal.To("Fluffy"); - }, Throws.Nothing); - Assert.That(() => - { - Expect(dog) - .To.Be.An.Instance.Of() - .With.Property(d => d.Name) - .Equal.To("Rex"); - }, Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void ShouldAllowContinuationsFromAssignableType() + Expect(dog) + .To.Be.An.Instance.Of() + .With.Property(d => d.Name) + .Equal.To("Fluffy"); + }, Throws.Nothing); + Assert.That(() => { - // Arrange - var dog = new Dog() { Name = "Rufus" } as IAnimal; - // Act - Assert.That(() => - { - Expect(dog) - .To.Be.An.Instance.Of() - .With.Property(o => o.Name) - .Equal.To("Rufus"); - }, Throws.Nothing); - // Assert - } - - [TestCase(".With.Required")] - public void ShouldBeAbleToDangle_(string moo) + Expect(dog) + .To.Be.An.Instance.Of() + .With.Property(d => d.Name) + .Equal.To("Rex"); + }, Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void ShouldAllowContinuationsFromAssignableType() + { + // Arrange + var dog = new Dog() { Name = "Rufus" } as IAnimal; + // Act + Assert.That(() => { - // Arrange - var passingAnimal = new Dog() - { - Name = "Rex" - }; - var failingAnimal = new Dog() - { - Name = "Spot" - }; - // Act - Assert.That(() => - { - Expect(passingAnimal) - .To.Be.An.Instance.Of() - .With.Required.Name("Rex"); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(failingAnimal) - .To.Be.An.Instance.Of() - .With.Required.Name("Rex"); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(dog) + .To.Be.An.Instance.Of() + .With.Property(o => o.Name) + .Equal.To("Rufus"); + }, Throws.Nothing); + // Assert } - public static class WithExtensions + [TestCase(".With.Required")] + public void ShouldBeAbleToDangle_(string moo) { - public static IMore Name( - this IRequired required, - string requiredName - ) + // Arrange + var passingAnimal = new Dog() { - return required.AddMatcher(actual => - { - var name = actual.Name; // ?.GetOrDefault(nameof(Animal.Name), null as string); - var passed = name == requiredName; - return new MatcherResult( - passed, - () => $"Expected {actual} to have name '{requiredName}'" - ); - }); - } - - public static IMore Name( - this IWith with, - string expected) + Name = "Rex" + }; + var failingAnimal = new Dog() { - return with.AddMatcher(actual => - { - var name = actual.Name; // ?.GetOrDefault(nameof(Animal.Name), null as string); - var passed = name == expected; - return new MatcherResult( - passed, - () => $"Expected {actual} to have name '{expected}'" - ); - }); - } + Name = "Spot" + }; + // Act + Assert.That(() => + { + Expect(passingAnimal) + .To.Be.An.Instance.Of() + .With.Required.Name("Rex"); + }, Throws.Nothing); + + Assert.That(() => + { + Expect(failingAnimal) + .To.Be.An.Instance.Of() + .With.Required.Name("Rex"); + }, Throws.Exception.InstanceOf()); + // Assert + } +} + +public static class WithExtensions +{ + public static IMore Name( + this IRequired required, + string requiredName + ) + { + return required.AddMatcher(actual => + { + var name = actual.Name; // ?.GetOrDefault(nameof(Animal.Name), null as string); + var passed = name == requiredName; + return new MatcherResult( + passed, + () => $"Expected {actual} to have name '{requiredName}'" + ); + }); } - public abstract class Animal : IAnimal + public static IMore Name( + this IWith with, + string expected) { - public string Name { get; init; } + return with.AddMatcher(actual => + { + var name = actual.Name; // ?.GetOrDefault(nameof(Animal.Name), null as string); + var passed = name == expected; + return new MatcherResult( + passed, + () => $"Expected {actual} to have name '{expected}'" + ); + }); } +} + +public abstract class Animal : IAnimal +{ + public string Name { get; init; } +} - public interface IAnimal; +public interface IAnimal; - public class Dog : Animal; +public class Dog : Animal; - public class Cat : Animal; +public class Cat : Animal; - public class Frog : Animal; -} \ No newline at end of file +public class Frog : Animal; diff --git a/src/NExpect.Tests/DeepEqualityTesting.cs b/src/NExpect.Tests/DeepEqualityTesting.cs index dfe1ce3e..6c094a85 100644 --- a/src/NExpect.Tests/DeepEqualityTesting.cs +++ b/src/NExpect.Tests/DeepEqualityTesting.cs @@ -1,128 +1,127 @@ -using System; +using System; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class DeepEqualityTesting { - [TestFixture] - public class DeepEqualityTesting + [Test] + public void ShouldNotComparePrivateFieldsOrProperties() { - [Test] - public void ShouldNotComparePrivateFieldsOrProperties() - { - // Arrange - var left = new HasPrivates(1, "Bob", "red"); - var right = new HasPrivates(1, "Mary", "blue"); - // Act - Assert.That(() => Expect(left).To.Deep.Equal(right), Throws.Nothing); - // Assert - } + // Arrange + var left = new HasPrivates(1, "Bob", "red"); + var right = new HasPrivates(1, "Mary", "blue"); + // Act + Assert.That(() => Expect(left).To.Deep.Equal(right), Throws.Nothing); + // Assert + } - [Test] - public void ShouldNotStackOverflowOnEnum() + [Test] + public void ShouldNotStackOverflowOnEnum() + { + // Arrange + var left = new { - // Arrange - var left = new - { - LogLevel = LogLevel.Critical - }; - var right = new - { - LogLevel = LogLevel.Critical - }; - // Act - Assert.That(() => Expect(left) - .To.Deep.Equal(right), Throws.Nothing); - // Assert - } - - [Test] - public void ShouldIgnoreStaticProperties() + LogLevel = LogLevel.Critical + }; + var right = new { - // Arrange - HasAStaticProp.Id = GetRandomInt(); - var data = new HasAStaticProp() - { - Name = GetRandomString() - }; - // Act - Assert.That(() => - { - Expect(data) - .To.Deep.Equal( - new - { - Name = data.Name, - Id = HasAStaticProp.Id - }); - }, Throws.Exception.InstanceOf()); - Assert.That(() => - { - Expect(data) - .To.Deep.Equal( - new - { - Name = data.Name, - }); - }, Throws.Nothing); - // Assert - } + LogLevel = LogLevel.Critical + }; + // Act + Assert.That(() => Expect(left) + .To.Deep.Equal(right), Throws.Nothing); + // Assert + } - [Test] - [Ignore("TODO: need to figure out a nice api for this without muddling the existing ones")] - public void ShouldBeAbleToExcludePropertiesByName() + [Test] + public void ShouldIgnoreStaticProperties() + { + // Arrange + HasAStaticProp.Id = GetRandomInt(); + var data = new HasAStaticProp() { - // Arrange - var left = GetRandom(); - var right = left.DeepClone() - .With(o => o.Id++) - .With(o => o.Created = GetAnother(o.Created)); - // Act - Assert.That(() => - { - - }, Throws.Nothing); - // Assert - } - - public class HasManyProps + Name = GetRandomString() + }; + // Act + Assert.That(() => { - public int Id { get; set; } - public string Name { get; set; } - public DateTime Created { get; set; } - public bool Flag { get; set; } - } - - public class HasAStaticProp + Expect(data) + .To.Deep.Equal( + new + { + Name = data.Name, + Id = HasAStaticProp.Id + }); + }, Throws.Exception.InstanceOf()); + Assert.That(() => { - public string Name { get; set; } - public static int Id { get; set; } - } + Expect(data) + .To.Deep.Equal( + new + { + Name = data.Name, + }); + }, Throws.Nothing); + // Assert + } - public enum LogLevel + [Test] + [Ignore("TODO: need to figure out a nice api for this without muddling the existing ones")] + public void ShouldBeAbleToExcludePropertiesByName() + { + // Arrange + var left = GetRandom(); + var right = left.DeepClone() + .With(o => o.Id++) + .With(o => o.Created = GetAnother(o.Created)); + // Act + Assert.That(() => { - Trace, - Debug, - Information, - Warning, - Error, - Critical, - None, - } + + }, Throws.Nothing); + // Assert + } - public class HasPrivates - { - public int Id { get; set; } - private string Name { get; set; } - private string _color; + public class HasManyProps + { + public int Id { get; set; } + public string Name { get; set; } + public DateTime Created { get; set; } + public bool Flag { get; set; } + } - public HasPrivates(int id, string name, string color) - { - Id = id; - Name = name; - _color = color; - } + public class HasAStaticProp + { + public string Name { get; set; } + public static int Id { get; set; } + } + + public enum LogLevel + { + Trace, + Debug, + Information, + Warning, + Error, + Critical, + None, + } + + public class HasPrivates + { + public int Id { get; set; } + private string Name { get; set; } + private string _color; + + public HasPrivates(int id, string name, string color) + { + Id = id; + Name = name; + _color = color; } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Defaults.cs b/src/NExpect.Tests/Defaults.cs index eec6ade6..8405dd74 100644 --- a/src/NExpect.Tests/Defaults.cs +++ b/src/NExpect.Tests/Defaults.cs @@ -2,160 +2,159 @@ using NExpect.Exceptions; using PeanutButter.RandomGenerators; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class Defaults { - [TestFixture] - public class Defaults + [Test] + public void ShouldBeAbleToAssertThatNewObjectHasDefaultProperties() { - [Test] - public void ShouldBeAbleToAssertThatNewObjectHasDefaultProperties() + // Arrange + var defaults = new Person(); + var notDefaults = GetRandom(); + // Act + Assert.That(() => { - // Arrange - var defaults = new Person(); - var notDefaults = GetRandom(); - // Act - Assert.That(() => - { - Expect(defaults) - .To.Have.Default.Properties(); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(defaults) - .Not.To.Have.Default.Properties(); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(notDefaults) - .Not.To.Have.Default.Properties(); - }, Throws.Nothing); - Assert.That(() => - { - Expect(notDefaults) - .To.Have.Default.Properties(); - }, Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertThatNewRefObjectHasDefaultFields() + Expect(defaults) + .To.Have.Default.Properties(); + }, Throws.Nothing); + + Assert.That(() => { - // Arrange - var defaults = new HasFields(); - var notDefaults = GetRandom(); - - // Act - Assert.That(() => - { - Expect(defaults) - .To.Have.Default.Fields(); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(defaults) - .Not.To.Have.Default.Fields(); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(notDefaults) - .Not.To.Have.Default.Fields(); - }, Throws.Nothing); - Assert.That(() => - { - Expect(notDefaults) - .To.Have.Default.Fields(); - }, Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertFieldsAndPropsViaAnd() + Expect(defaults) + .Not.To.Have.Default.Properties(); + }, Throws.Exception.InstanceOf()); + + Assert.That(() => { - // Arrange - var defaults = new HasFieldsAndProps(); - var notDefaults = GetRandom(); - var defaultPropertyOnly = new HasFieldsAndProps(); - - // Act - Assert.That(() => - { - Expect(defaults) - .To.Have.Default.Fields() - .And - .To.Have.Default.Properties(); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(defaults) - .Not.To.Have.Default.Fields() - .And - .Not.To.Have.Default.Properties(); - }, Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(notDefaults) - .Not.To.Have.Default.Fields() - .And - .Not.To.Have.Default.Properties(); - }, Throws.Nothing); - Assert.That(() => - { - Expect(notDefaults) - .To.Have.Default.Fields() - .And - .To.Have.Default.Properties(); - }, Throws.Exception.InstanceOf()); - - - Assert.That(() => - { - Expect(defaultPropertyOnly) - .To.Have.Default.Fields() - .And - .Not.To.Have.Default.Properties(); - }, Throws.Exception.InstanceOf()); - Assert.That(() => - { - Expect(defaultPropertyOnly) - .Not.To.Have.Default.Properties() - .And - .To.Have.Default.Fields(); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(notDefaults) + .Not.To.Have.Default.Properties(); + }, Throws.Nothing); + Assert.That(() => + { + Expect(notDefaults) + .To.Have.Default.Properties(); + }, Throws.Exception.InstanceOf()); + // Assert } - public class HasFieldsAndProps + [Test] + public void ShouldBeAbleToAssertThatNewRefObjectHasDefaultFields() { - public int Id { get; set; } - public string Name; - } + // Arrange + var defaults = new HasFields(); + var notDefaults = GetRandom(); - [RequireNonZeroId] - public class HasFieldsAndPropsBuilder : GenericBuilder - { - } + // Act + Assert.That(() => + { + Expect(defaults) + .To.Have.Default.Fields(); + }, Throws.Nothing); - public struct HasFields - { - public int Id; - public string Name; - public AnotherStruct Friend; - public Person Acquaintance; - } + Assert.That(() => + { + Expect(defaults) + .Not.To.Have.Default.Fields(); + }, Throws.Exception.InstanceOf()); - [RequireNonZeroId] - public class HasFieldsBuilder : GenericBuilder - { + Assert.That(() => + { + Expect(notDefaults) + .Not.To.Have.Default.Fields(); + }, Throws.Nothing); + Assert.That(() => + { + Expect(notDefaults) + .To.Have.Default.Fields(); + }, Throws.Exception.InstanceOf()); + // Assert } - public struct AnotherStruct + [Test] + public void ShouldBeAbleToAssertFieldsAndPropsViaAnd() { + // Arrange + var defaults = new HasFieldsAndProps(); + var notDefaults = GetRandom(); + var defaultPropertyOnly = new HasFieldsAndProps(); + + // Act + Assert.That(() => + { + Expect(defaults) + .To.Have.Default.Fields() + .And + .To.Have.Default.Properties(); + }, Throws.Nothing); + + Assert.That(() => + { + Expect(defaults) + .Not.To.Have.Default.Fields() + .And + .Not.To.Have.Default.Properties(); + }, Throws.Exception.InstanceOf()); + + Assert.That(() => + { + Expect(notDefaults) + .Not.To.Have.Default.Fields() + .And + .Not.To.Have.Default.Properties(); + }, Throws.Nothing); + Assert.That(() => + { + Expect(notDefaults) + .To.Have.Default.Fields() + .And + .To.Have.Default.Properties(); + }, Throws.Exception.InstanceOf()); + + + Assert.That(() => + { + Expect(defaultPropertyOnly) + .To.Have.Default.Fields() + .And + .Not.To.Have.Default.Properties(); + }, Throws.Exception.InstanceOf()); + Assert.That(() => + { + Expect(defaultPropertyOnly) + .Not.To.Have.Default.Properties() + .And + .To.Have.Default.Fields(); + }, Throws.Exception.InstanceOf()); + // Assert } -} \ No newline at end of file +} + +public class HasFieldsAndProps +{ + public int Id { get; set; } + public string Name; +} + +[RequireNonZeroId] +public class HasFieldsAndPropsBuilder : GenericBuilder +{ +} + +public struct HasFields +{ + public int Id; + public string Name; + public AnotherStruct Friend; + public Person Acquaintance; +} + +[RequireNonZeroId] +public class HasFieldsBuilder : GenericBuilder +{ +} + +public struct AnotherStruct +{ +} diff --git a/src/NExpect.Tests/EnablingUserComposition.cs b/src/NExpect.Tests/EnablingUserComposition.cs index 00ef2604..366f7a36 100644 --- a/src/NExpect.Tests/EnablingUserComposition.cs +++ b/src/NExpect.Tests/EnablingUserComposition.cs @@ -1,214 +1,213 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NExpect.Interfaces; using NExpect.MatcherLogic; using NUnit.Framework; // ReSharper disable ExpressionIsAlwaysNull -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class EnablingUserComposition { [TestFixture] - public class EnablingUserComposition + public class SimpleChain { - [TestFixture] - public class SimpleChain + [Test] + public void PositiveExpectation_WhenPasses_ShouldNotThrow() + { + // Arrange + var person = new Person() + { + Name = "Sally", + Gender = Genders.Female + }; + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(person).To.Be.A.Sally().And.A.Female(); + }, Throws.Nothing); + // Assert + } + + [Test] + public void PositiveExpectation_WhenFirstPartFails_ShouldThrow() + { + // Arrange + var person = new Person() + { + Name = "Mary", + Gender = Genders.Female + }; + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(person).To.Be.A.Sally().And.A.Female(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("Sally")); + // Assert + } + + [Test] + public void PositiveExpectation_WhenSecondPartFails_ShouldThrow() { - [Test] - public void PositiveExpectation_WhenPasses_ShouldNotThrow() - { - // Arrange - var person = new Person() - { - Name = "Sally", - Gender = Genders.Female - }; - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(person).To.Be.A.Sally().And.A.Female(); - }, Throws.Nothing); - // Assert - } - - [Test] - public void PositiveExpectation_WhenFirstPartFails_ShouldThrow() - { - // Arrange - var person = new Person() - { - Name = "Mary", - Gender = Genders.Female - }; - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(person).To.Be.A.Sally().And.A.Female(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("Sally")); - // Assert - } - - [Test] - public void PositiveExpectation_WhenSecondPartFails_ShouldThrow() - { - // Arrange - var person = new Person() - { - Name = "Sally", - Gender = Genders.Unknown - }; - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(person).To.Be.A.Sally().And.A.Female(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("Female")); - // Assert - } - - [Test] - public void NegativeExpectation_WhenFirstPartFails_ShouldThrow() - { - // Arrange - var person = new Person() - { - Name = "Sally", - Gender = Genders.Female - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person).Not.To.Be.A.Sally().And.Not.To.Be.A.Female(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("Sally")); - // Assert - } - - [Test] - public void NegativeExpectation_WhenSecondPartFails_ShouldThrow() - { - // Arrange - var person = new Person() - { - Name = "Mary", - Gender = Genders.Female - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person).Not.To.Be.A.Sally().And.Not.To.Be.A.Female(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("Female")); - // Assert - } + // Arrange + var person = new Person() + { + Name = "Sally", + Gender = Genders.Unknown + }; + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(person).To.Be.A.Sally().And.A.Female(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("Female")); + // Assert } - [TestFixture] - public class NullTestShouldAllowUserComposition + [Test] + public void NegativeExpectation_WhenFirstPartFails_ShouldThrow() { - [Test] - public void Contrived_PositiveChain_WhenAllPasses_ShouldNotThrow() - { - // Arrange - var contrived = GetRandomString(2); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(contrived).Not.To.Be.Null().And.To.Equal(contrived); - }, Throws.Nothing); - // Assert - } - - [Test] - public void MorePractical_PositiveChain_WhenAllPassws_ShouldNotThrow() - { - // Arrange - var person = new Person() - { - Name = "Benny", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person) - .Not.To.Be.Null() - .And.To.Be.A.Benny(); - }, Throws.Nothing); - // Assert - } - - [Test] - public void MorePractical_PositiveThenNegativeChain_WhenAllPasses_ShouldNotThrow() - { - // Arrange - var person = new Person() - { - Name = "Bob", - Gender = Genders.Male - }; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person) - .Not.To.Be.Null() - .And.Not.To.Be.A.Benny(); - }, Throws.Nothing); - // Assert - } - - [Test] - public void MorePractical_WhenFirstPartFails_ShouldThrowThere() - { - // Arrange - Person person = null; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(person) - .Not.To.Be.Null() - .And.Not.To.Be.A.Sally(); - }, Throws.Exception.InstanceOf() - .With.Message.Contains("Expected not to get null")); - // Assert - } + // Arrange + var person = new Person() + { + Name = "Sally", + Gender = Genders.Female + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person).Not.To.Be.A.Sally().And.Not.To.Be.A.Female(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("Sally")); + // Assert + } + + [Test] + public void NegativeExpectation_WhenSecondPartFails_ShouldThrow() + { + // Arrange + var person = new Person() + { + Name = "Mary", + Gender = Genders.Female + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person).Not.To.Be.A.Sally().And.Not.To.Be.A.Female(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("Female")); + // Assert } } - internal static class MorePersonMatchers + [TestFixture] + public class NullTestShouldAllowUserComposition { - internal static IMore Sally( - this IA a - ) + [Test] + public void Contrived_PositiveChain_WhenAllPasses_ShouldNotThrow() { - a.Compose(person => + // Arrange + var contrived = GetRandomString(2); + // Pre-Assert + // Act + Assert.That(() => { - Expect(person.Name).To.Equal("Sally"); - }); - return a.More(); + Expect(contrived).Not.To.Be.Null().And.To.Equal(contrived); + }, Throws.Nothing); + // Assert } - internal static IMore Female( - this IA a - ) + [Test] + public void MorePractical_PositiveChain_WhenAllPassws_ShouldNotThrow() { - a.Compose(person => + // Arrange + var person = new Person() + { + Name = "Benny", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => { - Expect(person.Gender).To.Equal(Genders.Female); - }); - return a.More(); + Expect(person) + .Not.To.Be.Null() + .And.To.Be.A.Benny(); + }, Throws.Nothing); + // Assert } + + [Test] + public void MorePractical_PositiveThenNegativeChain_WhenAllPasses_ShouldNotThrow() + { + // Arrange + var person = new Person() + { + Name = "Bob", + Gender = Genders.Male + }; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person) + .Not.To.Be.Null() + .And.Not.To.Be.A.Benny(); + }, Throws.Nothing); + // Assert + } + + [Test] + public void MorePractical_WhenFirstPartFails_ShouldThrowThere() + { + // Arrange + Person person = null; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(person) + .Not.To.Be.Null() + .And.Not.To.Be.A.Sally(); + }, Throws.Exception.InstanceOf() + .With.Message.Contains("Expected not to get null")); + // Assert + } + } +} + +internal static class MorePersonMatchers +{ + internal static IMore Sally( + this IA a + ) + { + a.Compose(person => + { + Expect(person.Name).To.Equal("Sally"); + }); + return a.More(); + } + + internal static IMore Female( + this IA a + ) + { + a.Compose(person => + { + Expect(person.Gender).To.Equal(Genders.Female); + }); + return a.More(); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Exceptions/EnforcingMessage.cs b/src/NExpect.Tests/Exceptions/EnforcingMessage.cs index 6185d79e..0c618f27 100644 --- a/src/NExpect.Tests/Exceptions/EnforcingMessage.cs +++ b/src/NExpect.Tests/Exceptions/EnforcingMessage.cs @@ -1,105 +1,85 @@ -using System; +using System; using System.Linq; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests.Exceptions +namespace NExpect.Tests.Exceptions; + +[TestFixture] +public class EnforcingMessage { - [TestFixture] - public class EnforcingMessage + [Test] + public void ShouldNotThrowWhenMessageContainsOneExpectedFragment() { - [Test] - public void ShouldNotThrowWhenMessageContainsOneExpectedFragment() + // Arrange + // Act + Assert.That( + () => + { + Expect( + () => + throw new ArgumentException("foo") + ).To.Throw() + .With.Message.Containing("foo"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldSupportEasyCaseInsensitiveContains_Like() + { + // Arrange + var message = GetRandomWords(5, 10); + var words = message.Split(" "); + var skip = GetRandomInt(2, 4); + var take = GetRandomInt(2, 4); + var seek = words.Skip(skip).Take(take).JoinWith(" ").ToRandomCase(); + while (message.Contains(seek, StringComparison.Ordinal)) { - // Arrange - // Act - Assert.That( - () => - { - Expect( - () => - throw new ArgumentException("foo") - ).To.Throw() - .With.Message.Containing("foo"); - }, - Throws.Nothing - ); - // Assert + seek = seek.ToRandomCase(); } - [Test] - public void ShouldSupportEasyCaseInsensitiveContains_Like() - { - // Arrange - var message = GetRandomWords(5, 10); - var words = message.Split(" "); - var skip = GetRandomInt(2, 4); - var take = GetRandomInt(2, 4); - var seek = words.Skip(skip).Take(take).JoinWith(" ").ToRandomCase(); - while (message.Contains(seek, StringComparison.Ordinal)) + // Act + Assert.That( + () => { - seek = seek.ToRandomCase(); - } - - // Act - Assert.That( - () => - { - Expect(() => throw new Exception(message)) - .To.Throw() - .With.Message.Containing(seek); - }, - Throws.Exception.InstanceOf() - ); - - Assert.That( - () => - { - Expect(() => throw new Exception(message)) - .To.Throw() - .With.Message.Like(seek); - }, - Throws.Nothing - ); - var other = GetAnother(seek); - Assert.That( - () => - { - Expect(() => throw new Exception(message)) - .To.Throw() - .With.Message.Like(other); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + Expect(() => throw new Exception(message)) + .To.Throw() + .With.Message.Containing(seek); + }, + Throws.Exception.InstanceOf() + ); - [TestFixture] - public class WhenStringComparisonSupplied - { - [Test] - public void ShouldNotThrowWhenMessageContainsOneExpectedFragment() + Assert.That( + () => { - // Arrange - // Act - Assert.That( - () => - { - Expect( - () => - throw new ArgumentException("foo") - ).To.Throw() - .With.Message.Containing("FOO", StringComparison.OrdinalIgnoreCase); - }, - Throws.Nothing - ); - // Assert - } - } + Expect(() => throw new Exception(message)) + .To.Throw() + .With.Message.Like(seek); + }, + Throws.Nothing + ); + var other = GetAnother(seek); + Assert.That( + () => + { + Expect(() => throw new Exception(message)) + .To.Throw() + .With.Message.Like(other); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + [TestFixture] + public class WhenStringComparisonSupplied + { [Test] - public void ShouldNotThrowWhenMessageContainsTwoExpectedFragments() + public void ShouldNotThrowWhenMessageContainsOneExpectedFragment() { // Arrange // Act @@ -108,34 +88,53 @@ public void ShouldNotThrowWhenMessageContainsTwoExpectedFragments() { Expect( () => - throw new ArgumentException("foo, bar") + throw new ArgumentException("foo") ).To.Throw() - .With.Message.Containing("foo") - .And.Containing("bar"); + .With.Message.Containing("FOO", StringComparison.OrdinalIgnoreCase); }, Throws.Nothing ); // Assert } + } - [Test] - public void ShouldNotThrowWhenTwoNegatedAdditions() - { - // Arrange - // Act - var fieldName = GetRandomString(10); - Assert.That( - () => - { - Expect(() => throw new ArgumentException("NO", fieldName)) - .To.Throw() - .With.Message.Containing(fieldName) - .And.Not.Containing("{") - .And.Not.Containing("}"); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void ShouldNotThrowWhenMessageContainsTwoExpectedFragments() + { + // Arrange + // Act + Assert.That( + () => + { + Expect( + () => + throw new ArgumentException("foo, bar") + ).To.Throw() + .With.Message.Containing("foo") + .And.Containing("bar"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldNotThrowWhenTwoNegatedAdditions() + { + // Arrange + // Act + var fieldName = GetRandomString(10); + Assert.That( + () => + { + Expect(() => throw new ArgumentException("NO", fieldName)) + .To.Throw() + .With.Message.Containing(fieldName) + .And.Not.Containing("{") + .And.Not.Containing("}"); + }, + Throws.Nothing + ); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Exceptions/EnforcingThrownExceptions.cs b/src/NExpect.Tests/Exceptions/EnforcingThrownExceptions.cs index 4d3f2de2..8bbdf7a6 100644 --- a/src/NExpect.Tests/Exceptions/EnforcingThrownExceptions.cs +++ b/src/NExpect.Tests/Exceptions/EnforcingThrownExceptions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -17,15 +17,132 @@ // ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper disable ConvertToLambdaExpression -namespace NExpect.Tests.Exceptions +namespace NExpect.Tests.Exceptions; + +[TestFixture] +public class EnforcingThrownExceptions { - [TestFixture] - public class EnforcingThrownExceptions + [Test] + public void Throw_ForAction_WithNoGenericType_WhenSUTThrows_ShouldNotThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.DoesNotThrow( + () => + { + Expect( + () => + { + throw new Exception(GetRandomString()); + } + ) + .To.Throw(); + } + ); + // Assert + } + + [Test] + public void Throw_WhenShouldNotThrowButDoes_ShouldIncludeOriginalExceptionIn_UnmetExpectation_InnerException() + { + // Arrange + var expected = new InvalidOperationException(GetRandomString(20)); + // Pre-assert + // Act + try + { + Expect(() => throw expected).Not.To.Throw(); + } + catch (UnmetExpectationException ex) + { + Expect(ex.InnerException).To.Be(expected); + } + + // Assert + } + + [Test] + public void Throw_ForAction_WithNoGenericType_WhenSUTDoesNotThrow_ShouldThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + } + ) + .To.Throw(); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void Throw_ForFunc_WithNoGenericType_WhenSUTThrows_ShouldNotThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.DoesNotThrow( + () => + { + Expect( + () => + { + if (MakeFalse()) + { + return 1; + } + + throw new Exception(GetRandomString()); + } + ) + .To.Throw(); + } + ); + // Assert + } + + private bool MakeFalse() + { + return false; + } + + [Test] + public void Throw_ForFunc_ForAction_WithNoGenericType_WhenSUTDoesNotThrow_ShouldThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + return "moo"; + } + ) + .To.Throw(); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + public class MessageContaining { [Test] - public void Throw_ForAction_WithNoGenericType_WhenSUTThrows_ShouldNotThrow() + public void Throw_WithNoGenericType_WhenThrows_ShouldBeAbleToContinueWith_WithMessage_HappyPath() { // Arrange + var expected = GetRandomString(); // Pre-Assert // Act Assert.DoesNotThrow( @@ -34,38 +151,92 @@ public void Throw_ForAction_WithNoGenericType_WhenSUTThrows_ShouldNotThrow() Expect( () => { - throw new Exception(GetRandomString()); + throw new Exception(expected); } ) - .To.Throw(); + .To.Throw() + .With.Message.Containing(expected); } ); // Assert } [Test] - public void Throw_WhenShouldNotThrowButDoes_ShouldIncludeOriginalExceptionIn_UnmetExpectation_InnerException() + public void Throw_WhenGettingMessageViaProperty_ShouldNotFailWhenHaveSoughtStringAtStartOfMessage() { // Arrange - var expected = new InvalidOperationException(GetRandomString(20)); + var message = "LOOKFORME could not moo"; + var seek = "LOOKFORME"; // Pre-assert // Act - try - { - Expect(() => throw expected).Not.To.Throw(); - } - catch (UnmetExpectationException ex) - { - Expect(ex.InnerException).To.Be(expected); - } + Assert.That( + () => + { + Expect(() => throw new Exception(message)) + .To.Throw() + .With.Property(e => e.Message) + .Containing(seek); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(() => throw new Exception(message)) + .To.Throw().With.Type(typeof(Exception)) + .And.Property(e => e.Message) + .Containing(seek); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldIncludeStacktraceWhenFailing1() + { + // Arrange + var message = "LOOKFORME could not moo"; + + // Act + Expect( + () => + Expect(() => throw new Exception(message)) + .Not.To.Throw() + ).To.Throw() + .With.Message.Containing(message) + .Then("Stacktrace") + .Then(" at "); + + // Assert + } + + [Test] + public void ShouldIncludeStacktraceWhenFailing2() + { + // Arrange + var message = "LOOKFORME could not moo"; + + // Act + Expect( + () => + Expect(() => throw new Exception(message)) + .To.Throw() + ).To.Throw() + .With.Message.Containing(message) + .Then("Stacktrace") + .Then(" at "); // Assert } [Test] - public void Throw_ForAction_WithNoGenericType_WhenSUTDoesNotThrow_ShouldThrow() + public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_HappyPath() { // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); // Pre-Assert // Act Assert.That( @@ -74,50 +245,108 @@ public void Throw_ForAction_WithNoGenericType_WhenSUTDoesNotThrow_ShouldThrow() Expect( () => { + throw new Exception(message); } ) - .To.Throw(); + .To.Throw() + .With.Message.Containing(e1) + .And(e2); }, - Throws.Exception.InstanceOf() + Throws.Nothing ); // Assert } [Test] - public void Throw_ForFunc_WithNoGenericType_WhenSUTThrows_ShouldNotThrow() + public void Throw_WithNoGenericType_WhenThrows_ShouldBeAbleToContinueWith_WithMessage_SadPath() { // Arrange + var expected = GetRandomString(); + var other = GetAnother(expected); // Pre-Assert // Act - Assert.DoesNotThrow( + Assert.That( () => { Expect( () => { - if (MakeFalse()) - { - return 1; - } + throw new Exception(other); + } + ) + .To.Throw() + .With.Message.Containing(expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains($"to contain \"{expected}\"") + ); + // Assert + } - throw new Exception(GetRandomString()); + [Test] + public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_SadPath() + { + // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var e3 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new Exception(message); } ) - .To.Throw(); - } + .To.Throw() + .With.Message.Containing(e1) + .And(e3); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(e3) ); // Assert } - private bool MakeFalse() + [Test] + public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_SadPathNegated() { - return false; + // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var e3 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new Exception(message); + } + ) + .To.Throw() + .With.Message.Containing(e1) + .And.Not.Containing(e3); + }, + Throws.Nothing + ); + // Assert } [Test] - public void Throw_ForFunc_ForAction_WithNoGenericType_WhenSUTDoesNotThrow_ShouldThrow() + public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_HappyPathNegated() { // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); // Pre-Assert // Act Assert.That( @@ -126,573 +355,744 @@ public void Throw_ForFunc_ForAction_WithNoGenericType_WhenSUTDoesNotThrow_Should Expect( () => { - return "moo"; + throw new Exception(message); } ) - .To.Throw(); + .To.Throw() + .With.Message.Not.Containing(e1) + .And(e2); }, Throws.Exception.InstanceOf() ); // Assert } + } - public class MessageContaining + public class WithMessageMatching + { + [Test] + public void WhenMessageMatches_ShouldNotThrow() { - [Test] - public void Throw_WithNoGenericType_WhenThrows_ShouldBeAbleToContinueWith_WithMessage_HappyPath() - { - // Arrange - var expected = GetRandomString(); - // Pre-Assert - // Act - Assert.DoesNotThrow( - () => - { - Expect( - () => - { - throw new Exception(expected); - } - ) - .To.Throw() - .With.Message.Containing(expected); - } - ); - // Assert - } + // Arrange + var msg = GetRandomString(); + var re = new Regex(msg); + // Pre-Assert - [Test] - public void Throw_WhenGettingMessageViaProperty_ShouldNotFailWhenHaveSoughtStringAtStartOfMessage() - { - // Arrange - var message = "LOOKFORME could not moo"; - var seek = "LOOKFORME"; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new Exception(message)) - .To.Throw() - .With.Property(e => e.Message) - .Containing(seek); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(() => throw new Exception(message)) - .To.Throw().With.Type(typeof(Exception)) - .And.Property(e => e.Message) - .Containing(seek); - }, - Throws.Nothing - ); - // Assert - } + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new Exception(msg); + } + ) + .To.Throw() + .With.Message.Matching(s => s == msg); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect( + () => + { + throw new Exception(msg); + } + ) + .To.Throw() + .With.Message.Matching(re); + }, + Throws.Nothing + ); - [Test] - public void ShouldIncludeStacktraceWhenFailing1() - { - // Arrange - var message = "LOOKFORME could not moo"; + // Assert + } - // Act - Expect( - () => - Expect(() => throw new Exception(message)) - .Not.To.Throw() - ).To.Throw() - .With.Message.Containing(message) - .Then("Stacktrace") - .Then(" at "); + [Test] + public void WhenMessageDoesNotMatch_ShouldThrow() + { + // Arrange + var msg = GetRandomString(); + // Pre-Assert - // Assert - } + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new Exception(GetAnother(msg)); + } + ) + .To.Throw() + .With.Message.Matching(s => s == msg); + }, + Throws.Exception.InstanceOf() + ); - [Test] - public void ShouldIncludeStacktraceWhenFailing2() - { - // Arrange - var message = "LOOKFORME could not moo"; + // Assert + } - // Act - Expect( - () => - Expect(() => throw new Exception(message)) - .To.Throw() - ).To.Throw() - .With.Message.Containing(message) - .Then("Stacktrace") - .Then(" at "); + [Test] + public void Negated_WhenMessageDoesNotMatch_ShouldThrow() + { + // Arrange + var msg = GetRandomString(); + // Pre-Assert - // Assert - } + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new Exception(GetAnother(msg)); + } + ) + .To.Throw() + .With.Message.Not.Matching(s => s == msg); + }, + Throws.Exception.InstanceOf() + ); - [Test] - public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_HappyPath() + // Assert + } + } + + [Test] + public void Throw_WithGenericType_WhenThrowsThatType_ShouldNotThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That( + () => { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(message); - } - ) - .To.Throw() - .With.Message.Containing(e1) - .And(e2); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void Throw_WithNoGenericType_WhenThrows_ShouldBeAbleToContinueWith_WithMessage_SadPath() - { - // Arrange - var expected = GetRandomString(); - var other = GetAnother(expected); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(other); - } - ) - .To.Throw() - .With.Message.Containing(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"to contain \"{expected}\"") - ); - // Assert - } - - [Test] - public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_SadPath() + Expect(() => throw new InvalidOperationException("moo")) + .To.Throw(); + }, + Throws.Nothing + ); + Assert.That( + () => { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var e3 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(message); - } - ) - .To.Throw() - .With.Message.Containing(e1) - .And(e3); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(e3) - ); - // Assert - } + Expect(() => throw new InvalidOperationException("moo")) + .To.Throw("Custom message"); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_SadPathNegated() - { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var e3 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(message); - } - ) - .To.Throw() - .With.Message.Containing(e1) - .And.Not.Containing(e3); - }, - Throws.Nothing - ); - // Assert - } + public class GenericMessageContaining + { + [Test] + public void Throw_WithGenericType_AllowsMultipleSubStringContainingOnMessage_SadPath() + { + // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var e3 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new InvalidOperationException(message); + } + ) + .To.Throw() + .With.Message.Containing(e1) + .And(e3); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(e3) + ); - [Test] - public void Throw_WithNoGenericType_AllowsMultipleSubStringContainingOnMessage_HappyPathNegated() - { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(message); - } - ) - .To.Throw() - .With.Message.Not.Containing(e1) - .And(e2); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + // Assert } - public class WithMessageMatching + [Test] + public void ShouldIncludeCustomMessage() { - [Test] - public void WhenMessageMatches_ShouldNotThrow() - { - // Arrange - var msg = GetRandomString(); - var re = new Regex(msg); - // Pre-Assert + // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); + var customMessage = $"{GetRandomString()} (custom message)"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new InvalidOperationException(message); + } + ) + .To.Throw(customMessage); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(customMessage) + ); + // Assert + } - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(msg); - } - ) - .To.Throw() - .With.Message.Matching(s => s == msg); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(msg); - } - ) - .To.Throw() - .With.Message.Matching(re); - }, - Throws.Nothing - ); + [Test] + public void Throw_WithGenericType_AllowsMultipleSubStringContainingOnMessage_SadPathNegated() + { + // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var e3 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new ArgumentNullException(message); + } + ) + .To.Throw() + .With.Message.Containing(e1) + .And.Not.Containing(e3); + }, + Throws.Nothing + ); + // Assert + } - // Assert - } + [Test] + public void Throw_WithGenericType_AllowsMultipleSubStringContainingOnMessage_HappyPathNegated() + { + // Arrange + var e1 = GetRandomString(); + var e2 = GetRandomString(); + var message = new[] { e1, e2 }.Randomize().JoinWith(" "); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new ArgumentOutOfRangeException(message); + } + ) + .To.Throw() + .With.Message.Not.Containing(e1) + .And(e2); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } - [Test] - public void WhenMessageDoesNotMatch_ShouldThrow() - { - // Arrange - var msg = GetRandomString(); - // Pre-Assert + [Test] + public void Throw_Negated_WithGenericType_WhenThrowsThatType_ShouldThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(() => throw new InvalidOperationException("moo")) + .Not.To.Throw(); + }, + Throws.Exception.InstanceOf() + .With.Message.Matches( + $"Expected not to throw an exception of type (System.|){typeof(InvalidOperationException).Name}" + ) + ); + // Assert + } - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(GetAnother(msg)); - } - ) - .To.Throw() - .With.Message.Matching(s => s == msg); - }, - Throws.Exception.InstanceOf() - ); + [Test] + public void Throw_WithGenericType_WhenThrowsAnotherTypeType_ShouldThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(() => throw new InvalidOperationException("moo")) + .To.Throw(); + }, + Throws.Exception.InstanceOf() + .With.Message.Matches( + $"Expected to throw an exception of type (System.|){typeof(NotImplementedException).Name} but {typeof(InvalidOperationException).Name} was thrown instead" + ) + ); + // Assert + } - // Assert - } + [Test] + public void Throw_WithGenericType_ShouldContinueOnToMessageTest_HappyPath() + { + // Arrange + var expected = GetRandomString(); - [Test] - public void Negated_WhenMessageDoesNotMatch_ShouldThrow() - { - // Arrange - var msg = GetRandomString(); - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new Exception(GetAnother(msg)); - } - ) - .To.Throw() - .With.Message.Not.Matching(s => s == msg); - }, - Throws.Exception.InstanceOf() - ); + // Act + Assert.That( + () => + { + Expect(() => throw new AccessViolationException(expected)) + .To.Throw() + .With.Message.Containing(expected); + }, + Throws.Nothing + ); - // Assert - } + // Assert } + } + [TestFixture] + public class ShouldBeAbleToTestAgainstExceptionTypeArgument + { [Test] - public void Throw_WithGenericType_WhenThrowsThatType_ShouldNotThrow() + public void ShouldNotFailWhenDoesThrow() { // Arrange - // Pre-Assert // Act Assert.That( () => { - Expect(() => throw new InvalidOperationException("moo")) - .To.Throw(); + var message = GetRandomString(); + Expect(() => throw new LocalException(message)) + .To.Throw() + .With.Type(typeof(LocalException)) + .And.Message.Equal.To(message); }, Throws.Nothing ); Assert.That( () => { - Expect(() => throw new InvalidOperationException("moo")) - .To.Throw("Custom message"); + var message = GetRandomString(); + Expect(() => throw new LocalException(message)) + .To.Throw() + .With.Type(typeof(LocalException)) + .And.Property(e => (e as LocalException).MyMessage == message); }, Throws.Nothing ); // Assert } - public class GenericMessageContaining + [Test] + public void ShouldFailWhenDoesNotThrowCorrectType() { - [Test] - public void Throw_WithGenericType_AllowsMultipleSubStringContainingOnMessage_SadPath() - { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var e3 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new InvalidOperationException(message); - } - ) - .To.Throw() - .With.Message.Containing(e1) - .And(e3); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(e3) - ); - - // Assert - } + // Arrange + // Act + Assert.That( + () => + { + var message = GetRandomString(); + Expect(() => throw new Exception(message)) + .To.Throw() + .With.Type(typeof(LocalException)) + .And.Message.Equal.To(message); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } - [Test] - public void ShouldIncludeCustomMessage() - { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - var customMessage = $"{GetRandomString()} (custom message)"; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new InvalidOperationException(message); - } - ) - .To.Throw(customMessage); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(customMessage) - ); - // Assert - } + [Test] + public void ShouldFailWhenDoesNotThrow() + { + // Arrange + // Act + Assert.That( + () => + { + var message = GetRandomString(); + Expect( + () => + { + } + ) + .To.Throw() + .With.Type(typeof(LocalException)) + .And.Message.Equal.To(message); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } - [Test] - public void Throw_WithGenericType_AllowsMultipleSubStringContainingOnMessage_SadPathNegated() + public class LocalException : InvalidOperationException + { + public string MyMessage { get; } + + public LocalException( + string message + ) : base(message) { - // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var e3 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new ArgumentNullException(message); - } - ) - .To.Throw() - .With.Message.Containing(e1) - .And.Not.Containing(e3); - }, - Throws.Nothing - ); - // Assert + MyMessage = message; } + } + } + + [TestFixture] + public class ExceptionMessages + { + [Test] + public void ShouldNotThrowWhenMessageIsExactMatch() + { + // Arrange + var expected = GetRandomString(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(() => throw new AccessViolationException(expected)) + .To.Throw() + .With.Message.Equal.To(expected); + }, + Throws.Nothing + ); + // alt. syntax - for the lazy! (like me) + Assert.That( + () => + { + Expect(() => throw new AccessViolationException(expected)) + .To.Throw() + .With.Message(expected); + }, + Throws.Nothing + ); + + // Assert + } + + [Test] + public void ShouldThrowWhenMessageIsNotExactMatch() + { + // Arrange + var expected = GetRandomString(); + var unexpected = GetAnother(expected); + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(() => throw new AccessViolationException(unexpected)) + .To.Throw() + .With.Message.Equal.To(expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected\n\"{unexpected}\"\nto equal\n\"{expected}\"") + ); + + // Assert + } + } + + + [TestFixture] + public class GenericProperty + { + [Test] + public void ShouldContinueOnToEqualTo() + { + // Arrange + var expected = GetRandomString(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(() => throw new ArgumentNullException(expected)) + .To.Throw() + .With.Property(ex => ex.ParamName) + .Equal.To(expected); + }, + Throws.Nothing + ); + + // Assert + } + + [Test] + public void ShouldContinueToNegatedEqualTo() + { + // Arrange + var expected = GetRandomString(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(() => throw new ArgumentNullException(expected)) + .To.Throw() + .With.Property(ex => ex.ParamName) + .Not.Equal.To(GetAnother(expected)); + }, + Throws.Nothing + ); + + // Assert + } + + [Test] + public void ShouldThrowWhenExtractedPropertyDoesNotMatch() + { + // Arrange + var expected = GetRandomString(); + var unexpected = GetAnother(expected); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(() => throw new ArgumentNullException(unexpected)) + .To.Throw() + .With.Property(ex => ex.ParamName) + .Equal.To(expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected\n\"{unexpected}\"\nto equal\n\"{expected}\"") + ); + // Assert + } + + [Test] + public void ShouldNotThrowWhenExtractedPropertyContainsExpectedSubstring() + { + // Arrange + var expected = GetRandomString( + 8, + 8 + ); + var expectedSubstring = expected.Substring( + 2, + 4 + ); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(() => throw new ArgumentNullException(expected)) + .To.Throw() + .With.Property(ex => ex.ParamName) + .Containing(expectedSubstring); + }, + Throws.Nothing + ); + + // Assert + } + + [Test] + public void ShouldNotThrowWhenExtractedPropertyContainsExpectedSubstrings() + { + // Arrange + var expected = GetRandomString( + 10, + 20 + ); + var expectedSubstring = expected.Substring( + 2, + 4 + ); + var next = expected.Substring( + 6, + 4 + ); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(() => throw new ArgumentNullException(expected)) + .To.Throw() + .With.Property(ex => ex.ParamName) + .Containing(expectedSubstring) + .Then(next); + }, + Throws.Nothing + ); + + // Assert + } + + [TestFixture] + public class EndingWith + { [Test] - public void Throw_WithGenericType_AllowsMultipleSubStringContainingOnMessage_HappyPathNegated() + public void HappyPath() { // Arrange - var e1 = GetRandomString(); - var e2 = GetRandomString(); - var message = new[] { e1, e2 }.Randomize().JoinWith(" "); - // Pre-Assert + var message = GetRandomString( + 10, + 20 + ); + var search = message.Substring(10); + // Pre-assert // Act Assert.That( () => { - Expect( - () => - { - throw new ArgumentOutOfRangeException(message); - } - ) - .To.Throw() - .With.Message.Not.Containing(e1) - .And(e2); + Expect(() => throw new InvalidOperationException(message)) + .To.Throw() + .With.Message.Ending.With(search); }, - Throws.Exception.InstanceOf() + Throws.Nothing ); // Assert } [Test] - public void Throw_Negated_WithGenericType_WhenThrowsThatType_ShouldThrow() + public void SadPath() { // Arrange - // Pre-Assert + var message = GetRandomString( + 10, + 20 + ); + var search = GetAnother(message.Substring(10)); + // Pre-assert // Act Assert.That( () => { - Expect(() => throw new InvalidOperationException("moo")) - .Not.To.Throw(); + Expect(() => throw new InvalidOperationException(message)) + .To.Throw() + .With.Message.Ending.With(search); }, Throws.Exception.InstanceOf() - .With.Message.Matches( - $"Expected not to throw an exception of type (System.|){typeof(InvalidOperationException).Name}" + .With.Message.Contains( + $"\"{message}\" to end with \"{search}\"" ) ); // Assert } [Test] - public void Throw_WithGenericType_WhenThrowsAnotherTypeType_ShouldThrow() + public void Negated_HappyPath() { // Arrange - // Pre-Assert + var message = GetRandomString( + 10, + 20 + ); + var search = GetAnother(message.Substring(10)); + // Pre-assert // Act Assert.That( () => { - Expect(() => throw new InvalidOperationException("moo")) - .To.Throw(); + Expect(() => throw new InvalidOperationException(message)) + .To.Throw() + .With.Message.Not.Ending.With(search); }, - Throws.Exception.InstanceOf() - .With.Message.Matches( - $"Expected to throw an exception of type (System.|){typeof(NotImplementedException).Name} but {typeof(InvalidOperationException).Name} was thrown instead" - ) + Throws.Nothing ); // Assert } + } + [TestFixture] + public class StartingWith + { [Test] - public void Throw_WithGenericType_ShouldContinueOnToMessageTest_HappyPath() + public void HappyPath() { // Arrange - var expected = GetRandomString(); - - // Pre-Assert - + var message = GetRandomString( + 10, + 20 + ); + var search = message.Substring( + 0, + 5 + ); + var next = message.Substring( + 5, + 5 + ); + // Pre-assert // Act Assert.That( () => { - Expect(() => throw new AccessViolationException(expected)) - .To.Throw() - .With.Message.Containing(expected); + Expect(() => throw new OverflowException(message)) + .To.Throw() + .With.Message.Starting.With(search) + .Then(next); }, Throws.Nothing ); - // Assert } - } - [TestFixture] - public class ShouldBeAbleToTestAgainstExceptionTypeArgument - { [Test] - public void ShouldNotFailWhenDoesThrow() + public void HappyPathWithComparer() { // Arrange - // Act - Assert.That( - () => - { - var message = GetRandomString(); - Expect(() => throw new LocalException(message)) - .To.Throw() - .With.Type(typeof(LocalException)) - .And.Message.Equal.To(message); - }, - Throws.Nothing + var message = GetRandomString( + 10, + 20 + ); + var search = message.Substring( + 0, + 5 ); + var next = message.Substring( + 5, + 5 + ); + // Pre-assert + // Act Assert.That( () => { - var message = GetRandomString(); - Expect(() => throw new LocalException(message)) - .To.Throw() - .With.Type(typeof(LocalException)) - .And.Property(e => (e as LocalException).MyMessage == message); + Expect(() => throw new OverflowException(message)) + .To.Throw() + .With.Message.Starting.With( + search.ToUpper(), + StringComparison.OrdinalIgnoreCase + ).Then( + next.ToUpper(), + StringComparison.OrdinalIgnoreCase + ); }, Throws.Nothing ); @@ -700,1060 +1100,659 @@ public void ShouldNotFailWhenDoesThrow() } [Test] - public void ShouldFailWhenDoesNotThrowCorrectType() + public void SadPath() { // Arrange + var message = GetRandomString( + 10, + 20 + ); + var search = GetAnother( + message.Substring( + 0, + 10 + ) + ); + // Pre-assert // Act Assert.That( () => { - var message = GetRandomString(); - Expect(() => throw new Exception(message)) - .To.Throw() - .With.Type(typeof(LocalException)) - .And.Message.Equal.To(message); + Expect(() => throw new InvalidOperationException(message)) + .To.Throw() + .With.Message.Starting.With(search); }, Throws.Exception.InstanceOf() + .With.Message.Contains( + $"\"{message}\" to start with \"{search}\"" + ) ); // Assert } [Test] - public void ShouldFailWhenDoesNotThrow() + public void Negated_HappyPath() { // Arrange + var message = GetRandomString( + 10, + 20 + ); + var search = GetAnother( + message.Substring( + 0, + 10 + ) + ); + // Pre-assert // Act Assert.That( () => { - var message = GetRandomString(); - Expect( - () => - { - } - ) - .To.Throw() - .With.Type(typeof(LocalException)) - .And.Message.Equal.To(message); + Expect(() => throw new InvalidOperationException(message)) + .To.Throw() + .With.Message.Not.Starting.With(search); }, - Throws.Exception.InstanceOf() + Throws.Nothing ); // Assert } - - public class LocalException : InvalidOperationException - { - public string MyMessage { get; } - - public LocalException( - string message - ) : base(message) - { - MyMessage = message; - } - } } - [TestFixture] - public class ExceptionMessages + [Test] + public void + Throw_WithArgumentNullType_GivenParamNameProperty_ShouldContinueOnToPropertyTest_SadPath_TestingContains() { - [Test] - public void ShouldNotThrowWhenMessageIsExactMatch() - { - // Arrange - var expected = GetRandomString(); + // Arrange + var expected = GetRandomString(); + var unexpected = GetAnother(expected); + var unexpectedSubstring = GetRandomString( + 4, + 4 + ); - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => - { - Expect(() => throw new AccessViolationException(expected)) - .To.Throw() - .With.Message.Equal.To(expected); - }, - Throws.Nothing - ); - // alt. syntax - for the lazy! (like me) - Assert.That( - () => - { - Expect(() => throw new AccessViolationException(expected)) - .To.Throw() - .With.Message(expected); - }, - Throws.Nothing - ); - - // Assert - } + // Act + Assert.That( + () => + { + Expect(() => throw new ArgumentNullException(unexpected)) + .To.Throw() + .With.Property(ex => ex.ParamName) + .Containing(unexpectedSubstring); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected \"{unexpected}\" to contain \"{unexpectedSubstring}\"") + ); - [Test] - public void ShouldThrowWhenMessageIsNotExactMatch() - { - // Arrange - var expected = GetRandomString(); - var unexpected = GetAnother(expected); - // Pre-Assert + // Assert + } - // Act - Assert.That( - () => - { - Expect(() => throw new AccessViolationException(unexpected)) - .To.Throw() - .With.Message.Equal.To(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected\n\"{unexpected}\"\nto equal\n\"{expected}\"") - ); + public class ExceptionWithInts : Exception + { + public IEnumerable Ints { get; } - // Assert + public ExceptionWithInts( + IEnumerable ints + ) + { + Ints = ints; } } - - [TestFixture] - public class GenericProperty + [Test] + public void Throw_UsingProperty_ShouldDoCollectionComparisonOnCollections() { - [Test] - public void ShouldContinueOnToEqualTo() - { - // Arrange - var expected = GetRandomString(); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(expected)) - .To.Throw() - .With.Property(ex => ex.ParamName) - .Equal.To(expected); - }, - Throws.Nothing - ); + // Arrange + var expected = new[] { 1, 2 }; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect( + () => + { + throw new ExceptionWithInts(new[] { 1, 2 }); + } + ) + .To.Throw() + .With.CollectionProperty(e => e.Ints) + .Equal.To(expected); + }, + Throws.Nothing + ); + // Assert + } - // Assert - } + public class SomeExtendedNode : SomeNode + { + public DateTime Created { get; set; } + } - [Test] - public void ShouldContinueToNegatedEqualTo() + [Test] + public void ExceptionPropertyCollectionDeepEqualityTesting() + { + // Arrange + var expected = new SomeNode() { - // Arrange - var expected = GetRandomString(); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(expected)) - .To.Throw() - .With.Property(ex => ex.ParamName) - .Not.Equal.To(GetAnother(expected)); - }, - Throws.Nothing - ); - - // Assert - } - - [Test] - public void ShouldThrowWhenExtractedPropertyDoesNotMatch() + Id = 1, + Name = "Moo" + }; + var test = new[] { - // Arrange - var expected = GetRandomString(); - var unexpected = GetAnother(expected); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(unexpected)) - .To.Throw() - .With.Property(ex => ex.ParamName) - .Equal.To(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected\n\"{unexpected}\"\nto equal\n\"{expected}\"") - ); - - // Assert - } + new SomeNode() + { + Id = 1, + Name = "Moo" + } + }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(() => throw new ExceptionWithNode(expected)) + .To.Throw() + .With.CollectionProperty(e => e.Nodes) + .Deep.Equal.To(test); + }, + Throws.Nothing + ); + } - [Test] - public void ShouldNotThrowWhenExtractedPropertyContainsExpectedSubstring() + [Test] + public void ExceptionPropertyCollectionEquivalenceTesting() + { + // Arrange + var expected = new SomeNode() { - // Arrange - var expected = GetRandomString( - 8, - 8 - ); - var expectedSubstring = expected.Substring( - 2, - 4 - ); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(expected)) - .To.Throw() - .With.Property(ex => ex.ParamName) - .Containing(expectedSubstring); - }, - Throws.Nothing - ); - - // Assert - } - - [Test] - public void ShouldNotThrowWhenExtractedPropertyContainsExpectedSubstrings() + Id = 1, + Name = "Moo" + }; + var test = new[] { - // Arrange - var expected = GetRandomString( - 10, - 20 - ); - var expectedSubstring = expected.Substring( - 2, - 4 - ); - var next = expected.Substring( - 6, - 4 - ); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(expected)) - .To.Throw() - .With.Property(ex => ex.ParamName) - .Containing(expectedSubstring) - .Then(next); - }, - Throws.Nothing - ); + new SomeNode() + { + Id = 1, + Name = "Moo" + } + }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(() => throw new ExceptionWithNode(expected)) + .To.Throw() + .With.CollectionProperty(e => e.Nodes) + .Equivalent.To(test); + }, + Throws.Nothing + ); + } - // Assert - } + [Test] + public void ExceptionPropertyIntersectionEqualityTesting() + { + // Arrange + var expected = new SomeExtendedNode() + { + Id = 1, + Name = "Moo", + Created = DateTime.Now + }; + var test = new[] + { + new SomeNode() + { + Id = 1, + Name = "Moo" + } + }; + // Pre-assert + // Act + Assert.That( + () => + { + Expect(() => throw new ExceptionWithNode(expected)) + .To.Throw() + .With.CollectionProperty(e => e.Nodes) + .Intersection.Equal.To(test); + }, + Throws.Nothing + ); + } + } + [TestFixture] + public class WithGenericPropertyFetcher + { + [TestFixture] + public class WithArgumentNullType + { [TestFixture] - public class EndingWith + public class GivenParamNameProperty { [Test] - public void HappyPath() + public void Throw_ShouldContinueOnToPropertyTest_HappyPath_TestingEqualTo() { // Arrange - var message = GetRandomString( - 10, - 20 - ); - var search = message.Substring(10); - // Pre-assert + var expected = GetRandomString(); + + // Pre-Assert + // Act Assert.That( () => { - Expect(() => throw new InvalidOperationException(message)) - .To.Throw() - .With.Message.Ending.With(search); + Expect(() => throw new ArgumentNullException(expected)) + .To.Throw() + .With(ex => ex.ParamName) + .Equal.To(expected); }, Throws.Nothing ); + // Assert } [Test] - public void SadPath() + public void ContinuingOnToPropertyTestWithNullPropertyValue_ShouldThrowForCorrectReason() { // Arrange - var message = GetRandomString( - 10, - 20 - ); - var search = GetAnother(message.Substring(10)); // Pre-assert // Act Assert.That( () => { - Expect(() => throw new InvalidOperationException(message)) - .To.Throw() - .With.Message.Ending.With(search); + Expect(() => throw new ArgumentException("message, forgetting paramName")) + .To.Throw() + .With.Property(e => e.ParamName) + .Equal.To("expectedParameter"); }, Throws.Exception.InstanceOf() - .With.Message.Contains( - $"\"{message}\" to end with \"{search}\"" - ) + .With.Message.Not.Contains("Object reference not set to an instance of an object") ); // Assert } [Test] - public void Negated_HappyPath() + public void Throw_ShouldContinueOnToPropertyTest_SadPath_TestingEqualTo() { // Arrange - var message = GetRandomString( - 10, - 20 - ); - var search = GetAnother(message.Substring(10)); - // Pre-assert + var expected = GetRandomString(); + var unexpected = GetAnother(expected); + + // Pre-Assert + // Act Assert.That( () => { - Expect(() => throw new InvalidOperationException(message)) - .To.Throw() - .With.Message.Not.Ending.With(search); + Expect(() => throw new ArgumentNullException(unexpected)) + .To.Throw() + .With(ex => ex.ParamName) + .Equal.To(expected); }, - Throws.Nothing + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected\n\"{unexpected}\"\nto equal\n\"{expected}\"") ); + // Assert } } + } + } - [TestFixture] - public class StartingWith - { - [Test] - public void HappyPath() + [TestFixture] + public class AsyncFunctions + { + private async Task ThrowStuffAction() + { + await Task.Run( + () => { - // Arrange - var message = GetRandomString( - 10, - 20 - ); - var search = message.Substring( - 0, - 5 - ); - var next = message.Substring( - 5, - 5 - ); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new OverflowException(message)) - .To.Throw() - .With.Message.Starting.With(search) - .Then(next); - }, - Throws.Nothing - ); - // Assert } + ); + throw new InvalidOperationException("moo cows"); + } - [Test] - public void HappyPathWithComparer() + private async Task ThrowStuffFunc() + { + await Task.Run( + () => { - // Arrange - var message = GetRandomString( - 10, - 20 - ); - var search = message.Substring( - 0, - 5 - ); - var next = message.Substring( - 5, - 5 - ); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new OverflowException(message)) - .To.Throw() - .With.Message.Starting.With( - search.ToUpper(), - StringComparison.OrdinalIgnoreCase - ).Then( - next.ToUpper(), - StringComparison.OrdinalIgnoreCase - ); - }, - Throws.Nothing - ); - // Assert } + ); + throw new InvalidOperationException("moo cows"); + } - [Test] - public void SadPath() - { - // Arrange - var message = GetRandomString( - 10, - 20 - ); - var search = GetAnother( - message.Substring( - 0, - 10 - ) - ); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new InvalidOperationException(message)) - .To.Throw() - .With.Message.Starting.With(search); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"\"{message}\" to start with \"{search}\"" - ) - ); - // Assert - } - - [Test] - public void Negated_HappyPath() - { - // Arrange - var message = GetRandomString( - 10, - 20 - ); - var search = GetAnother( - message.Substring( - 0, - 10 - ) - ); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new InvalidOperationException(message)) - .To.Throw() - .With.Message.Not.Starting.With(search); - }, - Throws.Nothing - ); - // Assert - } - } - - [Test] - public void - Throw_WithArgumentNullType_GivenParamNameProperty_ShouldContinueOnToPropertyTest_SadPath_TestingContains() - { - // Arrange - var expected = GetRandomString(); - var unexpected = GetAnother(expected); - var unexpectedSubstring = GetRandomString( - 4, - 4 - ); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(unexpected)) - .To.Throw() - .With.Property(ex => ex.ParamName) - .Containing(unexpectedSubstring); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected \"{unexpected}\" to contain \"{unexpectedSubstring}\"") - ); - - // Assert - } - - public class ExceptionWithInts : Exception - { - public IEnumerable Ints { get; } - - public ExceptionWithInts( - IEnumerable ints - ) - { - Ints = ints; - } - } - - [Test] - public void Throw_UsingProperty_ShouldDoCollectionComparisonOnCollections() - { - // Arrange - var expected = new[] { 1, 2 }; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect( - () => - { - throw new ExceptionWithInts(new[] { 1, 2 }); - } - ) - .To.Throw() - .With.CollectionProperty(e => e.Ints) - .Equal.To(expected); - }, - Throws.Nothing - ); - // Assert - } - - public class SomeExtendedNode : SomeNode - { - public DateTime Created { get; set; } - } - - [Test] - public void ExceptionPropertyCollectionDeepEqualityTesting() - { - // Arrange - var expected = new SomeNode() - { - Id = 1, - Name = "Moo" - }; - var test = new[] + [Test] + public void ShouldHandleAsyncActions() + { + // Arrange + // Pre-Assert + // Act + Assert.That( + () => { - new SomeNode() - { - Id = 1, - Name = "Moo" - } - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new ExceptionWithNode(expected)) - .To.Throw() - .With.CollectionProperty(e => e.Nodes) - .Deep.Equal.To(test); - }, - Throws.Nothing - ); - } + Expect(ThrowStuffAction) + .To.Throw() + .With.Message.Containing("moo cows"); + }, + Throws.Nothing + ); - [Test] - public void ExceptionPropertyCollectionEquivalenceTesting() - { - // Arrange - var expected = new SomeNode() - { - Id = 1, - Name = "Moo" - }; - var test = new[] + Assert.That( + () => { - new SomeNode() - { - Id = 1, - Name = "Moo" - } - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new ExceptionWithNode(expected)) - .To.Throw() - .With.CollectionProperty(e => e.Nodes) - .Equivalent.To(test); - }, - Throws.Nothing - ); - } + Expect(ThrowStuffAction) + .To.Throw(); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void ExceptionPropertyIntersectionEqualityTesting() - { - // Arrange - var expected = new SomeExtendedNode() + [Test] + public void ShouldHandleAsyncFuncs() + { + // Arrange + // Pre-Assert + // Act + Assert.That( + () => { - Id = 1, - Name = "Moo", - Created = DateTime.Now - }; - var test = new[] + Expect(ThrowStuffFunc) + .To.Throw(); + }, + Throws.Nothing + ); + Assert.That( + () => { - new SomeNode() - { - Id = 1, - Name = "Moo" - } - }; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new ExceptionWithNode(expected)) - .To.Throw() - .With.CollectionProperty(e => e.Nodes) - .Intersection.Equal.To(test); - }, - Throws.Nothing - ); - } + Expect(ThrowStuffFunc) + .To.Throw(); + }, + Throws.Nothing + ); + // Assert } + } - [TestFixture] - public class WithGenericPropertyFetcher + [TestFixture] + public class RippingOffComplexPropertiesOnExceptions + { + [Test] + public void ShouldBeAbleToDoAIntersectionEqual() { - [TestFixture] - public class WithArgumentNullType - { - [TestFixture] - public class GivenParamNameProperty + // Arrange + var expected = GetRandom(); + // Pre-assert + // Act + Assert.That( + () => { - [Test] - public void Throw_ShouldContinueOnToPropertyTest_HappyPath_TestingEqualTo() - { - // Arrange - var expected = GetRandomString(); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(expected)) - .To.Throw() - .With(ex => ex.ParamName) - .Equal.To(expected); - }, - Throws.Nothing - ); - - // Assert - } - - [Test] - public void ContinuingOnToPropertyTestWithNullPropertyValue_ShouldThrowForCorrectReason() - { - // Arrange - // Pre-assert - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentException("message, forgetting paramName")) - .To.Throw() - .With.Property(e => e.ParamName) - .Equal.To("expectedParameter"); - }, - Throws.Exception.InstanceOf() - .With.Message.Not.Contains("Object reference not set to an instance of an object") - ); - // Assert - } - - [Test] - public void Throw_ShouldContinueOnToPropertyTest_SadPath_TestingEqualTo() - { - // Arrange - var expected = GetRandomString(); - var unexpected = GetAnother(expected); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(() => throw new ArgumentNullException(unexpected)) - .To.Throw() - .With(ex => ex.ParamName) - .Equal.To(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected\n\"{unexpected}\"\nto equal\n\"{expected}\"") - ); - - // Assert - } - } - } + Expect( + () => throw new ExceptionWithComplex( + GetRandomString(1), + expected + ) + ) + .To.Throw() + .With.Property(e => e.Complex) + .Intersection.Equal.To(expected); + }, + Throws.Nothing + ); + // Assert } - [TestFixture] - public class AsyncFunctions + [Test] + public void ShouldBeAbleToDoAIntersectionEqual_Fail() { - private async Task ThrowStuffAction() - { - await Task.Run( - () => - { - } - ); - throw new InvalidOperationException("moo cows"); - } - - private async Task ThrowStuffFunc() - { - await Task.Run( - () => - { - } - ); - throw new InvalidOperationException("moo cows"); - } - - [Test] - public void ShouldHandleAsyncActions() - { - // Arrange - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(ThrowStuffAction) - .To.Throw() - .With.Message.Containing("moo cows"); - }, - Throws.Nothing - ); - - Assert.That( - () => - { - Expect(ThrowStuffAction) - .To.Throw(); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldHandleAsyncFuncs() - { - // Arrange - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(ThrowStuffFunc) - .To.Throw(); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(ThrowStuffFunc) - .To.Throw(); - }, - Throws.Nothing - ); - // Assert - } + // Arrange + var expected = GetRandom(); + var unexpected = GetRandom(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect( + () => throw new ExceptionWithComplex( + GetRandomString(1), + expected + ) + ) + .To.Throw() + .With.Property(e => e.Complex) + .Intersection.Equal.To(unexpected); + }, + Throws.Exception.InstanceOf() + ); + // Assert } - [TestFixture] - public class RippingOffComplexPropertiesOnExceptions + [Test] + public void ShouldBeAbleToDoAIntersectionEqual_Negated() { - [Test] - public void ShouldBeAbleToDoAIntersectionEqual() - { - // Arrange - var expected = GetRandom(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect( - () => throw new ExceptionWithComplex( - GetRandomString(1), - expected - ) - ) - .To.Throw() - .With.Property(e => e.Complex) - .Intersection.Equal.To(expected); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void ShouldBeAbleToDoAIntersectionEqual_Fail() - { - // Arrange - var expected = GetRandom(); - var unexpected = GetRandom(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect( - () => throw new ExceptionWithComplex( - GetRandomString(1), - expected - ) + // Arrange + var expected = GetRandom(); + var unexpected = GetRandom(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect( + () => throw new ExceptionWithComplex( + GetRandomString(1), + expected ) - .To.Throw() - .With.Property(e => e.Complex) - .Intersection.Equal.To(unexpected); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + ) + .To.Throw() + .With.Property(e => e.Complex) + .Not.Intersection.Equal.To(unexpected); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void ShouldBeAbleToDoAIntersectionEqual_Negated() - { - // Arrange - var expected = GetRandom(); - var unexpected = GetRandom(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect( - () => throw new ExceptionWithComplex( - GetRandomString(1), - expected - ) + [Test] + public void ShouldBeAbleToDoADeepEqual() + { + // Arrange + var expected = GetRandom(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect( + () => throw new ExceptionWithComplex( + GetRandomString(1), + expected ) - .To.Throw() - .With.Property(e => e.Complex) - .Not.Intersection.Equal.To(unexpected); - }, - Throws.Nothing - ); - // Assert - } + ) + .To.Throw() + .With.Property(e => e.Complex) + .Deep.Equal.To(expected); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void ShouldBeAbleToDoADeepEqual() - { - // Arrange - var expected = GetRandom(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect( - () => throw new ExceptionWithComplex( - GetRandomString(1), - expected - ) + [Test] + public void ShouldBeAbleToDoADeepEqual_Fail() + { + // Arrange + var expected = GetRandom(); + var unexpected = GetRandom(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect( + () => throw new ExceptionWithComplex( + GetRandomString(1), + expected ) - .To.Throw() - .With.Property(e => e.Complex) - .Deep.Equal.To(expected); - }, - Throws.Nothing - ); - // Assert - } + ) + .To.Throw() + .With.Property(e => e.Complex) + .Deep.Equal.To(unexpected); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } - [Test] - public void ShouldBeAbleToDoADeepEqual_Fail() - { - // Arrange - var expected = GetRandom(); - var unexpected = GetRandom(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect( - () => throw new ExceptionWithComplex( - GetRandomString(1), - expected - ) + [Test] + public void ShouldBeAbleToDoADeepEqual_Negated() + { + // Arrange + var expected = GetRandom(); + var unexpected = GetRandom(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect( + () => throw new ExceptionWithComplex( + GetRandomString(1), + expected ) - .To.Throw() - .With.Property(e => e.Complex) - .Deep.Equal.To(unexpected); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + ) + .To.Throw() + .With.Property(e => e.Complex) + .Not.Deep.Equal.To(unexpected); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void ShouldBeAbleToDoADeepEqual_Negated() - { - // Arrange - var expected = GetRandom(); - var unexpected = GetRandom(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect( - () => throw new ExceptionWithComplex( - GetRandomString(1), - expected - ) - ) - .To.Throw() - .With.Property(e => e.Complex) - .Not.Deep.Equal.To(unexpected); - }, - Throws.Nothing - ); - // Assert - } + public class Complex + { + public int Id { get; set; } + public string Name { get; set; } + } - public class Complex - { - public int Id { get; set; } - public string Name { get; set; } - } + public class ExceptionWithComplex : Exception + { + public Complex Complex { get; set; } - public class ExceptionWithComplex : Exception + public ExceptionWithComplex( + string message, + Complex complex + ) : base(message) { - public Complex Complex { get; set; } - - public ExceptionWithComplex( - string message, - Complex complex - ) : base(message) - { - Complex = complex; - } + Complex = complex; } } + } - [TestFixture] - public class ConvenienceSyntaxForArgumentException + [TestFixture] + public class ConvenienceSyntaxForArgumentException + { + [Test] + public void ShouldHaveShorthandToVerifyParameterName() { - [Test] - public void ShouldHaveShorthandToVerifyParameterName() - { - // Arrange - var expected = GetRandomString(4); - // Act - Assert.That( - () => - { - Expect( - () => throw new ArgumentException( - "message here", - expected - ) + // Arrange + var expected = GetRandomString(4); + // Act + Assert.That( + () => + { + Expect( + () => throw new ArgumentException( + "message here", + expected ) - .To.Throw() - .For(expected) - .With.Message.Containing("message here"); - }, - Throws.Nothing - ); + ) + .To.Throw() + .For(expected) + .With.Message.Containing("message here"); + }, + Throws.Nothing + ); - Assert.That( - () => - { - Expect( - () => throw new ArgumentException( - "message", - expected - ) + Assert.That( + () => + { + Expect( + () => throw new ArgumentException( + "message", + expected ) - .Not.To.Throw() - .For(expected); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + ) + .Not.To.Throw() + .For(expected); + }, + Throws.Exception.InstanceOf() + ); + // Assert } } +} - public class ExceptionWithNode : Exception - { - public SomeNode[] Nodes { get; set; } +public class ExceptionWithNode : Exception +{ + public SomeNode[] Nodes { get; set; } - public ExceptionWithNode( - params SomeNode[] nodes - ) - : base($"{nodes[0].Id} / {nodes[0].Name}") - { - Nodes = nodes; - } + public ExceptionWithNode( + params SomeNode[] nodes + ) + : base($"{nodes[0].Id} / {nodes[0].Name}") + { + Nodes = nodes; } +} - public class SomeNode - { - public int Id { get; set; } - public string Name { get; set; } +public class SomeNode +{ + public int Id { get; set; } + public string Name { get; set; } - public override bool Equals( - object obj - ) + public override bool Equals( + object obj + ) + { + var other = obj as SomeNode; + if (other == null) { - var other = obj as SomeNode; - if (other == null) - { - return false; - } - - return Id == other.Id && - Name == other.Name; + return false; } - protected bool Equals( - SomeNode other - ) - { - return Id == other.Id && - String.Equals( - Name, - other.Name - ); - } + return Id == other.Id && + Name == other.Name; + } - public override int GetHashCode() + protected bool Equals( + SomeNode other + ) + { + return Id == other.Id && + String.Equals( + Name, + other.Name + ); + } + + public override int GetHashCode() + { + unchecked { - unchecked - { - return (Id * 397) ^ (Name != null - ? Name.GetHashCode() - : 0); - } + return (Id * 397) ^ (Name != null + ? Name.GetHashCode() + : 0); } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Exceptions/UserSpaceImplementations.cs b/src/NExpect.Tests/Exceptions/UserSpaceImplementations.cs index 2e69fca7..ce7840b4 100644 --- a/src/NExpect.Tests/Exceptions/UserSpaceImplementations.cs +++ b/src/NExpect.Tests/Exceptions/UserSpaceImplementations.cs @@ -1,4 +1,4 @@ -using System; +using System; using NExpect.Interfaces; using NUnit.Framework; using PeanutButter.Utils; @@ -7,92 +7,91 @@ // ReSharper disable UnassignedGetOnlyAutoProperty // ReSharper disable MemberCanBePrivate.Global -namespace NExpect.Tests.Exceptions +namespace NExpect.Tests.Exceptions; + +[TestFixture] +public class UserSpaceImplementations { - [TestFixture] - public class UserSpaceImplementations + [Test] + public void ShouldPullExceptionPropertyWhenExists() { - [Test] - public void ShouldPullExceptionPropertyWhenExists() - { - // Arrange - var ex = new ArgumentNullException("moo"); - var subject = new MyContinuationWithExceptionProperty(ex); - // Pre-assert - // Act - var result = subject.With(e => e.ParamName); - // Assert - var actual = result.GetPropertyValue("Actual"); - Expect(actual).To.Equal("moo"); - } + // Arrange + var ex = new ArgumentNullException("moo"); + var subject = new MyContinuationWithExceptionProperty(ex); + // Pre-assert + // Act + var result = subject.With(e => e.ParamName); + // Assert + var actual = result.GetPropertyValue("Actual"); + Expect(actual).To.Equal("moo"); + } - [Test] - public void WhenNoExceptionProperty() + [Test] + public void WhenNoExceptionProperty() + { + // Arrange + var ex = new ArgumentNullException("moo"); + var subject = new MyContinuationWithoutExceptionProperty(ex); + // Pre-assert + // Act + Assert.That(() => { - // Arrange - var ex = new ArgumentNullException("moo"); - var subject = new MyContinuationWithoutExceptionProperty(ex); - // Pre-assert - // Act - Assert.That(() => - { - subject.With(e => e.ParamName); - }, Throws.Exception.InstanceOf() - .With.Message.Contain("something with an Exception property")); - // Assert - } + subject.With(e => e.ParamName); + }, Throws.Exception.InstanceOf() + .With.Message.Contain("something with an Exception property")); + // Assert + } - [Test] - public void WhenExceptionPropertyThrows() + [Test] + public void WhenExceptionPropertyThrows() + { + // Arrange + var ex = new ArgumentNullException("moo"); + var subject = new MyContinuationWithBrokenExceptionProperty(ex); + // Pre-assert + // Act + Assert.That(() => { - // Arrange - var ex = new ArgumentNullException("moo"); - var subject = new MyContinuationWithBrokenExceptionProperty(ex); - // Pre-assert - // Act - Assert.That(() => - { - subject.With(e => e.ParamName); - }, Throws.Exception.InstanceOf() - .With.Message.Contain("something with an Exception property")); - // Assert - } + subject.With(e => e.ParamName); + }, Throws.Exception.InstanceOf() + .With.Message.Contain("something with an Exception property")); + // Assert + } - public class MyContinuationWithExceptionProperty : IThrowContinuation where T : Exception + public class MyContinuationWithExceptionProperty : IThrowContinuation where T : Exception + { + public MyContinuationWithExceptionProperty(T exception) { - public MyContinuationWithExceptionProperty(T exception) - { - Exception = exception; - Id = Guid.NewGuid(); - } - - public IWithAfterThrowContinuation With { get; } - public T Exception { get; } - public Guid Id { get; } - public IExpectationContext Parent { get; } + Exception = exception; + Id = Guid.NewGuid(); } - public class MyContinuationWithBrokenExceptionProperty : IThrowContinuation where T : Exception - { - public MyContinuationWithBrokenExceptionProperty(T exception) - { - Id = Guid.NewGuid(); - } + public IWithAfterThrowContinuation With { get; } + public T Exception { get; } + public Guid Id { get; } + public IExpectationContext Parent { get; } + } - public IWithAfterThrowContinuation With { get; } - public T Exception => throw new NotImplementedException(); - public Guid Id { get; } - public IExpectationContext Parent { get; } + public class MyContinuationWithBrokenExceptionProperty : IThrowContinuation where T : Exception + { + public MyContinuationWithBrokenExceptionProperty(T exception) + { + Id = Guid.NewGuid(); } - public class MyContinuationWithoutExceptionProperty : IThrowContinuation where T : Exception - { - public MyContinuationWithoutExceptionProperty(T ex) - { - } + public IWithAfterThrowContinuation With { get; } + public T Exception => throw new NotImplementedException(); + public Guid Id { get; } + public IExpectationContext Parent { get; } + } - public IWithAfterThrowContinuation With { get; } - public IExpectationContext Parent { get; } + public class MyContinuationWithoutExceptionProperty : IThrowContinuation where T : Exception + { + public MyContinuationWithoutExceptionProperty(T ex) + { } + + public IWithAfterThrowContinuation With { get; } + public IExpectationContext Parent { get; } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/Issues.cs b/src/NExpect.Tests/Issues.cs index 11615f14..66ed082c 100644 --- a/src/NExpect.Tests/Issues.cs +++ b/src/NExpect.Tests/Issues.cs @@ -266,6 +266,23 @@ public void ShouldEnforceAbsoluteSizeOfCollection() // Assert } } + + [Test] + [Ignore("FIXME: when a uint?-targeting Expect is added, it breaks generated tests for more/less and numeric types")] + public void ShouldBeAbleToAssertAgainstNullableUint() + { + // // Arrange + // var expected = GetRandomInt(1); + // var value = (uint?)expected; + // // Act + // Assert.That(() => + // { + // Expect(value) + // .To.Equal(expected); + // }, Throws.Nothing); + // // Assert + } + } public static class CustomNonNExpectMatchers diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnDateTimes.cs b/src/NExpect.Tests/ObjectEquality/ActingOnDateTimes.cs index 695e3210..e46b1d9f 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnDateTimes.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnDateTimes.cs @@ -2,72 +2,71 @@ using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnDateTimes { - [TestFixture] - public class ActingOnDateTimes + [Test] + public void ShouldThrowWhenAttemptingToCompareDateTimesWithDifferentSpecifiedKinds() { - [Test] - public void ShouldThrowWhenAttemptingToCompareDateTimesWithDifferentSpecifiedKinds() - { - // Arrange - var utc = new DateTime( - 2020, - 8, - 5, - 13, - 56, - 37, - DateTimeKind.Utc); - var local = new DateTime( - 2020, - 8, - 5, - 15, - 56, - 37, - DateTimeKind.Local); - // Act - Assert.That(() => - Expect(local) - .To.Be.Greater.Than - .Or.Equal.To(utc), - Throws.Exception.InstanceOf() - .With.Message.Contains("have different kinds") - ); + // Arrange + var utc = new DateTime( + 2020, + 8, + 5, + 13, + 56, + 37, + DateTimeKind.Utc); + var local = new DateTime( + 2020, + 8, + 5, + 15, + 56, + 37, + DateTimeKind.Local); + // Act + Assert.That(() => + Expect(local) + .To.Be.Greater.Than + .Or.Equal.To(utc), + Throws.Exception.InstanceOf() + .With.Message.Contains("have different kinds") + ); - // Assert - } - - [Test] - public void ShouldNotThrowIfOneDateTimeKindIsUnSpecified() - { - // Arrange - var utc = new DateTime( - 2020, - 8, - 5, - 13, - 56, - 37, - DateTimeKind.Unspecified); - var local = new DateTime( - 2020, - 8, - 5, - 15, - 56, - 37, - DateTimeKind.Local); - // Act - Assert.That(() => - Expect(local) - .To.Be.Greater.Than - .Or.Equal.To(utc), - Throws.Nothing - ); + // Assert + } + + [Test] + public void ShouldNotThrowIfOneDateTimeKindIsUnSpecified() + { + // Arrange + var utc = new DateTime( + 2020, + 8, + 5, + 13, + 56, + 37, + DateTimeKind.Unspecified); + var local = new DateTime( + 2020, + 8, + 5, + 15, + 56, + 37, + DateTimeKind.Local); + // Act + Assert.That(() => + Expect(local) + .To.Be.Greater.Than + .Or.Equal.To(utc), + Throws.Nothing + ); - // Assert - } + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnDecimalComparedWithNullableDecimal.cs b/src/NExpect.Tests/ObjectEquality/ActingOnDecimalComparedWithNullableDecimal.cs index ab269d23..b09389ea 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnDecimalComparedWithNullableDecimal.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnDecimalComparedWithNullableDecimal.cs @@ -5,148 +5,147 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnDecimalComparedWithNullableDecimal { [TestFixture] - public class ActingOnDecimalComparedWithNullableDecimal + public class GreaterThan + { + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10); + var expected = GetRandomDecimal(0, 4) as decimal?; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual.Value).To.Be.Greater.Than(actual.Value); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10); + var expected = GetRandomDecimal(11, 20) as decimal?; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5); + var expected = GetRandomDecimal(-5, 0) as decimal?; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).Not.To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + } + + [TestFixture] + public class LessThan { - [TestFixture] - public class GreaterThan + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5); + var expected = GetRandomDecimal(6, 12) as decimal?; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10); - var expected = GetRandomDecimal(0, 4) as decimal?; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual.Value).To.Be.Greater.Than(actual.Value); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10); - var expected = GetRandomDecimal(11, 20) as decimal?; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5); - var expected = GetRandomDecimal(-5, 0) as decimal?; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).Not.To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = actual; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual.Value).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } - [TestFixture] - public class LessThan + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5); - var expected = GetRandomDecimal(6, 12) as decimal?; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = actual; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual.Value).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } - - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5); - var expected = GetRandomDecimal(-5, 0) as decimal?; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } + // Arrange + var actual = GetRandomDecimal(1, 5); + var expected = GetRandomDecimal(-5, 0) as decimal?; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnDecimals.cs b/src/NExpect.Tests/ObjectEquality/ActingOnDecimals.cs index 95588992..e52764ec 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnDecimals.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnDecimals.cs @@ -5,148 +5,147 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnDecimals { [TestFixture] - public class ActingOnDecimals + public class GreaterThan + { + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10); + var expected = GetRandomDecimal(0, 4); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(actual); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10); + var expected = GetRandomDecimal(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5); + var expected = GetRandomDecimal(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).Not.To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + } + + [TestFixture] + public class LessThan { - [TestFixture] - public class GreaterThan + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5); + var expected = GetRandomDecimal(6, 12); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10); - var expected = GetRandomDecimal(0, 4); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(actual); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10); - var expected = GetRandomDecimal(11, 20); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5); - var expected = GetRandomDecimal(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).Not.To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } + // Arrange + var actual = GetRandomDecimal(1, 5); + var expected = actual; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } - [TestFixture] - public class LessThan + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5); - var expected = GetRandomDecimal(6, 12); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5); - var expected = actual; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } - - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5); - var expected = GetRandomDecimal(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } + // Arrange + var actual = GetRandomDecimal(1, 5); + var expected = GetRandomDecimal(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnDoubles.cs b/src/NExpect.Tests/ObjectEquality/ActingOnDoubles.cs index bd33d38d..f3f0a8be 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnDoubles.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnDoubles.cs @@ -5,131 +5,130 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnDoubles { [TestFixture] - public class ActingOnDoubles + public class GreaterThan { - [TestFixture] - public class GreaterThan + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDouble(5, 10); + var expected = GetRandomDouble(0, 4); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDouble(5, 10); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(actual); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDouble(5, 10); + var expected = GetRandomDouble(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + } + + [TestFixture] + public class LessThan + { + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDouble(1, 5); + var expected = GetRandomDouble(6, 12); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDouble(5, 10); - var expected = GetRandomDouble(0, 4); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDouble(5, 10); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(actual); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDouble(5, 10); - var expected = GetRandomDouble(11, 20); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } + // Arrange + var actual = GetRandomDouble(1, 5); + var expected = actual; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } - [TestFixture] - public class LessThan + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDouble(1, 5); - var expected = GetRandomDouble(6, 12); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDouble(1, 5); - var expected = actual; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } - - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDouble(1, 5); - var expected = GetRandomDouble(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } + // Arrange + var actual = GetRandomDouble(1, 5); + var expected = GetRandomDouble(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnFloats.cs b/src/NExpect.Tests/ObjectEquality/ActingOnFloats.cs index 1811a8fc..bc8a03ff 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnFloats.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnFloats.cs @@ -5,131 +5,130 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnFloats { [TestFixture] - public class ActingOnFloats + public class GreaterThan { - [TestFixture] - public class GreaterThan + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomFloat(5, 10); + var expected = GetRandomFloat(0, 4); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomFloat(5, 10); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(actual); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomFloat(5, 10); + var expected = GetRandomFloat(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + } + + [TestFixture] + public class LessThan + { + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomFloat(1, 5); + var expected = GetRandomFloat(6, 12); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomFloat(5, 10); - var expected = GetRandomFloat(0, 4); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomFloat(5, 10); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(actual); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomFloat(5, 10); - var expected = GetRandomFloat(11, 20); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } + // Arrange + var actual = GetRandomFloat(1, 5); + var expected = actual; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{(double) actual} to be less than {expected}")); + // Assert } - [TestFixture] - public class LessThan + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomFloat(1, 5); - var expected = GetRandomFloat(6, 12); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomFloat(1, 5); - var expected = actual; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{(double) actual} to be less than {expected}")); - // Assert - } - - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomFloat(1, 5); - var expected = GetRandomFloat(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{(double) actual} to be less than {expected}")); - // Assert - } + // Arrange + var actual = GetRandomFloat(1, 5); + var expected = GetRandomFloat(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{(double) actual} to be less than {expected}")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnInts.cs b/src/NExpect.Tests/ObjectEquality/ActingOnInts.cs index 1eafbdd1..3d594a5b 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnInts.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnInts.cs @@ -8,1341 +8,1340 @@ // ReSharper disable UnusedMember.Global // ReSharper disable MemberHidesStaticFromOuterClass // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnInts { [TestFixture] - public class ActingOnInts + public class Greater { [TestFixture] - public class Greater + public class Than + { + [Test] + public void WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = 8; + var expected = 8; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("8 to be greater than 8")); + // Assert + } + + [Test] + public void WhenActualIsLessThanToExpected_ShouldThrow() + { + // Arrange + var actual = 7; + var expected = 8; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("7 to be greater than 8")); + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(5, 10); + var expected = RandomValueGen.GetRandomInt(0, 4); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDecimals() + { + // Arrange + decimal start = 0.6M; + decimal test = 0.5M; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDecimalsAndDouble() + { + // Arrange + decimal start = 0.6M; + double test = 0.5; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDoublesAndDecimal() + { + // Arrange + double start = 0.6; + decimal test = 0.5M; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDoublesAndFloat() + { + // Arrange + double start = 0.6; + float test = 0.5F; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDoublesAndLong() + { + // Arrange + double start = 0.6; + long test = -1; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithFloatAndDecimal() + { + // Arrange + float start = 0.6F; + decimal test = 0.5M; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithFloatAndLong() + { + // Arrange + double start = 0.6; + long test = 0; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithLongAndDecimal() + { + // Arrange + long start = 1; + decimal test = 0; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithLongAndDouble() + { + // Arrange + long start = 1; + double test = 0; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDecimalsAndFloat() + { + // Arrange + decimal start = 0.6M; + float test = 0.5F; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDecimalsAndInt() + { + // Arrange + decimal start = 0.6M; + int test = 0; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_ShouldWorkWithDecimalsAndLong() + { + // Arrange + decimal start = 0.6M; + long test = 0; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(start).To.Be.Greater.Than(test); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(5, 10); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(actual); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(5, 10); + var expected = RandomValueGen.GetRandomInt(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_Negated_WhenActualIsGreaterThanExpected_ShouldThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(5, 10); + var expected = RandomValueGen.GetRandomInt(1, 4); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).Not.To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void GreaterThan_AltNegated_WhenActualIsGreaterThanExpected_ShouldThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(5, 10); + var expected = RandomValueGen.GetRandomInt(1, 4); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Not.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } + + [TestFixture] + public class And { [TestFixture] - public class Than + public class Less { - [Test] - public void WhenActualIsEqualToExpected_ShouldThrow() + [TestFixture] + public class Than { - // Arrange - var actual = 8; - var expected = 8; - // Pre-Assert - // Act - Assert.That( - () => + [TestFixture] + public class HomogenousTypes + { + [TestFixture] + public class IntIntInt { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("8 to be greater than 8")); - // Assert - } + private (int min, int max, int actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } - [Test] - public void WhenActualIsLessThanToExpected_ShouldThrow() - { - // Arrange - var actual = 7; - var expected = 8; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("7 to be greater than 8")); - // Assert - } + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(5, 10); - var expected = RandomValueGen.GetRandomInt(0, 4); + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } - // Pre-Assert + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } - // Act - Assert.That( - () => + [TestFixture] + public class LongLongLong { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); + private (long min, long max, long actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } - // Assert - } + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } - [Test] - public void GreaterThan_ShouldWorkWithDecimals() - { - // Arrange - decimal start = 0.6M; - decimal test = 0.5M; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void GreaterThan_ShouldWorkWithDecimalsAndDouble() - { - // Arrange - decimal start = 0.6M; - double test = 0.5; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } - [Test] - public void GreaterThan_ShouldWorkWithDoublesAndDecimal() - { - // Arrange - double start = 0.6; - decimal test = 0.5M; - // Pre-Assert - // Act - Assert.That( - () => + [TestFixture] + public class DecimalDecimalDecimal { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + private (decimal min, decimal max, decimal actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } - [Test] - public void GreaterThan_ShouldWorkWithDoublesAndFloat() - { - // Arrange - double start = 0.6; - float test = 0.5F; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } - [Test] - public void GreaterThan_ShouldWorkWithDoublesAndLong() - { - // Arrange - double start = 0.6; - long test = -1; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void GreaterThan_ShouldWorkWithFloatAndDecimal() - { - // Arrange - float start = 0.6F; - decimal test = 0.5M; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } - [Test] - public void GreaterThan_ShouldWorkWithFloatAndLong() - { - // Arrange - double start = 0.6; - long test = 0; - // Pre-Assert - // Act - Assert.That( - () => + [TestFixture] + public class FloatFloatFloat { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + private (float min, float max, float actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } - [Test] - public void GreaterThan_ShouldWorkWithLongAndDecimal() - { - // Arrange - long start = 1; - decimal test = 0; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } - [Test] - public void GreaterThan_ShouldWorkWithLongAndDouble() - { - // Arrange - long start = 1; - double test = 0; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void GreaterThan_ShouldWorkWithDecimalsAndFloat() - { - // Arrange - decimal start = 0.6M; - float test = 0.5F; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } - [Test] - public void GreaterThan_ShouldWorkWithDecimalsAndInt() - { - // Arrange - decimal start = 0.6M; - int test = 0; - // Pre-Assert - // Act - Assert.That( - () => + [TestFixture] + public class DoubleDoubleDouble { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + private (double min, double max, double actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } - [Test] - public void GreaterThan_ShouldWorkWithDecimalsAndLong() - { - // Arrange - decimal start = 0.6M; - long test = 0; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(start).To.Be.Greater.Than(test); - }, - Throws.Nothing); - // Assert - } + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(5, 10); + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } - // Pre-Assert + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } - // Act - Assert.That( - () => + [TestFixture] + public class DateTimeDateTimeDateTime { - Expect(actual).To.Be.Greater.Than(actual); - }, - Throws.Exception.InstanceOf()); + private (DateTime min, DateTime max, DateTime actual) Source() + { + var min = RandomValueGen.GetRandomDate(new DateTime(2001, 1, 1)); + var max = RandomValueGen.GetRandomDate(new DateTime(2030, 1, 1)); + var actual = RandomValueGen.GetRandomDate( + min.AddMilliseconds(1), + max.AddMilliseconds(-1) + ); + return (min, max, actual); + } - // Assert - } + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(5, 10); - var expected = RandomValueGen.GetRandomInt(11, 20); + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } - // Pre-Assert + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } - // Act - Assert.That( - () => + [Test] + public void ShouldTakeDateTimeKindIntoAccount() + { + // Arrange + var src = RandomValueGen.GetRandomDate(); + var local = + new DateTime( + src.Year, + src.Month, + src.Day, + src.Hour, + src.Minute, + src.Second, + DateTimeKind.Local); + var utc = + new DateTime( + src.Year, + src.Month, + src.Day, + src.Hour, + src.Minute, + src.Second, + DateTimeKind.Utc); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(local).To.Equal(utc); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } + } + + [TestFixture] + public class HeterogeneousTypes + { + [TestFixture] + public class DoubleDoubleDecimal { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); + private (double min, double max, decimal actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } - // Assert - } + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } - [Test] - public void GreaterThan_Negated_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(5, 10); - var expected = RandomValueGen.GetRandomInt(1, 4); - // Pre-Assert - // Act - Assert.That( - () => + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } + + [TestFixture] + public class LongLongDecimal { - Expect(actual).Not.To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - // Assert - } + private (long min, long max, decimal actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } - [Test] - public void GreaterThan_AltNegated_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(5, 10); - var expected = RandomValueGen.GetRandomInt(1, 4); - // Pre-Assert - // Act - Assert.That( - () => + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } + + [TestFixture] + public class DecimalDecimalLong { - Expect(actual).To.Not.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - // Assert - } - } + private (decimal min, decimal max, long actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } + + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } - [TestFixture] - public class And - { - [TestFixture] - public class Less - { - [TestFixture] - public class Than - { [TestFixture] - public class HomogenousTypes + public class DoubleDoubleLong { - [TestFixture] - public class IntIntInt + private (double min, double max, long actual) Source() + { + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); + } + + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() { - private (int min, int max, int actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert } - [TestFixture] - public class LongLongLong + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() { - private (long min, long max, long actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert } - [TestFixture] - public class DecimalDecimalDecimal + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() { - private (decimal min, decimal max, decimal actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert } + } - [TestFixture] - public class FloatFloatFloat + [TestFixture] + public class DecimalDecimalDouble + { + private (decimal min, decimal max, double actual) Source() { - private (float min, float max, float actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); } - [TestFixture] - public class DoubleDoubleDouble + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() { - private (double min, double max, double actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert } - [TestFixture] - public class DateTimeDateTimeDateTime + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() { - private (DateTime min, DateTime max, DateTime actual) Source() - { - var min = RandomValueGen.GetRandomDate(new DateTime(2001, 1, 1)); - var max = RandomValueGen.GetRandomDate(new DateTime(2030, 1, 1)); - var actual = RandomValueGen.GetRandomDate( - min.AddMilliseconds(1), - max.AddMilliseconds(-1) - ); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void ShouldTakeDateTimeKindIntoAccount() - { - // Arrange - var src = RandomValueGen.GetRandomDate(); - var local = - new DateTime( - src.Year, - src.Month, - src.Day, - src.Hour, - src.Minute, - src.Second, - DateTimeKind.Local); - var utc = - new DateTime( - src.Year, - src.Month, - src.Day, - src.Hour, - src.Minute, - src.Second, - DateTimeKind.Utc); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(local).To.Equal(utc); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert } } [TestFixture] - public class HeterogeneousTypes + public class LongLongDouble { - [TestFixture] - public class DoubleDoubleDecimal + private (long min, long max, double actual) Source() { - private (double min, double max, decimal actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); } - [TestFixture] - public class LongLongDecimal + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() { - private (long min, long max, decimal actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert } - [TestFixture] - public class DecimalDecimalLong + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() { - private (decimal min, decimal max, long actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert } - [TestFixture] - public class DoubleDoubleLong + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() + { + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert + } + } + + [TestFixture] + public class IntDoubleFloat + { + private (int min, double max, float actual) Source() { - private (double min, double max, long actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + var min = RandomValueGen.GetRandomInt(1, 5); + var max = RandomValueGen.GetRandomInt(10, 15); + var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); + return (min, max, actual); } - [TestFixture] - public class DecimalDecimalDouble + [Test] + public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() { - private (decimal min, decimal max, double actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Nothing); + // Assert } - [TestFixture] - public class LongLongDouble + [Test] + public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() { - private (long min, long max, double actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert } - [TestFixture] - public class IntDoubleFloat + [Test] + public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() { - private (int min, double max, float actual) Source() - { - var min = RandomValueGen.GetRandomInt(1, 5); - var max = RandomValueGen.GetRandomInt(10, 15); - var actual = RandomValueGen.GetRandomInt(min + 1, max - 1); - return (min, max, actual); - } - - [Test] - public void PositiveExpectation_WhenIntsWithinRange_ShouldNotThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void NegativeExpectation_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .Not.To.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void NegativeExpectation_AltSyntax_WhenIntsWithinRange_ShouldThrow() - { - // Arrange - var (min, max, actual) = Source(); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Not.Be.Greater.Than(min) - .And.Less.Than(max); - }, - Throws.Exception.InstanceOf()); - // Assert - } + // Arrange + var (min, max, actual) = Source(); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Not.Be.Greater.Than(min) + .And.Less.Than(max); + }, + Throws.Exception.InstanceOf()); + // Assert } } } } } } + } - [TestFixture] - public class LessThan + [TestFixture] + public class LessThan + { + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(1, 5); - var expected = RandomValueGen.GetRandomInt(6, 12); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } + // Arrange + var actual = RandomValueGen.GetRandomInt(1, 5); + var expected = RandomValueGen.GetRandomInt(6, 12); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(1, 5); - var expected = actual; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(1, 5); + var expected = actual; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert + } - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(1, 5); - var expected = RandomValueGen.GetRandomInt(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(1, 5); + var expected = RandomValueGen.GetRandomInt(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert + } - [Test] - public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = RandomValueGen.GetRandomInt(1, 5); - var expected = RandomValueGen.GetRandomInt(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).Not.To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } + [Test] + public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = RandomValueGen.GetRandomInt(1, 5); + var expected = RandomValueGen.GetRandomInt(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).Not.To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimalComparedWithNonNullableDecimal.cs b/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimalComparedWithNonNullableDecimal.cs index c6a97edb..9eedbd02 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimalComparedWithNonNullableDecimal.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimalComparedWithNonNullableDecimal.cs @@ -5,148 +5,147 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnNullableDecimalComparedWithNonNullableDecimal { [TestFixture] - public class ActingOnNullableDecimalComparedWithNonNullableDecimal + public class GreaterThan + { + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + var expected = GetRandomDecimal(0, 4); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(actual.Value); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + var expected = GetRandomDecimal(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = GetRandomDecimal(-5, 0) as decimal?; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).Not.To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + } + + [TestFixture] + public class LessThan { - [TestFixture] - public class GreaterThan + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = GetRandomDecimal(6, 12) as decimal?; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - var expected = GetRandomDecimal(0, 4); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(actual.Value); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - var expected = GetRandomDecimal(11, 20); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = GetRandomDecimal(-5, 0) as decimal?; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).Not.To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = actual; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } - [TestFixture] - public class LessThan + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = GetRandomDecimal(6, 12) as decimal?; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = actual; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } - - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = GetRandomDecimal(-5, 0) as decimal?; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = GetRandomDecimal(-5, 0) as decimal?; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimals.cs b/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimals.cs index bd1487e7..f5f1ca1b 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimals.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnNullableDecimals.cs @@ -6,229 +6,228 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnNullableDecimals { [TestFixture] - public class ActingOnNullableDecimals + public class GreaterThan { - [TestFixture] - public class GreaterThan + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - var expected = GetRandomDecimal(0, 4) as decimal?; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(actual); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - var expected = GetRandomDecimal(11, 20) as decimal?; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = GetRandomDecimal(-5, 0) as decimal?; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).Not.To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + var expected = GetRandomDecimal(0, 4) as decimal?; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert } - - [TestFixture] - public class GreaterThanLong + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(actual); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + var expected = GetRandomDecimal(11, 20) as decimal?; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = GetRandomDecimal(-5, 0) as decimal?; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).Not.To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + } + + [TestFixture] + public class GreaterThanLong + { + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + var expected = GetRandomLong(0, 4); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = Convert.ToDecimal(GetRandomLong(5, 10)) as decimal?; + var expected = (long)actual; + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomDecimal(5, 10) as decimal?; + var expected = GetRandomLong(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = GetRandomLong(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).Not.To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + } + + [TestFixture] + public class LessThan + { + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = GetRandomLong(6, 12); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - var expected = GetRandomLong(0, 4); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = Convert.ToDecimal(GetRandomLong(5, 10)) as decimal?; - var expected = (long)actual; - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(5, 10) as decimal?; - var expected = GetRandomLong(11, 20); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void LessThan_Negated_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = GetRandomLong(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).Not.To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } + // Arrange + var actual = Convert.ToDecimal(GetRandomLong(1, 5)) as decimal?; + var expected = actual.Value; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } - [TestFixture] - public class LessThan + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = GetRandomLong(6, 12); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = Convert.ToDecimal(GetRandomLong(1, 5)) as decimal?; - var expected = actual.Value; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } - - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomDecimal(1, 5) as decimal?; - var expected = GetRandomLong(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{actual} to be less than {expected}")); - // Assert - } + // Arrange + var actual = GetRandomDecimal(1, 5) as decimal?; + var expected = GetRandomLong(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{actual} to be less than {expected}")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/ActingOnTimeSpans.cs b/src/NExpect.Tests/ObjectEquality/ActingOnTimeSpans.cs index 3e61d256..756dc612 100644 --- a/src/NExpect.Tests/ObjectEquality/ActingOnTimeSpans.cs +++ b/src/NExpect.Tests/ObjectEquality/ActingOnTimeSpans.cs @@ -5,131 +5,130 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global // ReSharper disable RedundantArgumentDefaultValue -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class ActingOnTimeSpans { [TestFixture] - public class ActingOnTimeSpans + public class GreaterThan { - [TestFixture] - public class GreaterThan + [Test] + public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomTimeSpan(21, 30); + var expected = GetRandomTimeSpan(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomTimeSpan(); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(actual); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() + { + // Arrange + var actual = GetRandomTimeSpan(5, 10); + var expected = GetRandomTimeSpan(11, 20); + + // Pre-Assert + + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Greater.Than(expected); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + } + + [TestFixture] + public class LessThan + { + [Test] + public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() + { + // Arrange + var actual = GetRandomTimeSpan(1, 5); + var expected = GetRandomTimeSpan(6, 12); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() { - [Test] - public void GreaterThan_WhenActualIsGreaterThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomTimeSpan(21, 30); - var expected = GetRandomTimeSpan(11, 20); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomTimeSpan(); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(actual); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void GreaterThan_WhenActualIsLessThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomTimeSpan(5, 10); - var expected = GetRandomTimeSpan(11, 20); - - // Pre-Assert - - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Greater.Than(expected); - }, - Throws.Exception.InstanceOf()); - - // Assert - } + // Arrange + var actual = GetRandomFloat(1, 5); + var expected = actual; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{(double) actual} to be less than {expected}")); + // Assert } - [TestFixture] - public class LessThan + [Test] + public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() { - [Test] - public void LessThan_WhenActualIsLessThanExpected_ShouldNotThrow() - { - // Arrange - var actual = GetRandomTimeSpan(1, 5); - var expected = GetRandomTimeSpan(6, 12); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void LessThan_WhenActualIsEqualToExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomFloat(1, 5); - var expected = actual; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{(double) actual} to be less than {expected}")); - // Assert - } - - [Test] - public void LessThan_WhenActualIsGreaterThanExpected_ShouldThrow() - { - // Arrange - var actual = GetRandomFloat(1, 5); - var expected = GetRandomFloat(-5, 0); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual).To.Be.Less.Than(expected); - }, - Throws.Exception - .InstanceOf() - .With.Message.Contains($"{(double) actual} to be less than {expected}")); - // Assert - } + // Arrange + var actual = GetRandomFloat(1, 5); + var expected = GetRandomFloat(-5, 0); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual).To.Be.Less.Than(expected); + }, + Throws.Exception + .InstanceOf() + .With.Message.Contains($"{(double) actual} to be less than {expected}")); + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/MultipleIComparableTypes.cs b/src/NExpect.Tests/ObjectEquality/MultipleIComparableTypes.cs index 4d148bd1..e54537ea 100644 --- a/src/NExpect.Tests/ObjectEquality/MultipleIComparableTypes.cs +++ b/src/NExpect.Tests/ObjectEquality/MultipleIComparableTypes.cs @@ -6,405 +6,404 @@ using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +// TODO: something similar for .To.Equal() +[TestFixture] +public class MultipleIComparableTypes { - // TODO: something similar for .To.Equal() - [TestFixture] - public class MultipleIComparableTypes - { - private static Type NullableType = typeof(Nullable<>); + private static Type NullableType = typeof(Nullable<>); - private static readonly Type[] NonNullableNumerics = new[] - { - typeof(char), - typeof(byte), - typeof(int), - typeof(uint), - typeof(long), - typeof(ulong), - typeof(float), - typeof(double), - typeof(decimal) - }; + private static readonly Type[] NonNullableNumerics = new[] + { + typeof(char), + typeof(byte), + typeof(int), + typeof(uint), + typeof(long), + typeof(ulong), + typeof(float), + typeof(double), + typeof(decimal) + }; + + private static readonly Type[] NullableNumerics + = NonNullableNumerics.Select(n => NullableType.MakeGenericType(n)) + .ToArray(); + + private static readonly Type[] Numerics = + NonNullableNumerics.And(NullableNumerics); + + // basically runs all permutations of numeric Greater.Than / Less.Than invocations + public static IEnumerable NumericsGenerator() + { + return GenerateTestCasesFor(Numerics, 1, 2) + .Select(o => new GeneratedTestData(o)); + ; + } - private static readonly Type[] NullableNumerics - = NonNullableNumerics.Select(n => NullableType.MakeGenericType(n)) - .ToArray(); + public static IEnumerable TimeSpansGenerator() + { + return GenerateTestCasesFor(new[] { typeof(TimeSpan) }, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)) + .Select(o => new GeneratedTestData(o)); + } - private static readonly Type[] Numerics = - NonNullableNumerics.And(NullableNumerics); + public class GeneratedTestData + { + public (string dotted, object actual, object expected, bool shouldPass) TestParameters { get; } - // basically runs all permutations of numeric Greater.Than / Less.Than invocations - public static IEnumerable NumericsGenerator() + public GeneratedTestData( + (string dotted, object actual, object expected, bool shouldPass) testParameters + ) { - return GenerateTestCasesFor(Numerics, 1, 2) - .Select(o => new GeneratedTestData(o)); - ; + TestParameters = testParameters; } - public static IEnumerable TimeSpansGenerator() + public override string ToString() { - return GenerateTestCasesFor(new[] { typeof(TimeSpan) }, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)) - .Select(o => new GeneratedTestData(o)); + return + $"{(TestParameters.shouldPass ? "✔" : "❌")} Expect({TestParameters.actual}).{TestParameters.dotted}({TestParameters.expected})"; } + } - public class GeneratedTestData + private static object Coerce(object value, Type type) + { + if (type.IsGenericType) { - public (string dotted, object actual, object expected, bool shouldPass) TestParameters { get; } - - public GeneratedTestData( - (string dotted, object actual, object expected, bool shouldPass) testParameters - ) + var genericType = type.GetGenericTypeDefinition(); + if (genericType != NullableType) { - TestParameters = testParameters; + throw new InvalidOperationException($"Dunno what to do with {value} => {type}"); } - public override string ToString() - { - return - $"{(TestParameters.shouldPass ? "✔" : "❌")} Expect({TestParameters.actual}).{TestParameters.dotted}({TestParameters.expected})"; - } + var underlyingType = type.GetGenericArguments()[0]; + var newValue = Convert.ChangeType(value, underlyingType); + var generic = typeof(MultipleIComparableTypes).GetMethod(nameof(MakeNullable), + BindingFlags.NonPublic | BindingFlags.Static); + var specific = generic.MakeGenericMethod(underlyingType); + return specific.Invoke(null, new object[] { newValue }); } - private static object Coerce(object value, Type type) - { - if (type.IsGenericType) - { - var genericType = type.GetGenericTypeDefinition(); - if (genericType != NullableType) - { - throw new InvalidOperationException($"Dunno what to do with {value} => {type}"); - } - - var underlyingType = type.GetGenericArguments()[0]; - var newValue = Convert.ChangeType(value, underlyingType); - var generic = typeof(MultipleIComparableTypes).GetMethod(nameof(MakeNullable), - BindingFlags.NonPublic | BindingFlags.Static); - var specific = generic.MakeGenericMethod(underlyingType); - return specific.Invoke(null, new object[] { newValue }); - } - - return Convert.ChangeType(value, type); - } + return Convert.ChangeType(value, type); + } + // ReSharper disable once ConvertNullableToShortForm + private static Nullable MakeNullable(T value) where T : struct + { // ReSharper disable once ConvertNullableToShortForm - private static Nullable MakeNullable(T value) where T : struct + return new Nullable(value); + } + + private static IEnumerable<( + string dotted, + object actual, + object expected, + bool shouldPass + )> GenerateTestCasesFor( + Type[] types, + T minValue, + T maxValue) + { + foreach (var actualType in types) { - // ReSharper disable once ConvertNullableToShortForm - return new Nullable(value); + foreach (var expectedType in types) + { + yield return ( + "To.Be.Less.Than", + Coerce(minValue, actualType), + Coerce(maxValue, expectedType), + true + ); + yield return ( + "To.Be.Less.Than", + Coerce(maxValue, actualType), + Coerce(minValue, expectedType), + false + ); + + yield return ( + "To.Be.Greater.Than", + Coerce(maxValue, expectedType), + Coerce(minValue, actualType), + true + ); + yield return ( + "To.Be.Greater.Than", + Coerce(minValue, actualType), + Coerce(maxValue, expectedType), + false + ); + + yield return ( + "To.Be.Greater.Than.Or.Equal.To", + Coerce(maxValue, actualType), + Coerce(minValue, expectedType), + true + ); + yield return ( + "To.Be.Greater.Than.Or.Equal.To", + Coerce(maxValue, actualType), + Coerce(maxValue, expectedType), + true + ); + yield return ( + "To.Be.Greater.Than.Or.Equal.To", + Coerce(minValue, actualType), + Coerce(maxValue, expectedType), + false + ); + yield return ( + "To.Be.At.Least", + Coerce(maxValue, actualType), + Coerce(minValue, expectedType), + true + ); + yield return ( + "To.Be.At.Least", + Coerce(maxValue, actualType), + Coerce(maxValue, expectedType), + true + ); + yield return ( + "To.Be.At.Least", + Coerce(minValue, actualType), + Coerce(maxValue, expectedType), + false + ); + + + yield return ( + "To.Be.Less.Than.Or.Equal.To", + Coerce(minValue, actualType), + Coerce(maxValue, expectedType), + true + ); + yield return ( + "To.Be.Less.Than.Or.Equal.To", + Coerce(maxValue, actualType), + Coerce(maxValue, actualType), + true + ); + yield return ( + "To.Be.Less.Than.Or.Equal.To", + Coerce(maxValue, actualType), + Coerce(minValue, expectedType), + false + ); + yield return ( + "To.Be.At.Most", + Coerce(minValue, actualType), + Coerce(maxValue, expectedType), + true + ); + yield return ( + "To.Be.At.Most", + Coerce(maxValue, actualType), + Coerce(maxValue, actualType), + true + ); + yield return ( + "To.Be.At.Most", + Coerce(maxValue, actualType), + Coerce(minValue, expectedType), + false + ); + } } + } - private static IEnumerable<( - string dotted, - object actual, - object expected, - bool shouldPass - )> GenerateTestCasesFor( - Type[] types, - T minValue, - T maxValue) - { - foreach (var actualType in types) + [TestCaseSource(nameof(NumericsGenerator))] + [TestCaseSource(nameof(TimeSpansGenerator))] + [Test] + public void _( + GeneratedTestData generatedTestData + ) + { + // Arrange + var testCase = generatedTestData.TestParameters; + // Act + var actualType = testCase.actual.GetType(); + var expectedType = testCase.expected.GetType(); + var expectMethods = typeof(Expectations).GetMethods(BindingFlags.Public | BindingFlags.Static); + var expectation = expectMethods + .Select(mi => { - foreach (var expectedType in types) + if (mi.Name != "Expect") { - yield return ( - "To.Be.Less.Than", - Coerce(minValue, actualType), - Coerce(maxValue, expectedType), - true - ); - yield return ( - "To.Be.Less.Than", - Coerce(maxValue, actualType), - Coerce(minValue, expectedType), - false - ); - - yield return ( - "To.Be.Greater.Than", - Coerce(maxValue, expectedType), - Coerce(minValue, actualType), - true - ); - yield return ( - "To.Be.Greater.Than", - Coerce(minValue, actualType), - Coerce(maxValue, expectedType), - false - ); - - yield return ( - "To.Be.Greater.Than.Or.Equal.To", - Coerce(maxValue, actualType), - Coerce(minValue, expectedType), - true - ); - yield return ( - "To.Be.Greater.Than.Or.Equal.To", - Coerce(maxValue, actualType), - Coerce(maxValue, expectedType), - true - ); - yield return ( - "To.Be.Greater.Than.Or.Equal.To", - Coerce(minValue, actualType), - Coerce(maxValue, expectedType), - false - ); - yield return ( - "To.Be.At.Least", - Coerce(maxValue, actualType), - Coerce(minValue, expectedType), - true - ); - yield return ( - "To.Be.At.Least", - Coerce(maxValue, actualType), - Coerce(maxValue, expectedType), - true - ); - yield return ( - "To.Be.At.Least", - Coerce(minValue, actualType), - Coerce(maxValue, expectedType), - false - ); - - - yield return ( - "To.Be.Less.Than.Or.Equal.To", - Coerce(minValue, actualType), - Coerce(maxValue, expectedType), - true - ); - yield return ( - "To.Be.Less.Than.Or.Equal.To", - Coerce(maxValue, actualType), - Coerce(maxValue, actualType), - true - ); - yield return ( - "To.Be.Less.Than.Or.Equal.To", - Coerce(maxValue, actualType), - Coerce(minValue, expectedType), - false - ); - yield return ( - "To.Be.At.Most", - Coerce(minValue, actualType), - Coerce(maxValue, expectedType), - true - ); - yield return ( - "To.Be.At.Most", - Coerce(maxValue, actualType), - Coerce(maxValue, actualType), - true - ); - yield return ( - "To.Be.At.Most", - Coerce(maxValue, actualType), - Coerce(minValue, expectedType), - false - ); + return null; } - } - } - [TestCaseSource(nameof(NumericsGenerator))] - [TestCaseSource(nameof(TimeSpansGenerator))] - [Test] - public void _( - GeneratedTestData generatedTestData - ) + try + { + return mi.Invoke(null, new[] { testCase.actual }); + } + catch (Exception) + { + return null; + } + }) + .FirstOrDefault(o => o != null); + if (expectation is null) { - // Arrange - var testCase = generatedTestData.TestParameters; - // Act - var actualType = testCase.actual.GetType(); - var expectedType = testCase.expected.GetType(); - var expectMethods = typeof(Expectations).GetMethods(BindingFlags.Public | BindingFlags.Static); - var expectation = expectMethods - .Select(mi => + // try invoke the generic one + expectation = expectMethods.Select( + mi => { - if (mi.Name != "Expect") + if (mi.Name != "Expect" || !mi.IsGenericMethod) { return null; } try { - return mi.Invoke(null, new[] { testCase.actual }); + var genericExpect = mi.MakeGenericMethod(actualType); + return genericExpect.Invoke(null, new[] { testCase.actual }); } - catch (Exception) + catch { return null; } - }) - .FirstOrDefault(o => o != null); - if (expectation is null) - { - // try invoke the generic one - expectation = expectMethods.Select( - mi => - { - if (mi.Name != "Expect" || !mi.IsGenericMethod) - { - return null; - } + }).FirstOrDefault(o => o != null); + } - try - { - var genericExpect = mi.MakeGenericMethod(actualType); - return genericExpect.Invoke(null, new[] { testCase.actual }); - } - catch - { - return null; - } - }).FirstOrDefault(o => o != null); - } + if (expectation == null) + { + Assert.Fail($"Can't start Expectation for {testCase.actual} ({testCase.actual.GetType()})"); + } - if (expectation == null) - { - Assert.Fail($"Can't start Expectation for {testCase.actual} ({testCase.actual.GetType()})"); - } + var dottedParts = testCase.dotted.Split('.'); + var final = dottedParts.Last(); + var last = expectation; + var queue = new Queue(dottedParts.Take(dottedParts.Length - 1)); + while (queue.Count > 0) + { + last = last.GetPropertyValue(queue.Dequeue()); + } - var dottedParts = testCase.dotted.Split('.'); - var final = dottedParts.Last(); - var last = expectation; - var queue = new Queue(dottedParts.Take(dottedParts.Length - 1)); - while (queue.Count > 0) - { - last = last.GetPropertyValue(queue.Dequeue()); - } + var matcherTypes = new[] + { + typeof(GreaterThanMatchers), + typeof(GreaterThanOrEqualMatchers), + typeof(LessThanMatchers), + typeof(LessThanOrEqualMatchers), + typeof(AtLeastMatchers), + typeof(AtMostMatchers) + }; + + var extMethods = matcherTypes.Select(t => t.GetMethods()) + .SelectMany(o => o) + .Where(mi => mi.Name == final && mi.GetParameters().Length == 2) + .ToArray(); - var matcherTypes = new[] + var ext = extMethods + .Where(mi => !mi.IsGenericMethod) + .Select(mi => { - typeof(GreaterThanMatchers), - typeof(GreaterThanOrEqualMatchers), - typeof(LessThanMatchers), - typeof(LessThanOrEqualMatchers), - typeof(AtLeastMatchers), - typeof(AtMostMatchers) - }; - - var extMethods = matcherTypes.Select(t => t.GetMethods()) - .SelectMany(o => o) - .Where(mi => mi.Name == final && mi.GetParameters().Length == 2) - .ToArray(); - - var ext = extMethods - .Where(mi => !mi.IsGenericMethod) - .Select(mi => + try { - try + mi.Invoke(null, new object[] { last, testCase.expected }); + return mi; + } + catch (Exception ex) + { + if (ex.InnerException != null && ex.InnerException is UnmetExpectationException) { - mi.Invoke(null, new object[] { last, testCase.expected }); return mi; } - catch (Exception ex) - { - if (ex.InnerException != null && ex.InnerException is UnmetExpectationException) - { - return mi; - } - var foo = ex; - // invocation fails -- no implicit upcast, perhaps - return null; - } - }).FirstOrDefault(o => o != null); + var foo = ex; + // invocation fails -- no implicit upcast, perhaps + return null; + } + }).FirstOrDefault(o => o != null); - if (ext is null) - { - // try find the generic with T1, T2 - var lastType = last.GetType(); - var lastGenericType = lastType.GetGenericTypeDefinition(); - var lastGenericTypeArgs = lastGenericType.GetGenericArguments(); - ext = extMethods - .Where(mi => + if (ext is null) + { + // try find the generic with T1, T2 + var lastType = last.GetType(); + var lastGenericType = lastType.GetGenericTypeDefinition(); + var lastGenericTypeArgs = lastGenericType.GetGenericArguments(); + ext = extMethods + .Where(mi => + { + if (!mi.IsGenericMethod) { - if (!mi.IsGenericMethod) - { - return false; - } + return false; + } - var methodParameters = mi.GetParameters(); - if (methodParameters.Length != 2) - { - return false; - } + var methodParameters = mi.GetParameters(); + if (methodParameters.Length != 2) + { + return false; + } - var firstParameterType = methodParameters[0].ParameterType; - if (!firstParameterType.IsGenericType) - { - return false; - } + var firstParameterType = methodParameters[0].ParameterType; + if (!firstParameterType.IsGenericType) + { + return false; + } - var firstArgGenericArgs = firstParameterType.GetGenericArguments(); - if (firstArgGenericArgs.Length != lastGenericTypeArgs.Length) - { - return false; - } + var firstArgGenericArgs = firstParameterType.GetGenericArguments(); + if (firstArgGenericArgs.Length != lastGenericTypeArgs.Length) + { + return false; + } - var firstArgGenericType = firstParameterType.GetGenericTypeDefinition(); - var concreted = firstArgGenericType.MakeGenericType(lastGenericTypeArgs); - return lastGenericType.Implements(concreted); - }) - .Select(mi => + var firstArgGenericType = firstParameterType.GetGenericTypeDefinition(); + var concreted = firstArgGenericType.MakeGenericType(lastGenericTypeArgs); + return lastGenericType.Implements(concreted); + }) + .Select(mi => + { + MethodInfo genericExt; + try { - MethodInfo genericExt; - try - { - genericExt = mi.MakeGenericMethod( - last.GetType().GetGenericArguments()[0], - expectedType - ); - } - catch - { - return null; - } + genericExt = mi.MakeGenericMethod( + last.GetType().GetGenericArguments()[0], + expectedType + ); + } + catch + { + return null; + } - try + try + { + genericExt.Invoke(null, new[] { last, testCase.expected }); + return genericExt; + } + catch (Exception ex) + { + if (ex.InnerException != null && + ex.InnerException is UnmetExpectationException) { - genericExt.Invoke(null, new[] { last, testCase.expected }); return genericExt; } - catch (Exception ex) - { - if (ex.InnerException != null && - ex.InnerException is UnmetExpectationException) - { - return genericExt; - } - return null; - } - }).FirstOrDefault(o => o != null); - } + return null; + } + }).FirstOrDefault(o => o != null); + } - if (ext == null) - { - Assert.Fail($"Can't find .{final} to operate on {last.GetType()} with expected type {expectedType}"); - } + if (ext == null) + { + Assert.Fail($"Can't find .{final} to operate on {last.GetType()} with expected type {expectedType}"); + } - // Assert - if (testCase.shouldPass) - { - Assert.That( - () => ext.Invoke(last, new object[] { last, testCase.expected }), - Throws.Nothing); - } - else - { - Assert.That( - () => ext.Invoke(last, new object[] { last, testCase.expected }), - Throws.InstanceOf() - .With.InnerException.InstanceOf()); - } + // Assert + if (testCase.shouldPass) + { + Assert.That( + () => ext.Invoke(last, new object[] { last, testCase.expected }), + Throws.Nothing); + } + else + { + Assert.That( + () => ext.Invoke(last, new object[] { last, testCase.expected }), + Throws.InstanceOf() + .With.InnerException.InstanceOf()); } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Safety.cs b/src/NExpect.Tests/ObjectEquality/Safety.cs index 2f0e8b81..6fca6c1b 100644 --- a/src/NExpect.Tests/ObjectEquality/Safety.cs +++ b/src/NExpect.Tests/ObjectEquality/Safety.cs @@ -1,72 +1,71 @@ -using System; +using System; using NUnit.Framework; // ReSharper disable SuspiciousTypeConversion.Global // ReSharper disable ReturnValueOfPureMethodIsNotUsed -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class Safety { - [TestFixture] - public class Safety + [OneTimeSetUp] + public void OneTimeSetup() { - [OneTimeSetUp] - public void OneTimeSetup() - { - Assertions.DisableTracking(); - } + Assertions.DisableTracking(); + } - [OneTimeTearDown] - public void OneTimeTeardown() - { - Assertions.EnableTracking(); - } + [OneTimeTearDown] + public void OneTimeTeardown() + { + Assertions.EnableTracking(); + } - [Test] - public void To_Equals_ShouldThrow() + [Test] + public void To_Equals_ShouldThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - // Pre-Assert - // Act - Assert.That(() => - { - Expect(1).To.Equals(1); - }, Throws.Exception.InstanceOf() - .With.Message.Contains( - "You probably intend to use .Equal(), not .Equals()" - )); - // Assert - } + Expect(1).To.Equals(1); + }, Throws.Exception.InstanceOf() + .With.Message.Contains( + "You probably intend to use .Equal(), not .Equals()" + )); + // Assert + } - [Test] - public void ToAfterNot_Equals_ShouldThrow() + [Test] + public void ToAfterNot_Equals_ShouldThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - // Pre-Assert - // Act - Assert.That(() => - { - Expect(1).Not.To.Equals(1); - }, Throws.Exception.InstanceOf() - .With.Message.Contains( - "You probably intend to use .Equal(), not .Equals()" - )); - // Assert - } + Expect(1).Not.To.Equals(1); + }, Throws.Exception.InstanceOf() + .With.Message.Contains( + "You probably intend to use .Equal(), not .Equals()" + )); + // Assert + } - [Test] - public void NotAfterTo_Equals_ShouldThrow() + [Test] + public void NotAfterTo_Equals_ShouldThrow() + { + // Arrange + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - // Pre-Assert - // Act - Assert.That(() => - { - Expect(1).To.Not.Equals(1); - }, Throws.Exception.InstanceOf() - .With.Message.Contains( - "You probably intend to use .Equal(), not .Equals()" - )); - // Assert - } + Expect(1).To.Not.Equals(1); + }, Throws.Exception.InstanceOf() + .With.Message.Contains( + "You probably intend to use .Equal(), not .Equals()" + )); + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/Containing.cs b/src/NExpect.Tests/ObjectEquality/Strings/Containing.cs index fee2052f..724af380 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/Containing.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/Containing.cs @@ -2,327 +2,326 @@ using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class Containing { [TestFixture] - public class Containing + public class To { [TestFixture] - public class To + public class Contain { + [Test] + public void WhenActualContainsSearch_ShouldNotThrow() + { + // Arrange + var actual = "cow-moo-cow"; + var search = "moo"; + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(actual).To.Contain(search); + }, + Throws.Nothing); + + Assert.That(() => + { + Expect(actual).To.Contain(search) + .And + .To.Contain("-"); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void WhenActualDoesNotContainSearch_ShouldThrow() + { + // Arrange + var actual = "cow-moo-cow"; + var search = "foo"; + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(actual).To.Contain(search); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("Expected\n\"cow-moo-cow\"\nto contain\n\"foo\"")); + + // Assert + } + + [Test] + public void WhenActualDoesNotContainSearch_ShouldThrowWithCustomMessage() + { + // Arrange + var actual = "cow-moo-cow"; + var search = "foo"; + var expected = GetRandomString(2); + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(actual).To.Contain(search, expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected)); + + // Assert + } + [TestFixture] - public class Contain + public class And { [Test] - public void WhenActualContainsSearch_ShouldNotThrow() + public void WhenActualContainsBothBits_ShouldNotThrow() { // Arrange - var actual = "cow-moo-cow"; - var search = "moo"; + var actual = "a-b-c"; + var first = "a"; + var second = "b"; // Pre-Assert - // Act Assert.That(() => { - Expect(actual).To.Contain(search); + Expect(actual).To.Contain(first).And(second); }, Throws.Nothing); + // Assert + } + [Test] + public void WhenActualMissingFirstBit_ShouldThrow() + { + // Arrange + var actual = "a-b-c"; + var first = "d"; + var second = "b"; + // Pre-Assert + // Act Assert.That(() => { - Expect(actual).To.Contain(search) - .And - .To.Contain("-"); + Expect(actual).To.Contain(first).And(second); }, - Throws.Nothing); - + Throws.Exception.InstanceOf() + .With.Message.Contains("\"a-b-c\"\nto contain\n\"d\"")); // Assert } [Test] - public void WhenActualDoesNotContainSearch_ShouldThrow() + public void WhenActualMissingSecondBit_ShouldThrow() { // Arrange - var actual = "cow-moo-cow"; - var search = "foo"; + var actual = "a-b-c"; + var first = "b"; + var second = "f"; // Pre-Assert - // Act Assert.That(() => { - Expect(actual).To.Contain(search); + Expect(actual).To.Contain(first).And(second); }, Throws.Exception.InstanceOf() - .With.Message.Contains("Expected\n\"cow-moo-cow\"\nto contain\n\"foo\"")); - + .With.Message.Contains("\"a-b-c\"\nto contain\n\"f\"")); // Assert } [Test] - public void WhenActualDoesNotContainSearch_ShouldThrowWithCustomMessage() + public void WhenActualMissingSecondBit_ShouldThrowWithCustomMessage() { // Arrange - var actual = "cow-moo-cow"; - var search = "foo"; + var actual = "a-b-c"; + var first = "b"; + var second = "f"; var expected = GetRandomString(2); // Pre-Assert - // Act Assert.That(() => { - Expect(actual).To.Contain(search, expected); + Expect(actual).To.Contain(first).And(second, expected); }, Throws.Exception.InstanceOf() .With.Message.Contains(expected)); - // Assert } [TestFixture] - public class And + public class AndAgain { [Test] - public void WhenActualContainsBothBits_ShouldNotThrow() + public void ShouldKeepOnChecking_HappyPath() { // Arrange var actual = "a-b-c"; - var first = "a"; - var second = "b"; // Pre-Assert // Act Assert.That(() => { - Expect(actual).To.Contain(first).And(second); + Expect(actual).To.Contain("a").And("b").And("c"); }, Throws.Nothing); // Assert } [Test] - public void WhenActualMissingFirstBit_ShouldThrow() + public void ShouldKeepOnChecking_SadPath() { // Arrange var actual = "a-b-c"; - var first = "d"; - var second = "b"; // Pre-Assert // Act Assert.That(() => { - Expect(actual).To.Contain(first).And(second); + Expect(actual).To.Contain("a").And("b").And("d"); }, Throws.Exception.InstanceOf() - .With.Message.Contains("\"a-b-c\"\nto contain\n\"d\"")); + .With.Message.Contains("\"d\"")); // Assert } + } + [TestFixture] + public class FragmentsInOrder + { [Test] - public void WhenActualMissingSecondBit_ShouldThrow() + public void WhenHaveAllFragmentsInOrder_ShouldNotThrow() { // Arrange - var actual = "a-b-c"; - var first = "b"; - var second = "f"; + var src = new[] + { + "Line the first", + "This is the second line", + "Another line here", + "And the final line" + }.JoinWith("\n"); // Pre-Assert // Act Assert.That(() => - { - Expect(actual).To.Contain(first).And(second); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("\"a-b-c\"\nto contain\n\"f\"")); + { + Expect(src) + .To.Contain.In.Order( + "the first", + "line here", + "final line" + ); + }, Throws.Nothing); // Assert } [Test] - public void WhenActualMissingSecondBit_ShouldThrowWithCustomMessage() + public void WhenHaveAllFragmentsInOrder_ShouldAllowMore() { // Arrange - var actual = "a-b-c"; - var first = "b"; - var second = "f"; - var expected = GetRandomString(2); + var src = new[] + { + "Line the first", + "This is the second line", + "Another line here", + "And the final line" + }.JoinWith("\n"); // Pre-Assert // Act Assert.That(() => - { - Expect(actual).To.Contain(first).And(second, expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected)); + { + Expect(src) + .To.Contain.In.Order( + "the first", + "line here" + ).Then("final line"); + }, Throws.Nothing); // Assert } - [TestFixture] - public class AndAgain + [Test] + public void WhenHaveAllFragmentsOutOfOrder_ShouldThrow() { - [Test] - public void ShouldKeepOnChecking_HappyPath() + // Arrange + var src = new[] { - // Arrange - var actual = "a-b-c"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual).To.Contain("a").And("b").And("c"); - }, - Throws.Nothing); - // Assert - } + "Line the first", + "This is the second line", + "Another line here", + "And the final line" + }.JoinWith("\n"); - [Test] - public void ShouldKeepOnChecking_SadPath() + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var actual = "a-b-c"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual).To.Contain("a").And("b").And("d"); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("\"d\"")); - // Assert - } + Expect(src) + .To.Contain.In.Order( + "line here", + "the first", + "final line" + ); + }, Throws.Exception.InstanceOf()); + // Assert } - [TestFixture] - public class FragmentsInOrder + [Test] + public void WhenHaveMissingFragment_ShouldThrow() { - [Test] - public void WhenHaveAllFragmentsInOrder_ShouldNotThrow() - { - // Arrange - var src = new[] - { - "Line the first", - "This is the second line", - "Another line here", - "And the final line" - }.JoinWith("\n"); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(src) - .To.Contain.In.Order( - "the first", - "line here", - "final line" - ); - }, Throws.Nothing); - // Assert - } - - [Test] - public void WhenHaveAllFragmentsInOrder_ShouldAllowMore() + // Arrange + var src = new[] { - // Arrange - var src = new[] - { - "Line the first", - "This is the second line", - "Another line here", - "And the final line" - }.JoinWith("\n"); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(src) - .To.Contain.In.Order( - "the first", - "line here" - ).Then("final line"); - }, Throws.Nothing); - // Assert - } + "Line the first", + "This is the second line", + "Another line here", + "And the final line" + }.JoinWith("\n"); - [Test] - public void WhenHaveAllFragmentsOutOfOrder_ShouldThrow() + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var src = new[] - { - "Line the first", - "This is the second line", - "Another line here", - "And the final line" - }.JoinWith("\n"); - - // Pre-Assert - // Act - Assert.That(() => - { - Expect(src) - .To.Contain.In.Order( - "line here", - "the first", - "final line" - ); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(src) + .To.Contain.In.Order( + "the first", + "moo, said the cow", + "final line" + ); + }, Throws.Exception.InstanceOf()); + // Assert + } - [Test] - public void WhenHaveMissingFragment_ShouldThrow() + [Test] + public void WhenHaveMissingFragment_ShouldThrow_IncludingCustomMessage() + { + // Arrange + var src = new[] { - // Arrange - var src = new[] - { - "Line the first", - "This is the second line", - "Another line here", - "And the final line" - }.JoinWith("\n"); + "Line the first", + "This is the second line", + "Another line here", + "And the final line" + }.JoinWith("\n"); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(src) - .To.Contain.In.Order( - "the first", - "moo, said the cow", - "final line" - ); - }, Throws.Exception.InstanceOf()); - // Assert - } - - [Test] - public void WhenHaveMissingFragment_ShouldThrow_IncludingCustomMessage() + var expected = GetRandomString(10); + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var src = new[] - { - "Line the first", - "This is the second line", - "Another line here", - "And the final line" - }.JoinWith("\n"); - - var expected = GetRandomString(10); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(src) - .To.Contain.In.Order(new[] - { - "the first", - "moo, said the cow", - "final line" - }, expected); - }, Throws.Exception.InstanceOf() - .With.Message.Contains(expected)); - // Assert - } + Expect(src) + .To.Contain.In.Order(new[] + { + "the first", + "moo, said the cow", + "final line" + }, expected); + }, Throws.Exception.InstanceOf() + .With.Message.Contains(expected)); + // Assert } } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/Empty.cs b/src/NExpect.Tests/ObjectEquality/Strings/Empty.cs index 2c95cd62..6bd3a18b 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/Empty.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/Empty.cs @@ -1,81 +1,80 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class Empty { [TestFixture] - public class Empty + public class To { [TestFixture] - public class To + public class Be { [TestFixture] - public class Be + public class Empty { - [TestFixture] - public class Empty + [Test] + public void WhenIsEmpty_ShouldNotThrow() { - [Test] - public void WhenIsEmpty_ShouldNotThrow() - { - // Arrange - var input = ""; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Empty(); - }, - Throws.Nothing); - // Assert - } + // Arrange + var input = ""; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Empty(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Negated_WhenIsNotEmpty_ShouldNotThrow() - { - // Arrange - var input = "123"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Empty(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void Negated_WhenIsNotEmpty_ShouldNotThrow() + { + // Arrange + var input = "123"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Empty(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void WhenIsNotEmpty_ShouldThrow() - { - // Arrange - var input = "123"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Empty(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void WhenIsNotEmpty_ShouldThrow() + { + // Arrange + var input = "123"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Empty(); + }, + Throws.InstanceOf()); + // Assert + } - [Test] - public void Negated_WhenIsEmpty_ShouldThrow() - { - // Arrange - var input = ""; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Empty(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void Negated_WhenIsEmpty_ShouldThrow() + { + // Arrange + var input = ""; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Empty(); + }, + Throws.InstanceOf()); + // Assert } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/EndingWith.cs b/src/NExpect.Tests/ObjectEquality/Strings/EndingWith.cs index d2187ef8..c307da35 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/EndingWith.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/EndingWith.cs @@ -1,109 +1,108 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class EndingWith { [TestFixture] - public class EndingWith + public class To { [TestFixture] - public class To + public class Start { [TestFixture] - public class Start + public class With { - [TestFixture] - public class With + [Test] + public void PositiveAssertion_WhenStringStartsWithExpected_ShouldNotThrow() { - [Test] - public void PositiveAssertion_WhenStringStartsWithExpected_ShouldNotThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - // Pre-Assert - // Act - Assert.That(() => - { - Expect($"{start}{end}").To.End.With(end); - }, Throws.Nothing); - // Assert - } - - [Test] - public void PositiveAssertion_WhenStringDoesNotStartsWithExpected_ShouldThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetAnother(start, () => GetRandomString(2)); - var actual = $"{start}{end}"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual).To.End.With(start); - }, Throws.Exception.InstanceOf() - .With.Message - .Contains($"Expected\n\"{actual}\"\nto end with\n\"{start}\"") - ); - // Assert - } - - [Test] - public void PositiveAssertion_WithCustomMessage_WhenStringDoesNotStartsWithExpected_ShouldThrow() + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - var actual = $"{start}{end}"; - var customMessage = GetRandomString(2); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual).To.End.With(start, customMessage); - }, Throws.Exception.InstanceOf() - .With.Message - .Contains(customMessage) - ); - // Assert - } + Expect($"{start}{end}").To.End.With(end); + }, Throws.Nothing); + // Assert + } - [Test] - public void NegativeAssertion_WhenStringStartsWithUnexpected_ShouldThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - var actual = $"{start}{end}"; - // Pre-Assert - // Act - Assert.That(() => + [Test] + public void PositiveAssertion_WhenStringDoesNotStartsWithExpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetAnother(start, () => GetRandomString(2)); + var actual = $"{start}{end}"; + // Pre-Assert + // Act + Assert.That(() => { - Expect(actual).Not.To.End.With(end); + Expect(actual).To.End.With(start); }, Throws.Exception.InstanceOf() - .With.Message.Contains($"\"{actual}\"\nnot to end with\n\"{end}\"")); - // Assert - } + .With.Message + .Contains($"Expected\n\"{actual}\"\nto end with\n\"{start}\"") + ); + // Assert + } - [Test] - public void NegativeAssertion_AltGrammar_WhenStringStartsWithUnexpected_ShouldThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - var actual = $"{start}{end}"; - // Pre-Assert - // Act - Assert.That(() => + [Test] + public void PositiveAssertion_WithCustomMessage_WhenStringDoesNotStartsWithExpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + var actual = $"{start}{end}"; + var customMessage = GetRandomString(2); + // Pre-Assert + // Act + Assert.That(() => { - Expect(actual).To.Not.End.With(end); + Expect(actual).To.End.With(start, customMessage); }, Throws.Exception.InstanceOf() - .With.Message.Contains($"\"{actual}\"\nnot to end with\n\"{end}\"")); - // Assert - } + .With.Message + .Contains(customMessage) + ); + // Assert + } + + [Test] + public void NegativeAssertion_WhenStringStartsWithUnexpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + var actual = $"{start}{end}"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(actual).Not.To.End.With(end); + }, Throws.Exception.InstanceOf() + .With.Message.Contains($"\"{actual}\"\nnot to end with\n\"{end}\"")); + // Assert + } + + [Test] + public void NegativeAssertion_AltGrammar_WhenStringStartsWithUnexpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + var actual = $"{start}{end}"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(actual).To.Not.End.With(end); + }, Throws.Exception.InstanceOf() + .With.Message.Contains($"\"{actual}\"\nnot to end with\n\"{end}\"")); + // Assert } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/Equality.cs b/src/NExpect.Tests/ObjectEquality/Strings/Equality.cs index ef3187ba..af9ce4b1 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/Equality.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/Equality.cs @@ -5,149 +5,148 @@ // ReSharper disable MemberHidesStaticFromOuterClass -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class Equality { [TestFixture] - public class Equality + public class To { [TestFixture] - public class To + public class Equal { + [Test] + public void WithCustomMessage() + { + // Arrange + var expected = RandomValueGen.GetRandomString(); + var test = RandomValueGen.GetRandomString(); + var nonMatch = RandomValueGen.GetAnother(test); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(test).To.Equal(nonMatch, expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + ); + Assert.That( + () => + { + Expect(test).To.Equal(nonMatch, () => expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + ); + // Assert + } + [TestFixture] - public class Equal + public class DifferenceOutput { [Test] - public void WithCustomMessage() + public void ShouldBeHelpful() { // Arrange - var expected = RandomValueGen.GetRandomString(); - var test = RandomValueGen.GetRandomString(); - var nonMatch = RandomValueGen.GetAnother(test); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(test).To.Equal(nonMatch, expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - Assert.That( - () => - { - Expect(test).To.Equal(nonMatch, () => expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } - - [TestFixture] - public class DifferenceOutput - { - [Test] - public void ShouldBeHelpful() - { - // Arrange - var input = @" + var input = @" { - ""Foo_Bar"": ""value_bar"", - ""Quux_Bar"": ""1value_var"" +""Foo_Bar"": ""value_bar"", +""Quux_Bar"": ""1value_var"" } "; - var expected = @" + var expected = @" { - ""Foo_Bar"": ""value_bar"", - ""Quux_Bar"": ""value_var"" +""Foo_Bar"": ""value_bar"", +""Quux_Bar"": ""value_var"" } "; - // Act - var threw = false; - try - { - Expect(input) - .To.Equal(expected); - } - catch (UnmetExpectationException e) - { - threw = true; - Expect(e.Message) - .To.Contain("first difference found") - .Then( - "ux_Bar\": \"1value_var\"", - () => "should show the error in the actual string, not the expected string" - ); - } - - // Assert - Expect(threw) - .To.Be.True( - () => "Should have failed the expectation" + // Act + var threw = false; + try + { + Expect(input) + .To.Equal(expected); + } + catch (UnmetExpectationException e) + { + threw = true; + Expect(e.Message) + .To.Contain("first difference found") + .Then( + "ux_Bar\": \"1value_var\"", + () => "should show the error in the actual string, not the expected string" ); } - [Test] - public void ShouldDealWithShortMisses() - { - // Arrange - var input = @" + // Assert + Expect(threw) + .To.Be.True( + () => "Should have failed the expectation" + ); + } + + [Test] + public void ShouldDealWithShortMisses() + { + // Arrange + var input = @" line1 line2 "; - var expected = @" + var expected = @" line1 line3 "; - // Act - var result = DifferenceHighlighting.ProvideMoreInfoFor( - input, - expected - ); - Console.WriteLine(result); - // Assert - Expect(result) - .To.End.With(@" + // Act + var result = DifferenceHighlighting.ProvideMoreInfoFor( + input, + expected + ); + Console.WriteLine(result); + // Assert + Expect(result) + .To.End.With(@" line2 ----^"); - } } } + } + [TestFixture] + public class Be + { [TestFixture] - public class Be + public class Equal { [TestFixture] - public class Equal + public class To { - [TestFixture] - public class To + [Test] + public void WithCustomMessage() { - [Test] - public void WithCustomMessage() - { - // Arrange - var expected = RandomValueGen.GetRandomString(); - var test = RandomValueGen.GetRandomString(); - var nonMatch = RandomValueGen.GetAnother(test); - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(test).To.Be.Equal.To(nonMatch, expected); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(expected) - ); - // Assert - } + // Arrange + var expected = RandomValueGen.GetRandomString(); + var test = RandomValueGen.GetRandomString(); + var nonMatch = RandomValueGen.GetAnother(test); + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(test).To.Be.Equal.To(nonMatch, expected); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(expected) + ); + // Assert } } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/MoreStringExpectations.cs b/src/NExpect.Tests/ObjectEquality/Strings/MoreStringExpectations.cs index 1277325d..e5affdb1 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/MoreStringExpectations.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/MoreStringExpectations.cs @@ -1,60 +1,86 @@ -using System; +using System; using System.Linq; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.Utils; using static PeanutButter.Utils.PyLike; -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class MoreStringExpectations { [TestFixture] - public class MoreStringExpectations + public class Like { - [TestFixture] - public class Like + [Test] + public void ShouldBeEquivalentToContainsCaseInsensitive() { - [Test] - public void ShouldBeEquivalentToContainsCaseInsensitive() - { - // Arrange - var message = "The Quick Brown Fox Tripped Over The Dog's Tail And Wiped Out!"; - // Act - Assert.That( - () => - { - Expect(message) - .To.Be.Like("quick brown fox"); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(message) - .Not.To.Be.Like("foo to the bar"); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(message) - .To.Be.Like("jumped over the lazy dog"); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(message) - .Not.To.Be.Like("jumped over the lazy dog"); - }, - Throws.Nothing - ); - // Assert - } + // Arrange + var message = "The Quick Brown Fox Tripped Over The Dog's Tail And Wiped Out!"; + // Act + Assert.That( + () => + { + Expect(message) + .To.Be.Like("quick brown fox"); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(message) + .Not.To.Be.Like("foo to the bar"); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(message) + .To.Be.Like("jumped over the lazy dog"); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(message) + .Not.To.Be.Like("jumped over the lazy dog"); + }, + Throws.Nothing + ); + // Assert } + } + + [Test] + public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(start) + .And.To.Contain(middle) + .And.To.End.With(end); + }, + Throws.Nothing + ); + // Assert + } + [TestFixture] + public class WhenProvidedStringComparison + { [Test] public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() { @@ -62,87 +88,350 @@ public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() var start = GetRandomString(2); var middle = GetRandomString(); var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; + var afterStart = GetRandomString(); + var actual = $"{start}{afterStart}{middle}{end}"; // Pre-Assert // Act Assert.That( () => { Expect(actual) - .To.Start.With(start) - .And.To.Contain(middle) - .And.To.End.With(end); + .To.Start.With(start.ToUpper(), StringComparison.OrdinalIgnoreCase) + .Then(afterStart.ToUpper(), StringComparison.OrdinalIgnoreCase) + .And.To.Contain(middle.ToRandomCase(), StringComparison.OrdinalIgnoreCase) + .And.To.End.With(end.ToRandomCase(), StringComparison.OrdinalIgnoreCase); }, Throws.Nothing ); // Assert } + } - [TestFixture] - public class WhenProvidedStringComparison - { - [Test] - public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() + [Test] + public void MultipleNegativeAssertions() + { + // Arrange + var str = "hello, world!"; + // Act + Assert.That( + () => + { + Expect(str) + .To.Contain("hello") + .And.Not.To.Contain("submarine") + .And.Not.To.Contain("yellow") + .And.To.Contain("world"); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void PositiveAssertion_Reversed_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.End.With(end) + .And.To.Start.With(start); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void NegativeAssertion1_WhenShouldPass_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .Not.To.Start.With(start) + .And.To.Contain(middle) + .And.To.End.With(end); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void NegativeAssertion2_WhenShouldPass_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(start) + .And.Not.To.Contain(middle) + .And.To.End.With(end); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void NegativeAssertion3_WhenShouldPass_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(start) + .And.To.Contain(middle) + .And.Not.To.End.With(end); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void PositiveAssertion_ShorterSyntax_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(start) + .And.Contain(middle) + .And.End.With(end); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void PositiveAssertion_ShorterSyntaxFlipped_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => { - // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var afterStart = GetRandomString(); - var actual = $"{start}{afterStart}{middle}{end}"; - // Pre-Assert - // Act - Assert.That( - () => - { - Expect(actual) - .To.Start.With(start.ToUpper(), StringComparison.OrdinalIgnoreCase) - .Then(afterStart.ToUpper(), StringComparison.OrdinalIgnoreCase) - .And.To.Contain(middle.ToRandomCase(), StringComparison.OrdinalIgnoreCase) - .And.To.End.With(end.ToRandomCase(), StringComparison.OrdinalIgnoreCase); - }, - Throws.Nothing - ); - // Assert - } + Expect(actual) + .To.End.With(end) + .And.Contain(middle) + .And.Start.With(start); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void PositiveAssertion_ShorterSyntax_WhenShouldFail_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(end) + .And.Contain(middle) + .And.End.With(start); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void PositiveAssertion_ShorterSyntaxFlipped_WhenShouldFail_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.End.With(start) + .And.Contain(middle) + .And.Start.With(end); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void NegativeAssertion_ShorterSyntax_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(start) + .And.Contain(middle) + .And.Not.End.With(start); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void NegativeAssertion_ShorterSyntax_WhenShouldFail_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(start) + .And.Contain(middle) + .And.Not.End.With(end); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } + + [Test] + public void TestingEndWith_PositiveResult() + { + // Arrange + var start = GetRandomString(2); + var middle = GetRandomString(); + var end = GetRandomString(2); + var actual = $"{start}{middle}{end}"; + // Pre-Assert + // Act + Assert.That( + () => + { + Expect(actual) + .To.Start.With(start) + .And.Contain(middle) + .And.End.With(end); + }, + Throws.Nothing + ); + // Assert + } + + [TestFixture] + public class StringLength + { + [Test] + public void PositiveAssertion_WithPass() + { + // Arrange + var s = GetRandomString( + 5, + 10 + ); + var expected = s.Length; + // Pre-assert + // Act + Assert.That( + () => Expect(s).To.Have.Length(expected), + Throws.Nothing + ); + // Assert } [Test] - public void MultipleNegativeAssertions() + public void PositiveAssertion_WithFail() { // Arrange - var str = "hello, world!"; + var s = GetRandomString(); + var test = GetRandom(i => i != s.Length); + // Pre-assert // Act Assert.That( () => { - Expect(str) - .To.Contain("hello") - .And.Not.To.Contain("submarine") - .And.Not.To.Contain("yellow") - .And.To.Contain("world"); + Expect(s).To.Have.Length(test); }, - Throws.Nothing + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{s}\" to have length {test}") ); // Assert } [Test] - public void PositiveAssertion_Reversed_WhenShouldPass_ShouldNotThrow() + public void NegativeAssertion_WithPass() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomString(); + var test = GetRandom(i => i != s.Length); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.End.With(end) - .And.To.Start.With(start); + Expect(s).Not.To.Have.Length(test); }, Throws.Nothing ); @@ -150,91 +439,83 @@ public void PositiveAssertion_Reversed_WhenShouldPass_ShouldNotThrow() } [Test] - public void NegativeAssertion1_WhenShouldPass_ShouldThrow() + public void NegativeAssertion_WithFail() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomString(); + var test = s.Length; + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .Not.To.Start.With(start) - .And.To.Contain(middle) - .And.To.End.With(end); + Expect(s).Not.To.Have.Length(test); }, - Throws.Exception.InstanceOf() + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{s}\" not to have length {test}") ); // Assert } + } + [TestFixture] + public class Alphanumeric : MoreStringExpectations + { [Test] - public void NegativeAssertion2_WhenShouldPass_ShouldThrow() + public void PositiveAssertion_WithPass() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var alphaNumeric = GetRandomAlphaNumericString(); + var alphaOnly = GetRandomAlphaString(); + var numericOnly = GetRandomInt( + 10000, + 200000 + ).ToString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.Start.With(start) - .And.Not.To.Contain(middle) - .And.To.End.With(end); + Expect(alphaNumeric).To.Be.Alphanumeric(); + Expect(alphaOnly).To.Be.Alphanumeric(); + Expect(numericOnly).To.Be.Alphanumeric(); }, - Throws.Exception.InstanceOf() + Throws.Nothing ); // Assert } [Test] - public void NegativeAssertion3_WhenShouldPass_ShouldThrow() + public void PositiveAssertion_WithFail() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomNonAlphaNumericString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.Start.With(start) - .And.To.Contain(middle) - .And.Not.To.End.With(end); + Expect(s).To.Be.Alphanumeric(); }, - Throws.Exception.InstanceOf() + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{s}\" to be alpha-numeric") ); // Assert } [Test] - public void PositiveAssertion_ShorterSyntax_WhenShouldPass_ShouldNotThrow() + public void NegativeAssertion_WithPass() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomNonAlphaNumericString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.Start.With(start) - .And.Contain(middle) - .And.End.With(end); + Expect(s).Not.To.Be.Alphanumeric(); }, Throws.Nothing ); @@ -242,91 +523,75 @@ public void PositiveAssertion_ShorterSyntax_WhenShouldPass_ShouldNotThrow() } [Test] - public void PositiveAssertion_ShorterSyntaxFlipped_WhenShouldPass_ShouldNotThrow() + public void NegativeAssertion_WithFail() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomAlphaNumericString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.End.With(end) - .And.Contain(middle) - .And.Start.With(start); + Expect(s).Not.To.Be.Alphanumeric(); }, - Throws.Nothing + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{s}\" not to be alpha-numeric") ); // Assert } + } + [TestFixture] + public class Alpha : MoreStringExpectations + { [Test] - public void PositiveAssertion_ShorterSyntax_WhenShouldFail_ShouldThrow() + public void PositiveAssertion_WithPass() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var alphaOnly = GetRandomAlphaString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.Start.With(end) - .And.Contain(middle) - .And.End.With(start); + Expect(alphaOnly).To.Be.Alpha(); }, - Throws.Exception.InstanceOf() + Throws.Nothing ); // Assert } [Test] - public void PositiveAssertion_ShorterSyntaxFlipped_WhenShouldFail_ShouldThrow() + public void PositiveAssertion_WithFail() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomNonAlphaNumericString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.End.With(start) - .And.Contain(middle) - .And.Start.With(end); + Expect(s).To.Be.Alpha(); }, - Throws.Exception.InstanceOf() + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{s}\" to be alpha") ); // Assert } [Test] - public void NegativeAssertion_ShorterSyntax_WhenShouldPass_ShouldNotThrow() + public void NegativeAssertion_WithPass() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomNonAlphaNumericString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.Start.With(start) - .And.Contain(middle) - .And.Not.End.With(start); + Expect(s).Not.To.Be.Alpha(); }, Throws.Nothing ); @@ -334,405 +599,139 @@ public void NegativeAssertion_ShorterSyntax_WhenShouldPass_ShouldNotThrow() } [Test] - public void NegativeAssertion_ShorterSyntax_WhenShouldFail_ShouldThrow() + public void NegativeAssertion_WithFail() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var s = GetRandomAlphaString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.Start.With(start) - .And.Contain(middle) - .And.Not.End.With(end); + Expect(s).Not.To.Be.Alpha(); }, - Throws.Exception.InstanceOf() + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{s}\" not to be alpha") ); // Assert } + } + [TestFixture] + public class Numeric : MoreStringExpectations + { [Test] - public void TestingEndWith_PositiveResult() + public void PositiveAssertion_WithPass() { // Arrange - var start = GetRandomString(2); - var middle = GetRandomString(); - var end = GetRandomString(2); - var actual = $"{start}{middle}{end}"; - // Pre-Assert + var numericOnly = GetRandomInt( + 10000, + 200000 + ).ToString(); + // Pre-assert // Act Assert.That( () => { - Expect(actual) - .To.Start.With(start) - .And.Contain(middle) - .And.End.With(end); + Expect(numericOnly).To.Be.Numeric(); }, Throws.Nothing ); // Assert } - [TestFixture] - public class StringLength + [Test] + public void PositiveAssertion_WithFail() { - [Test] - public void PositiveAssertion_WithPass() - { - // Arrange - var s = GetRandomString( - 5, - 10 - ); - var expected = s.Length; - // Pre-assert - // Act - Assert.That( - () => Expect(s).To.Have.Length(expected), - Throws.Nothing - ); - // Assert - } - - [Test] - public void PositiveAssertion_WithFail() - { - // Arrange - var s = GetRandomString(); - var test = GetRandom(i => i != s.Length); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).To.Have.Length(test); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{s}\" to have length {test}") - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithPass() - { - // Arrange - var s = GetRandomString(); - var test = GetRandom(i => i != s.Length); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Have.Length(test); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithFail() - { - // Arrange - var s = GetRandomString(); - var test = s.Length; - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Have.Length(test); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{s}\" not to have length {test}") - ); - // Assert - } + // Arrange + var alpha = GetRandomAlphaString(); + var nonAlpha = GetRandomNonAlphaNumericString(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(alpha).To.Be.Numeric(); + }, + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{alpha}\" to be numeric") + ); + Assert.That( + () => + { + Expect(nonAlpha).To.Be.Numeric(); + }, + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{nonAlpha}\" to be numeric") + ); + // Assert } - [TestFixture] - public class Alphanumeric : MoreStringExpectations + [Test] + public void NegativeAssertion_WithPass() { - [Test] - public void PositiveAssertion_WithPass() - { - // Arrange - var alphaNumeric = GetRandomAlphaNumericString(); - var alphaOnly = GetRandomAlphaString(); - var numericOnly = GetRandomInt( - 10000, - 200000 - ).ToString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(alphaNumeric).To.Be.Alphanumeric(); - Expect(alphaOnly).To.Be.Alphanumeric(); - Expect(numericOnly).To.Be.Alphanumeric(); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void PositiveAssertion_WithFail() - { - // Arrange - var s = GetRandomNonAlphaNumericString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).To.Be.Alphanumeric(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{s}\" to be alpha-numeric") - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithPass() - { - // Arrange - var s = GetRandomNonAlphaNumericString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Be.Alphanumeric(); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithFail() - { - // Arrange - var s = GetRandomAlphaNumericString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Be.Alphanumeric(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{s}\" not to be alpha-numeric") - ); - // Assert - } + // Arrange + var s = GetRandomAlphaString(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(s).Not.To.Be.Numeric(); + }, + Throws.Nothing + ); + // Assert } - [TestFixture] - public class Alpha : MoreStringExpectations + [Test] + public void NegativeAssertion_WithFail() { - [Test] - public void PositiveAssertion_WithPass() - { - // Arrange - var alphaOnly = GetRandomAlphaString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(alphaOnly).To.Be.Alpha(); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void PositiveAssertion_WithFail() - { - // Arrange - var s = GetRandomNonAlphaNumericString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).To.Be.Alpha(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{s}\" to be alpha") - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithPass() - { - // Arrange - var s = GetRandomNonAlphaNumericString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Be.Alpha(); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithFail() - { - // Arrange - var s = GetRandomAlphaString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Be.Alpha(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{s}\" not to be alpha") - ); - // Assert - } + // Arrange + var s = GetRandomInt( + 1, + 10000 + ).ToString(); + // Pre-assert + // Act + Assert.That( + () => + { + Expect(s).Not.To.Be.Numeric(); + }, + Throws.Exception + .InstanceOf() + .With.Message.EqualTo($"Expected \"{s}\" not to be numeric") + ); + // Assert } + } - [TestFixture] - public class Numeric : MoreStringExpectations - { - [Test] - public void PositiveAssertion_WithPass() - { - // Arrange - var numericOnly = GetRandomInt( - 10000, - 200000 - ).ToString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(numericOnly).To.Be.Numeric(); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void PositiveAssertion_WithFail() - { - // Arrange - var alpha = GetRandomAlphaString(); - var nonAlpha = GetRandomNonAlphaNumericString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(alpha).To.Be.Numeric(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{alpha}\" to be numeric") - ); - Assert.That( - () => - { - Expect(nonAlpha).To.Be.Numeric(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{nonAlpha}\" to be numeric") - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithPass() - { - // Arrange - var s = GetRandomAlphaString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Be.Numeric(); - }, - Throws.Nothing - ); - // Assert - } - - [Test] - public void NegativeAssertion_WithFail() - { - // Arrange - var s = GetRandomInt( + private string GetRandomNonAlphaNumericString( + int minChars = 0, + int maxChars = 10 + ) + { + return Range( + 0, + GetRandomInt( 1, - 10000 - ).ToString(); - // Pre-assert - // Act - Assert.That( - () => - { - Expect(s).Not.To.Be.Numeric(); - }, - Throws.Exception - .InstanceOf() - .With.Message.EqualTo($"Expected \"{s}\" not to be numeric") - ); - // Assert - } - } - - private string GetRandomNonAlphaNumericString( - int minChars = 0, - int maxChars = 10 - ) - { - return Range( - 0, - GetRandomInt( - 1, - 10 - ) + 10 ) - .Select( - i => - GetRandom( - c => (c < 'A' || c > 'z') && (!"01234567890".Contains(c)), - () => (char)GetRandomInt( - 32, - 255 - ) + ) + .Select( + i => + GetRandom( + c => (c < 'A' || c > 'z') && (!"01234567890".Contains(c)), + () => (char)GetRandomInt( + 32, + 255 ) - ) - .JoinWith(""); - } + ) + ) + .JoinWith(""); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/NullOrWhitespace.cs b/src/NExpect.Tests/ObjectEquality/Strings/NullOrWhitespace.cs index e359a900..f395e0ce 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/NullOrWhitespace.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/NullOrWhitespace.cs @@ -3,142 +3,141 @@ // ReSharper disable ExpressionIsAlwaysNull -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class NullOrWhitespace { [TestFixture] - public class NullOrWhitespace + public class To { [TestFixture] - public class To + public class Be { [TestFixture] - public class Be + public class NullOrWhitespace { - [TestFixture] - public class NullOrWhitespace + [Test] + public void WhenIsEmpty_ShouldNotThrow() { - [Test] - public void WhenIsEmpty_ShouldNotThrow() - { - // Arrange - var input = ""; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null.Or.Whitespace(); - }, - Throws.Nothing); - // Assert - } + // Arrange + var input = ""; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null.Or.Whitespace(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void WhenIsWhitespace_ShouldNotThrow() - { - // Arrange - var input = " "; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null.Or.Whitespace(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void WhenIsWhitespace_ShouldNotThrow() + { + // Arrange + var input = " "; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null.Or.Whitespace(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void WhenIsNull_ShouldNotThrow() - { - // Arrange - var input = null as string; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null.Or.Whitespace(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void WhenIsNull_ShouldNotThrow() + { + // Arrange + var input = null as string; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null.Or.Whitespace(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Negated_WhenIsNotEmpty_ShouldNotThrow() - { - // Arrange - var input = "123"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null.Or.Whitespace(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void Negated_WhenIsNotEmpty_ShouldNotThrow() + { + // Arrange + var input = "123"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null.Or.Whitespace(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void WhenIsNotEmpty_ShouldThrow() - { - // Arrange - var input = "123"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null.Or.Whitespace("Le Error"); - }, - Throws.InstanceOf() - .With.Message.Contains("Le Error")); - // Assert - } + [Test] + public void WhenIsNotEmpty_ShouldThrow() + { + // Arrange + var input = "123"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null.Or.Whitespace("Le Error"); + }, + Throws.InstanceOf() + .With.Message.Contains("Le Error")); + // Assert + } - [Test] - public void Negated_WhenIsEmpty_ShouldThrow() - { - // Arrange - var input = ""; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null.Or.Whitespace(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void Negated_WhenIsEmpty_ShouldThrow() + { + // Arrange + var input = ""; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null.Or.Whitespace(); + }, + Throws.InstanceOf()); + // Assert + } - [Test] - public void Negated_WhenIsWhitespace_ShouldThrow() - { - // Arrange - var input = " "; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null.Or.Whitespace(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void Negated_WhenIsWhitespace_ShouldThrow() + { + // Arrange + var input = " "; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null.Or.Whitespace(); + }, + Throws.InstanceOf()); + // Assert + } - [Test] - public void Negated_WhenIsNull_ShouldThrow() - { - // Arrange - var input = null as string; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null.Or.Whitespace(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void Negated_WhenIsNull_ShouldThrow() + { + // Arrange + var input = null as string; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null.Or.Whitespace(); + }, + Throws.InstanceOf()); + // Assert } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/NullorEmpty.cs b/src/NExpect.Tests/ObjectEquality/Strings/NullorEmpty.cs index 0beea9cc..213879ce 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/NullorEmpty.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/NullorEmpty.cs @@ -3,126 +3,125 @@ // ReSharper disable ExpressionIsAlwaysNull -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class NullorEmpty { [TestFixture] - public class NullorEmpty + public class To { [TestFixture] - public class To + public class Be { [TestFixture] - public class Be + public class NullOrEmpty { - [TestFixture] - public class NullOrEmpty + [Test] + public void WhenIsEmpty_ShouldNotThrow() { - [Test] - public void WhenIsEmpty_ShouldNotThrow() - { - // Arrange - var input = ""; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null.Or.Empty(); - }, - Throws.Nothing); - // Assert - } + // Arrange + var input = ""; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null.Or.Empty(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void WhenIsNull_ShouldNotThrow() - { - // Arrange - var input = null as string; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null.Or.Empty(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void WhenIsNull_ShouldNotThrow() + { + // Arrange + var input = null as string; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null.Or.Empty(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void Negated_WhenIsNotEmpty_ShouldNotThrow() - { - // Arrange - var input = "123"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null.Or.Empty(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void Negated_WhenIsNotEmpty_ShouldNotThrow() + { + // Arrange + var input = "123"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null.Or.Empty(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void NegatedAlt_WhenIsNotEmpty_ShouldNotThrow() - { - // Arrange - var input = "123"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).Not.To.Be.Null.Or.Empty(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void NegatedAlt_WhenIsNotEmpty_ShouldNotThrow() + { + // Arrange + var input = "123"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).Not.To.Be.Null.Or.Empty(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void WhenIsNotEmpty_ShouldThrow() - { - // Arrange - var input = "123"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null.Or.Empty(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void WhenIsNotEmpty_ShouldThrow() + { + // Arrange + var input = "123"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null.Or.Empty(); + }, + Throws.InstanceOf()); + // Assert + } - [Test] - public void Negated_WhenIsEmpty_ShouldThrow() - { - // Arrange - var input = ""; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null.Or.Empty(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void Negated_WhenIsEmpty_ShouldThrow() + { + // Arrange + var input = ""; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null.Or.Empty(); + }, + Throws.InstanceOf()); + // Assert + } - [Test] - public void Negated_WhenIsNull_ShouldThrow() - { - // Arrange - var input = null as string; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null.Or.Empty(); - }, - Throws.InstanceOf()); - // Assert - } + [Test] + public void Negated_WhenIsNull_ShouldThrow() + { + // Arrange + var input = null as string; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null.Or.Empty(); + }, + Throws.InstanceOf()); + // Assert } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/OrderedPartialContaining.cs b/src/NExpect.Tests/ObjectEquality/Strings/OrderedPartialContaining.cs index e0f0f36e..b5c4fd1a 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/OrderedPartialContaining.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/OrderedPartialContaining.cs @@ -1,143 +1,142 @@ -using System; +using System; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class OrderedPartialContaining { - [TestFixture] - public class OrderedPartialContaining + [Test] + public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() { - [Test] - public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() - { - // Arrange - var start = GetRandomString(2); - var next = GetAnother(start); - var input = $"{start}{next}"; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input) - .To.Contain(start) - .Then(next); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void PositiveAssertion_WhenNextPartIsOutOfOrder_ShouldThrow() - { - // Arrange - var start = GetRandomString(2); - var next = GetAnother(start); - var input = $"{start}{next}"; - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input) - .To.Contain(next) - .Then(start); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - [Repeat(20)] - public void PositiveAssertion_MultipleChains() - { - // Arrange - var strings = GetRandomArray(6, 8); - var input = strings.JoinWith(""); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input) - .To - .Contain(strings[0]) - .Then(strings[1]) - .Then(strings[3]) - .Then(strings[5]); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void ThenShouldNotMatchPartialOfAlreadyMatchedPart() - { - // Arrange - var input = "moo said the cat"; + // Arrange + var start = GetRandomString(2); + var next = GetAnother(start); + var input = $"{start}{next}"; - // Pre-Assert + // Pre-Assert - // Act - Assert.That(() => + // Act + Assert.That(() => { Expect(input) - .To.Contain("moo") - .Then("o"); - }, Throws.Exception.InstanceOf()); + .To.Contain(start) + .Then(next); + }, + Throws.Nothing); - // Assert - } + // Assert + } - [Test] - public void PositiveAssertion_OnExceptionMessage_WhenShouldPass_ShouldNotThrow() - { - // Arrange - var first = GetRandomString(2); - var second = GetAnother(first); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(() => throw new Exception($"{first}{GetRandomString(15)}{second}")) - .To.Throw() - .With.Message.Containing(first) - .Then(second); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void PositiveAssertion_OnExceptionMessage_WhenShouldNotPass_ShouldThrow() + [Test] + public void PositiveAssertion_WhenNextPartIsOutOfOrder_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var next = GetAnother(start); + var input = $"{start}{next}"; + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input) + .To.Contain(next) + .Then(start); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + [Repeat(20)] + public void PositiveAssertion_MultipleChains() + { + // Arrange + var strings = GetRandomArray(6, 8); + var input = strings.JoinWith(""); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input) + .To + .Contain(strings[0]) + .Then(strings[1]) + .Then(strings[3]) + .Then(strings[5]); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void ThenShouldNotMatchPartialOfAlreadyMatchedPart() + { + // Arrange + var input = "moo said the cat"; + + // Pre-Assert + + // Act + Assert.That(() => { - // Arrange - var first = GetRandomString(2); - var second = GetAnother(first); - var another = GetAnother(new[] { first, second }); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(() => throw new Exception($"{first}{GetRandomString(15)}{second}")) - .To.Throw() - .With.Message.Containing(first) - .Then(another); - }, - Throws.Exception.InstanceOf()); - - // Assert - } + Expect(input) + .To.Contain("moo") + .Then("o"); + }, Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void PositiveAssertion_OnExceptionMessage_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var first = GetRandomString(2); + var second = GetAnother(first); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(() => throw new Exception($"{first}{GetRandomString(15)}{second}")) + .To.Throw() + .With.Message.Containing(first) + .Then(second); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void PositiveAssertion_OnExceptionMessage_WhenShouldNotPass_ShouldThrow() + { + // Arrange + var first = GetRandomString(2); + var second = GetAnother(first); + var another = GetAnother(new[] { first, second }); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(() => throw new Exception($"{first}{GetRandomString(15)}{second}")) + .To.Throw() + .With.Message.Containing(first) + .Then(another); + }, + Throws.Exception.InstanceOf()); + + // Assert } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/RegExMatching.cs b/src/NExpect.Tests/ObjectEquality/Strings/RegExMatching.cs index c46cad6b..da778ab4 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/RegExMatching.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/RegExMatching.cs @@ -1,200 +1,199 @@ -using System.Text.RegularExpressions; +using System.Text.RegularExpressions; using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class RegexMatching { [TestFixture] - public class RegexMatching + public class WithActualRegex { [TestFixture] - public class WithActualRegex + public class To { [TestFixture] - public class To + public class Be { [TestFixture] - public class Be + public class Matched { [TestFixture] - public class Matched + public class By { - [TestFixture] - public class By + [Test] + public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var input = GetRandomString(2); + var regex = new Regex(input); + Assert.That(regex.IsMatch(input), Is.True); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input).To.Be.Matched.By(regex); + }, + Throws.Nothing); + // shorter variant + Assert.That(() => + { + Expect(input).To.Match(regex); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void NegativeAssertion_WhenShouldPass_ShouldThrow() { - [Test] - public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() - { - // Arrange - var input = GetRandomString(2); - var regex = new Regex(input); - Assert.That(regex.IsMatch(input), Is.True); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input).To.Be.Matched.By(regex); - }, - Throws.Nothing); - // shorter variant - Assert.That(() => - { - Expect(input).To.Match(regex); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void NegativeAssertion_WhenShouldPass_ShouldThrow() - { - // Arrange - var input = GetRandomString(2); - var regex = new Regex($"^{input}$"); - Assert.That(regex.IsMatch(input), Is.True); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input).Not.To.Be.Matched.By(regex); - }, - Throws.Exception.InstanceOf()); - // shorter variant - Assert.That(() => - { - Expect(input).Not.To.Match(regex); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void NegativeAssertion_AltSyntax_WhenShouldPass_ShouldThrow() - { - // Arrange - var input = GetRandomString(2); - var regex = new Regex($"^{input}$"); - Assert.That(regex.IsMatch(input), Is.True); - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Matched.By(regex); - }, - Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(input).To.Not.Match(regex); - }, - Throws.Exception.InstanceOf()); - - // Assert - } + // Arrange + var input = GetRandomString(2); + var regex = new Regex($"^{input}$"); + Assert.That(regex.IsMatch(input), Is.True); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input).Not.To.Be.Matched.By(regex); + }, + Throws.Exception.InstanceOf()); + // shorter variant + Assert.That(() => + { + Expect(input).Not.To.Match(regex); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void NegativeAssertion_AltSyntax_WhenShouldPass_ShouldThrow() + { + // Arrange + var input = GetRandomString(2); + var regex = new Regex($"^{input}$"); + Assert.That(regex.IsMatch(input), Is.True); + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Matched.By(regex); + }, + Throws.Exception.InstanceOf()); + + Assert.That(() => + { + Expect(input).To.Not.Match(regex); + }, + Throws.Exception.InstanceOf()); + + // Assert } } } } } + } + [TestFixture] + public class WithStringRegex + { [TestFixture] - public class WithStringRegex + public class To { [TestFixture] - public class To + public class Be { [TestFixture] - public class Be + public class Matched { [TestFixture] - public class Matched + public class By { - [TestFixture] - public class By + [Test] + public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() + { + // Arrange + var input = GetRandomString(2); + var regex = $"^{input}$"; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input).To.Be.Matched.By(regex); + }, + Throws.Nothing); + Assert.That(() => + { + Expect(input).To.Match(regex); + }, + Throws.Nothing); + + // Assert + } + + [Test] + public void NegativeAssertion_WhenShouldPass_ShouldThrow() + { + // Arrange + var input = GetRandomString(2); + var regex = $"^{input}$"; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input).Not.To.Be.Matched.By(regex); + }, + Throws.Exception.InstanceOf()); + Assert.That(() => + { + Expect(input).Not.To.Match(regex); + }, + Throws.Exception.InstanceOf()); + + // Assert + } + + [Test] + public void NegativeAssertion_AltSyntax_WhenShouldPass_ShouldThrow() { - [Test] - public void PositiveAssertion_WhenShouldPass_ShouldNotThrow() - { - // Arrange - var input = GetRandomString(2); - var regex = $"^{input}$"; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input).To.Be.Matched.By(regex); - }, - Throws.Nothing); - Assert.That(() => - { - Expect(input).To.Match(regex); - }, - Throws.Nothing); - - // Assert - } - - [Test] - public void NegativeAssertion_WhenShouldPass_ShouldThrow() - { - // Arrange - var input = GetRandomString(2); - var regex = $"^{input}$"; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input).Not.To.Be.Matched.By(regex); - }, - Throws.Exception.InstanceOf()); - Assert.That(() => - { - Expect(input).Not.To.Match(regex); - }, - Throws.Exception.InstanceOf()); - - // Assert - } - - [Test] - public void NegativeAssertion_AltSyntax_WhenShouldPass_ShouldThrow() - { - // Arrange - var input = GetRandomString(2); - var regex = $"^{input}$"; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Matched.By(regex); - }, - Throws.Exception.InstanceOf()); - - Assert.That(() => - { - Expect(input).To.Not.Match(regex); - }, - Throws.Exception.InstanceOf()); - - // Assert - } + // Arrange + var input = GetRandomString(2); + var regex = $"^{input}$"; + + // Pre-Assert + + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Matched.By(regex); + }, + Throws.Exception.InstanceOf()); + + Assert.That(() => + { + Expect(input).To.Not.Match(regex); + }, + Throws.Exception.InstanceOf()); + + // Assert } } } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/Strings/StartingWith.cs b/src/NExpect.Tests/ObjectEquality/Strings/StartingWith.cs index 0b357371..4952915e 100644 --- a/src/NExpect.Tests/ObjectEquality/Strings/StartingWith.cs +++ b/src/NExpect.Tests/ObjectEquality/Strings/StartingWith.cs @@ -1,112 +1,111 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; // ReSharper disable ExpressionIsAlwaysNull // ReSharper disable MemberHidesStaticFromOuterClass -namespace NExpect.Tests.ObjectEquality.Strings +namespace NExpect.Tests.ObjectEquality.Strings; + +[TestFixture] +public class StartingWith { [TestFixture] - public class StartingWith + public class To { [TestFixture] - public class To + public class Start { [TestFixture] - public class Start + public class With { - [TestFixture] - public class With + [Test] + public void PositiveAssertion_WhenStringStartsWithExpected_ShouldNotThrow() { - [Test] - public void PositiveAssertion_WhenStringStartsWithExpected_ShouldNotThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - // Pre-Assert - // Act - Assert.That(() => - { - Expect($"{start}{end}").To.Start.With(start); - }, Throws.Nothing); - // Assert - } - - [Test] - public void PositiveAssertion_WhenStringDoesNotStartsWithExpected_ShouldThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - var actual = $"{start}{end}"; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual).To.Start.With(end); - }, Throws.Exception.InstanceOf() - .With.Message - .Contains($"Expected\n\"{actual}\"\nto start with\n\"{end}\"") - ); - // Assert - } - - [Test] - public void PositiveAssertion_WithCustomMessage_WhenStringDoesNotStartsWithExpected_ShouldThrow() + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + // Pre-Assert + // Act + Assert.That(() => { - // Arrange - var start = GetRandomString(2); - var end = GetAnother(start, () => GetRandomString(2)); - var actual = $"{start}{end}"; - var customMessage = GetRandomString(2); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(actual).To.Start.With(end, customMessage); - }, Throws.Exception.InstanceOf() - .With.Message - .Contains(customMessage) - ); - // Assert - } + Expect($"{start}{end}").To.Start.With(start); + }, Throws.Nothing); + // Assert + } - [Test] - public void NegativeAssertion_WhenStringStartsWithUnexpected_ShouldThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - var actual = $"{start}{end}"; - // Pre-Assert - // Act - Assert.That(() => + [Test] + public void PositiveAssertion_WhenStringDoesNotStartsWithExpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + var actual = $"{start}{end}"; + // Pre-Assert + // Act + Assert.That(() => { - Expect(actual).Not.To.Start.With(start); + Expect(actual).To.Start.With(end); }, Throws.Exception.InstanceOf() - .With.Message.Contains($"\"{actual}\"\nnot to start with\n\"{start}\"")); - // Assert - } + .With.Message + .Contains($"Expected\n\"{actual}\"\nto start with\n\"{end}\"") + ); + // Assert + } - [Test] - public void NegativeAssertion_AltGrammar_WhenStringStartsWithUnexpected_ShouldThrow() - { - // Arrange - var start = GetRandomString(2); - var end = GetRandomString(2); - var actual = $"{start}{end}"; - // Pre-Assert - // Act - Assert.That(() => + [Test] + public void PositiveAssertion_WithCustomMessage_WhenStringDoesNotStartsWithExpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetAnother(start, () => GetRandomString(2)); + var actual = $"{start}{end}"; + var customMessage = GetRandomString(2); + // Pre-Assert + // Act + Assert.That(() => { - Expect(actual).To.Not.Start.With(start); + Expect(actual).To.Start.With(end, customMessage); }, Throws.Exception.InstanceOf() - .With.Message.Contains($"\"{actual}\"\nnot to start with\n\"{start}\"")); - // Assert - } + .With.Message + .Contains(customMessage) + ); + // Assert + } + + [Test] + public void NegativeAssertion_WhenStringStartsWithUnexpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + var actual = $"{start}{end}"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(actual).Not.To.Start.With(start); + }, Throws.Exception.InstanceOf() + .With.Message.Contains($"\"{actual}\"\nnot to start with\n\"{start}\"")); + // Assert + } + + [Test] + public void NegativeAssertion_AltGrammar_WhenStringStartsWithUnexpected_ShouldThrow() + { + // Arrange + var start = GetRandomString(2); + var end = GetRandomString(2); + var actual = $"{start}{end}"; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(actual).To.Not.Start.With(start); + }, Throws.Exception.InstanceOf() + .With.Message.Contains($"\"{actual}\"\nnot to start with\n\"{start}\"")); + // Assert } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/TestReflectiveMatchers.cs b/src/NExpect.Tests/ObjectEquality/TestReflectiveMatchers.cs index 93ba26cc..3185d823 100644 --- a/src/NExpect.Tests/ObjectEquality/TestReflectiveMatchers.cs +++ b/src/NExpect.Tests/ObjectEquality/TestReflectiveMatchers.cs @@ -1,835 +1,618 @@ -using System; +using System; using System.ComponentModel.DataAnnotations; using System.Linq; using NExpect.Exceptions; using NUnit.Framework; using PeanutButter.Utils; -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class TestReflectiveMatchers { [TestFixture] - public class TestReflectiveMatchers + public class TestingPropertyValueByName { - [TestFixture] - public class TestingPropertyValueByName + public class Data { - public class Data - { - public int id { get; set; } + public int id { get; set; } - [Comment("this is commented!")] - public bool IsCommented { get; set; } + [Comment("this is commented!")] + public bool IsCommented { get; set; } - public bool IsNotCommented { get; set; } - - [Comment("moo-cakes")] - public void CommentedMethod() - { - } + public bool IsNotCommented { get; set; } - public int Add(int a, int b) - { - return a + b; - } - } - - [Test] - public void ShouldHandleMatchingNameAndValue() + [Comment("moo-cakes")] + public void CommentedMethod() { - // Arrange - var data = new Data - { - id = 1 - }; - // Act - Assert.That( - () => - { - Expect(data) - .To.Have.Property("id") - .With.Value(1); - }, - Throws.Nothing - ); - // Assert } - [Test] - public void ShouldHandlePropertyNameMismatch() + public int Add(int a, int b) { - // Arrange - var badData = new - { - foo = "bar" - }; - // Act - Assert.That( - () => - { - Expect(badData) - .To.Have.Property("id") - .With.Value(1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"{badData.GetType().PrettyName()} to have a public property named 'id'" - ) - ); - Assert.That( - () => - { - Expect(badData) - .Not.To.Have.Property("id") - .With.Value(1); - }, - Throws.Nothing - ); - // Assert + return a + b; } + } - [Test] - public void ShouldHandleValueMismatchOnFoundProperty() - { - // Arrange - var data = new + [Test] + public void ShouldHandleMatchingNameAndValue() + { + // Arrange + var data = new Data + { + id = 1 + }; + // Act + Assert.That( + () => { - id = 1 - }; - // Act - Assert.That( - () => - { - Expect(data) - .To.Have.Property("id") - .With.Value(2); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("expected to find value of 2 for property 'id', but found 1") - ); - - Assert.That( - () => - { - Expect(data) - .Not.To.Have.Property("id") - .With.Value(2); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("not to have property 'id'") - ); - // Assert - } + Expect(data) + .To.Have.Property("id") + .With.Value(1); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void ShouldFacilitateTypeTesting() - { - // Arrange - var data = new Data() + [Test] + public void ShouldHandlePropertyNameMismatch() + { + // Arrange + var badData = new + { + foo = "bar" + }; + // Act + Assert.That( + () => { - id = 1 - }; - // Act - Assert.That( - () => - { - Expect(data) - .To.Have.Property("id") - .With.Type(typeof(int)) - .And.Value(1); - }, - Throws.Nothing - ); - - Assert.That( - () => - { - Expect(data) - .To.Have.Property("id") - .Of.Type(typeof(int)) - .With.Value(1); - }, - Throws.Nothing - ); - - Assert.That( - () => - { - Expect(data) - .To.Have.Property("id") - .With.Value(1) - .And.Type(typeof(int)); - }, - Throws.Nothing - ); + Expect(badData) + .To.Have.Property("id") + .With.Value(1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + $"{badData.GetType().PrettyName()} to have a public property named 'id'" + ) + ); + Assert.That( + () => + { + Expect(badData) + .Not.To.Have.Property("id") + .With.Value(1); + }, + Throws.Nothing + ); + // Assert + } - Assert.That( - () => - { - Expect(data) - .To.Have.Property("id") - .With.Type(typeof(string)) - .And.Value(1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - "Expected property 'Data.id' to be of type 'System.String', but it has type 'System.Int32'" - ) - ); - // Assert - } + [Test] + public void ShouldHandleValueMismatchOnFoundProperty() + { + // Arrange + var data = new + { + id = 1 + }; + // Act + Assert.That( + () => + { + Expect(data) + .To.Have.Property("id") + .With.Value(2); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("expected to find value of 2 for property 'id', but found 1") + ); + + Assert.That( + () => + { + Expect(data) + .Not.To.Have.Property("id") + .With.Value(2); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("not to have property 'id'") + ); + // Assert + } - [Test] - public void ShouldBehaveOnTypeAsType() - { - // Arrange - var type = typeof(Data); - // Act - Assert.That( - () => - { - Expect(type) - .To.Have.Property(nameof(Data.id)) - .With.Type(typeof(int)); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void ShouldFacilitateTypeTesting() + { + // Arrange + var data = new Data() + { + id = 1 + }; + // Act + Assert.That( + () => + { + Expect(data) + .To.Have.Property("id") + .With.Type(typeof(int)) + .And.Value(1); + }, + Throws.Nothing + ); + + Assert.That( + () => + { + Expect(data) + .To.Have.Property("id") + .Of.Type(typeof(int)) + .With.Value(1); + }, + Throws.Nothing + ); + + Assert.That( + () => + { + Expect(data) + .To.Have.Property("id") + .With.Value(1) + .And.Type(typeof(int)); + }, + Throws.Nothing + ); + + Assert.That( + () => + { + Expect(data) + .To.Have.Property("id") + .With.Type(typeof(string)) + .And.Value(1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + "Expected property 'Data.id' to be of type 'System.String', but it has type 'System.Int32'" + ) + ); + // Assert + } - [Test] - public void ShouldBeAbleToAssertAgainstMethodAttributes() - { - // Arrange - var type = typeof(Data); - var sut = GetRandom(); - // Act - Assert.That(() => + [Test] + public void ShouldBehaveOnTypeAsType() + { + // Arrange + var type = typeof(Data); + // Act + Assert.That( + () => { Expect(type) - .To.Have.Method(nameof(Data.CommentedMethod)) - .With.Attribute(o => o.Comment == "moo-cakes"); - }, Throws.Nothing); - - Assert.That(() => - { - Expect(sut) - .To.Have.Method(nameof(Data.CommentedMethod)) - .With.Attribute(o => o.Comment == "moo-cakes"); - }, Throws.Nothing); - // Assert - } + .To.Have.Property(nameof(Data.id)) + .With.Type(typeof(int)); + }, + Throws.Nothing + ); + // Assert + } + + [Test] + public void ShouldBeAbleToAssertAgainstMethodAttributes() + { + // Arrange + var type = typeof(Data); + var sut = GetRandom(); + // Act + Assert.That(() => + { + Expect(type) + .To.Have.Method(nameof(Data.CommentedMethod)) + .With.Attribute(o => o.Comment == "moo-cakes"); + }, Throws.Nothing); - [Test] - public void ShouldBeAbleToAssertAgainstPropertyAttributes() + Assert.That(() => { - // Arrange - var data = GetRandom(); - // Act - Assert.That( - () => - { - Expect(data) - .To.Have.Property(nameof(data.IsCommented)) - .With.Attribute(); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(data) - .To.Have.Property(nameof(data.IsCommented)) - .With.Type() - .And - .With - .Attribute(o => o.Comment == "this is commented!"); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(data) - .To.Have.Property(nameof(data.IsCommented)) - .With.Type("I wanted a string!") - .And - .With.Attribute(o => o.Comment == "this is commented!"); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("I wanted a string!") - ); - Assert.That( - () => - { - Expect(data) + Expect(sut) + .To.Have.Method(nameof(Data.CommentedMethod)) + .With.Attribute(o => o.Comment == "moo-cakes"); + }, Throws.Nothing); + // Assert + } + + [Test] + public void ShouldBeAbleToAssertAgainstPropertyAttributes() + { + // Arrange + var data = GetRandom(); + // Act + Assert.That( + () => + { + Expect(data) + .To.Have.Property(nameof(data.IsCommented)) + .With.Attribute(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(data) .To.Have.Property(nameof(data.IsCommented)) .With.Type() .And - .With.Attribute(o => o.Comment == "moo"); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(data) - .To.Have.Property(nameof(data.IsCommented)) - .With.Attribute(); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + .With + .Attribute(o => o.Comment == "this is commented!"); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(data) + .To.Have.Property(nameof(data.IsCommented)) + .With.Type("I wanted a string!") + .And + .With.Attribute(o => o.Comment == "this is commented!"); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("I wanted a string!") + ); + Assert.That( + () => + { + Expect(data) + .To.Have.Property(nameof(data.IsCommented)) + .With.Type() + .And + .With.Attribute(o => o.Comment == "moo"); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(data) + .To.Have.Property(nameof(data.IsCommented)) + .With.Attribute(); + }, + Throws.Exception.InstanceOf() + ); + // Assert } + } - [TestFixture] - public class AssertingAgainstMethods + [TestFixture] + public class AssertingAgainstMethods + { + [Test] + public void ShouldBeAbleToAssertMethodPresence() { - [Test] - public void ShouldBeAbleToAssertMethodPresence() - { - // Arrange - var cow = new Cow(); - var goodMethod = nameof(cow.Moo); - var badMethod = "MooMoo"; - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(goodMethod); - }, - Throws.Nothing - ); - - Assert.That( - () => - { - Expect(cow) - .Not.To.Have.Method(goodMethod); - }, - Throws.Exception.InstanceOf() - ); - - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(badMethod); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(badMethod) - ); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertMethodPresenceOnType() - { - // Arrange - var cow = typeof(Cow); - var goodMethod = nameof(Cow.Moo); - var badMethod = "MooMoo"; - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(goodMethod); - }, - Throws.Nothing - ); - - Assert.That( - () => - { - Expect(cow) - .Not.To.Have.Method(goodMethod); - }, - Throws.Exception.InstanceOf() - ); - - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(badMethod); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(badMethod) - ); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertMethodAttributes() - { - // Arrange - var cow = new Cow(); - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(cow.Moo)) - .With.Attribute(); - Expect(cow) - .To.Have.Method(nameof(cow.Moo)) - .With.Attribute( - c => c.Comment == "it's what cows do" - ); - }, - Throws.Nothing - ); - - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(Cow.NotMoo)) - .With.Attribute( - c => c.Comment == "it's what cows do" - ); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("[Comment]") - ); - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(Cow.Moo)) - .With.Attribute( - c => c.Comment == "it's not what cows do" - ); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("[Comment]") - ); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertMethodDoesNotHaveAttribute() - { - // Arrange - var cow = new Cow(); - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(Cow.NotMoo)) - .Without.Attribute(); - }, - Throws.Nothing - ); - // Assert - } + // Arrange + var cow = new Cow(); + var goodMethod = nameof(cow.Moo); + var badMethod = "MooMoo"; + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(goodMethod); + }, + Throws.Nothing + ); + + Assert.That( + () => + { + Expect(cow) + .Not.To.Have.Method(goodMethod); + }, + Throws.Exception.InstanceOf() + ); + + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(badMethod); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(badMethod) + ); + // Assert + } - [Test] - public void ShouldBeAbleToDiscriminateBetweenOverloads() - { - // Arrange - var cow = new Cow(); + [Test] + public void ShouldBeAbleToAssertMethodPresenceOnType() + { + // Arrange + var cow = typeof(Cow); + var goodMethod = nameof(Cow.Moo); + var badMethod = "MooMoo"; + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(goodMethod); + }, + Throws.Nothing + ); + + Assert.That( + () => + { + Expect(cow) + .Not.To.Have.Method(goodMethod); + }, + Throws.Exception.InstanceOf() + ); + + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(badMethod); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(badMethod) + ); + // Assert + } - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method( - "NotMoo2", - mi => !mi.GetParameters().Any() - ); - Expect(cow) - .To.Have.Method( - "NotMoo2", - mi => mi.GetParameters().FirstOrDefault()?.ParameterType == typeof(string) - ); - }, - Throws.Nothing - ); + [Test] + public void ShouldBeAbleToAssertMethodAttributes() + { + // Arrange + var cow = new Cow(); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.Moo)) + .With.Attribute(); + Expect(cow) + .To.Have.Method(nameof(cow.Moo)) + .With.Attribute( + c => c.Comment == "it's what cows do" + ); + }, + Throws.Nothing + ); - Assert.That( - () => - { - Expect(cow) - .Not.To.Have.Method( - "NotMoo2", - mi => !mi.GetParameters().Any() - ); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(cow) - .To.Have.Method("NotMoo2"); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("single") - ); - // Assert - } + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(Cow.NotMoo)) + .With.Attribute( + c => c.Comment == "it's what cows do" + ); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("[Comment]") + ); + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(Cow.Moo)) + .With.Attribute( + c => c.Comment == "it's not what cows do" + ); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("[Comment]") + ); + // Assert + } - [Test] - public void ShouldBeAbleToAssertMethodParametersAgainstInstance() - { - // Arrange - var cow = new Cow(); - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(cow.Add)) - .With.Parameter("a") - .Of.Type(typeof(int)); - Expect(cow) - .To.Have.Method(nameof(cow.Add)) - .With.Parameter("b") - .Of.Type(typeof(int)); - Expect(cow) - .To.Have.Method(nameof(cow.Add)) - .Which.Returns(typeof(int)); - Expect(cow) - .To.Have.Method(nameof(cow.Echo)) - .With.Parameter() - .Of.Type(); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void ShouldBeAbleToAssertMethodDoesNotHaveAttribute() + { + // Arrange + var cow = new Cow(); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(Cow.NotMoo)) + .Without.Attribute(); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void ShouldBeAbleToAssertMethodParametersAgainstType() - { - // Arrange - var cow = typeof(Cow); - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(Cow.Add)) - .With.Parameter("a") - .Of.Type(typeof(int)); - Expect(cow) - .To.Have.Method(nameof(Cow.Add)) - .With.Parameter("b") - .Of.Type(typeof(int)); - Expect(cow) - .To.Have.Method(nameof(Cow.Add)) - .Which.Returns(typeof(int)); - Expect(cow) - .To.Have.Method(nameof(Cow.Echo)) - .With.Parameter() - .Of.Type(); - }, - Throws.Nothing - ); - // Assert - } + [Test] + public void ShouldBeAbleToDiscriminateBetweenOverloads() + { + // Arrange + var cow = new Cow(); - [TestFixture] - public class OptionalAndRequiredParameters - { - [Test] - public void ShouldBeAbleToAssertOptionalParameterOnType() - { - // Arrange - var type = typeof(Cow); - // Act - Assert.That( - () => - { - Expect(type) - .To.Have.Method("Random") - .With.Optional.Parameter("minimum") - .With.Type(typeof(int)); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(type) - .To.Have.Method("Random") - .With.Required.Parameter("minimum") - .With.Type(typeof(int)); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - "parameter minimum on Cow.Random to be required" - ) + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method( + "NotMoo2", + mi => !mi.GetParameters().Any() ); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertOptionalParameterOnMethodInfo() - { - // Arrange - var method = typeof(Cow) - .GetMethod(nameof(Cow.Random)); - // Act - Assert.That( - () => - { - Expect(method) - .To.Have.Optional.Parameter("minimum") - .With.Type(typeof(int)); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(method) - .To.Have.Required.Parameter("minimum") - .With.Type(typeof(int)); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - "parameter minimum on Cow.Random to be required" - ) + Expect(cow) + .To.Have.Method( + "NotMoo2", + mi => mi.GetParameters().FirstOrDefault()?.ParameterType == typeof(string) ); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertRequiredParameterOnType() - { - // Arrange - var type = typeof(Cow); - // Act - Assert.That( - () => - { - Expect(type) - .To.Have.Method("Add") - .With.Required.Parameter("a") - .With.Type(typeof(int)); - }, - Throws.Nothing - ); + }, + Throws.Nothing + ); - Assert.That( - () => - { - Expect(type) - .To.Have.Method("Add") - .With.Optional.Parameter("a") - .With.Type(typeof(int)); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("parameter a on Cow.Add to be optional") - ); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertRequiredParameterOnMethodInfo() - { - // Arrange - var method = typeof(Cow) - .GetMethod(nameof(Cow.Add)); - // Act - Assert.That( - () => - { - Expect(method) - .To.Have.Required.Parameter("a") - .With.Type(typeof(int)); - }, - Throws.Nothing - ); + Assert.That( + () => + { + Expect(cow) + .Not.To.Have.Method( + "NotMoo2", + mi => !mi.GetParameters().Any() + ); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(cow) + .To.Have.Method("NotMoo2"); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("single") + ); + // Assert + } - Assert.That( - () => - { - Expect(method) - .To.Have.Optional.Parameter("a") - .With.Type(typeof(int)); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("parameter a on Cow.Add to be optional") - ); - // Assert - } - } + [Test] + public void ShouldBeAbleToAssertMethodParametersAgainstInstance() + { + // Arrange + var cow = new Cow(); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.Add)) + .With.Parameter("a") + .Of.Type(typeof(int)); + Expect(cow) + .To.Have.Method(nameof(cow.Add)) + .With.Parameter("b") + .Of.Type(typeof(int)); + Expect(cow) + .To.Have.Method(nameof(cow.Add)) + .Which.Returns(typeof(int)); + Expect(cow) + .To.Have.Method(nameof(cow.Echo)) + .With.Parameter() + .Of.Type(); + }, + Throws.Nothing + ); + // Assert + } - [Test] - public void ShouldBeAbleToAssertParameterless() - { - // Arrange - var cow = new Cow(); - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(cow.NotMoo)) - .With.No.Parameters(); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(cow.Add)) - .With.No.Parameters(); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + [Test] + public void ShouldBeAbleToAssertMethodParametersAgainstType() + { + // Arrange + var cow = typeof(Cow); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(Cow.Add)) + .With.Parameter("a") + .Of.Type(typeof(int)); + Expect(cow) + .To.Have.Method(nameof(Cow.Add)) + .With.Parameter("b") + .Of.Type(typeof(int)); + Expect(cow) + .To.Have.Method(nameof(Cow.Add)) + .Which.Returns(typeof(int)); + Expect(cow) + .To.Have.Method(nameof(Cow.Echo)) + .With.Parameter() + .Of.Type(); + }, + Throws.Nothing + ); + // Assert + } + [TestFixture] + public class OptionalAndRequiredParameters + { [Test] - public void ShouldBeAbleToAssertMethodParameterAttributes() + public void ShouldBeAbleToAssertOptionalParameterOnType() { // Arrange - var cow = new Cow(); + var type = typeof(Cow); // Act Assert.That( () => { - Expect(cow) - .To.Have.Method(nameof(cow.Echo)) - .With.Parameter("toEcho") - .Of.Type() - .With.Attribute(); - Expect(cow) - .To.Have.Method(nameof(cow.Echo)) - .With.Parameter() - .Of.Type() - .With.Attribute(); + Expect(type) + .To.Have.Method("Random") + .With.Optional.Parameter("minimum") + .With.Type(typeof(int)); }, Throws.Nothing ); Assert.That( () => { - Expect(cow) - .To.Have.Method(nameof(cow.Echo)) - .With.Parameter() - .Of.Type() - .With.Attribute(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("[NotUsed]") - ); - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(cow.Echo)) - .With.Parameter("toEcho") - .Of.Type() - .With.Attribute(); + Expect(type) + .To.Have.Method("Random") + .With.Required.Parameter("minimum") + .With.Type(typeof(int)); }, Throws.Exception.InstanceOf() - .With.Message.Match(".*'toEcho'.*\\[NotUsed\\]") - ); + .With.Message.Contains( + "parameter minimum on Cow.Random to be required" + ) + ); // Assert } [Test] - public void ShouldBeAbleToAssertAgainstMultipleMethodAttributes() + public void ShouldBeAbleToAssertOptionalParameterOnMethodInfo() { // Arrange - var cow = new Cow(); + var method = typeof(Cow) + .GetMethod(nameof(Cow.Random)); // Act Assert.That( () => { - Expect(cow) - .To.Have.Method(nameof(Cow.MultipleComments)) - .With.Attribute( - a => a.Comment.Contains("best of times") - ); - Expect(cow) - .To.Have.Method(nameof(Cow.MultipleComments)) - .With.Attribute( - a => a.Comment.Contains("dark and stormy night") - ); + Expect(method) + .To.Have.Optional.Parameter("minimum") + .With.Type(typeof(int)); }, Throws.Nothing ); Assert.That( () => { - Expect(cow) - .To.Have.Method(nameof(Cow.MultipleComments)) - .With.Attribute( - o => o.Comment.Contains("yabba dabba doo") - ); + Expect(method) + .To.Have.Required.Parameter("minimum") + .With.Type(typeof(int)); }, Throws.Exception.InstanceOf() - ); - // Assert - } - - [Test] - public void ShouldBeAbleToAssertAgainstParameterThenMethodInfo() - { - // Arrange - var cow = new Cow(); - // Act - Assert.That( - () => - { - Expect(cow) - .To.Have.Method(nameof(cow.Echo)) - .With.Parameter() - .Of.Type() - .With.Attribute(); - Expect(cow) - .To.Have.Method(nameof(cow.Echo)) - .With.Attribute(); - }, - Throws.Nothing - ); - + .With.Message.Contains( + "parameter minimum on Cow.Random to be required" + ) + ); // Assert } - } - [TestFixture] - public class AssertingAgainstTypes - { [Test] - public void ShouldBeAbleToAssertTypeHasAttribute() + public void ShouldBeAbleToAssertRequiredParameterOnType() { // Arrange + var type = typeof(Cow); // Act Assert.That( () => { - Expect(typeof(HasAttribute)) - .To.Have.Attribute(); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(typeof(HasAttribute)) - .Not.To.Have.Attribute(); - }, - Throws.Exception.InstanceOf() - .With.Message.Match("HasAttribute.*\\[Comment\\]") - ); - - Assert.That( - () => - { - Expect(typeof(HasNoAttribute)) - .Not.To.Have.Attribute(); + Expect(type) + .To.Have.Method("Add") + .With.Required.Parameter("a") + .With.Type(typeof(int)); }, Throws.Nothing ); @@ -837,133 +620,349 @@ public void ShouldBeAbleToAssertTypeHasAttribute() Assert.That( () => { - Expect(typeof(HasNoAttribute)) - .To.Have.Attribute(); + Expect(type) + .To.Have.Method("Add") + .With.Optional.Parameter("a") + .With.Type(typeof(int)); }, Throws.Exception.InstanceOf() - .With.Message.Match("HasNoAttribute.*\\[Comment\\]") + .With.Message.Contains("parameter a on Cow.Add to be optional") ); - // Assert } [Test] - public void ShouldBeAbleToAssertAgainstAttributeData() + public void ShouldBeAbleToAssertRequiredParameterOnMethodInfo() { // Arrange + var method = typeof(Cow) + .GetMethod(nameof(Cow.Add)); // Act Assert.That( () => { - Expect(typeof(HasAttribute)) - .To.Have.Attribute( - a => a.Comment == "has a comment" - ); + Expect(method) + .To.Have.Required.Parameter("a") + .With.Type(typeof(int)); }, Throws.Nothing ); + Assert.That( () => { - Expect(typeof(HasAttribute)) - .To.Have.Attribute( - a => a.Comment == "has no comment" - ); + Expect(method) + .To.Have.Optional.Parameter("a") + .With.Type(typeof(int)); }, Throws.Exception.InstanceOf() + .With.Message.Contains("parameter a on Cow.Add to be optional") ); // Assert } + } - [Comment("has a comment")] - public class HasAttribute - { - } + [Test] + public void ShouldBeAbleToAssertParameterless() + { + // Arrange + var cow = new Cow(); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.NotMoo)) + .With.No.Parameters(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.Add)) + .With.No.Parameters(); + }, + Throws.Exception.InstanceOf() + ); + // Assert + } - public class HasNoAttribute - { - } + [Test] + public void ShouldBeAbleToAssertMethodParameterAttributes() + { + // Arrange + var cow = new Cow(); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.Echo)) + .With.Parameter("toEcho") + .Of.Type() + .With.Attribute(); + Expect(cow) + .To.Have.Method(nameof(cow.Echo)) + .With.Parameter() + .Of.Type() + .With.Attribute(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.Echo)) + .With.Parameter() + .Of.Type() + .With.Attribute(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("[NotUsed]") + ); + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.Echo)) + .With.Parameter("toEcho") + .Of.Type() + .With.Attribute(); + }, + Throws.Exception.InstanceOf() + .With.Message.Match(".*'toEcho'.*\\[NotUsed\\]") + ); + // Assert } - public class NotNullAttribute : Attribute + [Test] + public void ShouldBeAbleToAssertAgainstMultipleMethodAttributes() { + // Arrange + var cow = new Cow(); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(Cow.MultipleComments)) + .With.Attribute( + a => a.Comment.Contains("best of times") + ); + Expect(cow) + .To.Have.Method(nameof(Cow.MultipleComments)) + .With.Attribute( + a => a.Comment.Contains("dark and stormy night") + ); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(Cow.MultipleComments)) + .With.Attribute( + o => o.Comment.Contains("yabba dabba doo") + ); + }, + Throws.Exception.InstanceOf() + ); + // Assert } - public class NotUsedAttribute : Attribute + [Test] + public void ShouldBeAbleToAssertAgainstParameterThenMethodInfo() { + // Arrange + var cow = new Cow(); + // Act + Assert.That( + () => + { + Expect(cow) + .To.Have.Method(nameof(cow.Echo)) + .With.Parameter() + .Of.Type() + .With.Attribute(); + Expect(cow) + .To.Have.Method(nameof(cow.Echo)) + .With.Attribute(); + }, + Throws.Nothing + ); + + // Assert } + } - public class SuperCow : Cow + [TestFixture] + public class AssertingAgainstTypes + { + [Test] + public void ShouldBeAbleToAssertTypeHasAttribute() { - [Comment("super-cow")] - public override void Overrideable() - { - base.Overrideable(); - } + // Arrange + // Act + Assert.That( + () => + { + Expect(typeof(HasAttribute)) + .To.Have.Attribute(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(typeof(HasAttribute)) + .Not.To.Have.Attribute(); + }, + Throws.Exception.InstanceOf() + .With.Message.Match("HasAttribute.*\\[Comment\\]") + ); + + Assert.That( + () => + { + Expect(typeof(HasNoAttribute)) + .Not.To.Have.Attribute(); + }, + Throws.Nothing + ); + + Assert.That( + () => + { + Expect(typeof(HasNoAttribute)) + .To.Have.Attribute(); + }, + Throws.Exception.InstanceOf() + .With.Message.Match("HasNoAttribute.*\\[Comment\\]") + ); + + // Assert + } + + [Test] + public void ShouldBeAbleToAssertAgainstAttributeData() + { + // Arrange + // Act + Assert.That( + () => + { + Expect(typeof(HasAttribute)) + .To.Have.Attribute( + a => a.Comment == "has a comment" + ); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(typeof(HasAttribute)) + .To.Have.Attribute( + a => a.Comment == "has no comment" + ); + }, + Throws.Exception.InstanceOf() + ); + // Assert } - [AttributeUsage( - AttributeTargets.All, - AllowMultiple = true, - Inherited = true - )] - public class CommentAttribute : Attribute + [Comment("has a comment")] + public class HasAttribute { - public string Comment { get; } + } - public CommentAttribute( - string comment - ) - { - Comment = comment; - } + public class HasNoAttribute + { } + } + + public class NotNullAttribute : Attribute + { + } + + public class NotUsedAttribute : Attribute + { + } - public class Cow + public class SuperCow : Cow + { + [Comment("super-cow")] + public override void Overrideable() { - [Comment("it's what cows do")] - public void Moo() - { - } + base.Overrideable(); + } + } - public void NotMoo() - { - } + [AttributeUsage( + AttributeTargets.All, + AllowMultiple = true, + Inherited = true + )] + public class CommentAttribute : Attribute + { + public string Comment { get; } - public void NotMoo2() - { - } + public CommentAttribute( + string comment + ) + { + Comment = comment; + } + } - public void NotMoo2(string msg) - { - Console.WriteLine(msg); - } + public class Cow + { + [Comment("it's what cows do")] + public void Moo() + { + } - public int Add(int a, int b) - { - return a + b; - } + public void NotMoo() + { + } - [Comment("echos")] - public string Echo([NotNull] string toEcho) - { - return $"echo: {toEcho}"; - } + public void NotMoo2() + { + } - public virtual void Overrideable() - { - } + public void NotMoo2(string msg) + { + Console.WriteLine(msg); + } - [Comment("It was a dark and stormy night")] - [Comment("It was the best of times, it was the worst of times")] - public void MultipleComments() - { - } + public int Add(int a, int b) + { + return a + b; + } - public int Random(int minimum = 0) - { - return GetRandomInt(minimum, minimum + 10); - } + [Comment("echos")] + public string Echo([NotNull] string toEcho) + { + return $"echo: {toEcho}"; + } + + public virtual void Overrideable() + { + } + + [Comment("It was a dark and stormy night")] + [Comment("It was the best of times, it was the worst of times")] + public void MultipleComments() + { + } + + public int Random(int minimum = 0) + { + return GetRandomInt(minimum, minimum + 10); } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/TestingApproximateEquality.cs b/src/NExpect.Tests/ObjectEquality/TestingApproximateEquality.cs index 35867a33..ec91ada6 100644 --- a/src/NExpect.Tests/ObjectEquality/TestingApproximateEquality.cs +++ b/src/NExpect.Tests/ObjectEquality/TestingApproximateEquality.cs @@ -1,611 +1,610 @@ -using System; +using System; using System.Collections.Generic; using NUnit.Framework; using NExpect.EqualityComparers; using NExpect.Exceptions; using PeanutButter.Utils; -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class TestingApproximateEquality { [TestFixture] - public class TestingApproximateEquality + public class DateTimeValues { - [TestFixture] - public class DateTimeValues + [Test] + public void WhenWithinASecond_ShouldNotThrow() { - [Test] - public void WhenWithinASecond_ShouldNotThrow() - { - // Arrange - var d1 = GetRandomDate(); - var d2 = d1.AddMilliseconds(GetRandomInt(-1000, 1000)); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - Expect(d2) - .To.Approximately.Equal(d1); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void WhenAtASecond_ShouldNotThrow() - { - // Arrange - var d1 = GetRandomDate(); - var d2 = d1.AddMilliseconds(1000); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - Expect(d2) - .To.Approximately.Equal(d1); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void WhenProvidedDrift_ShouldNotThrow() - { - // Arrange - var d1 = GetRandomDate(); - var allowedDrift = TimeSpan.FromSeconds(2); - var d2 = d1.AddMilliseconds(GetRandomInt(-2000, 2000)); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, allowedDrift); - Expect(d2) - .To.Approximately.Equal(d1, allowedDrift); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void - WhenOutsideOneSecond_ShouldThrow_IncludingCustomMessageIfPresent() - { - // Arrange - var d1 = GetRandomDate(); - var d2 = d1.AddMilliseconds(GetRandomInt(1001, 2000)); - var message = GetRandomString(1); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, () => message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - }, - Throws.Exception.InstanceOf() - .With.Message - .Contains( - $"Expected {d1.Stringify()} to approximately equal {d2.Stringify()}")); - // Assert - } - - [Test] - public void - WhenOutsideAllowedDrift_ShouldThrow_IncludingCustomMessageIfPresent() - { - // Arrange - var d1 = GetRandomDate(); - var d2 = d1.AddMilliseconds(GetRandomInt(501, 1000)); - var allowedDrift = TimeSpan.FromMilliseconds(500); - var message = GetRandomString(1); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, allowedDrift, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - Assert.That(() => - { - Expect(d1) - .To.Approximately - .Equal(d2, allowedDrift, () => message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - var d1String = d1.Stringify(); - var d2String = d2.Stringify(); - - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, allowedDrift); - }, - Throws.Exception.InstanceOf() - .With.Message - .Contains( - $"Expected {d1String} to approximately equal {d2String}")); - // Assert - } + // Arrange + var d1 = GetRandomDate(); + var d2 = d1.AddMilliseconds(GetRandomInt(-1000, 1000)); + // Pre-assert + // Act + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2); + Expect(d2) + .To.Approximately.Equal(d1); + }, + Throws.Nothing); + // Assert } - [TestFixture] - public class NullableDateTimeValues + [Test] + public void WhenAtASecond_ShouldNotThrow() { - [Test] - public void WhenWithinASecond_ShouldNotThrow() - { - // Arrange - var d1 = GetRandomDate() as DateTime?; - var d2 = d1.Value.AddMilliseconds(GetRandomInt(-1000, 1000)); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - Expect(d2) - .To.Approximately.Equal(d1.Value); - }, - Throws.Nothing); - Assert.That(() => + // Arrange + var d1 = GetRandomDate(); + var d2 = d1.AddMilliseconds(1000); + // Pre-assert + // Act + Assert.That(() => { + Expect(d1) + .To.Approximately.Equal(d2); Expect(d2) .To.Approximately.Equal(d1); - Expect(d2) - .To.Approximately.Equal(d1.Value); - }, Throws.Nothing); - // Assert - } - - [Test] - public void WhenAtASecond_ShouldNotThrow() - { - // Arrange - var d1 = GetRandomDate() as DateTime?; - var d2 = d1.Value.AddMilliseconds(1000); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - Expect(d2) - .To.Approximately.Equal(d1.Value); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void WhenProvidedDrift_ShouldNotThrow() - { - // Arrange - var d1 = GetRandomDate() as DateTime?; - var allowedDrift = TimeSpan.FromSeconds(2); - var d2 = d1.Value.AddMilliseconds(GetRandomInt(-2000, 2000)); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, allowedDrift); - Expect(d2) - .To.Approximately.Equal(d1.Value, allowedDrift); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void - WhenOutsideOneSecond_ShouldThrow_IncludingCustomMessageIfPresent() - { - // Arrange - var d1 = GetRandomDate() as DateTime?; - var d2 = d1.Value.AddMilliseconds(GetRandomInt(1001, 2000)); - var message = GetRandomString(1); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, () => message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - }, - Throws.Exception.InstanceOf() - .With.Message - .Contains( - $"Expected {d1.Stringify()} to approximately equal {d2.Stringify()}")); - // Assert - } - - [Test] - public void - WhenOutsideAllowedDrift_ShouldThrow_IncludingCustomMessageIfPresent() - { - // Arrange - var d1 = GetRandomDate() as DateTime?; - var d2 = d1.Value.AddMilliseconds(GetRandomInt(501, 1000)); - var allowedDrift = TimeSpan.FromMilliseconds(500); - var message = GetRandomString(1); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, allowedDrift, message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - Assert.That(() => - { - Expect(d1) - .To.Approximately - .Equal(d2, allowedDrift, () => message); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains(message)); - - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, allowedDrift); - }, - Throws.Exception.InstanceOf() - .With.Message - .Contains( - $"Expected {d1.Stringify()} to approximately equal {d2.Stringify()}")); - // Assert - } + }, + Throws.Nothing); + // Assert } - [TestFixture] - public class DecimalValues + [Test] + public void WhenProvidedDrift_ShouldNotThrow() { - [Test] - public void ShouldDefaultToEqualityWithinTwoDecimalPlacesRounded() - { - // Arrange - var d1 = 0.1234M; - var d2 = 0.12456M; - var d3 = 0.119M; - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - Expect(d1) - .To.Approximately.Equal(d3); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void ShouldThrowWhenNotApproximatelyEqual() - { - // Arrange - var d1 = 1.1M; - var d2 = 1.2M; - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("approximately equal")); - // Assert - } - - [Test] - public void ShouldAllowCustomEqualityTesting() - { - // Arrange - var d1 = 1.5M; - var d2 = 2.1M; - var comparer = new DecimalsEqualToDecimalPlacesRounded(0); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, comparer); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void ShouldFacilitateSpecifyingTheAllowableDrift() - { - // Arrange - var d1 = 1.10M; - var d2 = 1.15M; - - // Act - Assert.That(() => + // Arrange + var d1 = GetRandomDate(); + var allowedDrift = TimeSpan.FromSeconds(2); + var d2 = d1.AddMilliseconds(GetRandomInt(-2000, 2000)); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.1); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2, allowedDrift); + Expect(d2) + .To.Approximately.Equal(d1, allowedDrift); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void + WhenOutsideOneSecond_ShouldThrow_IncludingCustomMessageIfPresent() + { + // Arrange + var d1 = GetRandomDate(); + var d2 = d1.AddMilliseconds(GetRandomInt(1001, 2000)); + var message = GetRandomString(1); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.1M); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.1f); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2, () => message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 1); - }, Throws.Nothing); - - Assert.That(() => + .To.Approximately.Equal(d2); + }, + Throws.Exception.InstanceOf() + .With.Message + .Contains( + $"Expected {d1.Stringify()} to approximately equal {d2.Stringify()}")); + // Assert + } + + [Test] + public void + WhenOutsideAllowedDrift_ShouldThrow_IncludingCustomMessageIfPresent() + { + // Arrange + var d1 = GetRandomDate(); + var d2 = d1.AddMilliseconds(GetRandomInt(501, 1000)); + var allowedDrift = TimeSpan.FromMilliseconds(500); + var message = GetRandomString(1); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.01); - }, Throws.Exception.InstanceOf()); - Assert.That(() => + .To.Approximately.Equal(d2, allowedDrift, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + Assert.That(() => { Expect(d1) - .Not.To.Approximately.Equal(d2, within: 0.01); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately + .Equal(d2, allowedDrift, () => message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + var d1String = d1.Stringify(); + var d2String = d2.Stringify(); + + Assert.That(() => { Expect(d1) - .Not.To.Approximately.Equal(d2, within: 0.1); - }, Throws.Exception.InstanceOf()); - // Assert - } - [Test] - public void ShouldFacilitateTestingNullables() - { - // Arrange - var d1 = 1.10M as decimal?; - var d2 = 1.15M as decimal?; - - // Act - Assert.That(() => + .To.Approximately.Equal(d2, allowedDrift); + }, + Throws.Exception.InstanceOf() + .With.Message + .Contains( + $"Expected {d1String} to approximately equal {d2String}")); + // Assert + } + } + + [TestFixture] + public class NullableDateTimeValues + { + [Test] + public void WhenWithinASecond_ShouldNotThrow() + { + // Arrange + var d1 = GetRandomDate() as DateTime?; + var d2 = d1.Value.AddMilliseconds(GetRandomInt(-1000, 1000)); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.1); - }, Throws.Nothing); - Assert.That(() => - { - Expect(d1.Value) - .To.Approximately.Equal(d2, within: 0.1); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2); + Expect(d2) + .To.Approximately.Equal(d1.Value); + }, + Throws.Nothing); + Assert.That(() => + { + Expect(d2) + .To.Approximately.Equal(d1); + Expect(d2) + .To.Approximately.Equal(d1.Value); + }, Throws.Nothing); + // Assert + } + + [Test] + public void WhenAtASecond_ShouldNotThrow() + { + // Arrange + var d1 = GetRandomDate() as DateTime?; + var d2 = d1.Value.AddMilliseconds(1000); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.1M); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2); + Expect(d2) + .To.Approximately.Equal(d1.Value); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void WhenProvidedDrift_ShouldNotThrow() + { + // Arrange + var d1 = GetRandomDate() as DateTime?; + var allowedDrift = TimeSpan.FromSeconds(2); + var d2 = d1.Value.AddMilliseconds(GetRandomInt(-2000, 2000)); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.1f); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2, allowedDrift); + Expect(d2) + .To.Approximately.Equal(d1.Value, allowedDrift); + }, + Throws.Nothing); + // Assert + } + + [Test] + public void + WhenOutsideOneSecond_ShouldThrow_IncludingCustomMessageIfPresent() + { + // Arrange + var d1 = GetRandomDate() as DateTime?; + var d2 = d1.Value.AddMilliseconds(GetRandomInt(1001, 2000)); + var message = GetRandomString(1); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 1); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: (decimal?)0.1); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2, () => message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: (decimal?)0.1M); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2); + }, + Throws.Exception.InstanceOf() + .With.Message + .Contains( + $"Expected {d1.Stringify()} to approximately equal {d2.Stringify()}")); + // Assert + } + + [Test] + public void + WhenOutsideAllowedDrift_ShouldThrow_IncludingCustomMessageIfPresent() + { + // Arrange + var d1 = GetRandomDate() as DateTime?; + var d2 = d1.Value.AddMilliseconds(GetRandomInt(501, 1000)); + var allowedDrift = TimeSpan.FromMilliseconds(500); + var message = GetRandomString(1); + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: (decimal?)0.1f); - }, Throws.Nothing); - Assert.That(() => + .To.Approximately.Equal(d2, allowedDrift, message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: (decimal?)1); - }, Throws.Nothing); - - Assert.That(() => + .To.Approximately + .Equal(d2, allowedDrift, () => message); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains(message)); + + Assert.That(() => { Expect(d1) - .To.Approximately.Equal(d2, within: 0.01); - }, Throws.Exception.InstanceOf()); - Assert.That(() => + .To.Approximately.Equal(d2, allowedDrift); + }, + Throws.Exception.InstanceOf() + .With.Message + .Contains( + $"Expected {d1.Stringify()} to approximately equal {d2.Stringify()}")); + // Assert + } + } + + [TestFixture] + public class DecimalValues + { + [Test] + public void ShouldDefaultToEqualityWithinTwoDecimalPlacesRounded() + { + // Arrange + var d1 = 0.1234M; + var d2 = 0.12456M; + var d3 = 0.119M; + // Pre-assert + // Act + Assert.That(() => { Expect(d1) - .Not.To.Approximately.Equal(d2, within: 0.01); - }, Throws.Nothing); - Assert.That(() => - { + .To.Approximately.Equal(d2); Expect(d1) - .Not.To.Approximately.Equal(d2, within: 0.1); - }, Throws.Exception.InstanceOf()); - // Assert - } + .To.Approximately.Equal(d3); + }, + Throws.Nothing); + // Assert } - [TestFixture] - public class DoubleValues + [Test] + public void ShouldThrowWhenNotApproximatelyEqual() { - [Test] - public void ShouldDefaultToEqualityWithinTwoDecimalPlacesRounded() - { - // Arrange - var d1 = 0.1234D; - var d2 = 0.12456D; - var d3 = 0.119D; - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - Expect(d1) - .To.Approximately.Equal(d3); - }, - Throws.Nothing); - // Assert - } - - [Test] - public void ShouldThrowWhenNotApproximatelyEqual() - { - // Arrange - var d1 = 1.1D; - var d2 = 1.2D; - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("approxmiately equal")); - // Assert - } - - [Test] - public void ShouldAllowCustomEqualityTesting() - { - // Arrange - var d1 = 1.5D; - var d2 = 2.1D; - var comparer = new DoublesEqualToDecimalPlacesRounded(0); - // Pre-assert - // Act - Assert.That(() => - { - Expect(d1) - .To.Approximately.Equal(d2, comparer); - }, - Throws.Nothing); - // Assert - } + // Arrange + var d1 = 1.1M; + var d2 = 1.2M; + // Pre-assert + // Act + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("approximately equal")); + // Assert } - } - [TestFixture] - public class TestDecimalsEqualToDecimalPlacesTruncated - { - public static - IEnumerable<(decimal x, decimal y, int places, bool expected)> - Generator() + [Test] + public void ShouldAllowCustomEqualityTesting() { - yield return (1.123M, 1.129M, 2, true); - yield return (1.111M, 1.199M, 1, true); - yield return (1.1234M, 1.1243M, 3, false); + // Arrange + var d1 = 1.5M; + var d2 = 2.1M; + var comparer = new DecimalsEqualToDecimalPlacesRounded(0); + // Pre-assert + // Act + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, comparer); + }, + Throws.Nothing); + // Assert } - [TestCaseSource(nameof(Generator))] - public void ShouldReturnFor_( - (decimal x, - decimal y, - int places, - bool expected) testCase) + [Test] + public void ShouldFacilitateSpecifyingTheAllowableDrift() { // Arrange - var sut = Create(testCase.places); - // Pre-assert + var d1 = 1.10M; + var d2 = 1.15M; + // Act - var result = sut.Equals(testCase.x, testCase.y); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.1); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.1M); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.1f); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 1); + }, Throws.Nothing); + + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.01); + }, Throws.Exception.InstanceOf()); + Assert.That(() => + { + Expect(d1) + .Not.To.Approximately.Equal(d2, within: 0.01); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .Not.To.Approximately.Equal(d2, within: 0.1); + }, Throws.Exception.InstanceOf()); // Assert - Expect(result).To.Equal(testCase.expected); } - - private DecimalsEqualToDecimalPlacesTruncated Create( - int places) + [Test] + public void ShouldFacilitateTestingNullables() { - return new DecimalsEqualToDecimalPlacesTruncated(places); + // Arrange + var d1 = 1.10M as decimal?; + var d2 = 1.15M as decimal?; + + // Act + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.1); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1.Value) + .To.Approximately.Equal(d2, within: 0.1); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.1M); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.1f); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 1); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: (decimal?)0.1); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: (decimal?)0.1M); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: (decimal?)0.1f); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: (decimal?)1); + }, Throws.Nothing); + + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, within: 0.01); + }, Throws.Exception.InstanceOf()); + Assert.That(() => + { + Expect(d1) + .Not.To.Approximately.Equal(d2, within: 0.01); + }, Throws.Nothing); + Assert.That(() => + { + Expect(d1) + .Not.To.Approximately.Equal(d2, within: 0.1); + }, Throws.Exception.InstanceOf()); + // Assert } } [TestFixture] - public class TestDoublesEqualToDecimalPlacesTruncated + public class DoubleValues { - public static - IEnumerable<(double x, double y, int places, bool expected)> - Generator() + [Test] + public void ShouldDefaultToEqualityWithinTwoDecimalPlacesRounded() { - yield return (1.123D, 1.129D, 2, true); - yield return (1.111D, 1.199D, 1, true); - yield return (1.1234D, 1.1243D, 3, false); + // Arrange + var d1 = 0.1234D; + var d2 = 0.12456D; + var d3 = 0.119D; + // Pre-assert + // Act + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2); + Expect(d1) + .To.Approximately.Equal(d3); + }, + Throws.Nothing); + // Assert } - [TestCaseSource(nameof(Generator))] - public void ShouldReturnFor_( - (double x, - double y, - int places, - bool expected) testCase) + [Test] + public void ShouldThrowWhenNotApproximatelyEqual() { // Arrange - var sut = Create(testCase.places); + var d1 = 1.1D; + var d2 = 1.2D; // Pre-assert // Act - var result = sut.Equals(testCase.x, testCase.y); + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("approxmiately equal")); // Assert - Expect(result).To.Equal(testCase.expected); } - private DoublesEqualToDecimalPlacesTruncated Create( - int places) + [Test] + public void ShouldAllowCustomEqualityTesting() { - return new DoublesEqualToDecimalPlacesTruncated(places); + // Arrange + var d1 = 1.5D; + var d2 = 2.1D; + var comparer = new DoublesEqualToDecimalPlacesRounded(0); + // Pre-assert + // Act + Assert.That(() => + { + Expect(d1) + .To.Approximately.Equal(d2, comparer); + }, + Throws.Nothing); + // Assert } } -} \ No newline at end of file +} + +[TestFixture] +public class TestDecimalsEqualToDecimalPlacesTruncated +{ + public static + IEnumerable<(decimal x, decimal y, int places, bool expected)> + Generator() + { + yield return (1.123M, 1.129M, 2, true); + yield return (1.111M, 1.199M, 1, true); + yield return (1.1234M, 1.1243M, 3, false); + } + + [TestCaseSource(nameof(Generator))] + public void ShouldReturnFor_( + (decimal x, + decimal y, + int places, + bool expected) testCase) + { + // Arrange + var sut = Create(testCase.places); + // Pre-assert + // Act + var result = sut.Equals(testCase.x, testCase.y); + // Assert + Expect(result).To.Equal(testCase.expected); + } + + private DecimalsEqualToDecimalPlacesTruncated Create( + int places) + { + return new DecimalsEqualToDecimalPlacesTruncated(places); + } +} + +[TestFixture] +public class TestDoublesEqualToDecimalPlacesTruncated +{ + public static + IEnumerable<(double x, double y, int places, bool expected)> + Generator() + { + yield return (1.123D, 1.129D, 2, true); + yield return (1.111D, 1.199D, 1, true); + yield return (1.1234D, 1.1243D, 3, false); + } + + [TestCaseSource(nameof(Generator))] + public void ShouldReturnFor_( + (double x, + double y, + int places, + bool expected) testCase) + { + // Arrange + var sut = Create(testCase.places); + // Pre-assert + // Act + var result = sut.Equals(testCase.x, testCase.y); + // Assert + Expect(result).To.Equal(testCase.expected); + } + + private DoublesEqualToDecimalPlacesTruncated Create( + int places) + { + return new DoublesEqualToDecimalPlacesTruncated(places); + } +} diff --git a/src/NExpect.Tests/ObjectEquality/TestingBooleanValues.cs b/src/NExpect.Tests/ObjectEquality/TestingBooleanValues.cs index b3e04cc8..ffa3fdfe 100644 --- a/src/NExpect.Tests/ObjectEquality/TestingBooleanValues.cs +++ b/src/NExpect.Tests/ObjectEquality/TestingBooleanValues.cs @@ -5,335 +5,334 @@ // ReSharper disable ConditionIsAlwaysTrueOrFalse -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class TestingBooleanValues { - [TestFixture] - public class TestingBooleanValues + public class TrueEquality { - public class TrueEquality + [Test] + public void ToBeTrue_WhenValueIsTrue_ShouldNotThrow() { - [Test] - public void ToBeTrue_WhenValueIsTrue_ShouldNotThrow() - { - // Arrange - var value = true; - - // Pre-Assert - - // Act - Assert.That( - () => Expect(value).To.Be.True(), - Throws.Nothing - ); - - // Assert - } - - [Test] - public void ToBeTrue_WhenValueIsNullableTrue_ShouldNotThrow() - { - // Arrange - bool? value = null; - value = true; - // Pre-assert - // Act - Assert.That(() => - { - Expect(value).To.Be.True(); - }, Throws.Nothing); - // Assert - } - - [Test] - public void ToBeTrue_WhenValueIsFalse_ShouldThrow() + // Arrange + var value = true; + + // Pre-Assert + + // Act + Assert.That( + () => Expect(value).To.Be.True(), + Throws.Nothing + ); + + // Assert + } + + [Test] + public void ToBeTrue_WhenValueIsNullableTrue_ShouldNotThrow() + { + // Arrange + bool? value = null; + value = true; + // Pre-assert + // Act + Assert.That(() => { - // Arrange - var value = false; + Expect(value).To.Be.True(); + }, Throws.Nothing); + // Assert + } - // Pre-Assert + [Test] + public void ToBeTrue_WhenValueIsFalse_ShouldThrow() + { + // Arrange + var value = false; - // Act - Assert.That( - () => Expect(value).To.Be.True(), - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected\ntrue\nbut got\n{value.Stringify()}") - ); + // Pre-Assert - // Assert - } + // Act + Assert.That( + () => Expect(value).To.Be.True(), + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected\ntrue\nbut got\n{value.Stringify()}") + ); - [Test] - public void ToBeTrue_WhenValueIsFalse_GivenCustomMessage_ShouldThrow() - { - // Arrange - var value = false; - var message = RandomValueGen.GetRandomString(12); - - // Pre-Assert - - // Act - Assert.That( - () => Expect(value).To.Be.True(message), - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected\ntrue\nbut got\n{value.Stringify()}") - ); - - Assert.That( - () => Expect(value).To.Be.True(message), - Throws.Exception.InstanceOf() - .With.Message.Contains(message) - ); - - // Assert - } - - [Test] - public void NotToBeTrue_WhenValueIsFalse_ShouldNotThrow() - { - // Arrange - var value = false; + // Assert + } - // Pre-Assert + [Test] + public void ToBeTrue_WhenValueIsFalse_GivenCustomMessage_ShouldThrow() + { + // Arrange + var value = false; + var message = RandomValueGen.GetRandomString(12); + + // Pre-Assert + + // Act + Assert.That( + () => Expect(value).To.Be.True(message), + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected\ntrue\nbut got\n{value.Stringify()}") + ); + + Assert.That( + () => Expect(value).To.Be.True(message), + Throws.Exception.InstanceOf() + .With.Message.Contains(message) + ); + + // Assert + } - // Act - Assert.That( - () => Expect(value).Not.To.Be.True(), - Throws.Nothing - ); + [Test] + public void NotToBeTrue_WhenValueIsFalse_ShouldNotThrow() + { + // Arrange + var value = false; - // Assert - } + // Pre-Assert - [Test] - public void ToBeNotTrue_WhenValueIsFalse_ShouldNotThrow() - { - // Arrange - var value = false; + // Act + Assert.That( + () => Expect(value).Not.To.Be.True(), + Throws.Nothing + ); - // Pre-Assert + // Assert + } - // Act - Assert.That( - () => Expect(value).To.Be.Not.True(), - Throws.Nothing - ); + [Test] + public void ToBeNotTrue_WhenValueIsFalse_ShouldNotThrow() + { + // Arrange + var value = false; - // Assert - } + // Pre-Assert - [Test] - public void NotToBeTrue_WhenValueIsTrue_ShouldThrow() - { - // Arrange - var value = true; + // Act + Assert.That( + () => Expect(value).To.Be.Not.True(), + Throws.Nothing + ); + + // Assert + } - // Pre-Assert + [Test] + public void NotToBeTrue_WhenValueIsTrue_ShouldThrow() + { + // Arrange + var value = true; - // Act - Assert.That( - () => Expect(value).Not.To.Be.True(), - Throws.Exception.InstanceOf() - ); + // Pre-Assert - // Assert - } + // Act + Assert.That( + () => Expect(value).Not.To.Be.True(), + Throws.Exception.InstanceOf() + ); - [Test] - public void ToBeNotTrue_WhenValueIsTrue_ShouldThrow() - { - // Arrange - var value = true; + // Assert + } - // Pre-Assert + [Test] + public void ToBeNotTrue_WhenValueIsTrue_ShouldThrow() + { + // Arrange + var value = true; - // Act - Assert.That( - () => Expect(value).To.Be.Not.True(), - Throws.Exception.InstanceOf() - ); + // Pre-Assert - // Assert - } + // Act + Assert.That( + () => Expect(value).To.Be.Not.True(), + Throws.Exception.InstanceOf() + ); - [Test] - public void ShouldWorkOnNullableBooleans() - { - // Arrange - var value = true as bool?; + // Assert + } + + [Test] + public void ShouldWorkOnNullableBooleans() + { + // Arrange + var value = true as bool?; - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => Expect(value).To.Be.True(), - Throws.Nothing - ); + // Act + Assert.That( + () => Expect(value).To.Be.True(), + Throws.Nothing + ); - // Assert - } + // Assert } + } - public class FalseEquality + public class FalseEquality + { + [Test] + public void ToBeFalse_WhenValueIsFalse_ShouldNotThrow() { - [Test] - public void ToBeFalse_WhenValueIsFalse_ShouldNotThrow() - { - // Arrange - var value = false; + // Arrange + var value = false; - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => Expect(value).To.Be.False(), - Throws.Nothing - ); + // Act + Assert.That( + () => Expect(value).To.Be.False(), + Throws.Nothing + ); - // Assert - } + // Assert + } - [Test] - public void ToBeFalse_WhenValueIsNullableFalse_ShouldNotThrow() - { - // Arrange - bool? value = null; - value = false; - // Pre-assert - // Act - Assert.That(() => - { - Expect(value).To.Be.False(); - }, Throws.Nothing); - // Assert - } - - [Test] - public void ToBeFalse_WhenValueIsTrue_ShouldThrow() + [Test] + public void ToBeFalse_WhenValueIsNullableFalse_ShouldNotThrow() + { + // Arrange + bool? value = null; + value = false; + // Pre-assert + // Act + Assert.That(() => { - // Arrange - var value = true; + Expect(value).To.Be.False(); + }, Throws.Nothing); + // Assert + } - // Pre-Assert + [Test] + public void ToBeFalse_WhenValueIsTrue_ShouldThrow() + { + // Arrange + var value = true; - // Act - Assert.That( - () => Expect(value).To.Be.False(), - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected\nfalse\nbut got\n{value.Stringify()}") - ); + // Pre-Assert - // Assert - } + // Act + Assert.That( + () => Expect(value).To.Be.False(), + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected\nfalse\nbut got\n{value.Stringify()}") + ); - [Test] - public void ToBeFalse_WhenValueIsTrue_GivenCustomMessage_ShouldThrow() - { - // Arrange - var value = true; - var message = RandomValueGen.GetRandomString(12); - - // Pre-Assert - - // Act - Assert.That( - () => Expect(value).To.Be.False(message), - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected\nfalse\nbut got\n{value.Stringify()}") - ); - - Assert.That( - () => Expect(value).To.Be.False(message), - Throws.Exception.InstanceOf() - .With.Message.Contains(message) - ); - - // Assert - } - - [Test] - public void NotToBeFalse_WhenValueIsTrue_ShouldNotThrow() - { - // Arrange - var value = true; + // Assert + } - // Pre-Assert + [Test] + public void ToBeFalse_WhenValueIsTrue_GivenCustomMessage_ShouldThrow() + { + // Arrange + var value = true; + var message = RandomValueGen.GetRandomString(12); + + // Pre-Assert + + // Act + Assert.That( + () => Expect(value).To.Be.False(message), + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected\nfalse\nbut got\n{value.Stringify()}") + ); + + Assert.That( + () => Expect(value).To.Be.False(message), + Throws.Exception.InstanceOf() + .With.Message.Contains(message) + ); + + // Assert + } - // Act - Assert.That( - () => Expect(value).Not.To.Be.False(), - Throws.Nothing - ); + [Test] + public void NotToBeFalse_WhenValueIsTrue_ShouldNotThrow() + { + // Arrange + var value = true; - // Assert - } + // Pre-Assert - [Test] - public void ToBeNotFalse_WhenValueIsFalse_ShouldNotThrow() - { - // Arrange - var value = true; + // Act + Assert.That( + () => Expect(value).Not.To.Be.False(), + Throws.Nothing + ); - // Pre-Assert + // Assert + } - // Act - Assert.That( - () => Expect(value).To.Be.Not.False(), - Throws.Nothing - ); + [Test] + public void ToBeNotFalse_WhenValueIsFalse_ShouldNotThrow() + { + // Arrange + var value = true; - // Assert - } + // Pre-Assert - [Test] - public void NotToBeFalse_WhenValueIsFalse_ShouldThrow() - { - // Arrange - var value = false; + // Act + Assert.That( + () => Expect(value).To.Be.Not.False(), + Throws.Nothing + ); - // Pre-Assert + // Assert + } - // Act - Assert.That( - () => Expect(value).Not.To.Be.False(), - Throws.Exception.InstanceOf() - ); + [Test] + public void NotToBeFalse_WhenValueIsFalse_ShouldThrow() + { + // Arrange + var value = false; - // Assert - } + // Pre-Assert - [Test] - public void ToBeNotFalse_WhenValueIsFalse_ShouldThrow() - { - // Arrange - var value = false; + // Act + Assert.That( + () => Expect(value).Not.To.Be.False(), + Throws.Exception.InstanceOf() + ); - // Pre-Assert + // Assert + } + + [Test] + public void ToBeNotFalse_WhenValueIsFalse_ShouldThrow() + { + // Arrange + var value = false; - // Act - Assert.That( - () => Expect(value).To.Be.Not.False(), - Throws.Exception.InstanceOf() - ); + // Pre-Assert - // Assert - } + // Act + Assert.That( + () => Expect(value).To.Be.Not.False(), + Throws.Exception.InstanceOf() + ); - [Test] - public void ShouldWorkOnNullableBooleans() - { - // Arrange - var value = false as bool?; + // Assert + } + + [Test] + public void ShouldWorkOnNullableBooleans() + { + // Arrange + var value = false as bool?; - // Pre-Assert + // Pre-Assert - // Act - Assert.That( - () => Expect(value).To.Be.False(), - Throws.Nothing - ); + // Act + Assert.That( + () => Expect(value).To.Be.False(), + Throws.Nothing + ); - // Assert - } + // Assert } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/TestingForNull.cs b/src/NExpect.Tests/ObjectEquality/TestingForNull.cs index 5923e936..5f145dad 100644 --- a/src/NExpect.Tests/ObjectEquality/TestingForNull.cs +++ b/src/NExpect.Tests/ObjectEquality/TestingForNull.cs @@ -1,138 +1,137 @@ -using NUnit.Framework; +using NUnit.Framework; using NExpect.Exceptions; // ReSharper disable InconsistentNaming // ReSharper disable ExpressionIsAlwaysNull -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class TestingForNull { [TestFixture] - public class TestingForNull + public class Expect_Value_To { [TestFixture] - public class Expect_Value_To + public class Be { [TestFixture] - public class Be + public class Null { - [TestFixture] - public class Null + [Test] + public void OperatingOnString_WhenIsNull_ShouldNotThrow() { - [Test] - public void OperatingOnString_WhenIsNull_ShouldNotThrow() - { - // Arrange - var input = null as string; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null(); - }, - Throws.Nothing); - // Assert - } + // Arrange + var input = null as string; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void OperatingOnStringNegated_WhenIsNull_ShouldThrow() - { - // Arrange - var input = null as string; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).Not.To.Be.Null(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("Expected not to get null")); - // Assert - } + [Test] + public void OperatingOnStringNegated_WhenIsNull_ShouldThrow() + { + // Arrange + var input = null as string; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).Not.To.Be.Null(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("Expected not to get null")); + // Assert + } - [Test] - public void - OperatingOnStringNegated_GivenCustomMessage_WhenIsNull_ShouldThrowIncludingCustomMessage() - { - // Arrange - var input = null as string; - var expected = GetRandomString(); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).Not.To.Be.Null(expected); - }, - Throws.Exception.InstanceOf() - .With.Message.EqualTo($"{expected}\n\nExpected not to get null")); - // Assert - } + [Test] + public void + OperatingOnStringNegated_GivenCustomMessage_WhenIsNull_ShouldThrowIncludingCustomMessage() + { + // Arrange + var input = null as string; + var expected = GetRandomString(); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).Not.To.Be.Null(expected); + }, + Throws.Exception.InstanceOf() + .With.Message.EqualTo($"{expected}\n\nExpected not to get null")); + // Assert + } - [Test] - public void OperatingOnObjectAltNegated_WhenIsNull_ShouldThrow() - { - // Arrange - var input = null as object; - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("Expected not to get null")); - // Assert - } + [Test] + public void OperatingOnObjectAltNegated_WhenIsNull_ShouldThrow() + { + // Arrange + var input = null as object; + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("Expected not to get null")); + // Assert + } - [Test] - public void OperatingOnString_WhenIsNotNull_ShouldThrow() - { - // Arrange - var input = GetRandomString(); - Assert.That(input, Is.Not.Null); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Be.Null(); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains($"Expected null but got\n\"{input}\"")); - // Assert - } + [Test] + public void OperatingOnString_WhenIsNotNull_ShouldThrow() + { + // Arrange + var input = GetRandomString(); + Assert.That(input, Is.Not.Null); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Be.Null(); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains($"Expected null but got\n\"{input}\"")); + // Assert + } - [Test] - public void OperatingOnString_Negated_WhenIsNotNull_ShouldNotThrow() - { - // Arrange - var input = GetRandomString(); - Assert.That(input, Is.Not.Null); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).Not.To.Be.Null(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void OperatingOnString_Negated_WhenIsNotNull_ShouldNotThrow() + { + // Arrange + var input = GetRandomString(); + Assert.That(input, Is.Not.Null); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).Not.To.Be.Null(); + }, + Throws.Nothing); + // Assert + } - [Test] - public void OperatingOnString_AltNegated_WhenIsNotNull_ShouldNotThrow() - { - // Arrange - var input = GetRandomString(); - Assert.That(input, Is.Not.Null); - // Pre-Assert - // Act - Assert.That(() => - { - Expect(input).To.Not.Be.Null(); - }, - Throws.Nothing); - // Assert - } + [Test] + public void OperatingOnString_AltNegated_WhenIsNotNull_ShouldNotThrow() + { + // Arrange + var input = GetRandomString(); + Assert.That(input, Is.Not.Null); + // Pre-Assert + // Act + Assert.That(() => + { + Expect(input).To.Not.Be.Null(); + }, + Throws.Nothing); + // Assert } } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/ObjectEquality/TestingNullableTypes.cs b/src/NExpect.Tests/ObjectEquality/TestingNullableTypes.cs index cc1becae..3d00ac74 100644 --- a/src/NExpect.Tests/ObjectEquality/TestingNullableTypes.cs +++ b/src/NExpect.Tests/ObjectEquality/TestingNullableTypes.cs @@ -1,67 +1,66 @@ -using NUnit.Framework; +using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests.ObjectEquality +namespace NExpect.Tests.ObjectEquality; + +[TestFixture] +public class TestingNullableTypes { [TestFixture] - public class TestingNullableTypes + public class NullableIntComparedToInt { - [TestFixture] - public class NullableIntComparedToInt + [Test] + public void PositiveAssertion_WhenAreEqual_ShouldNotThrow() { - [Test] - public void PositiveAssertion_WhenAreEqual_ShouldNotThrow() + // Arrange + var expected = GetRandomInt(1) as int?; + var actual = expected.Value; + + // Pre-Assert + + // Act + Assert.That(() => { - // Arrange - var expected = GetRandomInt(1) as int?; - var actual = expected.Value; + Expect(actual).To.Equal(expected); + }, Throws.Nothing); - // Pre-Assert + // Assert + } - // Act - Assert.That(() => - { - Expect(actual).To.Equal(expected); - }, Throws.Nothing); + [Test] + public void NegativeAssertion_WhenAreEqual_ShouldThrow() + { + // Arrange + var expected = GetRandomInt(1) as int?; + var actual = expected.Value; - // Assert - } + // Pre-Assert - [Test] - public void NegativeAssertion_WhenAreEqual_ShouldThrow() + // Act + Assert.That(() => { - // Arrange - var expected = GetRandomInt(1) as int?; - var actual = expected.Value; + Expect(actual).Not.To.Equal(expected); + }, Throws.Exception.InstanceOf()); - // Pre-Assert + // Assert + } - // Act - Assert.That(() => - { - Expect(actual).Not.To.Equal(expected); - }, Throws.Exception.InstanceOf()); + [Test] + public void NegativeAssertion_AltGrammar_WhenAreEqual_ShouldThrow() + { + // Arrange + var expected = GetRandomInt(1) as int?; + var actual = expected.Value; - // Assert - } + // Pre-Assert - [Test] - public void NegativeAssertion_AltGrammar_WhenAreEqual_ShouldThrow() + // Act + Assert.That(() => { - // Arrange - var expected = GetRandomInt(1) as int?; - var actual = expected.Value; - - // Pre-Assert - - // Act - Assert.That(() => - { - Expect(actual).To.Not.Equal(expected); - }, Throws.Exception.InstanceOf()); + Expect(actual).To.Not.Equal(expected); + }, Throws.Exception.InstanceOf()); - // Assert - } + // Assert } } } diff --git a/src/NExpect.Tests/SetupFixture.cs b/src/NExpect.Tests/SetupFixture.cs index 3c7f9a59..793faf1a 100644 --- a/src/NExpect.Tests/SetupFixture.cs +++ b/src/NExpect.Tests/SetupFixture.cs @@ -1,22 +1,21 @@ using NUnit.Framework; -namespace NExpect.Tests +namespace NExpect.Tests; + +[SetUpFixture] +public class SetupFixture { - [SetUpFixture] - public class SetupFixture + [OneTimeSetUp] + public void OneTimeSetUp() { - [OneTimeSetUp] - public void OneTimeSetUp() - { - TestUtils.ForceMessageLineBreaks(); - Assertions.EnableTracking(); - } + TestUtils.ForceMessageLineBreaks(); + Assertions.EnableTracking(); + } - [OneTimeTearDown] - public void OneTimeTearDown() - { - Assertions.VerifyNoIncompleteAssertions(); - } + [OneTimeTearDown] + public void OneTimeTearDown() + { + Assertions.VerifyNoIncompleteAssertions(); } +} -} \ No newline at end of file diff --git a/src/NExpect.Tests/Shims/TestDictionaryShim.cs b/src/NExpect.Tests/Shims/TestDictionaryShim.cs index b2b165ec..19730af0 100644 --- a/src/NExpect.Tests/Shims/TestDictionaryShim.cs +++ b/src/NExpect.Tests/Shims/TestDictionaryShim.cs @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; @@ -10,286 +10,285 @@ // ReSharper disable PossibleMultipleEnumeration // ReSharper disable InconsistentNaming -namespace NExpect.Tests.Shims +namespace NExpect.Tests.Shims; + +[TestFixture] +public class TestDictionaryShim { + [Test] + public void ShouldImplement_IDictionary() + { + // Arrange + var sut = typeof(DictionaryShim); + // Pre-Assert + // Act + Expect(sut).To.Implement>(); + // Assert + } + [TestFixture] - public class TestDictionaryShim + public class KeyValueSetAndGet { [Test] - public void ShouldImplement_IDictionary() + public void ShouldBeAbleToGetExistingValue() { // Arrange - var sut = typeof(DictionaryShim); + var actual = new NameValueCollection(); + var key = GetRandomString(2); + var value = GetRandomString(2); + actual[key] = value; + var sut = Create(actual); // Pre-Assert // Act - Expect(sut).To.Implement>(); + var result = sut[key]; + // Assert + Expect(result).To.Equal(value); } - [TestFixture] - public class KeyValueSetAndGet + [Test] + public void ShouldBeAbleToSetAndGetValue() { - [Test] - public void ShouldBeAbleToGetExistingValue() - { - // Arrange - var actual = new NameValueCollection(); - var key = GetRandomString(2); - var value = GetRandomString(2); - actual[key] = value; - var sut = Create(actual); - // Pre-Assert - // Act - var result = sut[key]; - - // Assert - Expect(result).To.Equal(value); - } - - [Test] - public void ShouldBeAbleToSetAndGetValue() - { - // Arrange - var actual = new NameValueCollection(); - var sut = Create(actual); - var key = GetRandomString(2); - var value = GetRandomString(2); - // Pre-Assert - // Act - sut[key] = value; - var result = sut[key]; - // Assert - Expect(result).To.Equal(value); - Expect(actual[key]).To.Equal(value); - } + // Arrange + var actual = new NameValueCollection(); + var sut = Create(actual); + var key = GetRandomString(2); + var value = GetRandomString(2); + // Pre-Assert + // Act + sut[key] = value; + var result = sut[key]; + // Assert + Expect(result).To.Equal(value); + Expect(actual[key]).To.Equal(value); + } - [Test] - public void ShouldBeAbleWToAddKeyAndValue() - { - // Arrange - var key = GetRandomString(2); - var value = GetRandomString(2); - var actual = new NameValueCollection(); - var sut = Create(actual); - // Pre-Assert - // Act - sut.Add(key, value); - // Assert - Expect(sut[key]).To.Equal(value); - Expect(actual[key]).To.Equal(value); - } + [Test] + public void ShouldBeAbleWToAddKeyAndValue() + { + // Arrange + var key = GetRandomString(2); + var value = GetRandomString(2); + var actual = new NameValueCollection(); + var sut = Create(actual); + // Pre-Assert + // Act + sut.Add(key, value); + // Assert + Expect(sut[key]).To.Equal(value); + Expect(actual[key]).To.Equal(value); + } - [Test] - public void ShouldBeAbleWToAddKeyValuePair() - { - // Arrange - var kvp = GetRandom>(); - var actual = new NameValueCollection(); - var sut = Create(actual); - // Pre-Assert - // Act - sut.Add(kvp.Key, kvp.Value); - // Assert - Expect(sut[kvp.Key]).To.Equal(kvp.Value); - Expect(actual[kvp.Key]).To.Equal(kvp.Value); - } + [Test] + public void ShouldBeAbleWToAddKeyValuePair() + { + // Arrange + var kvp = GetRandom>(); + var actual = new NameValueCollection(); + var sut = Create(actual); + // Pre-Assert + // Act + sut.Add(kvp.Key, kvp.Value); + // Assert + Expect(sut[kvp.Key]).To.Equal(kvp.Value); + Expect(actual[kvp.Key]).To.Equal(kvp.Value); + } - [Test] - public void ShouldBeAbleToClear() - { - // Arrange - var actual = new NameValueCollection(); - actual[GetRandomString(2)] = GetRandomString(2); - var sut = Create(actual); - // Pre-Assert - // Act - sut.Clear(); - // Assert - Expect(actual.Count).To.Equal(0); - Expect(sut).To.Be.Empty(); - } + [Test] + public void ShouldBeAbleToClear() + { + // Arrange + var actual = new NameValueCollection(); + actual[GetRandomString(2)] = GetRandomString(2); + var sut = Create(actual); + // Pre-Assert + // Act + sut.Clear(); + // Assert + Expect(actual.Count).To.Equal(0); + Expect(sut).To.Be.Empty(); + } - [Test] - public void ShouldBeAbleToRemoveByKey() - { - // Arrange - var key = GetRandomString(2); - var actual = new NameValueCollection(); - actual[key] = GetRandomString(2); - var sut = Create(actual); - // Pre-Assert - // Act - var result = sut.Remove(key); - // Assert - Expect(result).To.Be.True(); - Expect(actual.Count).To.Equal(0); - Expect(sut).To.Be.Empty(); - } + [Test] + public void ShouldBeAbleToRemoveByKey() + { + // Arrange + var key = GetRandomString(2); + var actual = new NameValueCollection(); + actual[key] = GetRandomString(2); + var sut = Create(actual); + // Pre-Assert + // Act + var result = sut.Remove(key); + // Assert + Expect(result).To.Be.True(); + Expect(actual.Count).To.Equal(0); + Expect(sut).To.Be.Empty(); + } - [Test] - public void ShouldBeAbleToRemoveByKeyValuePair() - { - // Arrange - var kvp = GetRandom>(); - var actual = new NameValueCollection(); - actual[kvp.Key] = kvp.Value; - var sut = Create(actual); - // Pre-Assert - // Act - var result = sut.Remove(kvp); - // Assert - Expect(result).To.Be.True(); - Expect(sut).To.Be.Empty(); - Expect(actual.Count).To.Equal(0); - } + [Test] + public void ShouldBeAbleToRemoveByKeyValuePair() + { + // Arrange + var kvp = GetRandom>(); + var actual = new NameValueCollection(); + actual[kvp.Key] = kvp.Value; + var sut = Create(actual); + // Pre-Assert + // Act + var result = sut.Remove(kvp); + // Assert + Expect(result).To.Be.True(); + Expect(sut).To.Be.Empty(); + Expect(actual.Count).To.Equal(0); + } - [Test] - public void ShouldRequireFullMatchForKeyValuePairRemoval() - { - // Arrange - var kvp = GetRandom>(); - var actual = new NameValueCollection {[kvp.Key] = kvp.Value}; - var sut = Create(actual); - // Pre-Assert - // Act - var result1 = sut.Remove(new KeyValuePair(kvp.Key, GetAnother(kvp.Value))); - var result2 = sut.Remove(new KeyValuePair(GetAnother(kvp.Key), kvp.Value)); - // Assert - Expect(result1).To.Be.False(); - Expect(result2).To.Be.False(); - Expect(sut).Not.To.Be.Empty(); - Expect(actual.Count).To.Equal(1); - } + [Test] + public void ShouldRequireFullMatchForKeyValuePairRemoval() + { + // Arrange + var kvp = GetRandom>(); + var actual = new NameValueCollection {[kvp.Key] = kvp.Value}; + var sut = Create(actual); + // Pre-Assert + // Act + var result1 = sut.Remove(new KeyValuePair(kvp.Key, GetAnother(kvp.Value))); + var result2 = sut.Remove(new KeyValuePair(GetAnother(kvp.Key), kvp.Value)); + // Assert + Expect(result1).To.Be.False(); + Expect(result2).To.Be.False(); + Expect(sut).Not.To.Be.Empty(); + Expect(actual.Count).To.Equal(1); + } - [Test] - public void TryGetValue_WhenHaveValue_ShouldSetAndReturnTrue() - { - // Arrange - var kvp = GetRandom>(); - var actual = new NameValueCollection {[kvp.Key] = kvp.Value}; - var sut = Create(actual); - // Pre-Assert - // Act - var result = sut.TryGetValue(kvp.Key, out var value); - // Assert - Expect(result).To.Be.True(); - Expect(value).To.Equal(kvp.Value); - } + [Test] + public void TryGetValue_WhenHaveValue_ShouldSetAndReturnTrue() + { + // Arrange + var kvp = GetRandom>(); + var actual = new NameValueCollection {[kvp.Key] = kvp.Value}; + var sut = Create(actual); + // Pre-Assert + // Act + var result = sut.TryGetValue(kvp.Key, out var value); + // Assert + Expect(result).To.Be.True(); + Expect(value).To.Equal(kvp.Value); + } - [Test] - public void TryGetValue_WhenDoNotHaveValue_ShouldReturnFalse() - { - // Arrange - var kvp = GetRandom>(); - var actual = new NameValueCollection {[kvp.Key] = kvp.Value}; - var sut = Create(actual); - // Pre-Assert - // Act - var result = sut.TryGetValue(GetAnother(kvp.Key), out var value); - // Assert - Expect(result).To.Be.False(); - } + [Test] + public void TryGetValue_WhenDoNotHaveValue_ShouldReturnFalse() + { + // Arrange + var kvp = GetRandom>(); + var actual = new NameValueCollection {[kvp.Key] = kvp.Value}; + var sut = Create(actual); + // Pre-Assert + // Act + var result = sut.TryGetValue(GetAnother(kvp.Key), out var value); + // Assert + Expect(result).To.Be.False(); + } - [Test] - public void Values_ShouldReturnAllValues() - { - // Arrange - var items = GetRandomCollection>(); - var actual = new NameValueCollection(); - items.ForEach(i => actual[i.Key] = i.Value); - var sut = Create(actual); - var expected = items.Select(kvp => kvp.Value).ToArray(); - // Pre-Assert - // Act - var result = sut.Values; - // Assert - Expect(result).To.Be.Equivalent.To(expected); - } + [Test] + public void Values_ShouldReturnAllValues() + { + // Arrange + var items = GetRandomCollection>(); + var actual = new NameValueCollection(); + items.ForEach(i => actual[i.Key] = i.Value); + var sut = Create(actual); + var expected = items.Select(kvp => kvp.Value).ToArray(); + // Pre-Assert + // Act + var result = sut.Values; + // Assert + Expect(result).To.Be.Equivalent.To(expected); + } - [Test] - public void IsReadOnly_ShouldBeFalse() - { - // Arrange - var sut = Create(new NameValueCollection()); - // Pre-Assert - // Act - var result = sut.IsReadOnly; - // Assert - Expect(result).To.Be.False(); - } + [Test] + public void IsReadOnly_ShouldBeFalse() + { + // Arrange + var sut = Create(new NameValueCollection()); + // Pre-Assert + // Act + var result = sut.IsReadOnly; + // Assert + Expect(result).To.Be.False(); + } - [Test] - public void Add_ShouldAddKeyValuePair() - { - // Arrange - var actual = new NameValueCollection(); - var sut = Create(actual); - var kvp = GetRandom>(); - // Pre-assert - // Act - sut.Add(kvp); - // Assert - Expect(actual[kvp.Key]).To.Equal(kvp.Value); - } + [Test] + public void Add_ShouldAddKeyValuePair() + { + // Arrange + var actual = new NameValueCollection(); + var sut = Create(actual); + var kvp = GetRandom>(); + // Pre-assert + // Act + sut.Add(kvp); + // Assert + Expect(actual[kvp.Key]).To.Equal(kvp.Value); } + } - [TestFixture] - public class Enumeration + [TestFixture] + public class Enumeration + { + [Test] + public void ShouldBeAbleToEnumerate() { - [Test] - public void ShouldBeAbleToEnumerate() - { - // Arrange - var k1 = GetRandomString(2); - var v1 = GetRandomString(2); - var k2 = GetAnother(k1); - var v2 = GetAnother(v1); - var actual = new NameValueCollection(); - actual[k1] = v1; - actual[k2] = v2; - var collector = new List>(); - var sut = Create(actual); - // Pre-Assert - // Act - foreach (var kvp in sut) - collector.Add(kvp); - // Assert - Expect(collector).To.Contain.Only(2).Items(); - Expect(collector).To.Contain(new KeyValuePair(k1, v1)); - Expect(collector).To.Contain(new KeyValuePair(k2, v2)); - } - - [Test] - public void IEnumerableInterface() - { - // Arrange - var k1 = GetRandomString(2); - var v1 = GetRandomString(2); - var k2 = GetAnother(k1); - var v2 = GetAnother(v1); - var actual = new NameValueCollection(); - actual[k1] = v1; - actual[k2] = v2; - var collector = new List>(); - var sut = Create(actual); - // Pre-assert - // Act - var expected = sut.GetEnumerator(); - var result = (sut as IEnumerable).GetEnumerator(); - // Assert - Expect(result).To.Deep.Equal(expected); - Expect(expected.GetType().Name).To.Equal("DictionaryShimEnumerator"); - Expect(result.GetType().Name).To.Equal("DictionaryShimEnumerator"); - Expect(result.GetPropertyValue("Subject")) - .To.Equal(expected.GetPropertyValue("Subject")); - } + // Arrange + var k1 = GetRandomString(2); + var v1 = GetRandomString(2); + var k2 = GetAnother(k1); + var v2 = GetAnother(v1); + var actual = new NameValueCollection(); + actual[k1] = v1; + actual[k2] = v2; + var collector = new List>(); + var sut = Create(actual); + // Pre-Assert + // Act + foreach (var kvp in sut) + collector.Add(kvp); + // Assert + Expect(collector).To.Contain.Only(2).Items(); + Expect(collector).To.Contain(new KeyValuePair(k1, v1)); + Expect(collector).To.Contain(new KeyValuePair(k2, v2)); } - - private static IDictionary Create(NameValueCollection actual) + [Test] + public void IEnumerableInterface() { - return new DictionaryShim(actual); + // Arrange + var k1 = GetRandomString(2); + var v1 = GetRandomString(2); + var k2 = GetAnother(k1); + var v2 = GetAnother(v1); + var actual = new NameValueCollection(); + actual[k1] = v1; + actual[k2] = v2; + var collector = new List>(); + var sut = Create(actual); + // Pre-assert + // Act + var expected = sut.GetEnumerator(); + var result = (sut as IEnumerable).GetEnumerator(); + // Assert + Expect(result).To.Deep.Equal(expected); + Expect(expected.GetType().Name).To.Equal("DictionaryShimEnumerator"); + Expect(result.GetType().Name).To.Equal("DictionaryShimEnumerator"); + Expect(result.GetPropertyValue("Subject")) + .To.Equal(expected.GetPropertyValue("Subject")); } } -} \ No newline at end of file + + + private static IDictionary Create(NameValueCollection actual) + { + return new DictionaryShim(actual); + } +} diff --git a/src/NExpect.Tests/TestBadMatcherHandling.cs b/src/NExpect.Tests/TestBadMatcherHandling.cs index f55a8218..effc3bd0 100644 --- a/src/NExpect.Tests/TestBadMatcherHandling.cs +++ b/src/NExpect.Tests/TestBadMatcherHandling.cs @@ -1,35 +1,34 @@ -using System.Collections.Generic; +using System.Collections.Generic; using NExpect.Interfaces; using NExpect.MatcherLogic; using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class TestBadMatcherHandling { - [TestFixture] - public class TestBadMatcherHandling + [Test] + public void WhenMatcherThrows_ShouldHaveReasonableMessage() { - [Test] - public void WhenMatcherThrows_ShouldHaveReasonableMessage() - { - // Arrange - // Pre-assert - // Act - Assert.That(() => Expect("someString").To.Be.Broken(), - Throws.Exception.InstanceOf() - .With.Message.StartsWith( - "Exception whilst running matcher: System.Collections.Generic.KeyNotFoundException: moo")); - // Assert - } + // Arrange + // Pre-assert + // Act + Assert.That(() => Expect("someString").To.Be.Broken(), + Throws.Exception.InstanceOf() + .With.Message.StartsWith( + "Exception whilst running matcher: System.Collections.Generic.KeyNotFoundException: moo")); + // Assert } +} - public static class BadMatchers +public static class BadMatchers +{ + public static void Broken( + this IBe be + ) { - public static void Broken( - this IBe be - ) - { - be.AddMatcher(actual => throw new KeyNotFoundException("moo")); - } + be.AddMatcher(actual => throw new KeyNotFoundException("moo")); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/TestEnumMatchers.cs b/src/NExpect.Tests/TestEnumMatchers.cs index 145c6551..ec79c51f 100644 --- a/src/NExpect.Tests/TestEnumMatchers.cs +++ b/src/NExpect.Tests/TestEnumMatchers.cs @@ -2,105 +2,104 @@ using NUnit.Framework; using NExpect.Exceptions; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class TestEnumMatchers { - [TestFixture] - public class TestEnumMatchers + [Test] + public void ShouldBeAbleToAssertEnumValueHasFlag() { - [Test] - public void ShouldBeAbleToAssertEnumValueHasFlag() - { - // Arrange - var value = Numbers.One | Numbers.Three; - // Act - Assert.That( - () => - { - Expect(value) - .To.Have.Flag(Numbers.One) - .And - .To.Have.Flag(Numbers.Three); - Expect(value) - .Not.To.Have.Flag(Numbers.Two) - .And - .Not.To.Have.Flag(Numbers.Four); - }, - Throws.Nothing - ); + // Arrange + var value = Numbers.One | Numbers.Three; + // Act + Assert.That( + () => + { + Expect(value) + .To.Have.Flag(Numbers.One) + .And + .To.Have.Flag(Numbers.Three); + Expect(value) + .Not.To.Have.Flag(Numbers.Two) + .And + .Not.To.Have.Flag(Numbers.Four); + }, + Throws.Nothing + ); - Assert.That( - () => - { - Expect(value) - .To.Have.Flag(Numbers.Two); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"Expected ({value}) to have flag ({Numbers.Two})" - ) - ); + Assert.That( + () => + { + Expect(value) + .To.Have.Flag(Numbers.Two); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + $"Expected ({value}) to have flag ({Numbers.Two})" + ) + ); - Assert.That( - () => - { - Expect(value) - .Not.To.Have.Flag(Numbers.One); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains( - $"Expected ({value}) not to have flag ({Numbers.One})" - ) - ); + Assert.That( + () => + { + Expect(value) + .Not.To.Have.Flag(Numbers.One); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains( + $"Expected ({value}) not to have flag ({Numbers.One})" + ) + ); - // Assert - } + // Assert + } - [Test] - public void ShouldAlwaysFailForNonFlagsEnums() - { - // Arrange - var value = NotFlags.Strange; - // Act - Assert.That( - () => - { - Expect(value) - .To.Have.Flag(NotFlags.Strange); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("not decorated with [Flags]") - ); - Assert.That( - () => - { - Expect(value) - .Not.To.Have.Flag(NotFlags.Strange); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("not decorated with [Flags]") - ); - // Assert - } + [Test] + public void ShouldAlwaysFailForNonFlagsEnums() + { + // Arrange + var value = NotFlags.Strange; + // Act + Assert.That( + () => + { + Expect(value) + .To.Have.Flag(NotFlags.Strange); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("not decorated with [Flags]") + ); + Assert.That( + () => + { + Expect(value) + .Not.To.Have.Flag(NotFlags.Strange); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("not decorated with [Flags]") + ); + // Assert + } - public enum NotFlags - { - Unknown, - Up, - Down, - Strange, - Charm, - Bottom, - Top - } + public enum NotFlags + { + Unknown, + Up, + Down, + Strange, + Charm, + Bottom, + Top + } - [Flags] - public enum Numbers - { - Zero = 0, - One = 1, - Two = 2, - Three = 4, - Four = 8 - } + [Flags] + public enum Numbers + { + Zero = 0, + One = 1, + Two = 2, + Three = 4, + Four = 8 } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/TestFuncFactory.cs b/src/NExpect.Tests/TestFuncFactory.cs index d8cc253a..14bf7301 100644 --- a/src/NExpect.Tests/TestFuncFactory.cs +++ b/src/NExpect.Tests/TestFuncFactory.cs @@ -2,30 +2,29 @@ using NUnit.Framework; using NExpect.Helpers; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class TestFuncFactory { - [TestFixture] - public class TestFuncFactory + [Test] + public void ShouldMemoize() { - [Test] - public void ShouldMemoize() + // Arrange + var calls = 0; + Func generator = () => { - // Arrange - var calls = 0; - Func generator = () => - { - calls++; - return GetRandomString(); - }; - var sut = FuncFactory.Memoize(generator); - // Act - var result1 = sut(); - var result2 = sut(); - // Assert - Expect(calls) - .To.Equal(1); - Expect(result1) - .To.Equal(result2); - } + calls++; + return GetRandomString(); + }; + var sut = FuncFactory.Memoize(generator); + // Act + var result1 = sut(); + var result2 = sut(); + // Assert + Expect(calls) + .To.Equal(1); + Expect(result1) + .To.Equal(result2); } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/TestIntsAndEnumMatchers.cs b/src/NExpect.Tests/TestIntsAndEnumMatchers.cs index 17cc4f1e..e25e8713 100644 --- a/src/NExpect.Tests/TestIntsAndEnumMatchers.cs +++ b/src/NExpect.Tests/TestIntsAndEnumMatchers.cs @@ -1,192 +1,191 @@ -using NExpect.Exceptions; +using NExpect.Exceptions; using NUnit.Framework; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class TestIntsAndEnumMatchers { [TestFixture] - public class TestIntsAndEnumMatchers + public class ComparingEnumsToInts { - [TestFixture] - public class ComparingEnumsToInts + [Test] + public void ShouldBeAbleToAssertEqualityEasily() { - [Test] - public void ShouldBeAbleToAssertEqualityEasily() - { - // Arrange - // Act - Assert.That( - () => - { - Expect(Numbers.One) - .To.Equal(1); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(Numbers.One) - .Not.To.Equal(1); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(Numbers.One) - .To.Not.Equal(1); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(Numbers.Two) - .To.Equal(1); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } - - [Test] - public void ShouldAlwaysFailIfExpectedValueIsOutOfRange() - { - // Arrange - Assert.That( - () => - { - Expect(Numbers.One) - .To.Equal(-1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - Assert.That( - () => - { - Expect(Numbers.One) - .Not.To.Equal(-1); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - Assert.That( - () => - { - Expect(Numbers.One) - .To.Equal(20); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - Assert.That( - () => - { - Expect(Numbers.One) - .Not.To.Equal(20); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - // Act - // Assert - } + // Arrange + // Act + Assert.That( + () => + { + Expect(Numbers.One) + .To.Equal(1); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(Numbers.One) + .Not.To.Equal(1); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(Numbers.One) + .To.Not.Equal(1); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(Numbers.Two) + .To.Equal(1); + }, + Throws.Exception.InstanceOf() + ); + // Assert } - [TestFixture] - public class ComparingIntsToEnums + [Test] + public void ShouldAlwaysFailIfExpectedValueIsOutOfRange() { - [Test] - public void ShouldBeAbleToAssertEqualityEasily() - { - // Arrange - // Act - Assert.That( - () => - { - Expect(1) - .To.Equal(Numbers.One); - }, - Throws.Nothing - ); - Assert.That( - () => - { - Expect(1) - .Not.To.Equal(Numbers.One); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(1) - .To.Not.Equal(Numbers.One); - }, - Throws.Exception.InstanceOf() - ); - Assert.That( - () => - { - Expect(1) - .To.Equal(Numbers.Two); - }, - Throws.Exception.InstanceOf() - ); - // Assert - } + // Arrange + Assert.That( + () => + { + Expect(Numbers.One) + .To.Equal(-1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + Assert.That( + () => + { + Expect(Numbers.One) + .Not.To.Equal(-1); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + Assert.That( + () => + { + Expect(Numbers.One) + .To.Equal(20); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + Assert.That( + () => + { + Expect(Numbers.One) + .Not.To.Equal(20); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + // Act + // Assert + } + } - [Test] - public void ShouldAlwaysFailIfExpectedValueIsOutOfRange() - { - // Arrange - Assert.That( - () => - { - Expect(-1) - .To.Equal(Numbers.One); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - Assert.That( - () => - { - Expect(-1) - .Not.To.Equal(Numbers.One); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - Assert.That( - () => - { - Expect(20) - .To.Equal(Numbers.One); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - Assert.That( - () => - { - Expect(20) - .Not.To.Equal(Numbers.One); - }, - Throws.Exception.InstanceOf() - .With.Message.Contains("cannot be mapped onto a pure value") - ); - // Act - // Assert - } + [TestFixture] + public class ComparingIntsToEnums + { + [Test] + public void ShouldBeAbleToAssertEqualityEasily() + { + // Arrange + // Act + Assert.That( + () => + { + Expect(1) + .To.Equal(Numbers.One); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(1) + .Not.To.Equal(Numbers.One); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(1) + .To.Not.Equal(Numbers.One); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(1) + .To.Equal(Numbers.Two); + }, + Throws.Exception.InstanceOf() + ); + // Assert } - public enum Numbers + [Test] + public void ShouldAlwaysFailIfExpectedValueIsOutOfRange() { - One = 1, - Two = 2, - Three = 3 + // Arrange + Assert.That( + () => + { + Expect(-1) + .To.Equal(Numbers.One); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + Assert.That( + () => + { + Expect(-1) + .Not.To.Equal(Numbers.One); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + Assert.That( + () => + { + Expect(20) + .To.Equal(Numbers.One); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + Assert.That( + () => + { + Expect(20) + .Not.To.Equal(Numbers.One); + }, + Throws.Exception.InstanceOf() + .With.Message.Contains("cannot be mapped onto a pure value") + ); + // Act + // Assert } } -} \ No newline at end of file + + public enum Numbers + { + One = 1, + Two = 2, + Three = 3 + } +} diff --git a/src/NExpect.Tests/TestRequireGrammar.cs b/src/NExpect.Tests/TestRequireGrammar.cs new file mode 100644 index 00000000..6f1d4e55 --- /dev/null +++ b/src/NExpect.Tests/TestRequireGrammar.cs @@ -0,0 +1,101 @@ +using NExpect.Exceptions; +using NExpect.Implementations; +using NExpect.Interfaces; +using NExpect.MatcherLogic; +using NUnit.Framework; + +namespace NExpect.Tests; + +[TestFixture] +public class TestRequireGrammar +{ + [Test] + public void ShouldBeAbleToUseRequireAsDangler() + { + // Arrange + var dog = new Dog(); + var rock = new Rock(); + // Act + Assert.That( + () => + { + Expect(dog) + .To.Require.Food(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(dog) + .Not.To.Require.Food(); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(dog) + .To.Not.Require.Food(); + }, + Throws.Exception.InstanceOf() + ); + + Assert.That( + () => + { + Expect(rock) + .To.Require.Food(); + }, + Throws.Exception.InstanceOf() + ); + Assert.That( + () => + { + Expect(rock) + .Not.To.Require.Food(); + }, + Throws.Nothing + ); + Assert.That( + () => + { + Expect(rock) + .To.Not.Require.Food(); + }, + Throws.Nothing + ); + // Assert + } + + public abstract class Animal + { + } + + public class Dog : Animal + { + } + + public class Rock + { + } +} + +public static class RequireTestExtensions +{ + public static IMore Food( + this IRequire require + ) + { + return require.AddMatcher( + actual => + { + var passed = actual is TestRequireGrammar.Animal; + return new MatcherResult( + passed, + () => $"Expected {typeof(T)} {passed.AsNot()}to require food" + ); + } + ); + } +} \ No newline at end of file diff --git a/src/NExpect.Tests/TestStringify.cs b/src/NExpect.Tests/TestStringify.cs index 12ec5a52..d375634a 100644 --- a/src/NExpect.Tests/TestStringify.cs +++ b/src/NExpect.Tests/TestStringify.cs @@ -1,124 +1,123 @@ -using NUnit.Framework; +using NUnit.Framework; using NExpect.Implementations; using PeanutButter.Utils; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class TestStringify { [TestFixture] - public class TestStringify + public class Enums { - [TestFixture] - public class Enums + public enum Numbers { - public enum Numbers - { - Zero, - One, - Two, - Three - } + Zero, + One, + Two, + Three + } - [Test] - public void EnumOnly_ShouldBeReportedAsString() - { - // Arrange - var value = Numbers.Zero; - var expected = "Zero"; - // Pre-Assert - // Act - var result = value.Stringify(); - // Assert - Expect(result).To.Equal(expected); - } + [Test] + public void EnumOnly_ShouldBeReportedAsString() + { + // Arrange + var value = Numbers.Zero; + var expected = "Zero"; + // Pre-Assert + // Act + var result = value.Stringify(); + // Assert + Expect(result).To.Equal(expected); + } - [Test] - public void NullableEnumOnly_WhenNotNull_ShouldBeReportedAsString() - { - // Arrange - Numbers? value = Numbers.Three; - var expected = "Three"; - // Pre-Assert - // Act - var result = value.Stringify(); - // Assert - Expect(result).To.Equal(expected); - } + [Test] + public void NullableEnumOnly_WhenNotNull_ShouldBeReportedAsString() + { + // Arrange + Numbers? value = Numbers.Three; + var expected = "Three"; + // Pre-Assert + // Act + var result = value.Stringify(); + // Assert + Expect(result).To.Equal(expected); + } - public class HasEnum - { - public Numbers Number { get; set; } - } + public class HasEnum + { + public Numbers Number { get; set; } + } - [Test] - public void AsProperty_ShouldBeReportedAsString() + [Test] + public void AsProperty_ShouldBeReportedAsString() + { + // Arrange + var data = GetRandom(); + var expected = new[] { - // Arrange - var data = GetRandom(); - var expected = new[] - { - "{", - $" Number: {data.Number.ToString()}", - "}" - }.JoinWith("\n"); - // Pre-Assert - // Act - var result = data.Stringify(); - // Assert - Expect(result).To.Equal(expected); - } + "{", + $" Number: {data.Number.ToString()}", + "}" + }.JoinWith("\n"); + // Pre-Assert + // Act + var result = data.Stringify(); + // Assert + Expect(result).To.Equal(expected); } + } - [TestFixture] - public class Arrays + [TestFixture] + public class Arrays + { + [Test] + public void AsPrimarySubject() { - [Test] - public void AsPrimarySubject() + // Arrange + var subject = new[] { - // Arrange - var subject = new[] - { - new { id = 1, name = "moo" } - }; - var expected = new[] - { - "[ {", - " id: 1", - " name: \"moo\"", - "} ]" - }.JoinWith("\n"); - // Pre-Assert - // Act - var result = subject.Stringify(); - // Assert - Expect(result).To.Equal(expected); - } - - [Test] - public void AsProperty() + new { id = 1, name = "moo" } + }; + var expected = new[] { - // Arrange - var subject = new - { - collection = new[] { - new { id = 1, name = "cow" } - } - }; - var expected = new[] - { - "{", - " collection: [ {", - " id: 1", - " name: \"cow\"", - " } ]", - "}" - }.JoinWith("\n"); - // Pre-Assert - // Act - var result = subject.Stringify(); - // Assert - Expect(result).To.Equal(expected); - } + "[ {", + " id: 1", + " name: \"moo\"", + "} ]" + }.JoinWith("\n"); + // Pre-Assert + // Act + var result = subject.Stringify(); + // Assert + Expect(result).To.Equal(expected); } + [Test] + public void AsProperty() + { + // Arrange + var subject = new + { + collection = new[] { + new { id = 1, name = "cow" } + } + }; + var expected = new[] + { + "{", + " collection: [ {", + " id: 1", + " name: \"cow\"", + " } ]", + "}" + }.JoinWith("\n"); + // Pre-Assert + // Act + var result = subject.Stringify(); + // Assert + Expect(result).To.Equal(expected); + } } -} \ No newline at end of file + +} diff --git a/src/NExpect.Tests/TestUtils.cs b/src/NExpect.Tests/TestUtils.cs index a98ab56e..7ee2b316 100644 --- a/src/NExpect.Tests/TestUtils.cs +++ b/src/NExpect.Tests/TestUtils.cs @@ -1,34 +1,33 @@ -using System; +using System; // ReSharper disable ClassNeverInstantiated.Global -namespace NExpect.Tests +namespace NExpect.Tests; + +internal class TestUtils { - internal class TestUtils + internal static void ForceMessageLineBreaks() { - internal static void ForceMessageLineBreaks() - { - Environment.SetEnvironmentVariable("COLS", "1"); - } + Environment.SetEnvironmentVariable("COLS", "1"); + } - internal static void ForceNoMessageLineBreaks() + internal static void ForceNoMessageLineBreaks() + { + Environment.SetEnvironmentVariable("COLS", "99999"); + } + + internal static void WithNoLineBreaks(Action action) + { + var beforeTest = Environment.GetEnvironmentVariable("COLS"); + try { - Environment.SetEnvironmentVariable("COLS", "99999"); + action(); } - - internal static void WithNoLineBreaks(Action action) + finally { - var beforeTest = Environment.GetEnvironmentVariable("COLS"); - try - { - action(); - } - finally + if (beforeTest != null) { - if (beforeTest != null) - { - Environment.SetEnvironmentVariable("COLS", beforeTest); - } + Environment.SetEnvironmentVariable("COLS", beforeTest); } } } -} \ No newline at end of file +} diff --git a/src/NExpect.Tests/UsingCustomAssertionsGenerator.cs b/src/NExpect.Tests/UsingCustomAssertionsGenerator.cs index 3ec96d62..2001efa4 100644 --- a/src/NExpect.Tests/UsingCustomAssertionsGenerator.cs +++ b/src/NExpect.Tests/UsingCustomAssertionsGenerator.cs @@ -1,73 +1,72 @@ -using System; +using System; using NExpect.Exceptions; using NExpect.Interfaces; using NExpect.MatcherLogic; using NUnit.Framework; -namespace NExpect.Tests +namespace NExpect.Tests; + +[TestFixture] +public class UsingCustomAssertionsGenerator { - [TestFixture] - public class UsingCustomAssertionsGenerator + [TearDown] + public void TearDown() { - [TearDown] - public void TearDown() - { - Assertions.UseDefaultAssertionsFactory(); - } + Assertions.UseDefaultAssertionsFactory(); + } - [Test] - public void RegisteringAssertionFactoryForMessageOnly() + [Test] + public void RegisteringAssertionFactoryForMessageOnly() + { + // Arrange + Assertions.RegisterAssertionsFactory( + message => new AssertionException(message)); + // Pre-assert + // Act + Assert.That(() => { - // Arrange - Assertions.RegisterAssertionsFactory( - message => new AssertionException(message)); - // Pre-assert - // Act - Assert.That(() => - { - Expect(5).To.Equal(4); - }, Throws.Exception.InstanceOf() - .With.Message.Matches("Expected\\s4\\sbut\\sgot\\s5")); - // Assert - } + Expect(5).To.Equal(4); + }, Throws.Exception.InstanceOf() + .With.Message.Matches("Expected\\s4\\sbut\\sgot\\s5")); + // Assert + } - [Test] - public void RegisteringAssertionFactoryForMessageAndInnerException() + [Test] + public void RegisteringAssertionFactoryForMessageAndInnerException() + { + // Arrange + Assertions.RegisterAssertionsFactory( + (message, inner) => new AssertionException(message, inner)); + // Pre-assert + // Act + Assert.That(() => { - // Arrange - Assertions.RegisterAssertionsFactory( - (message, inner) => new AssertionException(message, inner)); - // Pre-assert - // Act - Assert.That(() => - { - Expect(1).To.Fail(); - }, Throws.Exception.InstanceOf() - .With.InnerException.Message.EqualTo("my bad")); - // Assert - } + Expect(1).To.Fail(); + }, Throws.Exception.InstanceOf() + .With.InnerException.Message.EqualTo("my bad")); + // Assert + } - [Test] - public void ResettingAssertionsFactory() + [Test] + public void ResettingAssertionsFactory() + { + // Arrange + Assertions.RegisterAssertionsFactory(message => new AssertionException(message)); + // Pre-assert + // Act + Assertions.UseDefaultAssertionsFactory(); + Assert.That(() => { - // Arrange - Assertions.RegisterAssertionsFactory(message => new AssertionException(message)); - // Pre-assert - // Act - Assertions.UseDefaultAssertionsFactory(); - Assert.That(() => - { - Expect(true).To.Be.False(); - }, Throws.Exception.InstanceOf()); - // Assert - } + Expect(true).To.Be.False(); + }, Throws.Exception.InstanceOf()); + // Assert } +} - public static class ThrowingMatchers +public static class ThrowingMatchers +{ + public static void Fail(this ITo to) { - public static void Fail(this ITo to) - { - to.AddMatcher(actual => throw new Exception("my bad")); - } + to.AddMatcher(actual => throw new Exception("my bad")); } -} \ No newline at end of file +} diff --git a/src/NExpect/Implementations/Find.cs b/src/NExpect/Implementations/Find.cs index 6ff76d24..095baa8f 100644 --- a/src/NExpect/Implementations/Find.cs +++ b/src/NExpect/Implementations/Find.cs @@ -11,5 +11,9 @@ internal class Find public Find(Func actualFetcher) : base(actualFetcher) { } + + public Find(Func actualFetcher, bool negate) : base(actualFetcher, negate) + { + } } } \ No newline at end of file diff --git a/src/NExpect/Implementations/Fluency/NotAfterTo.cs b/src/NExpect/Implementations/Fluency/NotAfterTo.cs index 5beffc71..ecd3705f 100644 --- a/src/NExpect/Implementations/Fluency/NotAfterTo.cs +++ b/src/NExpect/Implementations/Fluency/NotAfterTo.cs @@ -1,6 +1,5 @@ using System; using NExpect.Implementations.Collections; -using NExpect.Implementations.Strings; using NExpect.Interfaces; // ReSharper disable MemberCanBeProtected.Global @@ -18,6 +17,9 @@ internal class NotAfterTo public IHave Have => Next>(); public IDeep Deep => Next>(); public IIntersection Intersection => Next>(); + public IApproximately Approximately => Next>(); + public IFind Find => Next>(); + public IRequire Require => Next>(); public NotAfterTo(Func actualFetcher) : base(actualFetcher) { diff --git a/src/NExpect/Implementations/Fluency/To.cs b/src/NExpect/Implementations/Fluency/To.cs index b17144ae..cf6f1e1c 100644 --- a/src/NExpect/Implementations/Fluency/To.cs +++ b/src/NExpect/Implementations/Fluency/To.cs @@ -20,6 +20,7 @@ internal class To public IIntersection Intersection => Next>(); public IApproximately Approximately => Next>(); public IFind Find => Next>(); + public IRequire Require => Next>(); public To(Func actualFetcher) : base(actualFetcher) { diff --git a/src/NExpect/Implementations/Fluency/ToAfterNot.cs b/src/NExpect/Implementations/Fluency/ToAfterNot.cs index 5658adee..3d93aaf9 100644 --- a/src/NExpect/Implementations/Fluency/ToAfterNot.cs +++ b/src/NExpect/Implementations/Fluency/ToAfterNot.cs @@ -20,6 +20,7 @@ internal class ToAfterNot public IIntersection Intersection => Next>(); public IApproximately Approximately => Next>(); public IFind Find => Next>(); + public IRequire Require => Next>(); public ToAfterNot(Func actualFetcher) : base(actualFetcher) { diff --git a/src/NExpect/Implementations/Require.cs b/src/NExpect/Implementations/Require.cs new file mode 100644 index 00000000..d236abb1 --- /dev/null +++ b/src/NExpect/Implementations/Require.cs @@ -0,0 +1,19 @@ +using System; +using NExpect.Interfaces; + +namespace NExpect.Implementations +{ + internal class Require + : ExpectationContextWithLazyActual, + IHasActual, + IRequire + { + public Require(Func actualFetcher) : base(actualFetcher) + { + } + + public Require(Func actualFetcher, bool negate) : base(actualFetcher, negate) + { + } + } +} \ No newline at end of file diff --git a/src/NExpect/Interfaces/IExpectation.cs b/src/NExpect/Interfaces/IExpectation.cs index 799c9055..030913ce 100644 --- a/src/NExpect/Interfaces/IExpectation.cs +++ b/src/NExpect/Interfaces/IExpectation.cs @@ -17,9 +17,9 @@ public interface IExpectation /// To continuation, ie Expect(value).To... /// ITo To { get; } + /// /// Not continuation, ie, Expect(value).Not... /// IPropertyNot Not { get; } - } \ No newline at end of file diff --git a/src/NExpect/Interfaces/INotAfterTo.cs b/src/NExpect/Interfaces/INotAfterTo.cs index 0458a0b9..de598951 100644 --- a/src/NExpect/Interfaces/INotAfterTo.cs +++ b/src/NExpect/Interfaces/INotAfterTo.cs @@ -6,27 +6,46 @@ namespace NExpect.Interfaces; /// Continuation of Not after To, ie ...Not.To... /// /// Underlying type of the continuation -public interface INotAfterTo: ICanAddMatcher +public interface INotAfterTo : ICanAddMatcher { /// /// Continuation of Be, carrying type /// IBe Be { get; } + /// /// Starts a test for contains on arbitrary objects /// IContain Contain { get; } + /// /// Starts a property expectation /// IHave Have { get; } + /// /// Starts a deep equality test expectation /// IDeep Deep { get; } + /// /// Starts an intersection equality test /// -> only tests properties and fields which match by name and type /// IIntersection Intersection { get; } + + /// + /// Starts an expectation for approximate equality + /// + IApproximately Approximately { get; } + + /// + /// Provides a dangling grammar point for extension (.Find) + /// + IFind Find { get; } + + /// + /// Provides a dangling grammar point for extension (.Require) + /// + IRequire Require { get; } } \ No newline at end of file diff --git a/src/NExpect/Interfaces/IRequire.cs b/src/NExpect/Interfaces/IRequire.cs new file mode 100644 index 00000000..a5a435ec --- /dev/null +++ b/src/NExpect/Interfaces/IRequire.cs @@ -0,0 +1,10 @@ +namespace NExpect.Interfaces +{ + /// + /// Provides the .Require grammar extension + /// + /// + public interface IRequire : ICanAddMatcher + { + } +} \ No newline at end of file diff --git a/src/NExpect/Interfaces/ITo.cs b/src/NExpect/Interfaces/ITo.cs index d1654503..87961f4a 100644 --- a/src/NExpect/Interfaces/ITo.cs +++ b/src/NExpect/Interfaces/ITo.cs @@ -48,4 +48,9 @@ public interface ITo : ICanAddMatcher /// Provides a dangling grammar point for extension (.Find) /// IFind Find { get; } + + /// + /// Provides a dangling grammar point for extension (.Require) + /// + IRequire Require { get; } } \ No newline at end of file diff --git a/src/NExpect/Interfaces/IToAfterNot.cs b/src/NExpect/Interfaces/IToAfterNot.cs index 35551c58..be0957cc 100644 --- a/src/NExpect/Interfaces/IToAfterNot.cs +++ b/src/NExpect/Interfaces/IToAfterNot.cs @@ -43,4 +43,9 @@ public interface IToAfterNot : ICanAddMatcher /// Provides a dangling grammar point for extension (.Find) /// IFind Find { get; } + + /// + /// Provides a dangling grammar point for extension (.Require) + /// + IRequire Require { get; } } \ No newline at end of file diff --git a/src/PeanutButter b/src/PeanutButter index 43f55ff4..23d6bdc3 160000 --- a/src/PeanutButter +++ b/src/PeanutButter @@ -1 +1 @@ -Subproject commit 43f55ff45bf6e9be110e0395f80fa414026c2b32 +Subproject commit 23d6bdc3e228a107ca6c29ca3ccb7655fba50cfd