From be20333f29e7e5ce2473b6795410cf98c68e6a95 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Mon, 22 Jan 2024 18:26:07 +0300 Subject: [PATCH] Add more tests --- .../JsonNetTypesDestructuringTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Destructurama.JsonNet.Tests/JsonNetTypesDestructuringTests.cs b/src/Destructurama.JsonNet.Tests/JsonNetTypesDestructuringTests.cs index 470820c..80890d1 100644 --- a/src/Destructurama.JsonNet.Tests/JsonNetTypesDestructuringTests.cs +++ b/src/Destructurama.JsonNet.Tests/JsonNetTypesDestructuringTests.cs @@ -14,7 +14,9 @@ using Destructurama.JsonNet.Tests.Support; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Serilog; +using Serilog.Core; using Serilog.Events; using Shouldly; using Xunit; @@ -85,4 +87,26 @@ public void TryDestructure_Should_Return_False_When_Called_With_Null() var policy = new JsonNetDestructuringPolicy(); policy.TryDestructure(null!, null!, out _).ShouldBeFalse(); } + + [Fact] + public void TryDestructure_Should_Handle_TypeToken_As_Ordinal_Property_When_Not_String() + { + var policy = new JsonNetDestructuringPolicy(); + var o = new JObject(new JProperty("$type", 42)); + policy.TryDestructure(o, new StubFactory(), out var value).ShouldBeTrue(); + var sv = value.ShouldBeOfType(); + sv.Properties.Count.ShouldBe(1); + sv.Properties[0].Name.ShouldBe("$type"); + sv.Properties[0].Value.LiteralValue().ShouldBe(42); + } + + private sealed class StubFactory : ILogEventPropertyValueFactory + { + public LogEventPropertyValue CreatePropertyValue(object? value, bool destructureObjects = false) + { + return ((JToken)value!).Value() == 42 + ? (LogEventPropertyValue)new ScalarValue(42) + : throw new NotImplementedException(); + } + } }