forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBclStructTests.cs
More file actions
80 lines (67 loc) · 1.94 KB
/
Copy pathBclStructTests.cs
File metadata and controls
80 lines (67 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Drawing;
using NUnit.Framework;
using ServiceStack.Common;
namespace ServiceStack.Text.Tests
{
public class BclStructTests : TestBase
{
static BclStructTests()
{
JsConfig<Color>.SerializeFn = c => c.ToString().Replace("Color ", "").Replace("[", "").Replace("]", "");
JsConfig<Color>.DeSerializeFn = Color.FromName;
}
[Test]
public void Can_serialize_Color()
{
var color = Color.Red;
var fromColor = Serialize(color);
Assert.That(fromColor, Is.EqualTo(color));
}
public enum MyEnum
{
Enum1,
Enum2,
Enum3,
}
[Test]
public void Can_serialize_arrays_of_enums()
{
var enums = new[] { MyEnum.Enum1, MyEnum.Enum2, MyEnum.Enum3 };
var fromEnums = Serialize(enums);
Assert.That(fromEnums[0], Is.EqualTo(MyEnum.Enum1));
Assert.That(fromEnums[1], Is.EqualTo(MyEnum.Enum2));
Assert.That(fromEnums[2], Is.EqualTo(MyEnum.Enum3));
}
[Flags]
public enum ExampleEnum
{
None = 0,
One = 1,
Two = 2,
Four = 4,
Eight = 8
}
public class ExampleType
{
public ExampleEnum Enum { get; set; }
public string EnumValues { get; set; }
public string Value { get; set; }
public int Foo { get; set; }
}
[Test]
public void Can_serialize_dto_with_enum_flags()
{
var serialized = TypeSerializer.SerializeToString(new ExampleType
{
Value = "test",
Enum = ExampleEnum.One | ExampleEnum.Four,
EnumValues = (ExampleEnum.One | ExampleEnum.Four).ToDescription(),
Foo = 1
});
var deserialized = TypeSerializer.DeserializeFromString<ExampleType>(serialized);
Console.WriteLine(deserialized.ToJsv());
Assert.That(deserialized.Enum, Is.EqualTo(ExampleEnum.One | ExampleEnum.Four));
}
}
}