0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Linq.Expressions;
|
|
5
|
|
6 using NUnit.Framework;
|
|
7
|
|
8 using BLToolkit.Common;
|
|
9 using BLToolkit.Mapping;
|
|
10
|
|
11 namespace Mapping
|
|
12 {
|
|
13 [TestFixture]
|
|
14 public class ExpressionMapperTest
|
|
15 {
|
|
16 [Test]
|
|
17 public void MapIntToString()
|
|
18 {
|
|
19 var mapper = Map.GetObjectMapper<int,string>();
|
|
20 var dest = mapper(42);
|
|
21
|
|
22 Assert.AreEqual("42", dest);
|
|
23 }
|
|
24
|
|
25 [Test]
|
|
26 public void MapStringToInt()
|
|
27 {
|
|
28 var mapper = Map.GetObjectMapper<string,int>();
|
|
29 var dest = mapper("42");
|
|
30
|
|
31 Assert.AreEqual(42, dest);
|
|
32 }
|
|
33
|
|
34 [Test]
|
|
35 public void MapGenderToString()
|
|
36 {
|
|
37 var mapper = Map.GetObjectMapper<Gender,string>();
|
|
38 var dest = mapper(Gender.Male);
|
|
39
|
|
40 Assert.AreEqual("M", dest);
|
|
41 }
|
|
42
|
|
43 [Test]
|
|
44 public void MapStringToGender()
|
|
45 {
|
|
46 var mapper = Map.GetObjectMapper<string,Gender>();
|
|
47 var dest = mapper("M");
|
|
48
|
|
49 Assert.AreEqual(Gender.Male, dest);
|
|
50 }
|
|
51
|
|
52 public enum Gender
|
|
53 {
|
|
54 [MapValue("F")] Female,
|
|
55 [MapValue("M")] Male,
|
|
56 [MapValue("U")] Unknown,
|
|
57 [MapValue("O")] Other
|
|
58 }
|
|
59
|
|
60 public enum Enum1
|
|
61 {
|
|
62 Value1 = 10,
|
|
63 Value2,
|
|
64 Value3,
|
|
65 Value4,
|
|
66 }
|
|
67
|
|
68 public enum Enum2
|
|
69 {
|
|
70 Value1,
|
|
71 Value2 = 10,
|
|
72 Value3,
|
|
73 Value4,
|
|
74 }
|
|
75
|
|
76 public class Dest
|
|
77 {
|
|
78 public int Field1;
|
|
79 [MapField("Field2")] public float Field3;
|
|
80 public int Field4;
|
|
81 public int? Field6;
|
|
82 public int Field7;
|
|
83 public int Field8;
|
|
84 public int Field9;
|
|
85 public string Field10;
|
|
86 public int Field11;
|
|
87 [NullValue(12)] public int Field12;
|
|
88 [NullValue(13)] public int Field13;
|
|
89 [NullValue(14)] public int Field14;
|
|
90 public Gender Field15;
|
|
91 public string Field16;
|
|
92 public Enum2 Field17;
|
|
93 }
|
|
94
|
|
95 public class Source
|
|
96 {
|
|
97 public int Field1 = 1;
|
|
98 public int Field2 = 2;
|
|
99 [MapField("Field4")] public float Field5 = 5;
|
|
100 public int Field6 = 6;
|
|
101 public int? Field7 = 7;
|
|
102 public int? Field8;
|
|
103 public decimal? Field9 = 9m;
|
|
104 public int Field10 = 10;
|
|
105 public string Field11 = "11";
|
|
106 public string Field12 { get; set; }
|
|
107 public int? Field13;
|
|
108 public decimal? Field14;
|
|
109 public string Field15 = "F";
|
|
110 public Gender Field16 = Gender.Male;
|
|
111 public Enum1 Field17 = Enum1.Value1;
|
|
112 }
|
|
113
|
|
114 void Clone(Expression<Func<Source,Dest>> mapper)
|
|
115 {
|
|
116 /*
|
|
117 Expression.Lambda<Func<Source, Dest>>(
|
|
118 Expression.MemberInit(
|
|
119 Expression.New((ConstructorInfo) methodof(Dest..ctor), new Expression[0]),
|
|
120 new MemberBinding[]
|
|
121 {
|
|
122 Expression.Bind(
|
|
123 fieldof(Dest.Field1),
|
|
124 Expression.Field(CS$0$0000 = Expression.Parameter(typeof(Source), "s"), fieldof(Source.Field1))),
|
|
125 Expression.Bind(
|
|
126 fieldof(Dest.Field2),
|
|
127 Expression.Convert(Expression.Field(CS$0$0000, fieldof(Source.Field2)), typeof(float)))
|
|
128 }), new ParameterExpression[] { CS$0$0000 });
|
|
129 */
|
|
130 }
|
|
131
|
|
132 [Test]
|
|
133 public void MapObjects()
|
|
134 {
|
|
135 var c = Convert<float,int>.From;
|
|
136
|
|
137 //Clone(s => new Dest { Field1 = s.Field1, Field10 = s.Field10.ToString() });
|
|
138
|
|
139 //Map.ObjectToObject(new Source(), typeof(Dest));
|
|
140
|
|
141 var mapper = Map.GetObjectMapper<Source,Dest>();
|
|
142 var src = new Source();
|
|
143 var dest = mapper(src);
|
|
144
|
|
145 Assert.AreEqual(1, dest.Field1);
|
|
146 Assert.AreEqual(2, dest.Field3);
|
|
147 Assert.AreEqual(src.Field5, dest.Field4);
|
|
148 Assert.AreEqual(src.Field6, dest.Field6.Value);
|
|
149 Assert.AreEqual(src.Field7.Value, dest.Field7);
|
|
150 Assert.AreEqual(src.Field8 ?? 0, dest.Field8);
|
|
151 Assert.AreEqual(src.Field9 ?? 0, dest.Field9);
|
|
152 Assert.AreEqual(src.Field10.ToString(), dest.Field10);
|
|
153 Assert.AreEqual(src.Field11, dest.Field11.ToString());
|
|
154 Assert.AreEqual(12, dest.Field12);
|
|
155 Assert.AreEqual(13, dest.Field13);
|
|
156 Assert.AreEqual(14, dest.Field14);
|
|
157 Assert.AreEqual(Gender.Female, dest.Field15);
|
|
158 Assert.AreEqual("M", dest.Field16);
|
|
159 Assert.AreEqual(Enum2.Value2, dest.Field17);
|
|
160 }
|
|
161
|
|
162 [Test]
|
|
163 public void MapObject()
|
|
164 {
|
|
165 var mapper = Map.GetObjectMapper<Source,Source>();
|
|
166 var src = new Source();
|
|
167 var dest = mapper(src);
|
|
168
|
|
169 Assert.AreNotSame(src, dest);
|
|
170 Assert.AreEqual (src.Field1, dest.Field1);
|
|
171 Assert.AreEqual (src.Field2, dest.Field2);
|
|
172 Assert.AreEqual (src.Field5, dest.Field5);
|
|
173 Assert.AreEqual (src.Field6, dest.Field6);
|
|
174 Assert.AreEqual (src.Field7, dest.Field7);
|
|
175 Assert.AreEqual (src.Field8, dest.Field8);
|
|
176 Assert.AreEqual (src.Field9, dest.Field9);
|
|
177 Assert.AreEqual (src.Field10, dest.Field10);
|
|
178 Assert.AreEqual (src.Field11, dest.Field11);
|
|
179 Assert.AreEqual (src.Field12, dest.Field12);
|
|
180 Assert.AreEqual (src.Field13, dest.Field13);
|
|
181 Assert.AreEqual (src.Field14, dest.Field14);
|
|
182 Assert.AreEqual (src.Field15, dest.Field15);
|
|
183 Assert.AreEqual (src.Field16, dest.Field16);
|
|
184 Assert.AreEqual (src.Field17, dest.Field17);
|
|
185 }
|
|
186
|
|
187 class Class1 { public int Field = 1; }
|
|
188 class Class2 { public int Field = 2; }
|
|
189 class Class3 { public Class1 Class = new Class1(); }
|
|
190 class Class4 { public Class2 Class = new Class2(); }
|
|
191
|
|
192 [Test]
|
|
193 public void MapInnerObject1()
|
|
194 {
|
|
195 var mapper = Map.GetObjectMapper<Class3,Class4>();
|
|
196 var src = new Class3();
|
|
197 var dest = mapper(src);
|
|
198
|
|
199 Assert.AreEqual(src.Class.Field, dest.Class.Field);
|
|
200 }
|
|
201
|
|
202 class Class5 { public Class1 Class1 = new Class1(); public Class1 Class2; }
|
|
203 class Class6 { public Class2 Class1 = new Class2(); public Class2 Class2 = null; }
|
|
204
|
|
205 [Test]
|
|
206 public void MapInnerObject2()
|
|
207 {
|
|
208 var mapper = Map.GetObjectMapper<Class5,Class6>();
|
|
209 var src = new Class5();
|
|
210
|
|
211 src.Class2 = src.Class1;
|
|
212
|
|
213 var dest = mapper(src);
|
|
214
|
|
215 Assert.IsNotNull(dest.Class1);
|
|
216 Assert.AreSame(dest.Class1, dest.Class2);
|
|
217 }
|
|
218
|
|
219 class Class7 { public Class9 Class; }
|
|
220 class Class8 { public Class10 Class = null; }
|
|
221 class Class9 { public Class7 Class = new Class7(); }
|
|
222 class Class10 { public Class8 Class = new Class8(); }
|
|
223
|
|
224 [Test]
|
|
225 public void SelfReference1()
|
|
226 {
|
|
227 var mapper = Map.GetObjectMapper<Class9,Class10>();
|
|
228 var src = new Class9();
|
|
229
|
|
230 src.Class.Class = src;
|
|
231
|
|
232 var dest = mapper(src);
|
|
233
|
|
234 Assert.AreSame(dest, dest.Class.Class);
|
|
235 }
|
|
236
|
|
237 class Class11 { public Class9 Class = new Class9(); }
|
|
238 class Class12 { public Class10 Class = new Class10(); }
|
|
239
|
|
240 [Test]
|
|
241 public void SelfReference2()
|
|
242 {
|
|
243 var mapper = Map.GetObjectMapper<Class11,Class12>();
|
|
244 var src = new Class11();
|
|
245
|
|
246 src.Class.Class.Class = src.Class;
|
|
247
|
|
248 var dest = mapper(src);
|
|
249
|
|
250 Assert.AreSame(dest.Class, dest.Class.Class.Class);
|
|
251 }
|
|
252
|
|
253 class Class13 { public Class1 Class = new Class1(); }
|
|
254 class Class14 { public Class1 Class = new Class1(); }
|
|
255
|
|
256 [Test]
|
|
257 public void DeepCopy1()
|
|
258 {
|
|
259 var mapper = Map.GetObjectMapper<Class13,Class14>();
|
|
260 var src = new Class13();
|
|
261
|
|
262 var dest = mapper(src);
|
|
263
|
|
264 Assert.AreNotSame(src.Class, dest.Class);
|
|
265 }
|
|
266
|
|
267 [Test]
|
|
268 public void DeepCopy2()
|
|
269 {
|
|
270 var mapper = Map.GetObjectMapper<Class13,Class14>(false);
|
|
271 var src = new Class13();
|
|
272
|
|
273 var dest = mapper(src);
|
|
274
|
|
275 Assert.AreSame(src.Class, dest.Class);
|
|
276 }
|
|
277
|
|
278 class Class15 { public List<Class1> List = new List<Class1> { new Class1(), new Class1() }; }
|
|
279 class Class16 { public List<Class2> List = null; }
|
|
280
|
|
281 [Test]
|
|
282 public void ObjectList()
|
|
283 {
|
|
284 var mapper = Map.GetObjectMapper<Class15,Class16>();
|
|
285 var src = new Class15();
|
|
286
|
|
287 src.List.Add(src.List[0]);
|
|
288
|
|
289 var dest = mapper(src);
|
|
290
|
|
291 Assert.AreEqual (3, dest.List.Count);
|
|
292 Assert.IsNotNull (dest.List[0]);
|
|
293 Assert.IsNotNull (dest.List[1]);
|
|
294 Assert.IsNotNull (dest.List[2]);
|
|
295 Assert.AreNotSame(dest.List[0], dest.List[1]);
|
|
296 Assert.AreSame (dest.List[0], dest.List[2]);
|
|
297 }
|
|
298
|
|
299 [Test]
|
|
300 public void ScalarList()
|
|
301 {
|
|
302 var mapper = Map.GetObjectMapper<List<int>,IList<string>>();
|
|
303 var dest = mapper(new List<int> { 1, 2, 3});
|
|
304
|
|
305 Assert.AreEqual("1", dest[0]);
|
|
306 Assert.AreEqual("2", dest[1]);
|
|
307 Assert.AreEqual("3", dest[2]);
|
|
308 }
|
|
309
|
|
310 [Test]
|
|
311 public void ScalarArray()
|
|
312 {
|
|
313 var mapper = Map.GetObjectMapper<int[],string[]>();
|
|
314 var dest = mapper(new[] { 1, 2, 3});
|
|
315
|
|
316 Assert.AreEqual("1", dest[0]);
|
|
317 Assert.AreEqual("2", dest[1]);
|
|
318 Assert.AreEqual("3", dest[2]);
|
|
319 }
|
|
320
|
|
321 class Class17
|
|
322 {
|
|
323 public IEnumerable<Class9> Arr { get { return GetEnumerable(); }}
|
|
324
|
|
325 static IEnumerable<Class9> GetEnumerable()
|
|
326 {
|
|
327 var c = new Class9();
|
|
328
|
|
329 yield return c;
|
|
330 yield return new Class9();
|
|
331 yield return c;
|
|
332 }
|
|
333 }
|
|
334
|
|
335 class Class18 { public Class9[] Arr = null; }
|
|
336
|
|
337 [Test]
|
|
338 public void ObjectArray1()
|
|
339 {
|
|
340 var mapper = Map.GetObjectMapper<Class17,Class18>();
|
|
341 var dest = mapper(new Class17());
|
|
342
|
|
343 Assert.AreEqual (3, dest.Arr.Length);
|
|
344 Assert.IsNotNull (dest.Arr[0]);
|
|
345 Assert.IsNotNull (dest.Arr[1]);
|
|
346 Assert.IsNotNull (dest.Arr[2]);
|
|
347 Assert.AreNotSame(dest.Arr[0], dest.Arr[1]);
|
|
348 Assert.AreSame (dest.Arr[0], dest.Arr[2]);
|
|
349 }
|
|
350
|
|
351 class Class19
|
|
352 {
|
|
353 public Class9[] Arr { get { return new Class17().Arr.ToArray(); }}
|
|
354 }
|
|
355
|
|
356 [Test]
|
|
357 public void ObjectArray2()
|
|
358 {
|
|
359 var mapper = Map.GetObjectMapper<Class19,Class18>();
|
|
360 var dest = mapper(new Class19());
|
|
361
|
|
362 Assert.AreEqual (3, dest.Arr.Length);
|
|
363 Assert.IsNotNull (dest.Arr[0]);
|
|
364 Assert.IsNotNull (dest.Arr[1]);
|
|
365 Assert.IsNotNull (dest.Arr[2]);
|
|
366 Assert.AreNotSame(dest.Arr[0], dest.Arr[1]);
|
|
367 Assert.AreSame (dest.Arr[0], dest.Arr[2]);
|
|
368 }
|
|
369
|
|
370 class Class20 { public Source Class1 = new Source(); public Source Class2; }
|
|
371 class Class21 { public Dest Class1 = null; public Dest Class2 = null; }
|
|
372
|
|
373 [Test]
|
|
374 public void NoCrossRef()
|
|
375 {
|
|
376 var mapper = new ExpressionMapper<Class20,Class21> { HandleBackReferences = false }.GetMapper();
|
|
377 var source = new Class20();
|
|
378
|
|
379 source.Class2 = source.Class1;
|
|
380
|
|
381 var dest = mapper(source);
|
|
382
|
|
383 Assert.IsNotNull (dest.Class1);
|
|
384 Assert.IsNotNull (dest.Class2);
|
|
385 Assert.AreNotSame(dest.Class1, dest.Class2);
|
|
386 }
|
|
387
|
|
388 class Object1
|
|
389 {
|
|
390 public int Field1;
|
|
391 [ExpressionMapIgnore]
|
|
392 public int Field2;
|
|
393 }
|
|
394
|
|
395 class Object2
|
|
396 {
|
|
397 public int Field1;
|
|
398 public int Field2;
|
|
399 }
|
|
400
|
|
401 [Test]
|
|
402 public void ExpressionMapIgnoreTest()
|
|
403 {
|
|
404 var mapper1 = Map.GetObjectMapper<Object1,Object2>();
|
|
405 var object2 = mapper1(new Object1 { Field1 = 1, Field2 = 2 });
|
|
406
|
|
407 Assert.That(object2.Field2, Is.Not.EqualTo(2));
|
|
408
|
|
409 var mapper2 = Map.GetObjectMapper<Object2,Object1>();
|
|
410 var object1 = mapper2(new Object2 { Field1 = 1, Field2 = 2 });
|
|
411
|
|
412 Assert.That(object1.Field2, Is.Not.EqualTo(2));
|
|
413 }
|
|
414
|
|
415 [MapField("SomethingColumnInDB", "MyInnerClass.Something")]
|
|
416 class MyClass
|
|
417 {
|
|
418 public int ID;
|
|
419 public string Name;
|
|
420 public MyInnerClass MyInnerClass;
|
|
421 }
|
|
422
|
|
423 class MyInnerClass
|
|
424 {
|
|
425 public string Something;
|
|
426 }
|
|
427
|
|
428 [Test]
|
|
429 public void MapFieldTest()
|
|
430 {
|
|
431 var entity = new MyClass
|
|
432 {
|
|
433 ID = 1,
|
|
434 Name = "Test",
|
|
435 MyInnerClass = new MyInnerClass { Something = "Something" }
|
|
436 };
|
|
437
|
|
438 var mapper = Map.GetObjectMapper<MyClass,MyClass>(true, true);
|
|
439 var clone = mapper(entity);
|
|
440
|
|
441 Assert.That(clone.MyInnerClass, Is.Not.Null);
|
|
442 }
|
|
443 }
|
|
444 }
|