public bool Compare(ComparisonContext context, ValueComparison valueComparison)
        {
            bool areEqual;
            var options = context.Rules.GetOptions<FloatValueComparatorOptions>(valueComparison);

            if (valueComparison.PropertyInfo.PropertyType == typeof(float))
            {
                areEqual = this.CompareFloats(
                    (float)valueComparison.ExpectedValue,
                    (float)valueComparison.ActualValue,
                    options.FloatEpsilon);
            }
            else
            {
                areEqual = this.CompareDoubles(
                    (double)valueComparison.ExpectedValue,
                    (double)valueComparison.ActualValue,
                    options.DoubleEpsilon);
            }

            if (!areEqual)
            {
                context.AddDifference(valueComparison);
            }

            return areEqual;
        }
示例#2
0
        public void Missing_actual_entry()
        {
            var context = new ComparisonContext();
            context.AddDifference(new MissingEntryDifference(MissingSide.Actual, "Index", "Value"));

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual[""Index""] not found (Expected[""Index""] = ""Value"")");
        }
示例#3
0
        public void Missing_actual_entry()
        {
            var context = new ComparisonContext();

            context.AddDifference(new MissingEntryDifference(MissingSide.Actual, "Index", "Value"));

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual[""Index""] not found (Expected[""Index""] = ""Value"")"    );
        }
示例#4
0
        public void null_difference()
        {
            var context = new ComparisonContext();

            context.AddDifference(null, new object());

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual != Expected ((null) != System.Object)"    );
        }
示例#5
0
        public void Single_string_difference()
        {
            var context = new ComparisonContext();

            context.AddDifference("a", "b");

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual != Expected (""a"" != ""b"")"    );
        }
示例#6
0
        public void Single_int_difference()
        {
            var context = new ComparisonContext();

            context.AddDifference(1, 2);

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual != Expected (1 != 2)"    );
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areEqual = object.Equals(comparison.ExpectedValue, comparison.ActualValue);
            if (!areEqual)
            {
                context.AddDifference(comparison);
            }

            return areEqual;
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areEqual = ReferenceEquals(comparison.ExpectedValue, comparison.ActualValue);
            if (!areEqual)
            {
                comparison.Message = "The instances are different, reference equals failed.";
                context.AddDifference(comparison);
            }

            return areEqual;
        }
示例#9
0
        public void Long_string_difference_start()
        {
            var context = new ComparisonContext();
            context.AddDifference(
                "01234567890123456789012345678901234567890123456789",
                "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij");

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual != Expected (""01234567890123456789..."" != ""abcdefghijabcdefghij..."")");
        }
示例#10
0
        public void Long_string_difference_middle()
        {
            var context = new ComparisonContext();
            context.AddDifference(
                "01234567890123456789012345first678901234567890123456789",
                "01234567890123456789012345second678901234567890123456789");

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual != Expected (""...6789012345first67890..."" != ""...6789012345second6789..."")");
        }
示例#11
0
        public void Long_string_difference_end_2()
        {
            var context = new ComparisonContext();
            context.AddDifference(
                "01234567890123456789012345678901234567890123",
                "01234567890123456789012345678901234567890123456789");

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual != Expected (""...45678901234567890123"" != ""...01234567890123456789"")");
        }
示例#12
0
        public void Long_string_difference_middle()
        {
            var context = new ComparisonContext();

            context.AddDifference(
                "01234567890123456789012345first678901234567890123456789",
                "01234567890123456789012345second678901234567890123456789");

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual != Expected (""...6789012345first67890..."" != ""...6789012345second6789..."")"    );
        }
示例#13
0
        public void Long_string_difference_start()
        {
            var context = new ComparisonContext();

            context.AddDifference(
                "01234567890123456789012345678901234567890123456789",
                "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij");

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual != Expected (""01234567890123456789..."" != ""abcdefghijabcdefghij..."")"    );
        }
示例#14
0
        public void Strings_around_same_length_as_max_length(string value1, string value2, string expected1, string expected2)
        {
            var context = new ComparisonContext();

            context.AddDifference(
                value1,
                value2);

            AssertExceptionMessage(context, string.Format(@"
Comparison Failed: The following 1 differences were found.
	Actual != Expected (""{0}"" != ""{1}"")"    , expected1, expected2));
        }
示例#15
0
        public void Long_string_difference_end_2()
        {
            var context = new ComparisonContext();

            context.AddDifference(
                "01234567890123456789012345678901234567890123",
                "01234567890123456789012345678901234567890123456789");

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual != Expected (""...45678901234567890123"" != ""...01234567890123456789"")"    );
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var maxDimentions = comparison.PropertyType.GetArrayRank();
            var currentIndices = new int[maxDimentions];

            if (!this.CompareArrayDimensionLengths(comparison, maxDimentions))
            {
                context.AddDifference(comparison);
                return false;
            }

            return this.CompareArrayItems(context, comparison, 0, currentIndices);
        }
        public bool Compare(ComparisonContext comparisonContext, ValueComparison valueComparison)
        {
            var expected = (IComparable)valueComparison.ExpectedValue;
            var actual = (IComparable)valueComparison.ActualValue;

            var areEqual = expected.CompareTo(actual) == 0;
            if (!areEqual)
            {
                comparisonContext.AddDifference(valueComparison);
            }

            return areEqual;
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areSame = this.customCompare.Invoke(comparison, context);
            if (!areSame)
            {
                if (string.IsNullOrEmpty(comparison.Message))
                {
                    comparison.Message = "Custom comparator returned false";
                }

                context.AddDifference(comparison);
            }

            return areSame;
        }
        public bool Compare(ComparisonContext context, ValueComparison valueComparison)
        {
            var options = context.Rules.GetOptions<StringValueComparatorOptions>(valueComparison);
            var areEqual = string.Equals(
                (string)valueComparison.ExpectedValue,
                (string)valueComparison.ActualValue,
                options.ComparisonType);

            if (!areEqual)
            {
                context.AddDifference(valueComparison);
            }

            return areEqual;
        }
        private bool AreKeysEqual(IDictionary expected, IDictionary actual, ComparisonContext comparisonContext, ValueComparison valueComparison)
        {
            var notInExpected = actual.Keys
                .Cast<object>()
                .Where(x => !expected.Contains(x));

            var notInActual = expected.Keys
                .Cast<object>()
                .Where(x => !actual.Contains(x));

            var result = true;

            foreach (var key in notInActual)
            {
                var keyComparison = this.CreateKeyNamedComparison(
                    valueComparison,
                    key.ToString(),
                    "The dictionaries differ, the key '{0}' is missing in the actual value.");
                
                comparisonContext.AddDifference(keyComparison);
                result = false;
            }

            foreach (var key in notInExpected)
            {        
                var keyComparison = this.CreateKeyNamedComparison(
                    valueComparison, 
                    key.ToString(), 
                    "The dictionaries differ, there is an extra key '{0}' in the actual value.");

                comparisonContext.AddDifference(keyComparison);
                result = false;
            }

            return result;
        }
示例#21
0
        public void Set_difference_actual()
        {
            var context = new ComparisonContext();

            context.AddDifference(new SetDifference(".Set", new List <object>(), new List <object> {
                1, 2, 3
            }));

            AssertExceptionMessage(context, @"
Comparison Failed: The following 1 differences were found.
	Actual.Set != Expected.Set
		Actual.Set contains the following unmatched elements:
			1
			2
			3"            );
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areEqual = true;
            var options = context.Rules.GetOptions<CollectionValueComparatorOptions>(comparison);

            var actual = (IEnumerable)comparison.ActualValue;
            var expected = (IEnumerable)comparison.ExpectedValue;

            if (options.OrderFunction != null)
            {
                actual = options.OrderFunction.Invoke(actual);
                expected = options.OrderFunction.Invoke(expected);
            }

            var actualEnumerator = actual.GetEnumerator();
            var expectedEnumerator = expected.GetEnumerator();

            var actualHasNext = actualEnumerator.MoveNext();
            var expectedHasNext = expectedEnumerator.MoveNext();

            var itemCounter = 0;
            while (actualHasNext && expectedHasNext)
            {
                var propertyPath = new PropertyPathItem(itemCounter.ToString(), comparison.PropertyPathItem);
                if (!context.CompareItem(expectedEnumerator.Current, actualEnumerator.Current, propertyPath))
                {
                    areEqual = false;
                }

                expectedHasNext = expectedEnumerator.MoveNext();
                actualHasNext = actualEnumerator.MoveNext();
                itemCounter++;
            }

            if (actualHasNext != expectedHasNext)
            {
                comparison.Message = string.Format(
                    "The collections have a different number of items, expected: {0}, actual: {1}.",
                    expected.Cast<object>().Count(),
                    actual.Cast<object>().Count());

                context.AddDifference(comparison);
                areEqual = false;
            }

            return areEqual;
        }
        public void Strings_around_same_length_as_max_length(string value1, string value2, string expected1, string expected2)
        {
            var context = new ComparisonContext();
            context.AddDifference(
                value1,
                value2);

            AssertExceptionMessage(context, string.Format(@"
            Comparison Failed: The following 1 differences were found.
            Actual != Expected (""{0}"" != ""{1}"")", expected1, expected2));
        }
示例#24
0
        public void Single_string_difference()
        {
            var context = new ComparisonContext();
            context.AddDifference("a", "b");

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual != Expected (""a"" != ""b"")");
        }
示例#25
0
        public void Set_difference_expected()
        {
            var context = new ComparisonContext();
            context.AddDifference(new SetDifference(".Set", new List<object>{1,2,3}, new List<object>()));

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual.Set != Expected.Set
            Expected.Set contains the following unmatched elements:
            1
            2
            3");
        }
示例#26
0
        public void null_difference()
        {
            var context = new ComparisonContext();
            context.AddDifference(null, new object());

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual != Expected ((null) != System.Object)");
        }
示例#27
0
        public void Single_int_difference()
        {
            var context = new ComparisonContext();
            context.AddDifference(1, 2);

            AssertExceptionMessage(context, @"
            Comparison Failed: The following 1 differences were found.
            Actual != Expected (1 != 2)");
        }