From e42a673261d0cdf5cea729279f57648c1545d421 Mon Sep 17 00:00:00 2001 From: Sam Segers Date: Sun, 4 May 2025 10:52:47 +0200 Subject: [PATCH 1/4] add ContainSubtree that takes a config --- Src/FluentAssertions.Json/JTokenAssertions.cs | 37 ++++++++++++++++++- .../JTokenAssertionsSpecs.cs | 29 +++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/Src/FluentAssertions.Json/JTokenAssertions.cs b/Src/FluentAssertions.Json/JTokenAssertions.cs index 19f82151..2cc65e07 100644 --- a/Src/FluentAssertions.Json/JTokenAssertions.cs +++ b/Src/FluentAssertions.Json/JTokenAssertions.cs @@ -27,7 +27,7 @@ static JTokenAssertions() /// Initializes a new instance of the class. /// /// The subject - /// + /// The assertion chain public JTokenAssertions(JToken subject, AssertionChain assertionChain) : base(subject, assertionChain) { @@ -496,6 +496,41 @@ public AndConstraint ContainSubtree(JToken subtree, string bec return BeEquivalentTo(subtree, true, options => options, because, becauseArgs); } + /// + /// Recursively asserts that the current contains at least the properties or elements of the specified . + /// + /// The subtree to search for + /// The options to consider while asserting values + /// + /// A formatted phrase as is supported by explaining why the assertion + /// is needed. If the phrase does not start with the word because, it is prepended automatically. + /// + /// + /// Zero or more objects to format using the placeholders in . + /// + /// Use this method to match the current against an arbitrary subtree, + /// permitting it to contain any additional properties or elements. This way we can test multiple properties on a at once, + /// or test if a contains any items that match a set of properties, assert that a JSON document has a given shape, etc. + /// + /// This example asserts the values of multiple properties of a child object within a JSON document. + /// + /// var json = JToken.Parse("{ success: true, data: { id: 123, type: 'my-type', name: 'Noone' } }"); + /// json.Should().ContainSubtree(JToken.Parse("{ success: true, data: { type: 'my-type', name: 'Noone' } }")); + /// + /// + /// This example asserts that a within a has at least one element with at least the given properties + /// + /// var json = JToken.Parse("{ id: 1, items: [ { id: 2, type: 'my-type', name: 'Alpha' }, { id: 3, type: 'other-type', name: 'Bravo' } ] }"); + /// json.Should().ContainSubtree(JToken.Parse("{ items: [ { type: 'my-type', name: 'Alpha' } ] }")); + /// + public AndConstraint ContainSubtree(JToken subtree, + Func, IJsonAssertionOptions> config, + string because = "", + params object[] becauseArgs) + { + return BeEquivalentTo(subtree, true, config, because, becauseArgs); + } + #pragma warning disable CA1822 // Making this method static is a breaking chan public string Format(JToken value, bool useLineBreaks = false) { diff --git a/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs b/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs index 6193dcd9..eae18b28 100644 --- a/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs +++ b/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs @@ -966,6 +966,35 @@ public void When_checking_subtree_with_an_invalid_expected_string_it_should_prov .WithInnerException(); } + [Fact] + public void When_a_float_is_within_approximation_ContainSubtree_check_should_succeed() + { + // Arrange + var actual = JToken.Parse("{ \"id\": 1.1232 }"); + var expected = JToken.Parse("{ \"id\": 1.1235 }"); + + // Act & Assert + actual.Should().ContainSubtree(expected, options => options + .Using(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-3)) + .WhenTypeIs()); + } + + [Fact] + public void When_a_float_is_not_within_approximation_ContainSubtree_check_should_throw() + { + // Arrange + var actual = JToken.Parse("{ \"id\": 1.1232 }"); + var expected = JToken.Parse("{ \"id\": 1.1235 }"); + + // Act & Assert + actual.Should(). + Invoking(x => x.ContainSubtree(expected, options => options + .Using(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-5)) + .WhenTypeIs())) + .Should().Throw() + .WithMessage("JSON document has a different value at $.id.*"); + } + #endregion private static string Format(JToken value, bool useLineBreaks = false) From c81e33f63f8117e61ca6fa79c2b48204d6f93034 Mon Sep 17 00:00:00 2001 From: Sam Segers Date: Tue, 30 Sep 2025 08:27:36 +0200 Subject: [PATCH 2/4] Fix example documentation in JTokenAssertions --- Src/FluentAssertions.Json/JTokenAssertions.cs | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/Src/FluentAssertions.Json/JTokenAssertions.cs b/Src/FluentAssertions.Json/JTokenAssertions.cs index 2cc65e07..0331a5d2 100644 --- a/Src/FluentAssertions.Json/JTokenAssertions.cs +++ b/Src/FluentAssertions.Json/JTokenAssertions.cs @@ -437,15 +437,17 @@ public AndConstraint HaveCount(int expected, string because = /// /// This example asserts the values of multiple properties of a child object within a JSON document. /// - /// var json = JToken.Parse("{ success: true, data: { id: 123, type: 'my-type', name: 'Noone' } }"); - /// json.Should().ContainSubtree(JToken.Parse("{ success: true, data: { type: 'my-type', name: 'Noone' } }")); + /// var json = JToken.Parse("{ success: true, data: { id: 123, type: 'my-type', name: 'None' } }"); + /// json.Should().ContainSubtree(JToken.Parse("{ success: true, data: { type: 'my-type', name: 'None' } }")); /// /// - /// This example asserts that a within a has at least one element with at least the given properties + /// + /// This example asserts that a within a has at least one element with at least the given properties /// /// var json = JToken.Parse("{ id: 1, items: [ { id: 2, type: 'my-type', name: 'Alpha' }, { id: 3, type: 'other-type', name: 'Bravo' } ] }"); /// json.Should().ContainSubtree(JToken.Parse("{ items: [ { type: 'my-type', name: 'Alpha' } ] }")); /// + /// public AndConstraint ContainSubtree(string subtree, string because = "", params object[] becauseArgs) { JToken subtreeToken; @@ -482,15 +484,17 @@ public AndConstraint ContainSubtree(string subtree, string bec /// /// This example asserts the values of multiple properties of a child object within a JSON document. /// - /// var json = JToken.Parse("{ success: true, data: { id: 123, type: 'my-type', name: 'Noone' } }"); - /// json.Should().ContainSubtree(JToken.Parse("{ success: true, data: { type: 'my-type', name: 'Noone' } }")); + /// var json = JToken.Parse("{ success: true, data: { id: 123, type: 'my-type', name: 'None' } }"); + /// json.Should().ContainSubtree(JToken.Parse("{ success: true, data: { type: 'my-type', name: 'None' } }")); /// /// - /// This example asserts that a within a has at least one element with at least the given properties + /// + /// This example asserts that a within a has at least one element with at least the given properties /// /// var json = JToken.Parse("{ id: 1, items: [ { id: 2, type: 'my-type', name: 'Alpha' }, { id: 3, type: 'other-type', name: 'Bravo' } ] }"); /// json.Should().ContainSubtree(JToken.Parse("{ items: [ { type: 'my-type', name: 'Alpha' } ] }")); /// + /// public AndConstraint ContainSubtree(JToken subtree, string because = "", params object[] becauseArgs) { return BeEquivalentTo(subtree, true, options => options, because, becauseArgs); @@ -511,18 +515,6 @@ public AndConstraint ContainSubtree(JToken subtree, string bec /// Use this method to match the current against an arbitrary subtree, /// permitting it to contain any additional properties or elements. This way we can test multiple properties on a at once, /// or test if a contains any items that match a set of properties, assert that a JSON document has a given shape, etc. - /// - /// This example asserts the values of multiple properties of a child object within a JSON document. - /// - /// var json = JToken.Parse("{ success: true, data: { id: 123, type: 'my-type', name: 'Noone' } }"); - /// json.Should().ContainSubtree(JToken.Parse("{ success: true, data: { type: 'my-type', name: 'Noone' } }")); - /// - /// - /// This example asserts that a within a has at least one element with at least the given properties - /// - /// var json = JToken.Parse("{ id: 1, items: [ { id: 2, type: 'my-type', name: 'Alpha' }, { id: 3, type: 'other-type', name: 'Bravo' } ] }"); - /// json.Should().ContainSubtree(JToken.Parse("{ items: [ { type: 'my-type', name: 'Alpha' } ] }")); - /// public AndConstraint ContainSubtree(JToken subtree, Func, IJsonAssertionOptions> config, string because = "", From 94f17ebd8dc558a4291acced106797d2044cdbd9 Mon Sep 17 00:00:00 2001 From: Sam Segers Date: Tue, 30 Sep 2025 08:49:13 +0200 Subject: [PATCH 3/4] Update tests to match new guidelines --- .../JTokenAssertionsSpecs.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs b/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs index eae18b28..4af04203 100644 --- a/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs +++ b/Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs @@ -967,7 +967,7 @@ public void When_checking_subtree_with_an_invalid_expected_string_it_should_prov } [Fact] - public void When_a_float_is_within_approximation_ContainSubtree_check_should_succeed() + public void Assert_property_with_approximation_succeeds() { // Arrange var actual = JToken.Parse("{ \"id\": 1.1232 }"); @@ -980,18 +980,19 @@ public void When_a_float_is_within_approximation_ContainSubtree_check_should_suc } [Fact] - public void When_a_float_is_not_within_approximation_ContainSubtree_check_should_throw() + public void Can_assert_on_a_field_with_approximation() { // Arrange var actual = JToken.Parse("{ \"id\": 1.1232 }"); var expected = JToken.Parse("{ \"id\": 1.1235 }"); - // Act & Assert - actual.Should(). - Invoking(x => x.ContainSubtree(expected, options => options - .Using(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-5)) - .WhenTypeIs())) - .Should().Throw() + // Act + Action act = () => actual.Should().ContainSubtree(expected, options => options + .Using(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-5)) + .WhenTypeIs()); + + // Assert + act.Should().Throw() .WithMessage("JSON document has a different value at $.id.*"); } From 061f6fd671db372dd67aef41ffd149c95353e0e8 Mon Sep 17 00:00:00 2001 From: Sam Segers Date: Tue, 21 Oct 2025 08:36:08 +0200 Subject: [PATCH 4/4] Update ApprovedApi --- .../ApprovedApi/FluentAssertions.Json/net47.verified.txt | 1 + .../FluentAssertions.Json/netstandard2.0.verified.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/net47.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/net47.verified.txt index 7b659d2f..f6952c42 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/net47.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/net47.verified.txt @@ -21,6 +21,7 @@ namespace FluentAssertions.Json public FluentAssertions.AndWhichConstraint ContainSingleItem(string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint ContainSubtree(Newtonsoft.Json.Linq.JToken subtree, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint ContainSubtree(string subtree, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint ContainSubtree(Newtonsoft.Json.Linq.JToken subtree, System.Func, FluentAssertions.Json.IJsonAssertionOptions> config, string because = "", params object[] becauseArgs) { } public string Format(Newtonsoft.Json.Linq.JToken value, bool useLineBreaks = false) { } public FluentAssertions.AndConstraint HaveCount(int expected, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndWhichConstraint HaveElement(string expected) { } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/netstandard2.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/netstandard2.0.verified.txt index 7b659d2f..f6952c42 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/netstandard2.0.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions.Json/netstandard2.0.verified.txt @@ -21,6 +21,7 @@ namespace FluentAssertions.Json public FluentAssertions.AndWhichConstraint ContainSingleItem(string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint ContainSubtree(Newtonsoft.Json.Linq.JToken subtree, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint ContainSubtree(string subtree, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint ContainSubtree(Newtonsoft.Json.Linq.JToken subtree, System.Func, FluentAssertions.Json.IJsonAssertionOptions> config, string because = "", params object[] becauseArgs) { } public string Format(Newtonsoft.Json.Linq.JToken value, bool useLineBreaks = false) { } public FluentAssertions.AndConstraint HaveCount(int expected, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndWhichConstraint HaveElement(string expected) { }