/// <summary> /// Asserts that the current <see cref="JToken" /> is equivalent to the <paramref name="expected" /> element, /// using its <see cref="JToken.DeepEquals(JToken, JToken)" /> implementation. /// </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) { ObjectDiffPatchResult diff = ObjectDiffPatch.GenerateDiff(Subject, expected); JToken firstDifferingToken = diff.NewValues?.First ?? diff.OldValues?.First; JTokenFormatter formatter = new JTokenFormatter(); Execute.Assertion .ForCondition(diff.AreEqual) .BecauseOf(because, becauseArgs) .FailWith($"Expected JSON document {formatter.ToString(Subject, true)}" + $" to be equivalent to {formatter.ToString(expected, true)}" + $"{{reason}}, but differs at {firstDifferingToken}."); return(new AndConstraint <JTokenAssertions>(this)); }
private static string GetNotEquivalentMessage(JToken actual, JToken expected, string reason = null, params object[] reasonArgs) { var diff = ObjectDiffPatch.GenerateDiff(actual, expected); var key = diff.NewValues?.First ?? diff.OldValues?.First; var because = string.Empty; if (!string.IsNullOrWhiteSpace(reason)) { because = " because " + string.Format(reason, reasonArgs); } var expectedMessage = $"Expected JSON document {_formatter.ToString(actual)}" + $" to be equivalent to {_formatter.ToString(expected)}" + $"{because}, but differs at {_formatter.ToString(key)}."; return(expectedMessage); }
/// <summary> /// Asserts that the number of items in the current <see cref="JToken" /> matches the supplied <paramref name="expected" /> amount. /// </summary> /// <param name="expected">The expected number of items in the current <see cref="JToken" />.</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 cref="because" />. /// </param> public AndConstraint <JTokenAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs) { var formatter = new JTokenFormatter(); string formattedDocument = formatter.ToString(Subject).Replace("{", "{{").Replace("}", "}}"); using (new AssertionScope("JSON document " + formattedDocument)) { EnumerableSubject.HaveCount(expected, because, becauseArgs); return(new AndConstraint <JTokenAssertions>(this)); } }
/// <summary> /// Expects the current <see cref="JToken" /> to contain only a single item. /// </summary> /// <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 cref="because" />. /// </param> public AndWhichConstraint <JTokenAssertions, JToken> ContainSingleItem(string because = "", params object[] becauseArgs) { var formatter = new JTokenFormatter(); string formattedDocument = formatter.ToString(Subject).Replace("{", "{{").Replace("}", "}}"); using (new AssertionScope("JSON document " + formattedDocument)) { var constraint = EnumerableSubject.ContainSingle(because, becauseArgs); return(new AndWhichConstraint <JTokenAssertions, JToken>(this, constraint.Which)); } }
public void Should_preserve_indenting() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var json = JToken.Parse("{ \"id\":1 }"); var sut = new JTokenFormatter(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var actual = sut.ToString(json, true); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actual.Should().Be(json.ToString(Newtonsoft.Json.Formatting.Indented)); }
public void Should_Remove_line_breaks_and_indenting() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var json = JToken.Parse("{ \"id\":1 }"); var sut = new JTokenFormatter(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- // ReSharper disable once RedundantArgumentDefaultValue var actual = sut.ToString(json, false); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actual.Should().Be(json.ToString().RemoveNewLines()); }
public void When_values_differ_Be_should_fail() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var testCases = new Dictionary <string, string> { { "{ id: 1 }", "{ id: 2 }" }, { "{ id: 1, admin: true }", "{ id: 1, admin: false }" } }; foreach (var testCase in testCases) { var actualJson = testCase.Key; var expectedJson = testCase.Value; var a = JToken.Parse(actualJson); var b = JToken.Parse(expectedJson); var expectedMessage = $"Expected JSON document to be {_formatter.ToString(b)}, but found {_formatter.ToString(a)}."; //----------------------------------------------------------------------------------------------------------- // Act & Assert //----------------------------------------------------------------------------------------------------------- a.Should().Invoking(x => x.Be(b)) .ShouldThrow <AssertFailedException>() .WithMessage(expectedMessage); } }