From 22861b96fe89277cd18c47e8e17c127bf4c9c489 Mon Sep 17 00:00:00 2001 From: Joshua Harms Date: Tue, 31 Dec 2024 16:41:58 -0600 Subject: [PATCH] Add tests for SystemJsonObjectEnumerator --- .../Json/SystemJsonObjectEnumeratorTests.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/ShopifySharp.Tests/Infrastructure/Serialization/Json/SystemJsonObjectEnumeratorTests.cs b/ShopifySharp.Tests/Infrastructure/Serialization/Json/SystemJsonObjectEnumeratorTests.cs index e69de29bb..ef78e448b 100644 --- a/ShopifySharp.Tests/Infrastructure/Serialization/Json/SystemJsonObjectEnumeratorTests.cs +++ b/ShopifySharp.Tests/Infrastructure/Serialization/Json/SystemJsonObjectEnumeratorTests.cs @@ -0,0 +1,71 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using FluentAssertions; +using JetBrains.Annotations; +using ShopifySharp.Infrastructure.Serialization.Json; +using Xunit; + +namespace ShopifySharp.Tests.Infrastructure.Serialization.Json; + +[Trait("Category", "Serialization"), TestSubject(typeof(SystemJsonObjectEnumerator))] +public class SystemJsonObjectEnumeratorTests +{ + [Fact] + public void GetEnumerator_ShouldEnumerateObjectValues() + { + // Setup + using var doc = JsonDocument.Parse("""{"foo":123,"bar":"abc"}"""); + var objectEnumerator = doc.RootElement.EnumerateObject(); + var sut = new SystemJsonObjectEnumerator(objectEnumerator); + + // Act + var results = sut.ToList(); + + // Assert + results.Should().HaveCount(2); + results[0].GetRawObject().Should().BeOfType().Which.GetInt32().Should().Be(123); + results[1].GetRawObject().Should().BeOfType().Which.GetString().Should().Be("abc"); + } + + [Fact] + public void GetEnumerator_WhenCastToAnIEnumerable_ShouldGetEnumeratorAndIterate() + { + // Setup + using var doc = JsonDocument.Parse("""{"foo":123,"bar":"abc"}"""); + var objectEnumerator = doc.RootElement.EnumerateObject(); + IEnumerable sut = new SystemJsonObjectEnumerator(objectEnumerator); + var props = new List(); + + // Act + foreach (var item in sut) + { + if (item is SystemJsonElement {ValueType: var valueType}) + props.Add(valueType); + } + + // Assert + props.Should().HaveCount(2); + props.Should().ContainInOrder(JsonValueType.Number, JsonValueType.String); + } + + [Fact] + public void Dispose_CalledMultipleTimes_DoesNotThrow() + { + // Setup + using var doc = JsonDocument.Parse("""{"foo":"bar"}"""); + var objectEnumerator = doc.RootElement.EnumerateObject(); + var sut = new SystemJsonObjectEnumerator(objectEnumerator); + + // Act + var act = () => + { + for (var i = 0; i < 4; i++) + sut.Dispose(); + }; + + // Assert + act.Should().NotThrow(); + } +}