From 4b4c1fa6a8e699dfdd143ea23ff44e42d89833d0 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 11 Nov 2020 21:17:54 +0800 Subject: [PATCH] Add unit test for nullable bool & enum. --- ...pSystemTextJsonSerializerProvider_Tests.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs index 143400c58c..dd4a366b03 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs @@ -26,6 +26,26 @@ namespace Volo.Abp.Json newJson.ShouldBe("{\"name\":\"abp\",\"isDeleted\":false}"); } + [Fact] + public void Serialize_Deserialize_With_Nullable_Boolean() + { + var json = "{\"name\":\"abp\",\"IsDeleted\":null}"; + var file = _jsonSerializer.Deserialize(json); + file.Name.ShouldBe("abp"); + file.IsDeleted.ShouldBeNull(); + + var newJson = _jsonSerializer.Serialize(file); + newJson.ShouldBe("{\"name\":\"abp\",\"isDeleted\":null}"); + + json = "{\"name\":\"abp\",\"IsDeleted\":\"true\"}"; + file = _jsonSerializer.Deserialize(json); + file.IsDeleted.ShouldNotBeNull(); + file.IsDeleted.Value.ShouldBeTrue(); + + newJson = _jsonSerializer.Serialize(file); + newJson.ShouldBe("{\"name\":\"abp\",\"isDeleted\":true}"); + } + [Fact] public void Serialize_Deserialize_With_Enum() { @@ -38,12 +58,38 @@ namespace Volo.Abp.Json newJson.ShouldBe("{\"name\":\"abp\",\"type\":2}"); } + [Fact] + public void Serialize_Deserialize_With_Nullable_Enum() + { + var json = "{\"name\":\"abp\",\"type\":null}"; + var file = _jsonSerializer.Deserialize(json); + file.Name.ShouldBe("abp"); + file.Type.ShouldBeNull(); + + var newJson = _jsonSerializer.Serialize(file); + newJson.ShouldBe("{\"name\":\"abp\",\"type\":null}"); + + json = "{\"name\":\"abp\",\"type\":\"Exe\"}"; + file = _jsonSerializer.Deserialize(json); + file.Type.ShouldNotBeNull(); + file.Type.ShouldBe(FileType.Exe); + + newJson = _jsonSerializer.Serialize(file); + newJson.ShouldBe("{\"name\":\"abp\",\"type\":2}"); + } + class FileWithBoolean { public string Name { get; set; } public bool IsDeleted { get; set; } + } + class FileWithNullableBoolean + { + public string Name { get; set; } + + public bool? IsDeleted { get; set; } } class FileWithEnum @@ -53,6 +99,12 @@ namespace Volo.Abp.Json public FileType Type { get; set; } } + class FileWithNullableEnum + { + public string Name { get; set; } + + public FileType? Type { get; set; } + } enum FileType {