public static void ImplementEquivalentTo <T>(ICheckLogic <object> checker, IEnumerable <T> content) { checker.Analyze((sut, test) => { if (sut == null) { if (content != null) { test.Fail("The {checked} is null whereas it should not."); } return; } if (content == null) { test.Fail("The {checked} must be null."); return; } var scan = FluentEquals(sut, content, EqualityMode.FluentEquals); if (scan.IsEquivalent || !scan.IsDifferent) { return; } test.Fail(scan.GetErrorMessage(sut, content, true)); }).DefineExpectedValue(content) .OnNegate("The {checked} is equivalent to the {expected} whereas it should not.").EndCheck(); }
/// <summary> /// Failing condition /// </summary> /// <param name="logic"></param> /// <param name="predicate">Predicate, returns true if test fails.</param> /// <param name="error">Error message on failure</param> /// <param name="newOptions"></param> /// <returns>Continuation object.</returns> public static ICheckLogic <T> FailWhen <T>(this ICheckLogic <T> logic, Func <T, bool> predicate, string error, MessageOption newOptions = MessageOption.None) { return(logic.Analyze((sut2, test) => { if (predicate(sut2)) { test.Fail(error, newOptions); } })); }
private static void ImplementContainsExactly <T, TU>(ICheckLogic <T> test, IEnumerable <TU> enumerable) where T : IEnumerable <TU> { var count = enumerable?.Count() ?? 0; test.DefineExpectedValues(enumerable, count). FailWhen(sut => sut == null && enumerable != null, "The {0} is null and thus does not contain exactly the {1}.", MessageOption.NoCheckedBlock). FailWhen(sut => sut != null && enumerable == null, "The {0} is not null whereas it should.", MessageOption.NoExpectedBlock). OnNegate("The {0} contains exactly the given values whereas it must not.", MessageOption.NoExpectedBlock); test.Analyze((sut, runner) => { if (sut == null) { return; } var index = 0; using (var first = sut.GetEnumerator()) { var comparer = new EqualityHelper.EqualityComparer <object>(); // ReSharper disable once PossibleNullReferenceException var expectedCount = count; var failed = false; using (var second = enumerable.GetEnumerator()) { while (first.MoveNext()) { if (!second.MoveNext() || !comparer.Equals(first.Current, second.Current)) { test.Fail( index == expectedCount ? $"The {{0}} does not contain exactly the expected value(s). There are extra elements starting at index #{index}." : $"The {{0}} does not contain exactly the expected value(s). First difference is at index #{index}."); test.SetValuesIndex(index); failed = true; break; } index++; } if (!second.MoveNext() || failed) { return; } test.Fail( $"The {{0}} does not contain exactly the expected value(s). Elements are missing starting at index #{index}."); test.SetValuesIndex(index); } } }); test.EndCheck(); }
private static void ImplementEquivalentTo <T>(ICheckLogic <IEnumerable <T> > checker, IEnumerable <T> content) { var length = content?.Count() ?? 0; checker.Analyze((sut, test) => { if (sut == null) { if (content != null) { test.Fail("The {checked} is null whereas it should not."); } return; } if (content == null) { test.Fail("The {checked} must be null."); return; } var expectedContent = new List <T>(content); var isOk = true; foreach (var item in sut) { if (!expectedContent.Remove(item)) { test.Fail( $"The {{checked}} does contain [{item.ToStringProperlyFormatted().DoubleCurlyBraces()}] whereas it should not."); isOk = false; } } if (isOk && expectedContent.Count > 0) { if (expectedContent.Count == 1) { test.Fail( $"The {{checked}} is missing: [{expectedContent[0].ToStringProperlyFormatted().DoubleCurlyBraces()}]."); } else { test.Fail( $"The {{checked}} is missing {expectedContent.Count} items: {expectedContent.ToStringProperlyFormatted().DoubleCurlyBraces()}."); } } }).DefineExpectedValues(content, length) .OnNegate("The {checked} is equivalent to the {expected} whereas it should not.").EndCheck(); }