Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
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 (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);
        }
Ejemplo n.º 8
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());
        }
Ejemplo n.º 9
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;
        }
Ejemplo n.º 10
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));
            }
                       ));
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
 }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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();
        }
Ejemplo n.º 15
0
 public static IComparisonContext AddDifference(this IComparisonContext context, object value1, object value2, string childProperty)
 {
     return(context.AddDifference(new BasicDifference(
                                      context.Breadcrumb,
                                      value1,
                                      value2,
                                      childProperty
                                      )));
 }
Ejemplo n.º 16
0
 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
     });
 }
Ejemplo n.º 17
0
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            var value1IsEnum = value1.GetType().IsEnum;
            var value2IsEnum = value2.GetType().IsEnum;

            if (value1IsEnum && value2IsEnum)
            {
                return(value1.ToString() == value2.ToString()
                                        ? (ComparisonResult.Pass, context)
                                        : (ComparisonResult.Fail, context.AddDifference(value1, value2)));
            }

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

            return(areEqual
                                ? (ComparisonResult.Pass, context)
                                : (ComparisonResult.Fail, context.AddDifference(value1, value2)));
        }
Ejemplo n.º 18
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)));
        }
Ejemplo n.º 19
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;
        }
Ejemplo n.º 20
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));
        }
Ejemplo n.º 21
0
        private (ComparisonResult result, IComparisonContext context) CompareDoublePrecision(IComparisonContext context, object value1, object value2)
        {
            var num1 = Convert.ToDouble(value1);
            var num2 = Convert.ToDouble(value2);

            if (num1 == num2)
            {
                return(ComparisonResult.Pass, context);
            }

            if (doubleTolerance == 0.0f)
            {
                return(ComparisonResult.Fail, context.AddDifference(value1, value2));
            }

            var epsilon = Math.Max(Math.Abs(num1), Math.Abs(num2)) * doubleTolerance;

            if (epsilon == 0.0 || Math.Abs(num1 - num2) < epsilon)
            {
                return(ComparisonResult.Pass, context);
            }

            return(ComparisonResult.Fail, context.AddDifference(value1, value2));
        }
Ejemplo n.º 22
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));
        }
Ejemplo n.º 23
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;
        }
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var type1 = value1.GetType();

            if (IsSkipped(type1))
            {
                return(ComparisonResult.Inconclusive);
            }

            if (value1.Equals(value2))
            {
                return(ComparisonResult.Pass);
            }

            if (ReflectionCache.IsValueType(type1))
            {
                context.AddDifference(value1, value2);
                return(ComparisonResult.Fail);
            }

            return(ComparisonResult.Inconclusive);
        }
Ejemplo n.º 26
0
        private (ComparisonResult result, IComparisonContext context) SetsEqual(IComparisonContext context, object[] set1, object[] set2)
        {
            var expected = set2.ToList();
            var extra    = new List <object>();

            foreach (var obj in set1)
            {
                var innerContext = new ComparisonContext();
                var found        = expected.FirstOrDefault(e => Inner.Compare(innerContext, obj, e).result == ComparisonResult.Pass);

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

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

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

            return(ComparisonResult.Pass, context);
        }
Ejemplo n.º 27
0
        public (ComparisonResult result, IComparisonContext context) Compare(IComparisonContext context, object value1, object value2)
        {
            var type1 = value1.GetType();
            var type2 = value2.GetType();

            if (IsSkipped(type1) || IsSkipped(type2))
            {
                return(ComparisonResult.Inconclusive, context);
            }

            if (type1 != type2)
            {
                if (CoerceValues(ref value1, ref value2))
                {
                    type1 = value1.GetType();
                    type2 = value2.GetType();

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

            if (value1.Equals(value2))
            {
                return(ComparisonResult.Pass, context);
            }

            if (ReflectionCache.IsValueType(type1))
            {
                return(ComparisonResult.Fail, context.AddDifference(value1, value2));
            }

            return(ComparisonResult.Inconclusive, context);
        }
Ejemplo n.º 28
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;
        }