private static bool SequenceEqual <T>(IEnumerable <T> expected, IEnumerable <T> actual, IEqualityComparer <T> comparer = null) { var enumerator1 = expected.GetEnumerator(); var enumerator2 = actual.GetEnumerator(); while (true) { var hasNext1 = enumerator1.MoveNext(); var hasNext2 = enumerator2.MoveNext(); if (hasNext1 != hasNext2) { return(false); } if (!hasNext1) { break; } var value1 = enumerator1.Current; var value2 = enumerator2.Current; if (!(comparer != null ? comparer.Equals(value1, value2) : AssertEqualityComparer <T> .Equals(value1, value2))) { return(false); } } return(true); }
public static void AreEqual <T>(T expected, T actual, string message = null, IEqualityComparer <T> comparer = null) { if (ReferenceEquals(expected, actual)) { return; } if (expected == null) { Fail("expected was null, but actual wasn't\r\n" + message); } else if (actual == null) { Fail("actual was null, but expected wasn't\r\n" + message); } else { if (!(comparer != null ? comparer.Equals(expected, actual) : AssertEqualityComparer <T> .Equals(expected, actual))) { Fail("Expected and actual were different.\r\n" + "Expected: " + expected + "\r\n" + "Actual: " + actual + "\r\n" + message); } } }
public static void Equal <T>(T expected, T actual) { var comparer = new AssertEqualityComparer <T>(); if (!comparer.Equals(expected, actual)) { throw new AssertActualExpectedException(expected, actual); } }
public void Should_return_false_if_expected_is_null_when_comparing_equality() { // Given var comparer = new AssertEqualityComparer <object>(); // When var results = comparer.Equals(null, new object()); // Then results.ShouldBeFalse(); }
public void Should_return_result_from_comparing_generic_comparables(int compareResult, bool expectedResult) { // Given var comparer = new AssertEqualityComparer <GenericCompareableModel>(); var actual = new GenericCompareableModel(compareResult); var expected = new GenericCompareableModel(); // When var result = comparer.Equals(expected, actual); // Then result.ShouldEqual(expectedResult); }
public void Should_invoke_compareto_with_expected_value_when_actual_is_generic_comparable() { // Given var comparer = new AssertEqualityComparer <GenericCompareableModel>(); var actual = new GenericCompareableModel(); var expected = new GenericCompareableModel(); // When comparer.Equals(expected, actual); // Then actual.ExpectedValueThatWasPassedIn.ShouldBeSameAs(expected); }
public void Should_return_result_from_comparing_equatables(bool expectedReturnValue) { // Given var comparer = new AssertEqualityComparer <EquatableModel>(); var actual = new EquatableModel(expectedReturnValue); var expected = new EquatableModel(); // When var result = comparer.Equals(expected, actual); // Then result.ShouldEqual(expectedReturnValue); }
private static bool SequenceEqual <T>(ReadOnlySpan <T> expected, ReadOnlySpan <T> actual, IEqualityComparer <T> comparer = null) { if (expected.Length != actual.Length) { return(false); } for (int i = 0; i < expected.Length; i++) { if (!(comparer is not null ? comparer.Equals(expected[i], actual[i]) : AssertEqualityComparer <T> .Equals(expected[i], actual[i]))) { return(false); } } return(true); }