-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for SystemJsonObjectEnumerator
- Loading branch information
1 parent
6fe606f
commit 22861b9
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<JsonElement>().Which.GetInt32().Should().Be(123); | ||
results[1].GetRawObject().Should().BeOfType<JsonElement>().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<JsonValueType>(); | ||
|
||
// 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(); | ||
} | ||
} |