From c0aae8bc58bdf4caab1e89d37dd87c55e65b6479 Mon Sep 17 00:00:00 2001 From: tr00d Date: Wed, 12 Jun 2024 07:25:33 +0200 Subject: [PATCH] feat: implement FluentAssertion extension for JsonElement --- .../Extensions/FluentAssertionExtensions.cs | 6 ++++- .../JsonElementAssertionExtensions.cs | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 Vonage.Test/Common/Extensions/JsonElementAssertionExtensions.cs diff --git a/Vonage.Test/Common/Extensions/FluentAssertionExtensions.cs b/Vonage.Test/Common/Extensions/FluentAssertionExtensions.cs index fb8b872d6..56f54ddab 100644 --- a/Vonage.Test/Common/Extensions/FluentAssertionExtensions.cs +++ b/Vonage.Test/Common/Extensions/FluentAssertionExtensions.cs @@ -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; @@ -13,4 +14,7 @@ public static ResultAssertionExtensions Should(this Result instance) => public static ResultAsyncAssertionExtensions Should(this Task> instance) => new ResultAsyncAssertionExtensions(instance); + + public static JsonElementAssertionExtensions Should(this JsonElement instance) => + new JsonElementAssertionExtensions(instance); } \ No newline at end of file diff --git a/Vonage.Test/Common/Extensions/JsonElementAssertionExtensions.cs b/Vonage.Test/Common/Extensions/JsonElementAssertionExtensions.cs new file mode 100644 index 000000000..782756f21 --- /dev/null +++ b/Vonage.Test/Common/Extensions/JsonElementAssertionExtensions.cs @@ -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 +{ + public JsonElementAssertionExtensions(JsonElement subject) : base(subject) + { + } + + protected override string Identifier => "JsonElement"; + + public AndConstraint 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(this); + } +} \ No newline at end of file