Skip to content

Commit 22861b9

Browse files
committed
Add tests for SystemJsonObjectEnumerator
1 parent 6fe606f commit 22861b9

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.Json;
5+
using FluentAssertions;
6+
using JetBrains.Annotations;
7+
using ShopifySharp.Infrastructure.Serialization.Json;
8+
using Xunit;
9+
10+
namespace ShopifySharp.Tests.Infrastructure.Serialization.Json;
11+
12+
[Trait("Category", "Serialization"), TestSubject(typeof(SystemJsonObjectEnumerator))]
13+
public class SystemJsonObjectEnumeratorTests
14+
{
15+
[Fact]
16+
public void GetEnumerator_ShouldEnumerateObjectValues()
17+
{
18+
// Setup
19+
using var doc = JsonDocument.Parse("""{"foo":123,"bar":"abc"}""");
20+
var objectEnumerator = doc.RootElement.EnumerateObject();
21+
var sut = new SystemJsonObjectEnumerator(objectEnumerator);
22+
23+
// Act
24+
var results = sut.ToList();
25+
26+
// Assert
27+
results.Should().HaveCount(2);
28+
results[0].GetRawObject().Should().BeOfType<JsonElement>().Which.GetInt32().Should().Be(123);
29+
results[1].GetRawObject().Should().BeOfType<JsonElement>().Which.GetString().Should().Be("abc");
30+
}
31+
32+
[Fact]
33+
public void GetEnumerator_WhenCastToAnIEnumerable_ShouldGetEnumeratorAndIterate()
34+
{
35+
// Setup
36+
using var doc = JsonDocument.Parse("""{"foo":123,"bar":"abc"}""");
37+
var objectEnumerator = doc.RootElement.EnumerateObject();
38+
IEnumerable sut = new SystemJsonObjectEnumerator(objectEnumerator);
39+
var props = new List<JsonValueType>();
40+
41+
// Act
42+
foreach (var item in sut)
43+
{
44+
if (item is SystemJsonElement {ValueType: var valueType})
45+
props.Add(valueType);
46+
}
47+
48+
// Assert
49+
props.Should().HaveCount(2);
50+
props.Should().ContainInOrder(JsonValueType.Number, JsonValueType.String);
51+
}
52+
53+
[Fact]
54+
public void Dispose_CalledMultipleTimes_DoesNotThrow()
55+
{
56+
// Setup
57+
using var doc = JsonDocument.Parse("""{"foo":"bar"}""");
58+
var objectEnumerator = doc.RootElement.EnumerateObject();
59+
var sut = new SystemJsonObjectEnumerator(objectEnumerator);
60+
61+
// Act
62+
var act = () =>
63+
{
64+
for (var i = 0; i < 4; i++)
65+
sut.Dispose();
66+
};
67+
68+
// Assert
69+
act.Should().NotThrow();
70+
}
71+
}

0 commit comments

Comments
 (0)