11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Runtime . Serialization ;
45using System . Text . Json ;
56using System . Text . Json . Serialization ;
67using FluentAssertions ;
78using NUnit . Framework ;
89using NUnit . Framework . Legacy ;
10+ using Plotly . Blazor . LayoutLib . XAxisLib ;
911
1012namespace Plotly . Blazor . Tests
1113{
@@ -101,7 +103,9 @@ public enum TestEnum
101103 [ EnumMember ( Value = "default" ) ]
102104 Enum1 ,
103105 [ EnumMember ( Value = "notDefault" ) ]
104- Enum2
106+ Enum2 ,
107+ [ EnumMember ( Value = @"true" ) ]
108+ True ,
105109 }
106110
107111 /// <summary>
@@ -120,8 +124,10 @@ public enum TestFlag
120124 EnumTwo = 4 ,
121125 [ EnumMember ( Value = "enum3" ) ]
122126 EnumThree = 8 ,
127+ [ EnumMember ( Value = "false" ) ]
128+ False = 16 ,
123129 [ EnumMember ( Value = "all" ) ]
124- All = EnumOne | EnumTwo | EnumThree
130+ All = EnumOne | EnumTwo | EnumThree | False
125131 }
126132
127133 public class ConverterTests
@@ -141,7 +147,7 @@ public void Setup()
141147 Converters = { new DateTimeConverter ( ) , new DateTimeOffsetConverter ( ) }
142148 } ;
143149 }
144-
150+
145151 /// <summary>
146152 /// Defines the test method FlagConverterTest.
147153 /// </summary>
@@ -151,41 +157,45 @@ public void FlagConvertingTest()
151157 // Test default serialization with EnumMemberAttribute
152158 var testObject = new TestClass
153159 {
154- TestFlag = TestFlag . EnumOne | TestFlag . EnumTwo
160+ TestFlag = TestFlag . EnumOne | TestFlag . EnumTwo | TestFlag . False
155161 } ;
156- var expected = "{\" testFlag\" :\" enum1\\ u002Benum2\" }" ;
162+ var expected = "{\" testFlag\" :\" enum1\\ u002Benum2\\ u002Bfalse \ " }" ;
157163 var actual = JsonSerializer . Serialize ( testObject , serializerOptions ) ;
158164 Assert . That ( actual , Is . EqualTo ( expected ) ) ;
159165
160166 // Test "all" value
161167 testObject = new TestClass
162168 {
163- TestFlag = TestFlag . EnumOne | TestFlag . EnumTwo | TestFlag . EnumThree
169+ TestFlag = TestFlag . EnumOne | TestFlag . EnumTwo | TestFlag . EnumThree | TestFlag . False
164170 } ;
165171 expected = "{\" testFlag\" :\" all\" }" ;
166172 actual = JsonSerializer . Serialize ( testObject , serializerOptions ) ;
167173 Assert . That ( actual , Is . EqualTo ( expected ) ) ;
168174 }
169-
170- [ Test ]
171- public void EnumConvertingTest ( )
175+
176+ [ TestCase ( TestEnum . Enum1 , "{\" testEnum\" :\" default\" }" ) ]
177+ [ TestCase ( TestEnum . True , "{\" testEnum\" :true}" ) ]
178+ public void EnumSerializationTest (
179+ TestEnum testEnum ,
180+ string expectedJson )
172181 {
173- // Test default serialization with EnumMemberAttribute
174182 var testObj = new TestClass
175183 {
176- TestEnum = 0
184+ TestEnum = testEnum
177185 } ;
178- var expectedJson = "{ \" testEnum \" : \" default \" }" ;
186+
179187 var actualJson = JsonSerializer . Serialize ( testObj , serializerOptions ) ;
180188 Assert . That ( actualJson , Is . EqualTo ( expectedJson ) ) ;
189+ }
181190
182- // Test read, write for other value
183- testObj = new TestClass
184- {
185- TestEnum = TestEnum . Enum2
186- } ;
187- var actual = JsonSerializer . Deserialize < TestClass > ( JsonSerializer . Serialize ( testObj , serializerOptions ) ) ;
188- Assert . That ( testObj . TestEnum , Is . EqualTo ( actual . TestEnum ) , "Read/Write the enum failed!" ) ;
191+ [ TestCase ( "{\" testEnum\" :\" notDefault\" }" , TestEnum . Enum2 ) ]
192+ [ TestCase ( "{\" testEnum\" :true}" , TestEnum . True ) ]
193+ public void EnumDeserializationTest (
194+ string json ,
195+ TestEnum expectedEnum )
196+ {
197+ var actual = JsonSerializer . Deserialize < TestClass > ( json , serializerOptions ) ;
198+ Assert . That ( expectedEnum , Is . EqualTo ( actual . TestEnum ) , "Read/Write the enum failed!" ) ;
189199 }
190200
191201 [ Test ]
0 commit comments