コード例 #1
0
        /// <summary>
        /// Returns true if <paramref name="a"/> and <paramref name="b"/> are Equal or are collections (<see cref="Array"/> / <see cref="IDictionary"/>) with equal
        /// elements (includes recursion support for nested collections e.g. arrays of arrays).
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool FlexibleEquals(object a, object b)
        {
            if (a == null || b == null)
            {
                return(a == b);
            }

            //types are different so most likely we are not equipped to deal with this problem let a decide if it is equal or not
            if (a.GetType() != b.GetType())
            {
                return(a.Equals(b));
            }

            //if they are both dictionaries
            if (DictionaryHelperMethods.IsDictionary(a) && DictionaryHelperMethods.IsDictionary(b))
            {
                //and they are dictionary equals
                return(DictionaryHelperMethods.DictionaryEquals((IDictionary)a, (IDictionary)b));
            }

            //if they are both arrays
            if (a is Array)
            {
                return(ArrayHelperMethods.ArrayEquals((Array)a, (Array)b));
            }

            //they are not dictionaries or arrays
            return(Equals(a, b));
        }
コード例 #2
0
        /// <summary>
        /// Returns a string representation of both <see cref="IDictionary"/>s highlighting differences in array elements
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="dict2"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static string AsciiArt(IDictionary dict, IDictionary dict2, string prefix = "")
        {
            var sb = new StringBuilder();

            List <object> keys1 = dict.Keys.Cast <object>().OrderBy(i => i).ToList();
            List <object> keys2 = dict2.Keys.Cast <object>().OrderBy(i => i).ToList();

            for (var i = 0; i < Math.Max(keys1.Count, keys2.Count); i++)
            {
                sb.Append(prefix + "[" + i + "] - ");

                //if run out of values in dictionary 1
                if (i > keys1.Count)
                {
                    sb.AppendLine(string.Format(" {0} - \t <NULL> \t {1}", keys2[i], dict2[keys2[i]]));
                }
                //if run out of values in dictionary 2
                else if (i > keys2.Count)
                {
                    sb.AppendLine(string.Format(" {0} - \t {1} \t <NULL>", keys1[i], dict[keys1[i]]));
                }
                else
                {
                    object val1 = dict[keys1[i]];
                    object val2 = dict2[keys2[i]];

                    if (val1 is Array && val2 is Array)
                    {
                        sb.Append(string.Format(" {0} : \r\n {1}",
                                                keys1[i],
                                                ArrayHelperMethods.AsciiArt((Array)val1,
                                                                            (Array)val2, prefix + "\t")));
                    }
                    else
                    //if both are dictionaries
                    if (IsDictionary(val1) && IsDictionary(val2))
                    {
                        sb.Append(string.Format(" {0} : \r\n {1}",
                                                keys1[i],
                                                AsciiArt((IDictionary)val1,
                                                         (IDictionary)val2, prefix + "\t")));
                    }
                    else
                    {
                        //if we haven't outrun of either array
                        sb.AppendLine(string.Format(" {0} - \t {1} \t {2} {3}",
                                                    keys1[i],
                                                    dict[keys1[i]],
                                                    dict2[keys2[i]],
                                                    FlexibleEquality.FlexibleEquals(dict[keys1[i]], dict2[keys2[i]]) ? "" : "<DIFF>"));
                    }
                }
            }

            return(sb.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Returns a string representation of the <paramref name="dict"/> suitable for human visualisation
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static string AsciiArt(IDictionary dict, string prefix = "")
        {
            var sb = new StringBuilder();

            List <object> keys1 = dict.Keys.Cast <object>().OrderBy(i => i).ToList();

            for (var i = 0; i < keys1.Count; i++)
            {
                sb.Append(prefix);

                //if run out of values in dictionary 1
                object val = dict[keys1[i]];

                if (val is Array)
                {
                    sb.Append(string.Format(" {0} : \r\n {1}",
                                            keys1[i],
                                            ArrayHelperMethods.AsciiArt((Array)val, prefix + "\t")));
                }
                else
                //if both are dictionaries
                if (IsDictionary(val))
                {
                    sb.Append(string.Format(" {0} : \r\n {1}",
                                            keys1[i],
                                            AsciiArt((IDictionary)val, prefix + "\t")));
                }
                else
                {
                    //if we haven't outrun of either array
                    sb.AppendLine(string.Format(" {0} - \t {1}",
                                                keys1[i],
                                                val));
                }
            }

            return(sb.ToString());
        }