Skip to content

Commit

Permalink
feat: implement FluentAssertion extension for JsonElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Jun 12, 2024
1 parent 39d7732 commit c0aae8b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Vonage.Test/Common/Extensions/FluentAssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Text.Json;
using System.Threading.Tasks;
using Vonage.Common.Monads;

namespace Vonage.Test.Common.Extensions;
Expand All @@ -13,4 +14,7 @@ public static class FluentAssertionExtensions

public static ResultAsyncAssertionExtensions<T> Should<T>(this Task<Result<T>> instance) =>
new ResultAsyncAssertionExtensions<T>(instance);

public static JsonElementAssertionExtensions Should(this JsonElement instance) =>
new JsonElementAssertionExtensions(instance);
}
25 changes: 25 additions & 0 deletions Vonage.Test/Common/Extensions/JsonElementAssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Text.Json;
using FluentAssertions;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

namespace Vonage.Test.Common.Extensions;

public class JsonElementAssertionExtensions : ReferenceTypeAssertions<JsonElement, JsonElementAssertionExtensions>
{
public JsonElementAssertionExtensions(JsonElement subject) : base(subject)
{
}

protected override string Identifier => "JsonElement";

public AndConstraint<JsonElementAssertionExtensions> Be(JsonElement expected)
{
Execute.Assertion
.WithExpectation("Expected {context:option} to be {0}{reason}, ", expected)
.Given(() => this.Subject)
.ForCondition(subject => subject.GetRawText().Equals(expected.GetRawText()))
.FailWith("but found {0}.", this.Subject);
return new AndConstraint<JsonElementAssertionExtensions>(this);
}
}

0 comments on commit c0aae8b

Please sign in to comment.