コード例 #1
0
ファイル: ListComparison.cs プロジェクト: rytmis/DeepEqual
        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());
        }
コード例 #2
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));
            }
                       ));
        }
コード例 #3
0
ファイル: ListComparison.cs プロジェクト: naraga/DeepEqual
        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();
        }
コード例 #4
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);
 }
コード例 #5
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);
        }
コード例 #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;
        }