ExpressionGenericMapperTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Masuit.Tools.Mapping;
  2. using Masuit.Tools.UnitTest.TestClasses;
  3. using System.Collections.Generic;
  4. using Xunit;
  5. namespace Masuit.Tools.UnitTest
  6. {
  7. public class ExpressionGenericMapperTest
  8. {
  9. /// <summary>
  10. /// 测试拷贝一个对象本身
  11. /// </summary>
  12. [Fact]
  13. public void TestSelfCopy()
  14. {
  15. TestClassA a = new TestClassA()
  16. {
  17. TestClassC = new TestClassC()
  18. {
  19. MyProperty = "string"
  20. },
  21. List = new List<TestClassC>()
  22. {
  23. new TestClassC(){MyProperty = "cstring"}
  24. }
  25. };
  26. var a2 = a.Copy();
  27. Assert.Equal(a.TestClassC.MyProperty, a2.TestClassC.MyProperty);
  28. Assert.Equal(a.List.Count, a2.List.Count);
  29. }
  30. /// <summary>
  31. /// 测试拷贝简单的属性
  32. /// </summary>
  33. [Fact]
  34. public void TestSimpleProperties()
  35. {
  36. TestClassA a = new TestClassA
  37. {
  38. MyProperty = "string"
  39. };
  40. TestClassB b = a.Map<TestClassA, TestClassB>();
  41. Assert.Equal(a.MyProperty, b.MyProperty);
  42. }
  43. /// <summary>
  44. /// 测试拷贝引用类型的属性
  45. /// </summary>
  46. [Fact]
  47. public void TestRefTypeProperties()
  48. {
  49. TestClassA a = new TestClassA()
  50. {
  51. TestClassC = new TestClassC()
  52. {
  53. MyProperty = "string"
  54. },
  55. List = new List<TestClassC>()
  56. {
  57. new TestClassC(){MyProperty = "cstring"}
  58. }
  59. };
  60. var b = a.Map<TestClassA, TestClassB>();
  61. Assert.Equal(a.MyProperty, b.MyProperty);
  62. Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty);
  63. Assert.Equal(a.List.Count, b.List.Count);
  64. }
  65. /// <summary>
  66. /// 测试可遍历的属性
  67. /// </summary>
  68. [Fact]
  69. public void TestEnumableProperties()
  70. {
  71. TestClassA a = new TestClassA()
  72. {
  73. TestClassC = new TestClassC()
  74. {
  75. MyProperty = "string"
  76. },
  77. List = new List<TestClassC>()
  78. {
  79. new TestClassC(){MyProperty = "cstring"},
  80. new TestClassC(){MyProperty = "cstring"},
  81. },
  82. MyProperty = "string",
  83. Array = new[]
  84. {
  85. new TestClassC()
  86. {
  87. MyProperty = "string",
  88. Obj = new TestClassD()
  89. {
  90. MyProperty = "sstring"
  91. }
  92. },
  93. new TestClassC()
  94. {
  95. MyProperty = "string",
  96. Obj = new TestClassD()
  97. {
  98. MyProperty = "sstring"
  99. }
  100. },
  101. }
  102. };
  103. var b = a.Map<TestClassA, TestClassB>();
  104. Assert.Equal(a.MyProperty, b.MyProperty);
  105. Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty);
  106. Assert.Equal(a.List.Count, b.List.Count);
  107. Assert.Equal(a.Array.Length, b.Array.Length);
  108. }
  109. /// <summary>
  110. /// 测试向已存在的对象拷贝属性值
  111. /// </summary>
  112. [Fact]
  113. public void TestCopyToExistingObject()
  114. {
  115. TestClassA a = new TestClassA()
  116. {
  117. TestClassC = new TestClassC()
  118. {
  119. MyProperty = "string"
  120. },
  121. List = new List<TestClassC>()
  122. {
  123. new TestClassC(){MyProperty = "cstring"},
  124. new TestClassC(){MyProperty = "cstring"},
  125. },
  126. MyProperty = "string",
  127. Array = new[]
  128. {
  129. new TestClassC()
  130. {
  131. MyProperty = "string",
  132. Obj = new TestClassD()
  133. {
  134. MyProperty = "sstring"
  135. }
  136. },
  137. new TestClassC()
  138. {
  139. MyProperty = "string",
  140. Obj = new TestClassD()
  141. {
  142. MyProperty = "sstring"
  143. }
  144. },
  145. }
  146. };
  147. var b = new TestClassB();
  148. a.MapTo(b);
  149. Assert.Equal(a.MyProperty, b.MyProperty);
  150. Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty);
  151. Assert.Equal(a.List.Count, b.List.Count);
  152. Assert.Equal(a.Array.Length, b.Array.Length);
  153. }
  154. }
  155. }