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 #2560

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

vbreuss
Copy link
Contributor

@vbreuss vbreuss commented Jan 14, 2024

Draft for #2559

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);

IMPORTANT

  • If the PR touches the public API, the changes have been approved in a separate issue with the "api-approved" label.
  • The code complies with the Coding Guidelines for C#.
  • The changes are covered by unit tests which follow the Arrange-Act-Assert syntax and the naming conventions such as is used in these tests.
  • If the PR adds a feature or fixes a bug, please update the release notes with a functional description that explains what the change means to consumers of this library, which are published on the website.
  • If the PR changes the public API the changes needs to be included by running AcceptApiChanges.ps1 or AcceptApiChanges.sh.
  • If the PR affects the documentation, please include your changes in this pull request so the documentation will appear on the website.
    • Please also run ./build.sh --target spellcheck or .\build.ps1 --target spellcheck before pushing and check the good outcome

Copy link

github-actions bot commented Jan 14, 2024

Qodana for .NET

44 new problems were found

Inspection name Severity Problems
Auto-property accessor is never used (private accessibility) 🔶 Warning 19
Non-accessed field (private accessibility) 🔶 Warning 13
Possible 'System.NullReferenceException' 🔶 Warning 4
Redundant type declaration body 🔶 Warning 3
Disposal of a variable already captured by the 'using' statement 🔶 Warning 2
Member hides static member from outer class 🔶 Warning 1
Return value of a method annotated with [MustDisposeResource] is never disposed 🔶 Warning 1
Possible 'System.InvalidOperationException' 🔶 Warning 1

💡 Qodana analysis was run in the pull request mode: only the changed files were checked
☁️ View the detailed Qodana report

Contact Qodana team

Contact us at qodana-support@jetbrains.com

@coveralls
Copy link

coveralls commented Jan 14, 2024

Pull Request Test Coverage Report for Build 7520186901

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 97.459%

Totals Coverage Status
Change from base Build 7517844575: -0.02%
Covered Lines: 11807
Relevant Lines: 11991

💛 - Coveralls

@vbreuss vbreuss force-pushed the topic/json-support/json-element-assertions branch 4 times, most recently from 679f3e7 to 78aea9c Compare January 14, 2024 15:48
@vbreuss vbreuss force-pushed the topic/json-support/json-element-assertions branch from 78aea9c to c38f29d Compare January 15, 2024 17:35
@vbreuss vbreuss force-pushed the topic/json-support/json-element-assertions branch from c38f29d to 0faf641 Compare January 15, 2024 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants