/// <summary> /// Returns a string representation of the array suitable for human visualisation /// </summary> /// <param name="a"></param> /// <param name="prefix"></param> /// <returns></returns> public static string AsciiArt(Array a, string prefix = "") { var sb = new StringBuilder(); for (var i = 0; i < a.Length; i++) { sb.Append(prefix + " [" + i + "] - "); //if run out of values in dictionary 1 object val = a.GetValue(i) ?? "Null"; if (DictionaryHelperMethods.IsDictionary(val)) { sb.AppendLine(string.Format("\r\n {0}", DictionaryHelperMethods.AsciiArt((IDictionary)val, prefix + "\t"))); } else if (val is Array) { sb.AppendLine(string.Format("\r\n {0}", AsciiArt((Array)val, prefix + "\t"))); } else { sb.AppendLine(val.ToString()); } } return(sb.ToString()); }
/// <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)); }
/// <summary> /// Returns a string representation of both arrays highlighting differences in array elements /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="prefix"></param> /// <returns></returns> public static string AsciiArt(Array a, Array b, string prefix = "") { var sb = new StringBuilder(); for (var i = 0; i < Math.Max(a.Length, b.Length); i++) { sb.Append(prefix + " [" + i + "] - "); //if run out of values in dictionary 1 if (i > a.Length) { sb.AppendLine(string.Format(" \t <NULL> \t {0}", b.GetValue(i))); } //if run out of values in dictionary 2 else if (i > b.Length) { sb.AppendLine(string.Format(" \t {0} \t <NULL>", a.GetValue(i))); } else { object val1 = a.GetValue(i); object val2 = b.GetValue(i); if (DictionaryHelperMethods.IsDictionary(val1) && DictionaryHelperMethods.IsDictionary(val2)) { sb.Append(string.Format("\r\n {0}", DictionaryHelperMethods.AsciiArt((IDictionary)val1, (IDictionary)val2, prefix + "\t"))); } else if (val1 is Array && val2 is Array) { sb.Append(string.Format("\r\n {0}", AsciiArt((Array)val1, (Array)val2, prefix + "\t"))); } else { //if we haven't outrun of either array sb.AppendLine(string.Format(" \t {0} \t {1} {2}", val1, val2, FlexibleEquality.FlexibleEquals(val1, val2) ? "" : "<DIFF>")); } } } return(sb.ToString()); }