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();
        }
Example #2
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;
        }