From 843cc620b6f480ffed06c1860b61b9e5b9b9223e Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 25 Feb 2024 09:39:12 -0500 Subject: [PATCH] add unit tests for aot.json --- LibraryCore.sln | 7 ++++ .../GlobalUsings.cs | 1 + .../JsonSerializationTest.cs | 35 +++++++++++++++++ .../LibraryCore.Tests.Aot.Json.csproj | 39 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 Tests/LibraryCore.Tests.Aot.Json/GlobalUsings.cs create mode 100644 Tests/LibraryCore.Tests.Aot.Json/JsonSerializationTest.cs create mode 100644 Tests/LibraryCore.Tests.Aot.Json/LibraryCore.Tests.Aot.Json.csproj diff --git a/LibraryCore.sln b/LibraryCore.sln index 3dff857b..c4736727 100644 --- a/LibraryCore.sln +++ b/LibraryCore.sln @@ -142,6 +142,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibraryCore.Aot.Json", "Src EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Aot", "Aot", "{896C5DB1-3EDC-4DCC-AAAB-73F705B52B18}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibraryCore.Tests.Aot.Json", "Tests\LibraryCore.Tests.Aot.Json\LibraryCore.Tests.Aot.Json.csproj", "{01CB3A66-9110-4BFA-9D7D-3117578E1575}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -288,6 +290,10 @@ Global {9169F37A-8A3E-4E73-A75E-830A3F8E3224}.Debug|Any CPU.Build.0 = Debug|Any CPU {9169F37A-8A3E-4E73-A75E-830A3F8E3224}.Release|Any CPU.ActiveCfg = Release|Any CPU {9169F37A-8A3E-4E73-A75E-830A3F8E3224}.Release|Any CPU.Build.0 = Release|Any CPU + {01CB3A66-9110-4BFA-9D7D-3117578E1575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {01CB3A66-9110-4BFA-9D7D-3117578E1575}.Debug|Any CPU.Build.0 = Debug|Any CPU + {01CB3A66-9110-4BFA-9D7D-3117578E1575}.Release|Any CPU.ActiveCfg = Release|Any CPU + {01CB3A66-9110-4BFA-9D7D-3117578E1575}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -347,6 +353,7 @@ Global {43270FFA-B5A6-4F20-AE0B-C7C3F9DBD957} = {EC8F3521-E377-420D-B5A1-82D2B29FB611} {9169F37A-8A3E-4E73-A75E-830A3F8E3224} = {896C5DB1-3EDC-4DCC-AAAB-73F705B52B18} {896C5DB1-3EDC-4DCC-AAAB-73F705B52B18} = {75C64940-5413-442D-B876-AEAA8DC20FC0} + {01CB3A66-9110-4BFA-9D7D-3117578E1575} = {896C5DB1-3EDC-4DCC-AAAB-73F705B52B18} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E61C7767-7D12-4313-8DA2-56AE8FC5AAD5} diff --git a/Tests/LibraryCore.Tests.Aot.Json/GlobalUsings.cs b/Tests/LibraryCore.Tests.Aot.Json/GlobalUsings.cs new file mode 100644 index 00000000..8c927eb7 --- /dev/null +++ b/Tests/LibraryCore.Tests.Aot.Json/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/Tests/LibraryCore.Tests.Aot.Json/JsonSerializationTest.cs b/Tests/LibraryCore.Tests.Aot.Json/JsonSerializationTest.cs new file mode 100644 index 00000000..3043c712 --- /dev/null +++ b/Tests/LibraryCore.Tests.Aot.Json/JsonSerializationTest.cs @@ -0,0 +1,35 @@ +using LibraryCore.Aot.Json; +using System.Text.Json; + +namespace LibraryCore.Tests.Aot.Json; + +public class JsonSerializationTest +{ + public record TestModel(int Id, string Text); + + [Fact] + public void NonAotWithGeneral() + { + var serializationOptions = new JsonSerializerOptions(JsonSerializerDefaults.General); + + var json = JsonSerializer.Serialize(new TestModel(99, "99"), serializationOptions); + + var backToModel = JsonSerializer.Deserialize(json, ResolveJsonType.ResolveJsonTypeInfo())!; + + Assert.Equal(99, backToModel.Id); + Assert.Equal("99", backToModel.Text); + } + + [Fact] + public void NonAotWithWeb() + { + var serializationOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web); + + var json = JsonSerializer.Serialize(new TestModel(88, "88"), serializationOptions); + + var backToModel = JsonSerializer.Deserialize(json, ResolveJsonType.ResolveJsonTypeInfo())!; + + Assert.Equal(88, backToModel.Id); + Assert.Equal("88", backToModel.Text); + } +} \ No newline at end of file diff --git a/Tests/LibraryCore.Tests.Aot.Json/LibraryCore.Tests.Aot.Json.csproj b/Tests/LibraryCore.Tests.Aot.Json/LibraryCore.Tests.Aot.Json.csproj new file mode 100644 index 00000000..e59651c0 --- /dev/null +++ b/Tests/LibraryCore.Tests.Aot.Json/LibraryCore.Tests.Aot.Json.csproj @@ -0,0 +1,39 @@ + + + + net6.0;net7.0;net8.0 + false + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + +