Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jan 22, 2024
1 parent 40d7ff6 commit 451517d
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Destructurama.JsonNet.Tests/JsonNetTypesDestructuringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void TryDestructure_Should_Return_False_When_Called_With_Null()
}

[Fact]
public void TryDestructure_Should_Handle_TypeToken_As_Ordinal_Property_When_Not_String()
public void TryDestructure_Should_Handle_TypeToken_As_Ordinal_Property_When_Its_Value_Not_String()
{
var policy = new JsonNetDestructuringPolicy();
var o = new JObject(new JProperty("$type", 42));
Expand All @@ -100,13 +100,30 @@ public void TryDestructure_Should_Handle_TypeToken_As_Ordinal_Property_When_Not_
sv.Properties[0].Value.LiteralValue().ShouldBe(42);
}

[Fact]
public void TryDestructure_Should_Handle_TypeToken_As_Ordinal_Property_When_Its_Not_JValue()
{
var policy = new JsonNetDestructuringPolicy();
var o = new JObject(new JProperty("$type", new JArray(1, 2, 3)));
policy.TryDestructure(o, new StubFactory(), out var value).ShouldBeTrue();
var sv = value.ShouldBeOfType<StructureValue>();
sv.Properties.Count.ShouldBe(1);
sv.Properties[0].Name.ShouldBe("$type");
var seq = sv.Properties[0].Value.ShouldBeOfType<SequenceValue>();
seq.Elements.Count.ShouldBe(3);
}

private sealed class StubFactory : ILogEventPropertyValueFactory
{
public LogEventPropertyValue CreatePropertyValue(object? value, bool destructureObjects = false)
{
return ((JToken)value!).Value<int>() == 42
? (LogEventPropertyValue)new ScalarValue(42)
: throw new NotImplementedException();
if (value is JArray arr && arr.Values<int>().SequenceEqual([1, 2, 3]))
return new SequenceValue(new[] { new ScalarValue(1), new ScalarValue(2), new ScalarValue(3) });

if (value is JToken t && t.Value<int>() == 42)
return new ScalarValue(42);

throw new NotImplementedException();
}
}
}

0 comments on commit 451517d

Please sign in to comment.