public static void Each <T>(this IAssertionTool assertTool, Expression <Func <IEnumerable <T> > > collectionExp, Expression <Action <IAssertionTool, T> > action, string msg, params object[] fmt) { if (string.IsNullOrWhiteSpace(msg)) { msg = string.Empty; fmt = fmt ?? new object[] { }; } else if (fmt == null) { fmt = new object[] { msg }; msg = "{0}"; } var evaluator = assertTool.GetExpressionEvaluator(); var context = assertTool.ContextGet(); using (context.Push()) { context.Set("Message", string.Format(msg, fmt)); var collection = evaluator.Eval(collectionExp); foreach (var ele in collection.Select((e, i) => new { e, i })) { context.Set("Element Index", ele.i.ToString()); evaluator.Eval(action, assertTool, ele.e); } } }
public static void Throws <T>(this IAssertionTool assertTool, Expression <Func <object> > test, Exception exc, string message, params object[] fmt) { if (fmt == null) { fmt = new object[] { }; } if (fmt.Length <= 0) { fmt = new object[] { message }; message = "{0}"; } Exception internalError = null; try { assertTool.GetExpressionEvaluator().Eval(test); } catch (Exception eError) { internalError = eError; } if (internalError is T) { assertTool.Accept(new AssertionSuccess(test.Body, null, null, null, null, message, fmt, exc, internalError, assertTool.ContextGetData())); } else { assertTool.Accept(new AssertionFailure(test.Body, null, null, null, null, message, fmt, exc, internalError, assertTool.ContextGetData())); } }
public static void EachSorted <T>(this IAssertionTool assertTool, Expression <Func <IEnumerable <T> > > expectedExp, Expression <Func <IEnumerable <T> > > actualExp, Expression <Action <IAssertionTool, T, T> > action, string msg, params object[] fmt) { if (string.IsNullOrWhiteSpace(msg)) { msg = string.Empty; fmt = fmt ?? new object[] { }; } else if (fmt == null) { fmt = new object[] { msg }; msg = "{0}"; } var evaluator = assertTool.GetExpressionEvaluator(); var context = assertTool.ContextGet(); var expected = evaluator.Eval(expectedExp); var actual = evaluator.Eval(actualExp); using (var eenum = expected.GetEnumerator()) using (var aenum = actual.GetEnumerator()) { int i; bool enext = false, anext = false; using (context.Push()) { if (!string.IsNullOrWhiteSpace(msg)) { context.Set("Message", string.Format(msg, fmt)); } for (i = 0; (enext = eenum.MoveNext()) && (anext = aenum.MoveNext()); i++) { context.Set("Element Index", i.ToString()); evaluator.Eval(action, assertTool, eenum.Current, aenum.Current); } } if (enext != anext) { int c = i; if (enext) { while (eenum.MoveNext()) { c++; } assertTool.AreEqual(c, i, "Element Count Doesn't Match. " + msg, fmt); } else { while (aenum.MoveNext()) { c++; } assertTool.AreEqual(i, c, "Element Count Doesn't Match. " + msg, fmt); } } } }
public static void IsEmpty <T>(this IAssertionTool assertTool, Expression <Func <IEnumerable <T> > > collection, string msg, params object[] fmt) { var xgr = new ExpressionGenerator(); var eval = assertTool.GetExpressionEvaluator(); var exp = xgr.FromFunc(collection, c => c.Count()); assertTool.AreEqual(0, exp, msg, fmt); }
public static void EachUnsorted <T>(this IAssertionTool assertTool, Expression <Func <IEnumerable <T> > > expectedExp, Expression <Func <IEnumerable <T> > > actualExp, Expression <Action <IAssertionTool, T, T> > action, string msg, params object[] fmt) { if (string.IsNullOrWhiteSpace(msg)) { msg = string.Empty; fmt = fmt ?? new object[] { }; } else if (fmt == null) { fmt = new object[] { msg }; msg = "{0}"; } var evaluator = assertTool.GetExpressionEvaluator(); var context = assertTool.ContextGet(); var expected = new Queue <T>(evaluator.Eval(expectedExp)); var actual = evaluator.Eval(actualExp).ToList(); var innerTool = new SoftAssertionTool(assertTool); int expectedIndex = 0; int lastValidElement = -1; using (context.Push()) { while (expected.Any()) { var eCurrent = expected.Dequeue(); context.Set("Expected Index", expectedIndex++.ToString()); innerTool.Reset(); int i, n; for (i = 0, n = actual.Count; i < n; i++) { int errors = innerTool.FailureCount; int inconclusive = innerTool.InconclusiveCount; int count = innerTool.Count; context.Set("Actual Index", i.ToString()); evaluator.Eval(action, innerTool, eCurrent, actual[i]); if (innerTool.FailureCount == errors && innerTool.InconclusiveCount == inconclusive) { innerTool.ReplayWhere(assertTool, (d, eidx) => eidx >= count, "If this assert fails it is an internal error in the assertion libray"); actual.RemoveAt(i); lastValidElement = expectedIndex; break; } } if (i >= n && n > 0) { innerTool.ReplayAll(assertTool, "No matching element found"); throw new InvalidOperationException("We should have thrown an assertion failure error in the prior call"); } } } if (lastValidElement != expectedIndex) { assertTool.AreEqual(expectedIndex, actual.Count, "Element count mismatch"); throw new InvalidOperationException("We should have thrown an assertion failure error in the prior call (2)"); } }
public static void Check <T1, T2>(this IAssertionTool assertTool, Expression <Func <T1> > expected1, Expression <Func <T2> > actual1, Expression <Func <T1, T2, bool> > test, Exception exc, string message, params object[] fmt) { if (fmt == null) { fmt = new object[] { }; } if (fmt.Length <= 0) { fmt = new object[] { message }; message = "{0}"; } bool result = false; Exception internalError = null; object[] actualVals = null; object[] expectedVals = null; ParameterExpression[] expectedRefs = null; ParameterExpression[] actualRefs = null; T1 expectedVal = default(T1); T2 actualVal = default(T2); EvaluatedExpressionException expectedException = null, actualException = null; // We are intentionally invokign the actual result first. // * This is very useful when you want the FilesystemAssertionRepository to automatically create expectations for your tests try { actualVal = assertTool.GetExpressionEvaluator().Eval(actual1); actualVals = new object[] { actualVal }; actualRefs = new[] { test.Parameters[1] }; } catch (Exception eError) { actualException = new EvaluatedExpressionException(actual1, eError); } try { expectedVal = assertTool.GetExpressionEvaluator().Eval(expected1); expectedVals = new object[] { expectedVal }; expectedRefs = new[] { test.Parameters[0] }; } catch (Exception eError) { expectedException = new EvaluatedExpressionException(expected1, eError); } if (expectedException == null && actualException == null) { try { result = assertTool.GetExpressionEvaluator().Eval(test, expectedVal, actualVal); } catch (Exception eError) { internalError = new EvaluatedExpressionException(test, eError); } } else if (expectedException != null && actualException != null) { internalError = new AggregateException(expectedException, actualException); } else if (expectedException != null) { internalError = expectedException; } else if (actualException != null) { internalError = actualException; } else { throw new InvalidOperationException("This exception should be logically impossible to reach"); } var finalExpression = Expression.Invoke( test, expected1.Body, actual1.Body ); if (!result) { assertTool.Accept(new AssertionFailure(finalExpression, expectedRefs, expectedVals, actualRefs, actualVals, message, fmt, exc, internalError, assertTool.ContextGetData())); } else { assertTool.Accept(new AssertionSuccess(finalExpression, expectedRefs, expectedVals, actualRefs, actualVals, message, fmt, exc, internalError, assertTool.ContextGetData())); } }