/// <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)); } }
/// <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)); }
public void Should_Handle_JToken() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var sut = new JTokenFormatter(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var actual = sut.CanHandle(JToken.Parse("{}")); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actual.Should().BeTrue(); }
/// <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); JTokenFormatter formatter = new JTokenFormatter(); var message = $"Expected JSON document {Format(Subject, true).Replace("{", "{{").Replace("}", "}}")}" + $" to be equivalent to {Format(expected, true).Replace("{", "{{").Replace("}", "}}")}" + $"{{reason}}, but {difference}."; Execute.Assertion .ForCondition(difference == null) .BecauseOf(because, becauseArgs) .FailWith(message); return(new AndConstraint <JTokenAssertions>(this)); }
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 Should_preserve_indenting() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var json = JToken.Parse("{ \"id\":1 }"); var sut = new JTokenFormatter(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var actual = sut.Format(json, new FormattingContext { UseLineBreaks = true }, (path, value) => ""); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actual.Should().Be(json.ToString(Newtonsoft.Json.Formatting.Indented)); }
public void Should_not_handle_anything_else() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var testCases = new object[] { null, string.Empty }; var sut = new JTokenFormatter(); foreach (var testCase in testCases) { //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var actual = sut.CanHandle(testCase); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actual.Should().BeFalse(); } }