diff --git a/tests/GameCult.Caching.Tests/CultMathJsonTests.cs b/tests/GameCult.Caching.Tests/CultMathJsonTests.cs index 54e01e24..33753074 100644 --- a/tests/GameCult.Caching.Tests/CultMathJsonTests.cs +++ b/tests/GameCult.Caching.Tests/CultMathJsonTests.cs @@ -131,6 +131,30 @@ namespace GameCult.Caching.Tests Parse("""{"cos":{"x":1,"y":9},"cosine":{"x":2}}""").cosine.Should().Be(new float4(2f, 0f, 0f, 0f)); } + [Test] + public void Reading_AcceptsTheLegacyPhasorNamesUnderDisallow() + { + var options = new JsonSerializerOptions { UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow }.AddCultMathConverters(); + var phasor = JsonSerializer.Deserialize("""{"cos":{"x":1,"y":2,"z":3,"w":4},"sin":{"x":5,"y":6,"z":7,"w":8}}""", options); + BitsOf(phasor).Should().Equal(BitsOf(new CultPhasor(new float4(1f, 2f, 3f, 4f), new float4(5f, 6f, 7f, 8f)))); + Action unknown = () => JsonSerializer.Deserialize("""{"cos":{"x":1},"coss":{"x":2}}""", options); + unknown.Should().Throw().WithMessage("*not a component*"); + } + + [Test] + public void Reading_TreatsAThirdOccurrenceOfAPhasorNameAsADuplicate() + { + var property = typeof(JsonSerializerOptions).GetProperty("AllowDuplicateProperties"); + Assert.That(property, Is.Not.Null, "the test runtime's System.Text.Json has AllowDuplicateProperties"); + var strict = new JsonSerializerOptions().AddCultMathConverters(); + property!.SetValue(strict, false); + Action thrice = () => JsonSerializer.Deserialize("""{"cos":{"x":1},"cosine":{"x":2},"cos":{"x":3}}""", strict); + thrice.Should().Throw().WithMessage("*appears more than once*"); + + // Allowed, the last occurrence replaces the whole value, including after a third. + Parse("""{"cos":{"x":1,"y":9},"cosine":{"x":2},"cos":{"z":3}}""").cosine.Should().Be(new float4(0f, 0f, 3f, 0f)); + } + [Test] public void Reading_SkipsUnknownNamesWhenCaseInsensitive() {