comparison UnitTests/CS/Mapping/MapValueAttributeTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2
3 using NUnit.Framework;
4
5 using BLToolkit.Mapping;
6
7 namespace Mapping
8 {
9 [TestFixture]
10 public class MapValueAttributeTest
11 {
12 public class Object1
13 {
14 [MapValue(true, "Y")]
15 [MapValue(false, "N")]
16 public bool Bool1;
17
18 [MapValue(true, "Y", "Yes")]
19 [MapValue(false, "N", "No")]
20 public bool Bool2;
21 }
22
23 [Test]
24 public void BoolTest1()
25 {
26 ObjectMapper om = Map.GetObjectMapper(typeof(Object1));
27
28 Object1 o = (Object1)om.CreateInstance();
29
30 om.SetValue(o, "Bool1", "Y");
31 om.SetValue(o, "Bool2", "Yes");
32
33 Assert.AreEqual(true, o.Bool1);
34 Assert.AreEqual(true, o.Bool2);
35
36 Assert.AreEqual("Y", om.GetValue(o, "Bool1"));
37 Assert.AreEqual("Y", om.GetValue(o, "Bool2"));
38 }
39
40 [MapValue(true, "Y")]
41 [MapValue(false, "N")]
42 public class Object2
43 {
44 public bool Bool1;
45
46 [MapValue(true, "Y", "Yes")]
47 [MapValue(false, "N", "No")]
48 public bool Bool2;
49 }
50
51 [Test]
52 public void BoolTest2()
53 {
54 ObjectMapper om = Map.GetObjectMapper(typeof(Object2));
55
56 Object2 o = (Object2)om.CreateInstance();
57
58 om.SetValue(o, "Bool1", "Y");
59 om.SetValue(o, "Bool2", "Yes");
60
61 Assert.AreEqual(true, o.Bool1);
62 Assert.AreEqual(true, o.Bool2);
63
64 Assert.AreEqual("Y", om.GetValue(o, "Bool1"));
65 Assert.AreEqual("Y", om.GetValue(o, "Bool2"));
66 }
67
68 [MapValue(Enum1.Value1, "1")]
69 [MapValue(Enum1.Value3, "3")]
70 public enum Enum1
71 {
72 Value1,
73 [MapValue("2")] Value2,
74 Value3,
75 [NullValue] Value4
76 }
77
78 public class Object3
79 {
80 public Enum1 Enum1;
81 public Enum1 Enum2;
82
83 [MapValue(Enum1.Value1, "10")]
84 [MapValue(Enum1.Value2, "20")]
85 [MapValue(Enum1.Value3, "30")]
86 [MapValue(Enum1.Value3, "32")]
87 [MapValue(Enum1.Value3, "31")]
88 public Enum1 Enum3;
89 public Enum1 Enum4;
90 }
91
92 [Test]
93 public void EnumTest1()
94 {
95 ObjectMapper om = Map.GetObjectMapper(typeof(Object3));
96
97 Object3 o = (Object3)om.CreateInstance();
98
99 om.SetValue(o, "Enum1", "1");
100 om.SetValue(o, "Enum2", "2");
101 om.SetValue(o, "Enum3", "30");
102 om.SetValue(o, "Enum4", null);
103
104 Assert.AreEqual(Enum1.Value1, o.Enum1);
105 Assert.AreEqual(Enum1.Value2, o.Enum2);
106 Assert.AreEqual(Enum1.Value3, o.Enum3);
107 Assert.AreEqual(Enum1.Value4, o.Enum4);
108
109 om.SetValue(o, "Enum3", "31");
110 Assert.AreEqual(Enum1.Value3, o.Enum3);
111
112 om.SetValue(o, "Enum3", "32");
113 Assert.AreEqual(Enum1.Value3, o.Enum3);
114
115 Assert.AreEqual("1", om.GetValue(o, "Enum1"));
116 Assert.AreEqual("2", om.GetValue(o, "Enum2"));
117 Assert.Contains(om.GetValue(o, "Enum3"), new[] {"30", "31", "32", "3"});
118 Assert.IsNull ( om.GetValue(o, "Enum4"));
119 }
120
121 [MapValue(typeof(DayOfWeek), DayOfWeek.Monday, "M")]
122 [MapValue( DayOfWeek.Friday, "F")]
123 public class Object4
124 {
125 public DayOfWeek Dow1;
126 public DayOfWeek Dow2;
127 }
128
129 [Test]
130 public void DayOfWeekTest1()
131 {
132 ObjectMapper om = Map.GetObjectMapper(typeof(Object4));
133
134 Object4 o = (Object4)om.CreateInstance();
135
136 om.SetValue(o, "Dow1", "M");
137 om.SetValue(o, "Dow2", "F");
138
139 Assert.AreEqual(DayOfWeek.Monday, o.Dow1);
140 Assert.AreEqual(DayOfWeek.Friday, o.Dow2);
141
142 Assert.AreEqual("M", om.GetValue(o, "Dow1"));
143 Assert.AreEqual("F", om.GetValue(o, "Dow2"));
144 }
145
146 // http://www.rsdn.ru/Forum/?mid=1809157
147 //
148 public enum StringAlignment
149 {
150 Far,
151 Near,
152 Center
153 }
154
155 public class SourceObject
156 {
157 public string test = "Near";
158 }
159
160 public class DestObject
161 {
162 [MapValue(StringAlignment.Near, "Near")]
163 [MapValue(StringAlignment.Far, "Far")]
164 [MapValue(StringAlignment.Center, "Center")]
165 public StringAlignment test;
166 }
167
168 [Test]
169 public void EnumTest()
170 {
171 SourceObject so = new SourceObject();
172 DestObject o = (DestObject)Map.ObjectToObject(so, typeof(DestObject));
173
174 Assert.AreEqual(StringAlignment.Near, o.test);
175 }
176
177 #region Nullable Enum
178
179 public enum Enum2
180 {
181 [MapValue("Near")] Value1,
182 }
183
184 public class Object5
185 {
186 public Enum2? test;
187 }
188
189 [Test]
190 public void NullableEnumTest()
191 {
192 SourceObject so = new SourceObject();
193
194 Object5 b = Map.ObjectToObject<Object5>(so);
195 Assert.AreEqual(Enum2.Value1, b.test);
196 }
197
198 #endregion
199 }
200 }