Ejemplo n.º 1
0
        public static void ShouldDeepEqual(this object actual, object expected, IComparison comparison, bool recursionProtection = true)
        {
            var context = new ComparisonContext {
                RecursionProtection = recursionProtection
            };

            var result = comparison.Compare(context, actual, expected);

            if (result != ComparisonResult.Fail)
            {
                return;
            }

            var sb = new StringBuilder();

            sb.Append("Comparison Failed");

            if (context.Differences.Count > 0)
            {
                sb.AppendFormat(": The following {0} differences were found.", context.Differences.Count);

                foreach (var difference in context.Differences)
                {
                    var lines = difference.ToString().Split(new[] { "\n" }, StringSplitOptions.None);

                    sb.Append("\n\t");
                    sb.Append(string.Join("\n\t", lines));
                }
            }

            throw new Exception(sb.ToString());
        }
Ejemplo n.º 2
0
        public static bool IsDeepEqual(this object actual, object expected, IComparison comparison)
        {
            var context = new ComparisonContext();

            var result = comparison.Compare(context, actual, expected);

            return(result == ComparisonResult.Pass);
        }
Ejemplo n.º 3
0
        public static bool IsDeepEqual(this object actual, object expected, IComparison comparison, bool recursionProtection = true)
        {
            var context = new ComparisonContext {
                RecursionProtection = recursionProtection
            };

            var result = comparison.Compare(context, actual, expected);

            return(result == ComparisonResult.Pass);
        }
        public static bool IsDeepEqual(this object actual, object expected, IComparison comparison)
        {
            comparison = comparison ?? new ComparisonBuilder().Create();

            var context = new ComparisonContext();

            var(result, _) = comparison.Compare(context, actual, expected);

            return(result != ComparisonResult.Fail);
        }
Ejemplo n.º 5
0
        protected override bool CompareImp(
            string input,
            int pos,
            int depth,
            RunState runState)
        {
            bool rv = !m_Comparison.Compare(input, pos, depth + 1, runState);

            return(rv);
        }
            public bool Equals(object x, object y)
            {
                var result = comparison.Compare(context, x, y);

                results.Add(result.result);

                // Objects without properties is compared to inconclusive by default by DeepEqual library, for testing Inconclusive means pass.
                // Equals cannot return Inconclusive, so this comparer would always either pass or fail and not continue to the next comparer for
                // further inspection. Therefore we always return true and consult the Results property afterwards.
                // Later we can adopt the DeepEquals library and change the default behavior for empty objects, so that they will pass instead of
                // be inconclusive when they are compared.
                return(true);
            }
Ejemplo n.º 7
0
        public static void ShouldDeepEqual(this object actual, object expected, IComparison comparison)
        {
            comparison = comparison ?? new ComparisonBuilder().Create();

            var context = new ComparisonContext();

            var result = comparison.Compare(context, actual, expected);

            if (result != ComparisonResult.Fail)
            {
                return;
            }

            throw new DeepEqualException(context);
        }
Ejemplo n.º 8
0
        protected override bool CompareAndAdvanceImp(
            string input,
            int pos,
            int depth,
            RunState runState,
            out int index)
        {
#if DEBUG
            if (pos == 1720)
            {
            }
#endif
            index = pos;
            bool rv = m_Comparison.Compare(input, pos, depth, runState);
            if (rv)
            {
            }
            return(rv);
        }
        public static void ShouldDeepEqual(
            this object actual,
            object expected,
            IComparison comparison,
            IDifferenceFormatterFactory formatterFactory)
        {
            var builder = new ComparisonBuilder();

            comparison       = comparison ?? builder.Create();
            formatterFactory = formatterFactory ?? builder.GetFormatterFactory();

            var context = new ComparisonContext();

            var(result, newContext) = comparison.Compare(context, actual, expected);

            if (result != ComparisonResult.Fail)
            {
                return;
            }

            var message = new DeepEqualExceptionMessageBuilder(newContext, formatterFactory).GetMessage();

            throw new DeepEqualException(message, newContext);
        }
Ejemplo n.º 10
0
        public T[] FastSort(T[] a, IComparison <T> comp)
        {
            if (a.Length == 0)
            {
                return(a);
            }
            else
            {
                if (a.Length <= 2)
                {
                    if (a.Length == 1)
                    {
                        return(a);
                    }
                    else
                    {
                        int compi = comp.Compare(a[0], a[1]);
                        T[] b     = new T[a.Length];
                        if (compi == 2)
                        {
                            return(a);
                        }
                        else if (compi == 3)
                        {
                            b[0] = a[0];
                            b[1] = a[1];
                            return(b);
                        }
                        else
                        {
                            b[0] = a[1];
                            b[1] = a[0];
                            return(b);
                        }
                    }
                }


                else
                {
                    T[] b = new T[0];
                    T[] c = new T[0];
                    T[] d = new T[0];
                    System.Console.WriteLine("gena");
                    for (int i = 1; i < a.Length; i++)
                    {
                        int compi = comp.Compare(a[0], a[i]);

                        if (compi == 1)
                        {
                            Array.Resize(ref c, c.Length + 1);
                            c[c.Length - 1] = a[i];
                        }
                        else if (compi == 2)
                        {
                            Array.Resize(ref d, d.Length + 1);
                            d[d.Length - 1] = a[i];
                        }
                        else
                        {
                            Array.Resize(ref b, b.Length + 1);
                            b[b.Length - 1] = a[i];
                        }
                    }
                    Array.Resize(ref d, d.Length + 1);
                    d[d.Length - 1] = a[0];
                    b = FastSort(b, comp);
                    c = FastSort(c, comp);
                    if (b == null & d == null)
                    {
                        return(c);
                    }
                    else if (c == null & d == null)
                    {
                        return(b);
                    }
                    else if (c == null & b == null)
                    {
                        return(d);
                    }
                    else
                    {
                        var z = new T[d.Length + c.Length];
                        c.CopyTo(z, 0);
                        d.CopyTo(z, c.Length);
                        var g = new T[z.Length + b.Length];
                        z.CopyTo(g, 0);
                        b.CopyTo(g, z.Length);
                        return(g);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        protected override bool CompareAndAdvanceImp(
            string str,
            int firstIndex,
            int depth,
            RunState runState,
            out int index)
        {
            index = -1;
            // Statement index
            int  stmtIndex;
            bool failedMatch = false;
            int  pos;
            int  depthPlusOne = depth + 1;

            for (stmtIndex = 0, pos = firstIndex; !failedMatch && stmtIndex < m_Statements.Count;
                 ++stmtIndex)
            {
                // Key nuance design decision. It's OK to perform operations/comparisons even if we have reached the end of
                //  the string. This is necessary for things like setting variables, comparing against the end of the
                //  string e.t.c. No doubt there will be bugs presuming this isn't the case but I've only realised
                //  retrospectively that this is a requirement.
                IStatement stmt = m_Statements[stmtIndex];
                if (stmt is IOperation)
                {
                    IOperation op = (IOperation)stmt;
                    op.Perform(str, pos, depthPlusOne, runState, out pos);
                }
                else
                {
                    if (stmt is IComparisonWithAdvance)
                    {
                        IComparisonWithAdvance comp = (IComparisonWithAdvance)stmt;
                        int previousPos             = pos;
                        if (!comp.CompareAndAdvance(str, pos, depthPlusOne, runState, out pos))
                        {
                            // This part of the pattern match failed. Start again (using the outer loop/index) but at
                            //  the next character
                            failedMatch = true;
                        }
                    }
                    else if (stmt is IComparison)
                    {
                        //sidtodo not sure about this - I'm not sure the design of the statement classes is right
                        IComparison comp = (IComparison)stmt;
                        if (!comp.Compare(str, pos, depthPlusOne, runState))
                        {
                            failedMatch = true;
                        }
                    }
                }
            }

            // Got this far without a match?
            if (!failedMatch)
            {
                // Did we execute all statements?
                if (stmtIndex == m_Statements.Count)
                {
                    // Exclusions?
                    if (Exclusion != null)
                    {
                        string subStr = str.Substring(firstIndex, pos - firstIndex);
                        failedMatch = Exclusion.Compare(subStr, 0, depthPlusOne, runState);
                    }
                    if (!failedMatch)
                    {
                        // Successful match
                        index = pos;
                    }
                }
                else
                {
                    // We did not complete all of the comparisons
                    // Go to the next character
                    // Potentially a speedup here? If we reach the end of the input string but cannot complete all
                    //  comparisons then copy the remaining string rather than trying to do any more comparisons?
                    failedMatch = true;
                }
            }

            return(!failedMatch);
        }