Example #1
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var list1 = ((IEnumerable) value1).Cast<object>().ToArray();
            var list2 = ((IEnumerable) value2).Cast<object>().ToArray();

            if (list1.Length != list2.Length)
            {
                context.AddDifference(list1.Length, list2.Length, "Count");
                return ComparisonResult.Fail;
            }

            if (list1.Length == 0)
            {
                return ComparisonResult.Pass;
            }

            var zip = list1.Zip(list2, Tuple.Create).ToArray();

            var results = new List<ComparisonResult>();
            var i = 0;
            foreach (var p in zip)
            {
                var innerContext = context.VisitingIndex(i++);
                results.Add(Inner.Compare(innerContext, p.Item1, p.Item2));
            }

            return results.ToResult();
        }
        public ComparisonResult CompareObjects(IComparisonContext context, object source, object destination)
        {
            PreparePropertyInfo(source, destination);

            foreach (var pair in propertyMap)
            {
                currentPair = pair;
                var sourceValue = new Lazy<object>(() => currentPair.Source.Read(source));
                var destinationValue = new Lazy<object>(() => currentPair.Destination.Read(destination));

                if (IsPropertyIgnored())
                {
                    continue;
                }

                if (HandleMissingValues(context, destinationValue, sourceValue))
                {
                    continue;
                }

                var innerContext = context.VisitingProperty(currentPair.Name);
                results.Add(inner.Compare(innerContext, sourceValue.Value, destinationValue.Value));
            }

            return results.ToResult();
        }
Example #3
0
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            var list1 = ((IEnumerable)value1).Cast <object>().ToArray();
            var list2 = ((IEnumerable)value2).Cast <object>().ToArray();

            var length = list1.Length;

            if (length != list2.Length)
            {
                return(ComparisonResult.Fail, context.AddDifference(length, list2.Length, "Count"));
            }

            if (length == 0)
            {
                return(ComparisonResult.Pass, context);
            }

            return(Enumerable
                   .Range(0, length)
                   .Select(i => (value1: list1[i], value2: list2[i], index: i))
                   .Aggregate(
                       (result: ComparisonResult.Inconclusive, context: context),
                       (acc, x) =>
            {
                var(newResult, newContext) = Inner.Compare(context.VisitingIndex(x.index), x.value1, x.value2);
                return (acc.result.Plus(newResult), acc.context.MergeDifferencesFrom(newContext));
            }
                       ));
        }
Example #4
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            if (value1 == null && value2 == null)
            {
                return ComparisonResult.Pass;
            }

            if (value1 == null || value2 == null)
            {
                context.AddDifference(value1, value2);
                return ComparisonResult.Fail;
            }

            foreach (var c in Comparisons)
            {
                if(!c.CanCompare(value1.GetType(), value2.GetType()))
                    continue;

                var result = c.Compare(context, value1, value2);

                if (result != ComparisonResult.Inconclusive)
                    return result;
            }

            return ComparisonResult.Inconclusive;
        }
        private bool HandleMissingValues(IComparisonContext context, Lazy<object> destinationValue, Lazy<object> sourceValue)
        {
            if (SourceAndDestinationPresent())
            {
                return false;
            }

            if (ignoreUnmatchedProperties)
            {
                return true;
            }

            if (currentPair.Source == null)
            {
                context.AddDifference("(missing)", destinationValue.Value, currentPair.Name);
                results.Add(ComparisonResult.Fail);
            }

            if (currentPair.Destination == null)
            {
                context.AddDifference(sourceValue.Value, "(missing)", currentPair.Name);
                results.Add(ComparisonResult.Fail);
            }

            return true;
        }
Example #6
0
        private bool SetsEqual(IComparisonContext context, object[] set1, object[] set2)
        {
            var expected = set2.ToList();
            var extra    = new List <object>();

            for (int i = 0; i < set1.Length; i++)
            {
                var obj          = set1[i];
                var innerContext = new ComparisonContext();
                var found        = expected.FirstOrDefault(e => Inner.Compare(innerContext, obj, e) == ComparisonResult.Pass);

                if (found != null)
                {
                    expected.RemoveAll(x => x.Equals(found));
                }
                else
                {
                    extra.Add(obj);
                }
            }

            var equal = expected.Count == 0 && extra.Count == 0;

            if (!equal)
            {
                context.AddDifference(new SetDifference(context.Breadcrumb, expected, extra));
            }

            return(equal);
        }
Example #7
0
        private bool SetsEqual(IComparisonContext context, object[] set1, object[] set2)
        {
            var expected = set2.ToList();
            var extra = new List<object>();

            for (int i = 0; i < set1.Length; i++)
            {
                var obj = set1[i];
                var innerContext = new ComparisonContext();
                var found = expected.FirstOrDefault(e => Inner.Compare(innerContext, obj, e) == ComparisonResult.Pass);

                if (found != null)
                    expected.RemoveAll(x => x.Equals(found));
                else
                    extra.Add(obj);
            }

            var equal = expected.Count == 0 && extra.Count == 0;

            if (!equal)
            {
                context.AddDifference(new SetDifference(context.Breadcrumb, expected, extra));
            }

            return equal;
        }
Example #8
0
        private bool HandleMissingValues(IComparisonContext context, Lazy <object> destinationValue, Lazy <object> sourceValue)
        {
            if (SourceAndDestinationPresent())
            {
                return(false);
            }

            if (ignoreUnmatchedProperties)
            {
                return(true);
            }

            if (currentPair.Source == null)
            {
                context.AddDifference("(missing)", destinationValue.Value, currentPair.Name);
                results.Add(ComparisonResult.Fail);
            }

            if (currentPair.Destination == null)
            {
                context.AddDifference(sourceValue.Value, "(missing)", currentPair.Name);
                results.Add(ComparisonResult.Fail);
            }

            return(true);
        }
Example #9
0
        public ComparisonResult CompareObjects(IComparisonContext context, object source, object destination)
        {
            PreparePropertyInfo(source, destination);

            foreach (var pair in propertyMap)
            {
                currentPair = pair;
                var sourceValue      = new Lazy <object>(() => currentPair.Source.Read(source));
                var destinationValue = new Lazy <object>(() => currentPair.Destination.Read(destination));

                if (IsPropertyIgnored())
                {
                    continue;
                }

                if (HandleMissingValues(context, destinationValue, sourceValue))
                {
                    continue;
                }

                var innerContext = context.VisitingProperty(currentPair.Name);
                results.Add(inner.Compare(innerContext, sourceValue.Value, destinationValue.Value));
            }

            return(results.ToResult());
        }
Example #10
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            if (value1 == null && value2 == null)
            {
                return(ComparisonResult.Pass);
            }

            if (value1 == null || value2 == null)
            {
                context.AddDifference(value1, value2);
                return(ComparisonResult.Fail);
            }

            foreach (var c in Comparisons)
            {
                if (!c.CanCompare(value1.GetType(), value2.GetType()))
                {
                    continue;
                }

                var result = c.Compare(context, value1, value2);

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

            return(ComparisonResult.Inconclusive);
        }
Example #11
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var value1IsEnum = value1.GetType().GetTypeInfo().IsEnum;
            var value2IsEnum = value2.GetType().GetTypeInfo().IsEnum;

            if (value1IsEnum && value2IsEnum)
            {
                if (value1.ToString() == value2.ToString())
                {
                    return(ComparisonResult.Pass);
                }

                context.AddDifference(value1, value2);
                return(ComparisonResult.Fail);
            }

            var result = value1IsEnum
                                             ? CompareEnumWithConversion(value1, value2)
                                             : CompareEnumWithConversion(value2, value1);

            if (!result)
            {
                context.AddDifference(value1, value2);
            }

            return(result ? ComparisonResult.Pass : ComparisonResult.Fail);
        }
Example #12
0
        public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
        {
            var areEqual           = true;
            var expectedEnumerable = (IEnumerable)expected;
            var actualEnumerable   = (IEnumerable)actual;
            var expectedEnumerator = expectedEnumerable.GetEnumerator();
            var actualEnumerator   = actualEnumerable.GetEnumerator();
            var expectedHasValue   = expectedEnumerator.MoveNext();
            var actualHasValue     = actualEnumerator.MoveNext();

            var yield = 0;

            while (expectedHasValue && actualHasValue)
            {
                areEqual = comparisonContext.ReportEquality(expectedEnumerator.Current, actualEnumerator.Current,
                                                            $"[{yield++}]") && areEqual;

                expectedHasValue = expectedEnumerator.MoveNext();
                actualHasValue   = actualEnumerator.MoveNext();
            }

            if (!expectedHasValue && actualHasValue)
            {
                areEqual = comparisonContext.ReportEquality(null, new UnexpectedElement(actualEnumerator.Current),
                                                            $"[{yield}]") && areEqual;
            }

            else if (expectedHasValue)
            {
                areEqual = comparisonContext.ReportEquality(expectedEnumerator.Current, new MissingElement(),
                                                            $"[{yield}]") && areEqual;
            }

            return(areEqual);
        }
Example #13
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var list1 = ((IEnumerable)value1).Cast <object>().ToArray();
            var list2 = ((IEnumerable)value2).Cast <object>().ToArray();

            if (list1.Length != list2.Length)
            {
                context.AddDifference(list1.Length, list2.Length, "Count");
                return(ComparisonResult.Fail);
            }

            if (list1.Length == 0)
            {
                return(ComparisonResult.Pass);
            }

            var zip = list1.Zip(list2, Tuple.Create).ToArray();

            var results = new List <ComparisonResult>();
            var i       = 0;

            foreach (var p in zip)
            {
                var innerContext = context.VisitingIndex(i++);
                results.Add(Inner.Compare(innerContext, p.Item1, p.Item2));
            }

            return(results.ToResult());
        }
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            if (value1 == null && value2 == null)
            {
                return(ComparisonResult.Pass, context);
            }

            if (value1 == null || value2 == null)
            {
                return(ComparisonResult.Fail, context.AddDifference(value1, value2));
            }

            foreach (var c in Comparisons)
            {
                if (!c.CanCompare(value1.GetType(), value2.GetType()))
                {
                    continue;
                }

                var(result, newContext) = c.Compare(context, value1, value2);
                context = newContext;

                if (result != ComparisonResult.Inconclusive)
                {
                    return(result, context);
                }
            }

            return(ComparisonResult.Inconclusive, context);
        }
Example #15
0
 private int CompareLists(IComparisonContext context, object[] list1, object[] list2, List <ComparisonResult> results, int i)
 {
     foreach (var listItem in list1)
     {
         var    matches = false;
         var    item1   = SerializeObject(listItem, 1);
         object listIt2 = new object();
         foreach (var listItem2 in list2)
         {
             var item2 = SerializeObject(listItem2, 1);
             if (item1.Equals(item2))
             {
                 listIt2 = listItem2;
                 matches = true;
                 break;
             }
         }
         if (!matches)
         {
             context.AddDifference(listItem, JsonConvert.SerializeObject(item1), "missing");
             results.Add(ComparisonResult.Fail);
         }
         else
         {
             var innerContext = context.VisitingIndex(i++);
             results.Add(Inner.Compare(innerContext, listItem, listIt2));
         }
     }
     return(i);
 }
        public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
        {
            bool areEqual = true;
            var expectedEnumerable = (IEnumerable)expected;
            var actualEnumerable = (IEnumerable)actual;
            IEnumerator expectedEnumerator = expectedEnumerable.GetEnumerator();
            IEnumerator actualEnumerator = actualEnumerable.GetEnumerator();
            bool expectedHasValue = expectedEnumerator.MoveNext();
            bool actualHasValue = actualEnumerator.MoveNext();

            int yield = 0;

            while (expectedHasValue && actualHasValue)
            {
                areEqual = comparisonContext.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, "[" + yield++ + "]") && areEqual;
                
                expectedHasValue = expectedEnumerator.MoveNext();
                actualHasValue = actualEnumerator.MoveNext();
            }

            if (!expectedHasValue && actualHasValue)
            {
                areEqual = comparisonContext.AreEqual(null, new UnexpectedElement(actualEnumerator.Current), "[" + yield + "]") &&
                areEqual;
            }
            else if (expectedHasValue)
            {
                areEqual = comparisonContext.AreEqual(expectedEnumerator.Current, new MissingElement(), "[" + yield + "]") &&
                areEqual;
            }

            return areEqual;
        }
Example #17
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var dict1 = (IDictionary)value1;
            var dict2 = CastToDictionaryEntries((IDictionary)value2).ToDictionary(e => e.Key, e => e.Value);

            if (dict1.Count != dict2.Count)
            {
                context.AddDifference(dict1.Count, dict2.Count, "Count");
                return(ComparisonResult.Fail);
            }

            if (dict1.Count == 0)
            {
                return(ComparisonResult.Pass);
            }

            var results = new List <ComparisonResult>();

            foreach (DictionaryEntry entry in dict1)
            {
                var key = FindKey(dict2, entry.Key);

                if (key == null)
                {
                    var difference = new MissingEntryDifference(MissingSide.Expected, entry.Key, entry.Value)
                    {
                        Breadcrumb = context.Breadcrumb,
                    };

                    context.AddDifference(difference);

                    continue;
                }

                var value = dict2[key];

                var innerContext = context.VisitingIndex(key);
                results.Add(ValueComparer.Compare(innerContext, entry.Value, value));

                dict2.Remove(key);
            }

            if (dict2.Count == 0)
            {
                return(results.ToResult());
            }

            foreach (var pair in dict2)
            {
                var difference = new MissingEntryDifference(MissingSide.Actual, pair.Key, pair.Value)
                {
                    Breadcrumb = context.Breadcrumb,
                };

                context.AddDifference(difference);
            }

            return(ComparisonResult.Fail);
        }
Example #18
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var type1 = value1.GetType();
            var type2 = value2.GetType();

            var props1 = ReflectionCache.GetProperties(type1);
            var props2 = ReflectionCache.GetProperties(type2).ToDictionary(p => p.Name);

            var ignored = GetIgnoredPropertiesForTypes(type1, type2);

            var results = new List <ComparisonResult>();

            foreach (var propertyInfo1 in props1)
            {
                if (ignored.Contains(propertyInfo1.Name))
                {
                    props2.Remove(propertyInfo1.Name);
                    continue;
                }

                var propValue1 = propertyInfo1.GetValue(value1);

                if (!props2.ContainsKey(propertyInfo1.Name))
                {
                    if (!IgnoreUnmatchedProperties)
                    {
                        context.AddDifference(propValue1, "(missing)", propertyInfo1.Name);
                        results.Add(ComparisonResult.Fail);
                    }
                    continue;
                }

                var propertyInfo2 = props2[propertyInfo1.Name];
                var propValue2    = propertyInfo2.GetValue(value2);

                var innerContext = context.VisitingProperty(propertyInfo1.Name);
                results.Add(Inner.Compare(innerContext, propValue1, propValue2));

                props2.Remove(propertyInfo1.Name);
            }

            if (!IgnoreUnmatchedProperties && props2.Count > 0)
            {
                foreach (var p in props2)
                {
                    if (ignored.Contains(p.Key))
                    {
                        continue;
                    }

                    var v = p.Value.GetValue(value2);
                    context.AddDifference("(missing)", v, p.Key);
                    results.Add(ComparisonResult.Fail);
                }
            }

            return(results.ToResult());
        }
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            if (IsDoublePrecision(value1.GetType()) || IsDoublePrecision(value2.GetType()))
            {
                return(CompareDoublePrecision(context, value1, value2));
            }

            return(CompareSinglePrecision(context, value1, value2));
        }
Example #20
0
 public static IComparisonContext AddDifference(this IComparisonContext context, object value1, object value2, string childProperty)
 {
     return(context.AddDifference(new BasicDifference(
                                      context.Breadcrumb,
                                      value1,
                                      value2,
                                      childProperty
                                      )));
 }
Example #21
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var dict1 = (IDictionary) value1;
            var dict2 = CastToDictionaryEntries((IDictionary) value2).ToDictionary(e => e.Key, e => e.Value);

            if (dict1.Count != dict2.Count)
            {
                context.AddDifference(dict1.Count, dict2.Count, "Count");
                return ComparisonResult.Fail;
            }

            if (dict1.Count == 0)
            {
                return ComparisonResult.Pass;
            }

            var results = new List<ComparisonResult>();

            foreach (DictionaryEntry entry in dict1)
            {
                var key = FindKey(dict2, entry.Key);

                if (key == null)
                {
                    var difference = new MissingEntryDifference(MissingSide.Expected, entry.Key, entry.Value)
                    {
                        Breadcrumb = context.Breadcrumb,
                    };

                    context.AddDifference(difference);

                    continue;
                }

                var value = dict2[key];

                var innerContext = context.VisitingIndex(key);
                results.Add(ValueComparer.Compare(innerContext, entry.Value, value));

                dict2.Remove(key);
            }

            if(dict2.Count == 0)
                return results.ToResult();

            foreach (var pair in dict2)
            {
                var difference = new MissingEntryDifference(MissingSide.Actual, pair.Key, pair.Value)
                {
                    Breadcrumb = context.Breadcrumb,
                };

                context.AddDifference(difference);
            }

            return ComparisonResult.Fail;
        }
 public static void AddDifference(this IComparisonContext context, object value1, object value2, string childProperty)
 {
     context.AddDifference(new Difference
     {
         Breadcrumb    = context.Breadcrumb,
         Value1        = value1,
         Value2        = value2,
         ChildProperty = childProperty
     });
 }
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            if (!context.ShouldVisitObjects(value1, value2))
            {
                return(ComparisonResult.Pass);
            }

            var comparer = new ComplexObjectComparer(Inner, IgnoreUnmatchedProperties, IgnoredProperties);

            return(comparer.CompareObjects(context, value1, value2));
        }
Example #24
0
 public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
 {
     try
     {
         return(((IComparable)expected).CompareTo(actual) == 0);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Example #25
0
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            var str   = (string)value1;
            var regex = (Regex)value2;

            if (regex.IsMatch(str))
            {
                return(ComparisonResult.Pass, context);
            }

            return(ComparisonResult.Fail, context.AddDifference(new RegexDifference(context.Breadcrumb, str, regex)));
        }
Example #26
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var x = (double)value1;
            var y = (double)value2;

            if (Math.Abs(x - y) < m_tolerance)
            {
                return(ComparisonResult.Pass);
            }

            return(ComparisonResult.Fail);
        }
Example #27
0
        private static void AssertExceptionMessage(
            IComparisonContext context,
            string expectedMessage,
            Dictionary <Type, IDifferenceFormatter> customFormatters = null)
        {
            expectedMessage = expectedMessage.Trim().Replace("\r\n", "\n");

            var messageBuilder = new DeepEqualExceptionMessageBuilder(context, new DifferenceFormatterFactory(customFormatters));

            var message = messageBuilder.GetMessage().Replace("\r\n", "\n");

            Assert.Equal(expectedMessage, message);
        }
        public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
        {
            const bool equal    = true;
            var        areEqual = comparisonContext.CompareProperties(expected, actual,
                                                                      (pi, actualPropertyInfo) =>
                                                                      CompareProperty(pi, actualPropertyInfo, expected, actual,
                                                                                      comparisonContext, equal));

            areEqual = comparisonContext.CompareFields(expected, actual,
                                                       (fi, actualFieldInfo) =>
                                                       CompareField(fi, actualFieldInfo, expected, actual, comparisonContext)) && areEqual;

            return(areEqual);
        }
        static bool CompareField(FieldInfo expectedFieldInfo, FieldInfo actualFieldInfo, object expected, object actual, IComparisonContext comparisonContext, bool equal)
        {
            object value1 = expectedFieldInfo.GetValue(expected);

            if (actualFieldInfo == null)
            {
                return comparisonContext
                    .AreEqual(value1, Activator.CreateInstance(typeof(MissingMember<>)
                                                                   .MakeGenericType(expectedFieldInfo.FieldType)), expectedFieldInfo.Name);
            }

            object value2 = actualFieldInfo.GetValue(actual);
            return comparisonContext.AreEqual(value1, value2, expectedFieldInfo.Name);  
        }
Example #30
0
        public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
        {
            var expectedOffset = (DateTimeOffset)expected;
            var actualOffset   = (DateTimeOffset)actual;

            if (expectedOffset.Date != actualOffset.Date)
            {
                return(false);
            }

            var expectedTimeSpan = new TimeSpan(expectedOffset.Day, expectedOffset.Hour, expectedOffset.Second, expectedOffset.Millisecond);
            var actualTimeSpan   = new TimeSpan(actualOffset.Day, actualOffset.Hour, actualOffset.Second, actualOffset.Millisecond);

            return(expectedTimeSpan == actualTimeSpan);
        }
        public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
        {
            const bool equal = true;
            bool areEqual = comparisonContext.CompareProperties(expected, actual,
                                                                (pi, actualPropertyInfo) =>
                                                                CompareProperty(pi, actualPropertyInfo, expected, actual,
                                                                                comparisonContext, equal));

            areEqual = comparisonContext.CompareFields(expected, actual,
                                                               (fi, actualFieldInfo) =>
                                                               CompareField(fi, actualFieldInfo, expected, actual,
                                                                               comparisonContext, equal)) && areEqual;

            return areEqual;
        }
Example #32
0
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            var custom1 = (Custom)value1;
            var custom2 = (Custom)value2;

            var str1 = custom1.Value.ToString();
            var str2 = custom2.Value.ToString();

            if (str1.StartsWith(str2) || str2.StartsWith(str1))
            {
                return(ComparisonResult.Pass, context);
            }

            return(ComparisonResult.Fail, context.AddDifference(value1, value2));
        }
Example #33
0
        static bool CompareStandardProperty(PropertyInfo pi1, PropertyInfo pi2, object expected, object actual,
                                            IComparisonContext comparisonContext)
        {
            object value1 = pi1.GetValue(expected, null);

            if (pi2 == null)
            {
                return(comparisonContext
                       .AreEqual(value1, Activator.CreateInstance(typeof(MissingMember <>)
                                                                  .MakeGenericType(pi1.PropertyType)), pi1.Name));
            }

            object value2 = pi2.GetValue(actual, null);

            return(comparisonContext.AreEqual(value1, value2, pi1.Name));
        }
Example #34
0
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            var set1 = ((IEnumerable)value1).Cast <object>().ToArray();
            var set2 = ((IEnumerable)value2).Cast <object>().ToArray();

            if (set1.Length != set2.Length)
            {
                return(ComparisonResult.Fail, context.AddDifference(set1.Length, set2.Length, "Count"));
            }

            if (set1.Length == 0)
            {
                return(ComparisonResult.Pass, context);
            }

            return(SetsEqual(context, set1, set2));
        }
        static bool CompareProperty(PropertyInfo expectedPropertyInfo, PropertyInfo actualPropertyInfo, object expected, object actual,
                             IComparisonContext comparisonContext, bool areEqual)
        {
            ParameterInfo[] indexes = expectedPropertyInfo.GetIndexParameters();

            if (indexes.Length == 0)
            {
                areEqual = CompareStandardProperty(expectedPropertyInfo, actualPropertyInfo, expected, actual, comparisonContext) &&
                           areEqual;
            }
            else
            {
                areEqual = CompareIndexedProperty(expectedPropertyInfo, expected, actual, indexes, comparisonContext) && areEqual;
            }

            return areEqual;
        }
Example #36
0
        public (ComparisonResult, IComparisonContext) CompareObjects(IComparisonContext context, object source, object destination)
        {
            PreparePropertyInfo(source, destination);

            var currentContext = context;

            foreach (var pair in propertyMap)
            {
                currentPair = pair;
                var sourceValue      = new Lazy <object>(() => currentPair.Source.Read(source));
                var destinationValue = new Lazy <object>(() => currentPair.Destination.Read(destination));

                if (IsPropertyIgnored())
                {
                    continue;
                }

                if (SourceAndDestinationPresent())
                {
                    var(result, innerContext) = inner.Compare(
                        context.VisitingProperty(currentPair.Name),
                        sourceValue.Value,
                        destinationValue.Value
                        );

                    results.Add(result);
                    currentContext = currentContext.MergeDifferencesFrom(innerContext);
                }
                else if (!ignoreUnmatchedProperties)
                {
                    if (currentPair.Source == null)
                    {
                        currentContext = currentContext.AddDifference("(missing)", destinationValue.Value, currentPair.Name);
                        results.Add(ComparisonResult.Fail);
                    }

                    if (currentPair.Destination == null)
                    {
                        currentContext = currentContext.AddDifference(sourceValue.Value, "(missing)", currentPair.Name);
                        results.Add(ComparisonResult.Fail);
                    }
                }
            }

            return(results.ToResult(), currentContext);
        }
Example #37
0
        static bool CompareProperty(PropertyInfo expectedPropertyInfo, PropertyInfo actualPropertyInfo, object expected, object actual,
                                    IComparisonContext comparisonContext, bool areEqual)
        {
            ParameterInfo[] indexes = expectedPropertyInfo.GetIndexParameters();

            if (indexes.Length == 0)
            {
                areEqual = CompareStandardProperty(expectedPropertyInfo, actualPropertyInfo, expected, actual, comparisonContext) &&
                           areEqual;
            }
            else
            {
                areEqual = CompareIndexedProperty(expectedPropertyInfo, expected, actual, indexes, comparisonContext) && areEqual;
            }

            return(areEqual);
        }
Example #38
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var list1 = ((IEnumerable)value1).Cast <object>().ToArray();
            var list2 = ((IEnumerable)value2).Cast <object>().ToArray();

            var results = new List <ComparisonResult>();
            int i       = 0;

            i = CompareLists(context, list1, list2, results, i);
            CompareLists(context, list2, list1, results, i);

            if (list1.Length == 0)
            {
                return(ComparisonResult.Pass);
            }

            return(results.ToResult());
        }
Example #39
0
        public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
        {
            var genericTypes = expected.GetType().GetGenericArguments();

            var getKey = GetMethodInfo("GetKey", genericTypes);
            var key1   = getKey.Invoke(this, new[] { expected });
            var key2   = getKey.Invoke(this, new[] { actual });

            var areEqual = comparisonContext.ReportEquality(key1, key2, "Key");


            var getValue = GetMethodInfo("GetValue", genericTypes);
            var value1   = getValue.Invoke(this, new[] { expected });
            var value2   = getValue.Invoke(this, new[] { actual });

            areEqual = comparisonContext.ReportEquality(value1, value2, "Value") && areEqual;

            return(areEqual);
        }
Example #40
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var set1 = ((IEnumerable)value1).Cast <object>().ToArray();
            var set2 = ((IEnumerable)value2).Cast <object>().ToArray();

            if (set1.Length != set2.Length)
            {
                context.AddDifference(set1.Length, set2.Length, "Count");
                return(ComparisonResult.Fail);
            }

            if (set1.Length == 0)
            {
                return(ComparisonResult.Pass);
            }

            var result = SetsEqual(context, set1, set2);

            return(result ? ComparisonResult.Pass : ComparisonResult.Fail);
        }
Example #41
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var set1 = ((IEnumerable) value1).Cast<object>().ToArray();
            var set2 = ((IEnumerable) value2).Cast<object>().ToArray();

            if (set1.Length != set2.Length)
            {
                context.AddDifference(set1.Length, set2.Length, "Count");
                return ComparisonResult.Fail;
            }

            if (set1.Length == 0)
            {
                return ComparisonResult.Pass;
            }

            var result = SetsEqual(context, set1, set2);

            return result ? ComparisonResult.Pass : ComparisonResult.Fail;
        }
        static bool CompareIndexedProperty(PropertyInfo pi, object expected, object actual, ParameterInfo[] indexes,
                                           IComparisonContext comparisonContext)
        {
            bool areEqual = true;

            foreach (ParameterInfo index in indexes)
            {
                if (index.ParameterType == typeof (int))
                {
                    PropertyInfo expectedCountPropertyInfo = expected.GetType().GetProperty("Count");

                    PropertyInfo actualCountPropertyInfo = actual.GetType().GetProperty("Count");

                    if (expectedCountPropertyInfo != null)
                    {
                        var expectedCount = (int) expectedCountPropertyInfo.GetValue(expected, null);
                        var actualCount = (int) actualCountPropertyInfo.GetValue(actual, null);

                        if (expectedCount != actualCount)
                        {
                            areEqual = false;
                            break;
                        }

                        for (int i = 0; i < expectedCount; i++)
                        {
                            object[] indexValues = {i};
                            object value1 = pi.GetValue(expected, indexValues);
                            object value2 = pi.GetValue(actual, indexValues);

                            if (!comparisonContext.AreEqual(value1, value2, pi.Name + "[" + i + "]"))
                            {
                                areEqual = false;
                            }
                        }
                    }
                }
            }

            return areEqual;
        }
Example #43
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var value1IsEnum = value1.GetType().IsEnum;
            var value2IsEnum = value2.GetType().IsEnum;

            if (value1IsEnum && value2IsEnum)
            {
                if (value1.ToString() == value2.ToString())
                    return ComparisonResult.Pass;

                context.AddDifference(value1, value2);
                return ComparisonResult.Fail;
            }

            var result = value1IsEnum
                             ? CompareEnumWithConversion(value1, value2)
                             : CompareEnumWithConversion(value2, value1);

            if(!result)
                context.AddDifference(value1, value2);

            return result ? ComparisonResult.Pass : ComparisonResult.Fail;
        }
 public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
 {
     return expected.Equals(actual);
 }
        static bool CompareStandardProperty(PropertyInfo pi1, PropertyInfo pi2, object expected, object actual,
                                            IComparisonContext comparisonContext)
        {
            object value1 = pi1.GetValue(expected, null);

            if (pi2 == null)
            {
                return comparisonContext
                    .AreEqual(value1, Activator.CreateInstance(typeof (MissingMember<>)
                                                                   .MakeGenericType(pi1.PropertyType)), pi1.Name);
            }

            object value2 = pi2.GetValue(actual, null);
            return comparisonContext.AreEqual(value1, value2, pi1.Name);
        }
Example #46
0
 public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
 {
     return result;
 }
Example #47
0
 public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
 {
     Console.WriteLine(actual);
     return false;
 }
 public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext)
 {
     return (((IComparable) expected).CompareTo(actual) == 0);
 }