예제 #1
0
        /// <summary>
        ///     Asserts that the current <see cref="JToken" /> is equivalent to the <paramref name="expected" /> element,
        ///     using an equivalent of <see cref="JToken.DeepEquals(JToken, JToken)" />.
        /// </summary>
        /// <param name="expected">The expected element</param>
        /// <param name="because">
        ///     A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        ///     is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        ///     Zero or more objects to format using the placeholders in <see paramref="because" />.
        /// </param>
        public AndConstraint <JTokenAssertions> BeEquivalentTo(JToken expected, string because = "",
                                                               params object[] becauseArgs)
        {
            Difference difference = JTokenDifferentiator.FindFirstDifference(Subject, expected);

            var message = $"JSON document {difference?.ToString().Escape(true)}.{Environment.NewLine}" +
                          $"Expected{Environment.NewLine}" +
                          $"{Format(Subject, true).Escape(true)}{Environment.NewLine}" +
                          $"to be equivalent to{Environment.NewLine}" +
                          $"{Format(expected, true).Escape(true)}{Environment.NewLine}" +
                          "{reason}.";

            Execute.Assertion
            .ForCondition(difference == null)
            .BecauseOf(because, becauseArgs)
            .FailWith(message);

            return(new AndConstraint <JTokenAssertions>(this));
        }
        private AndConstraint <JTokenAssertions> BeEquivalentTo(JToken expected, bool ignoreExtraProperties, string because = "",
                                                                params object[] becauseArgs)
        {
            Difference difference = JTokenDifferentiator.FindFirstDifference(Subject, expected, ignoreExtraProperties);

            var expectation = ignoreExtraProperties ? "was expected to contain" : "was expected to be equivalent to";

            var message = $"JSON document {difference?.ToString().EscapePlaceholders()}.{Environment.NewLine}" +
                          $"Actual document{Environment.NewLine}" +
                          $"{Format(Subject, true).EscapePlaceholders()}{Environment.NewLine}" +
                          $"{expectation}{Environment.NewLine}" +
                          $"{Format(expected, true).EscapePlaceholders()}{Environment.NewLine}" +
                          "{reason}.";

            Execute.Assertion
            .ForCondition(difference == null)
            .BecauseOf(because, becauseArgs)
            .FailWith(message);

            return(new AndConstraint <JTokenAssertions>(this));
        }