Skip to content

Commit

Permalink
Fix array enumerator (#8)
Browse files Browse the repository at this point in the history
Remove readonly from ArrayEnumeratorWrapper
to allow it to move between elements.
Add tests for array and object enumerators.
Updated version number.
  • Loading branch information
frankracis-litmos authored Jul 26, 2023
1 parent a8ac197 commit 66ab864
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/JsonEasyNavigation.Tests/ArrayTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using Shouldly;
using Xunit;
Expand Down Expand Up @@ -123,5 +124,21 @@ public void WhenEnumeratingKey_ShouldBeEmpty()

nav.Keys.ShouldBeEmpty();
}
}

[Fact]
public void EnumerateArray_ShouldEnumerateItems()
{
var json = @"[ ""item1"", ""item2"", ""item3"" ]";

var jsonDocument = JsonDocument.Parse(json);
var nav = jsonDocument.ToNavigation();

var list = new List<string>();
foreach (var item in nav)
{
list.Add(item.GetStringOrEmpty());
}
list.ShouldBe(new[] { "item1", "item2", "item3" });
}
}
}
32 changes: 32 additions & 0 deletions src/JsonEasyNavigation.Tests/PropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,37 @@ public void WhenHierarchyLevel1_ShouldNotExist()
var item = nav["item1"]["item3"];
item.Exist.ShouldBeFalse();
}

[Fact]
public void EnumerateObject_ShouldEnumerateProperties()
{
var json = @"{ ""item1"" : ""first"", ""item2"": ""second"" }";

var jsonDocument = JsonDocument.Parse(json);
var nav = jsonDocument.ToNavigation();

var list = new List<string>();
foreach (var item in nav)
{
list.Add(item.Name);
}
list.ShouldBe(new[] { "item1", "item2" });
}

[Fact]
public void EnumerateObject_CachedProperties_ShouldEnumerateProperties()
{
var json = @"{ ""item1"" : ""first"", ""item2"": ""second"" }";

var jsonDocument = JsonDocument.Parse(json);
var nav = jsonDocument.ToNavigation().WithCachedProperties();

var list = new List<string>();
foreach (var item in nav)
{
list.Add(item.Name);
}
list.ShouldBe(new[] { "item1", "item2" });
}
}
}
4 changes: 2 additions & 2 deletions src/JsonEasyNavigation/ArrayEnumeratorWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace JsonEasyNavigation
{
internal readonly struct ArrayEnumeratorWrapper : IEnumerator<JsonNavigationElement>
internal struct ArrayEnumeratorWrapper : IEnumerator<JsonNavigationElement>
{
private readonly JsonElement.ArrayEnumerator _enumerator;
private JsonElement.ArrayEnumerator _enumerator;

public ArrayEnumeratorWrapper(JsonElement jsonElement)
{
Expand Down
2 changes: 1 addition & 1 deletion src/JsonEasyNavigation/JsonEasyNavigation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<PackageIcon>logo.png</PackageIcon>
<Title>JsonEasyNavigation</Title>
<Authors>Sharkadi Andrey</Authors>
Expand Down

0 comments on commit 66ab864

Please sign in to comment.