| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using Masuit.Tools.Mapping;
- using Masuit.Tools.UnitTest.TestClasses;
- using System.Collections.Generic;
- using Xunit;
- namespace Masuit.Tools.UnitTest
- {
- public class ExpressionGenericMapperTest
- {
- /// <summary>
- /// 测试拷贝一个对象本身
- /// </summary>
- [Fact]
- public void TestSelfCopy()
- {
- TestClassA a = new TestClassA()
- {
- TestClassC = new TestClassC()
- {
- MyProperty = "string"
- },
- List = new List<TestClassC>()
- {
- new TestClassC(){MyProperty = "cstring"}
- }
- };
- var a2 = a.Copy();
- Assert.Equal(a.TestClassC.MyProperty, a2.TestClassC.MyProperty);
- Assert.Equal(a.List.Count, a2.List.Count);
- }
- /// <summary>
- /// 测试拷贝简单的属性
- /// </summary>
- [Fact]
- public void TestSimpleProperties()
- {
- TestClassA a = new TestClassA
- {
- MyProperty = "string"
- };
- TestClassB b = a.Map<TestClassA, TestClassB>();
- Assert.Equal(a.MyProperty, b.MyProperty);
- }
- /// <summary>
- /// 测试拷贝引用类型的属性
- /// </summary>
- [Fact]
- public void TestRefTypeProperties()
- {
- TestClassA a = new TestClassA()
- {
- TestClassC = new TestClassC()
- {
- MyProperty = "string"
- },
- List = new List<TestClassC>()
- {
- new TestClassC(){MyProperty = "cstring"}
- }
- };
- var b = a.Map<TestClassA, TestClassB>();
- Assert.Equal(a.MyProperty, b.MyProperty);
- Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty);
- Assert.Equal(a.List.Count, b.List.Count);
- }
- /// <summary>
- /// 测试可遍历的属性
- /// </summary>
- [Fact]
- public void TestEnumableProperties()
- {
- TestClassA a = new TestClassA()
- {
- TestClassC = new TestClassC()
- {
- MyProperty = "string"
- },
- List = new List<TestClassC>()
- {
- new TestClassC(){MyProperty = "cstring"},
- new TestClassC(){MyProperty = "cstring"},
- },
- MyProperty = "string",
- Array = new[]
- {
- new TestClassC()
- {
- MyProperty = "string",
- Obj = new TestClassD()
- {
- MyProperty = "sstring"
- }
- },
- new TestClassC()
- {
- MyProperty = "string",
- Obj = new TestClassD()
- {
- MyProperty = "sstring"
- }
- },
- }
- };
- var b = a.Map<TestClassA, TestClassB>();
- Assert.Equal(a.MyProperty, b.MyProperty);
- Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty);
- Assert.Equal(a.List.Count, b.List.Count);
- Assert.Equal(a.Array.Length, b.Array.Length);
- }
- /// <summary>
- /// 测试向已存在的对象拷贝属性值
- /// </summary>
- [Fact]
- public void TestCopyToExistingObject()
- {
- TestClassA a = new TestClassA()
- {
- TestClassC = new TestClassC()
- {
- MyProperty = "string"
- },
- List = new List<TestClassC>()
- {
- new TestClassC(){MyProperty = "cstring"},
- new TestClassC(){MyProperty = "cstring"},
- },
- MyProperty = "string",
- Array = new[]
- {
- new TestClassC()
- {
- MyProperty = "string",
- Obj = new TestClassD()
- {
- MyProperty = "sstring"
- }
- },
- new TestClassC()
- {
- MyProperty = "string",
- Obj = new TestClassD()
- {
- MyProperty = "sstring"
- }
- },
- }
- };
- var b = new TestClassB();
- a.MapTo(b);
- Assert.Equal(a.MyProperty, b.MyProperty);
- Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty);
- Assert.Equal(a.List.Count, b.List.Count);
- Assert.Equal(a.Array.Length, b.Array.Length);
- }
- }
- }
|