Ejemplo n.º 1
0
        /// <summary>
        /// Determines if two objects have the same exact values for
        /// all of their corresponding properties, excluding the properties
        /// associated with pathsToIgnore.  This method will print side-by-side
        /// comparisons if the two objects are not equal.
        /// NOTE: Requires Xunit
        /// NOTE: The current object should be the "actual" object
        /// </summary>
        /// <typeparam name="T">The type of the current object</typeparam>
        /// <param name="obj1">The current object</param>
        /// <param name="obj2">The object to compare</param>
        /// <param name="propertiesToIgnore">a string array of
        /// property names that will be ignored.</param>
        /// <param name="output">object used by Xunit to print to console</param>
        /// <param name="ignoreArrayElementOrder">Whether to ignore the order of array elements</param>
        /// <returns>true, if equal; false, otherwise</returns>
        /// <seealso cref="IsEqualOrWrite{T}(object, T)"/>
        /// <see href="https://github.com/json-path/JsonPath"/>
        public static bool IsEqualOrWrite <T>(this object obj1, T obj2,
                                              string[] propertiesToIgnore, ITestOutputHelper output, bool ignoreArrayElementOrder = false)
        {
            string json1 = JsonConvert.SerializeObject(obj1,
                                                       Formatting.Indented, new SafeJsonSerializerSettings(
                                                           DEFAULT_MAXDEPTH, propertiesToIgnore));
            string json2 = JsonConvert.SerializeObject(obj2,
                                                       Formatting.Indented, new SafeJsonSerializerSettings(
                                                           DEFAULT_MAXDEPTH, propertiesToIgnore));

            if (ignoreArrayElementOrder)
            {
                json1 = JsonSorter.Sort(json1);
                json2 = JsonSorter.Sort(json2);
            }

            var isEqual = (json1 == json2);

            if (!isEqual)
            {
                output.WriteLine(FileStringComparer.GetSideBySideFileStrings(json2, json1, "EXPECTED", "ACTUAL"));
            }

            return(isEqual);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates a side-by-side comparison of two objects
        /// as JSON strings.  This version uses a default value
        /// for maximum depth (99) and no property filters
        /// </summary>
        /// <typeparam name="T">The type of the current object</typeparam>
        /// <param name="obj1">The current object</param>
        /// <param name="obj2">The object to compare</param>
        /// <param name="label1">Label for the current object</param>
        /// <param name="label2">Label for the object to compare</param>
        /// <returns>side-by-side rendering of objects</returns>
        /// <seealso cref="Juxtapose{T}(object, T, string[])"/>
        public static string Juxtapose <T>(this object obj1, T obj2, string label1, string label2)
        {
            string json1 = JsonConvert.SerializeObject(obj1,
                                                       Formatting.Indented, new SafeJsonSerializerSettings());
            string json2 = JsonConvert.SerializeObject(obj2,
                                                       Formatting.Indented, new SafeJsonSerializerSettings());

            return(FileStringComparer.GetSideBySideFileStrings(json1, json2, label1, label2));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates a side-by-side comparison of two objects
        /// as JSON strings.  This version allows specification of
        /// maximum depth and property filters
        /// </summary>
        /// <typeparam name="T">The type of the current object</typeparam>
        /// <param name="obj1">The current object</param>
        /// <param name="obj2">The object to compare</param>
        /// <param name="label1">Label for the current object</param>
        /// <param name="label2">Label for the object to compare</param>
        /// <param name="maxDepth">The maximum depth of the object graph to serialize (1=flat)</param>
        /// <param name="propertiesToIgnore">a string array of
        /// property names that will be ignored.</param>
        /// <returns>side-by-side rendering of objects</returns>
        /// <seealso cref="Juxtapose{T}(object, T)"/>
        /// <see href="https://github.com/json-path/JsonPath"/>
        public static string Juxtapose <T>(this object obj1, T obj2, string label1, string label2, int maxDepth, string[] propertiesToIgnore)
        {
            CheckDepth(obj1, maxDepth);

            string json1 = JsonConvert.SerializeObject(obj1,
                                                       Formatting.Indented, new SafeJsonSerializerSettings(
                                                           maxDepth, propertiesToIgnore));
            string json2 = JsonConvert.SerializeObject(obj2,
                                                       Formatting.Indented, new SafeJsonSerializerSettings(
                                                           maxDepth, propertiesToIgnore));

            return(FileStringComparer.GetSideBySideFileStrings(json1, json2, label1, label2));
        }
Ejemplo n.º 4
0
        public void TestComplexObject()
        {
            var personA = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[0]));
            var personB = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[0]));
            var personC = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[1]));
            var personD = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[1]));
            var personE = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[0]));
            var personF = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[1]));
            var co      = new ComplexObject()
            {
                PersonDict = new Dictionary <string, Person> {
                    { "Moe", personA },
                    { "Larry", personC }
                },
                Persons     = new Person[] { persons[0], persons[1] },
                PersonDicts = new List <Dictionary <string, Person> >()
                {
                    new Dictionary <string, Person> {
                        { "Moe", personB },
                        { "Larry", personD }
                    }
                },
                PersonDictOrdered = (new Dictionary <string, Person> {
                    { "Moe", personE },
                    { "Larry", personF }
                }).OrderBy(x => x.Key)
            };

            var actual   = SafeJsonSerializer.Serialize(co);
            var expected = JsonSerializer.Serialize(co,
                                                    new JsonSerializerOptions {
                WriteIndented = true, IgnoreNullValues = true
            });

            if (actual != expected)
            {
                var fsc = FileStringComparer.GetSideBySideFileStrings(expected, actual, "EXPECTED", "ACTUAL");
                _output.WriteLine(fsc);
            }


            Assert.Equal(expected, actual);
        }
Ejemplo n.º 5
0
        public void TestMaxDepth(int testCase)
        {
            var actual   = SafeJsonSerializer.Serialize(aRecs, testCase);
            var expected = JsonSerializer.Serialize(
                JsonSerializer.Deserialize <A[]>
                    (File.ReadAllText($"StaticSerializeTests/TestMaxDepth/{testCase}.json")),
                new JsonSerializerOptions {
                WriteIndented = true, IgnoreNullValues = true
            });

            if (actual != expected)
            {
                var fsc = FileStringComparer.GetSideBySideFileStrings(expected, actual, "EXPECTED", "ACTUAL");
                _output.WriteLine(fsc);
            }


            Assert.Equal(expected, actual);
        }
Ejemplo n.º 6
0
        public void TestPropertiesToIgnore(int testCase)
        {
            var actual   = SafeJsonSerializer.Serialize(persons, 99, true, propertiesToIgnore[testCase]);
            var expected = JsonSerializer.Serialize(
                JsonSerializer.Deserialize <List <Person> >
                    (File.ReadAllText($"StaticSerializeTests/TestPropertiesToIgnore/{testCase}.json")),
                new JsonSerializerOptions {
                WriteIndented = true, IgnoreNullValues = true
            });

            if (actual != expected)
            {
                var fsc = FileStringComparer.GetSideBySideFileStrings(expected, actual, "EXPECTED", "ACTUAL");
                _output.WriteLine(fsc);
            }


            Assert.Equal(expected, actual);
        }