Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertions on JsonElement #2559

Open
vbreuss opened this issue Jan 14, 2024 · 3 comments
Open

Add assertions on JsonElement #2559

vbreuss opened this issue Jan 14, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation

Comments

@vbreuss
Copy link
Contributor

vbreuss commented Jan 14, 2024

Background and motivation

As discussed in #2205 we want support for System.Text.Json. I would suggest to keep it as similar as possible to the existing extension package FluentAssertions.Json.

API Proposal

Add a new class JsonElementAssertions which contains assertion methods on JsonElement?).
This class can then be extended with assertions similar to JTokenAssertions in FluentAssertions.Json.

In the first step, I would keep the public surface quite small and only add the following methods:

/// <summary>
/// Asserts that the number of items in the current <see cref="JsonElement" /> matches the supplied <paramref name="expected" /> amount.
/// </summary>
public AndConstraint<TAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs);

/// <summary>
/// Asserts that the number of items in the current <see cref="JsonElement" /> does not match the supplied <paramref name="unexpected" /> amount.
/// </summary>
public AndConstraint<TAssertions> NotHaveCount(int unexpected, string because = "", params object[] becauseArgs);

/// <summary>
/// Asserts that the current <see cref="JsonElement" /> has a direct child element with the specified <paramref name="expected" /> name.
/// </summary>
public AndWhichConstraint<TAssertions, JsonElement> HaveElement(string expected, string because = "", params object[] becauseArgs)

/// <summary>
/// Asserts that the current <see cref="JsonElement" /> has no direct child element with the specified <paramref name="expected" /> name.
/// </summary>
public AndConstraint<TAssertions> NotHaveElement(string expected, string because = "", params object[] becauseArgs)

In order to use these assertions, I would add the following methods to the AssertionExtensions:

public static JsonElementAssertions Should(this JsonDocument actualValue);
public static JsonElementAssertions Should(this JsonElement? actualValue);

API Usage

var subject = JsonDocument.Parse("""[ "Hello", "World!" ]""");
subject.Should().HaveCount(2);

var subject = JsonDocument.Parse("""{ "id": 42 }""");
subject.Should().HaveElement("id");

When #2556 is also implemented, this could also be used as following:

string subject = """[ "Hello", "World!" ]""";
subject.Should().BeValidJson().Which.Should().HaveCount(2);

string subject = """{ "id": 42 }""";
subject.Should().BeValidJson().Which.Should().HaveElement("id");

Alternative Designs

No response

Risks

No response

Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?

Yes, please assign this issue to me.

@vbreuss vbreuss added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jan 14, 2024
@dennisdoomen
Copy link
Member

Great. @jnyrup ?

@IT-VBFK
Copy link
Contributor

IT-VBFK commented Jan 14, 2024

What about the negative of HaveCount?

@vbreuss
Copy link
Contributor Author

vbreuss commented Jan 14, 2024

What about the negative of HaveCount?

That's a good question (the negative is also missing in JTokenAssertions), but it would probably be a good idea to add both (to be symmetrical).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation
Projects
None yet
Development

No branches or pull requests

3 participants