private static void ImplementIsOnlyMadeOf <T>(ICheckLogic <IEnumerable <T> > checker, IEnumerable expectedValues) { checker. DefineExpectedValues(expectedValues?.Cast <object>(), expectedValues.Count(), comparison: "only elements from", negatedComparison: "at least one element different from"). FailWhen(sut => sut == null & expectedValues != null, "The {0} is null and thus, does not contain exactly the given value(s)."). Analyze((sut, test) => { if (sut == null && expectedValues == null) { return; } var unexpectedValuesFound = ExtractUnexpectedValues(sut, expectedValues); if (unexpectedValuesFound.Count <= 0) { return; } test.Fail( string.Format( "The {{0}} does not contain only the given value(s)." + Environment.NewLine + "It contains also other values:" + Environment.NewLine + "\t{0}", unexpectedValuesFound.ToStringProperlyFormatted().DoubleCurlyBraces())); }). OnNegate("The {0} contains only the given values whereas it must not.").EndCheck(); }
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(); }