Permalink
Browse files

add unit tests for enum serialization

  • Loading branch information...
1 parent fc16c91 commit 9571e1b21d4df966143bc7ec693efb32b5ee22ee @kongo2002 kongo2002 committed Dec 14, 2012
@@ -0,0 +1,54 @@
+using System;
+using NUnit.Framework;
+
+namespace ServiceStack.Text.Tests
+{
+ [TestFixture]
+ public class EnumTests
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ JsConfig.Reset();
+ }
+
+ public enum EnumWithoutFlags
+ {
+ One = 1,
+ Two = 2
+ }
+
+ [Flags]
+ public enum EnumWithFlags
+ {
+ One = 1,
+ Two = 2
+ }
+
+ public class ClassWithEnums
+ {
+ public EnumWithFlags FlagsEnum { get; set; }
+ public EnumWithoutFlags NoFlagsEnum { get; set; }
+ public EnumWithFlags? NullableFlagsEnum { get; set; }
+ public EnumWithoutFlags? NullableNoFlagsEnum { get; set; }
+ }
+
+ [Test]
+ public void Can_correctly_serialize_enums()
+ {
+ var item = new ClassWithEnums
+ {
+ FlagsEnum = EnumWithFlags.One,
+ NoFlagsEnum = EnumWithoutFlags.One,
+ NullableFlagsEnum = EnumWithFlags.Two,
+ NullableNoFlagsEnum = EnumWithoutFlags.Two
+ };
+
+ var expected = "{\"FlagsEnum\":1,\"NoFlagsEnum\":\"One\",\"NullableFlagsEnum\":2,\"NullableNoFlagsEnum\":\"Two\"}";
+ var text = JsonSerializer.SerializeToString(item);
+
+ Assert.AreEqual(expected, text);
+ }
+ }
+}
+
@@ -319,6 +319,7 @@
<Compile Include="DynamicJsonTests.cs" />
<Compile Include="ExpandoTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="EnumTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ServiceStack.Text.Net40\ServiceStack.Text.Net40.csproj">

0 comments on commit 9571e1b

Please sign in to comment.