public static void Statisfying <T, C>(this IEnumerable <EnumerableElementAssertions <T, C> > assertions, Action <T, C> predicate) { using (var scope = new AssertionScope()) { int index = 0; foreach (var item in assertions) { try { predicate(item.Subject, item.MatchedElement); } catch (Exception ex) { scope.FailWith("Exception thrown: {0}", ex); } var failures = scope.Discard(); if (failures.Length > 0) { scope.FailWith("In the {context} the {0}. item didn't statisfyed all the condition on the matched element.", index); scope.FailWith("Item: {0}", item.Subject); scope.FailWith("MatchedElement: {0}", item.MatchedElement); scope.AddPreFormattedFailure("Failures:"); foreach (var failure in failures) { scope.AddPreFormattedFailure(failure); } break; } index++; } } }
private string[] CollectFailuresFromInspectors(IEnumerable <Action <T> > elementInspectors) { string[] collectionFailures; using (var collectionScope = new AssertionScope()) { int index = 0; foreach ((T element, Action <T> inspector) in Subject.Zip(elementInspectors, (element, inspector) => (element, inspector))) { string[] inspectorFailures; using (var itemScope = new AssertionScope()) { inspector(element); inspectorFailures = itemScope.Discard(); } if (inspectorFailures.Length > 0) { // Adding one tab and removing trailing dot to allow nested SatisfyRespectively string failures = string.Join(Environment.NewLine, inspectorFailures.Select(x => x.IndentLines().TrimEnd('.'))); collectionScope.AddPreFormattedFailure($"At index {index}:{Environment.NewLine}{failures}"); } index++; } collectionFailures = collectionScope.Discard(); } return(collectionFailures); }
private static string[] CollectFailuresFromInspectors <T, TAssertions>( this CollectionAssertions <IEnumerable <T>, TAssertions> assertions, Action <T> inspector) where TAssertions : CollectionAssertions <IEnumerable <T>, TAssertions> { using (AssertionScope assertionScope1 = new AssertionScope()) { int num = 0; foreach (var obj in assertions.Subject) { string[] strArray; using (AssertionScope assertionScope2 = new AssertionScope()) { inspector(obj); strArray = assertionScope2.Discard(); } if (strArray.Length != 0) { string str = string.Join(Environment.NewLine, strArray.Select(x => x.IndentLines().TrimEnd('.'))); assertionScope1.AddPreFormattedFailure($"At index {num}:{Environment.NewLine}{str}"); } ++num; } return(assertionScope1.Discard()); } }
private static string[] CollectFailuresFromInspectors <T, TAssertions>( this CollectionAssertions <IEnumerable <T>, TAssertions> assertions, Action <T> inspector) where TAssertions : CollectionAssertions <IEnumerable <T>, TAssertions> { using (AssertionScope assertionScope1 = new AssertionScope()) { int num = 0; foreach (var obj in assertions.Subject) { string[] strArray; using (AssertionScope assertionScope2 = new AssertionScope()) { inspector(obj); strArray = assertionScope2.Discard(); } if (strArray.Length != 0) { string str = string.Join(Environment.NewLine, strArray.Select(x => x.IndentLines().TrimEnd('.'))); string valueText = ""; if (obj != null && obj.GetType().GetMethod("ToString")?.DeclaringType != typeof(object)) { valueText = $" with value {obj}"; } assertionScope1.AddPreFormattedFailure($"At index {num}{valueText}:{Environment.NewLine}{str}"); } ++num; } return(assertionScope1.Discard()); } }
public static void Fail(this AssertionScope assertionScope, string message) { assertionScope.AddPreFormattedFailure(message); assertionScope.FailWith(string.Empty, Array.Empty <object>()); }