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);
                        }
                    }
                }
        }
예제 #2
0
        public void Test001()
        {
            var d1 = DateTimeNormalizationEvaluator.NormalizeDateTime(DateTime.Now);
            var d2 = new DateTime(d1.Ticks + 1);

            _introspective.ExpectAssertionSuccessNext();
            _assert.AreEqual(() => d1, () => d2);
        }
예제 #3
0
        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);
        }
예제 #4
0
        public void Test003()
        {
            var ac = new AssertionContext();

            ac.Set("abc", "def");

            var values = ac.GetData();

            assert.AreEqual(1, () => values.Count());
            assert.AreEqual(new
            {
                Key   = "abc",
                Value = "def",
                Depth = 0
            }, () => values.Select(kvp => new
            {
                Key   = kvp.Key,
                Value = kvp.Value,
                Depth = kvp.Depth
            }).Single());

            ac.Set("abc", "ghi");

            var values2 = ac.GetData();

            assert.AreEqual(1, () => values2.Count());
            assert.AreEqual(new
            {
                Key   = "abc",
                Value = "ghi",
                Depth = 0
            }, () => values2.Select(kvp => new
            {
                Key   = kvp.Key,
                Value = kvp.Value,
                Depth = kvp.Depth
            }).Single());
        }
        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 void Check(IAssertionTool assert, AssertionData data)
 {
     DoTests(assert, this, data);
     assert.AreEqual(() => this.ExpectedAssertionType, data.GetType(), "Expected assertion should be same type");
 }
 public void NoneOutstanding()
 {
     _inner.AreEqual(0, () => _expectations.Count, "There were 1 or more expected assertions which were not checked");
 }