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 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 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 IDisposable ContextPush(this IAssertionTool assert) { return(assert.ContextGet().Push()); }
public static IAssertionTool ContextSet(this IAssertionTool assert, string key, string value) { assert.ContextGet().Set(key, value); return(assert); }
public SoftAssertionTool(IAssertionTool parent) { var ctx = parent.ContextGet(); this.ContextSet(ctx); }