示例#1
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);
        }
示例#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;
        }