Пример #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);
        }
        public void Sort(string testCase)
        {
            var expected = File.ReadAllText($"Sort\\expected{testCase}.json");
            var input    = File.ReadAllText($"Sort\\input{testCase}.json");
            var actual   = JsonSorter.Sort(input);

            _output.WriteLine(actual);

            var expectedJToken = JToken.Parse(expected);
            var actualJToken   = JToken.Parse(actual);

            Assert.Equal(expectedJToken.ToString(), actualJToken.ToString());
        }
Пример #3
0
        /// <summary>
        /// Determines if two objects have the same exact values for
        /// all of their corresponding properties.  Note: this is a
        /// deep comparison.
        /// </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="ignoreArrayElementOrder">Whether to ignore the order of array elements</param>
        /// <returns>true, if equal; false, otherwise</returns>
        /// <seealso cref="IsEqual{T}(object, T, string[])"/>
        public static bool IsEqual <T>(this object obj1, T obj2, bool ignoreArrayElementOrder = false)
        {
            string json1 = JsonConvert.SerializeObject(obj1,
                                                       Formatting.Indented, new SafeJsonSerializerSettings());
            string json2 = JsonConvert.SerializeObject(obj2,
                                                       Formatting.Indented, new SafeJsonSerializerSettings());

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

            return(json1 == json2);
        }
Пример #4
0
        /// <summary>
        /// Determines if two objects have the same exact values for
        /// all of their corresponding properties, excluding the properties
        /// associated with pathsToIgnore.
        /// </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="maxDepth">The maximum depth of the object graph to serialize (1=flat)</param>
        /// <param name="propertiesToIgnore">a string array of
        /// <param name="ignoreArrayElementOrder">Whether to ignore the order of array elements</param>
        /// property names that will be ignored.</param>
        /// <returns>true, if equal; false, otherwise</returns>
        /// <seealso cref="IsEqual{T}(object, T)"/>
        /// <see href="https://github.com/json-path/JsonPath"/>
        public static bool IsEqual <T>(this object obj1, T obj2,
                                       int maxDepth, string[] propertiesToIgnore, bool ignoreArrayElementOrder = false)
        {
            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));

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

            return(json1 == json2);
        }