Skip to content

Commit

Permalink
Implemented: HaveValueKind.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Aug 17, 2023
1 parent 56e72c7 commit 2d736bd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
29 changes: 28 additions & 1 deletion Src/FluentAssertions/Json/JsonElementAssertions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
using System.Text.Json;
using System.Diagnostics;
using System.Text.Json;
using FluentAssertions.Execution;

namespace FluentAssertions.Json;

/// <summary>
/// Contains a number of methods to assert that an <see cref="JsonElement" /> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class JsonElementAssertions
{
public JsonElement Subject { get; }

/// <summary>
/// Initializes a new instance of the <see cref="JsonElementAssertions" /> class.
/// </summary>
/// <param name="subject">The subject.</param>
public JsonElementAssertions(JsonElement subject)
{
Subject = subject;
}

/// <summary>
/// Asserts that the current <see cref="JsonElement"/> has the specified <see cref="JsonValueKind"/>.
/// </summary>
/// <param name="valueKind">The JSON string.</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<JsonElementAssertions> HaveValueKind(JsonValueKind valueKind, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.ValueKind == valueKind)
.FailWith("Expected {context:JSON} to have value kind {0}{reason}, but found {1}.", valueKind, Subject.ValueKind);

return new(this);
}

public void Be(long jsonNumber)
{
Execute.Assertion
Expand Down
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Json/JsonSerializerOptionsAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace FluentAssertions.Json;
/// <summary>
/// Contains a number of methods to assert that an <see cref="JsonSerializerOptions" /> is in the expected state.
/// </summary>
// [DebuggerNonUserCode]
[DebuggerNonUserCode]
public class JsonSerializerOptionsAssertions : ReferenceTypeAssertions<JsonSerializerOptions, JsonSerializerOptionsAssertions>
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializerOptionsAssertions" /> class.
/// </summary>
/// <param name="subject">The subject</param>
/// <param name="subject">The subject.</param>
public JsonSerializerOptionsAssertions(JsonSerializerOptions subject)
: base(subject)
{
Expand Down
37 changes: 37 additions & 0 deletions Tests/FluentAssertions.Specs/Json/JsonElementAssertionsSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Text.Json;
using Xunit;
using Xunit.Sdk;

namespace FluentAssertions.Specs.Json;

public class JsonElementAssertionsSpecs
{
public class HaveValueKind
{
[Fact]
public void Throws_for_unexcepected_value_kind()
{
// Arrange
JsonSerializerOptions options = new();

// Act
Action act = () => options.Should().Serialize(42)
.And.Value.Should().HaveValueKind(JsonValueKind.String, because: "Why not?");

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected options to have value kind JsonValueKind.String {value: 3} because Why not?, but found JsonValueKind.Number {value: 4}");
}

[Fact]
public void Guards_excepected_value_kinds()
{
// Arrange
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize(42).And.Value.Should().HaveValueKind(JsonValueKind.Number);
}
}
}

0 comments on commit 2d736bd

Please sign in to comment.