MapperConfigurationTests.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Masuit.Tools.Mapping;
  2. using Masuit.Tools.Mapping.Core;
  3. using Masuit.Tools.Mapping.Exceptions;
  4. using Masuit.Tools.UnitTest.Mapping.ClassTests;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using System;
  7. using System.Linq.Expressions;
  8. using System.Reflection;
  9. namespace Masuit.Tools.UnitTest.Mapping.Core
  10. {
  11. [TestClass]
  12. public class MapperConfigurationTests
  13. {
  14. [TestMethod, TestCategory("Ignore")]
  15. public void Ignore_Add_Succes()
  16. {
  17. var actual = new MapperConfigurationTestContainer();
  18. actual.Ignore((d) => d.PropInt1);
  19. Assert.AreEqual(actual.GetIgnoreCount(), 1);
  20. }
  21. [TestMethod, TestCategory("AfterMap")]
  22. public void AfterMap_Add_Succes()
  23. {
  24. var actual = new MapperConfigurationTestContainer();
  25. actual.AfterMap((s, d) =>
  26. {
  27. //Nothing To Do
  28. });
  29. Assert.AreEqual(actual.GetAfterMapActionCount(), 1);
  30. }
  31. [TestMethod, TestCategory("ExecuteAfterActions")]
  32. public void ExecuteAfterActions_Succes()
  33. {
  34. bool excecutedAction = false;
  35. var actual = new MapperConfigurationTestContainer();
  36. actual.AfterMap((s, d) =>
  37. {
  38. excecutedAction = true;
  39. });
  40. actual.ExecuteAfterActions(new ClassSource(), new ClassDest());
  41. Assert.IsTrue(excecutedAction);
  42. }
  43. [TestMethod, TestCategory("ReverseMap")]
  44. public void ReverseMap_Success()
  45. {
  46. ExpressionMapper.Reset();
  47. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  48. var actual = expected.ReverseMap();
  49. Assert.IsInstanceOfType(actual, typeof(MapperConfiguration<ClassDest, ClassSource>));
  50. }
  51. [TestMethod, TestCategory("ReverseMap"), ExpectedException(typeof(MapperExistException))]
  52. public void ReverseMap_MapperAlreadyExist_Exception()
  53. {
  54. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  55. MapperConfiguration<ClassDest, ClassSource> actual = null;
  56. expected.ReverseMap();
  57. actual = expected.ReverseMap();
  58. MapperConfigurationCollectionContainer.Instance.RemoveAt(1);
  59. }
  60. [TestMethod, TestCategory("Exception"), ExpectedException(typeof(NotSameTypePropertyException))]
  61. public void NotSameTypePropertyException_Exception()
  62. {
  63. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  64. expected.ForMember(s => s.PropInt1, d => d.PropString2);
  65. expected.CreateMappingExpression(null);
  66. }
  67. [TestMethod, TestCategory("Exception"), ExpectedException(typeof(ReadOnlyPropertyException))]
  68. public void ReadOnlyPropertyExceptionException_Exception()
  69. {
  70. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  71. expected.ForMember(s => s.PropInt1, d => d.RealOnlyPropInt1);
  72. expected.CreateMappingExpression(null);
  73. }
  74. [TestMethod, TestCategory("GetSortedExpression")]
  75. public void GetSortedExpression_PropertyFound_Succes()
  76. {
  77. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  78. expected.CreateMappingExpression(null);
  79. var actual = expected.GetSortedExpression("PropInt1");
  80. Assert.IsNotNull(actual);
  81. }
  82. [TestMethod, TestCategory("GetSortedExpression"), ExpectedException(typeof(PropertyNoExistException))]
  83. public void GetSortedExpression_PropertyNotFound_Exception()
  84. {
  85. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  86. expected.CreateMappingExpression(null);
  87. expected.GetSortedExpression("PropNotExist");
  88. }
  89. [TestMethod, TestCategory("GetMapper"), ExpectedException(typeof(NoFoundMapperException))]
  90. public void GetMapper_NoFoundMapperException()
  91. {
  92. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  93. expected.GetMapperTest(typeof(string), typeof(string), true);
  94. }
  95. [TestMethod, TestCategory("CreateMappingExpression")]
  96. public void CreateMappingExpression_NotInitialise()
  97. {
  98. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  99. expected.CreateMappingExpression(null);
  100. int actual = expected.MemberToMapForNew.Count;
  101. Assert.IsTrue(actual > 0);
  102. }
  103. [TestMethod, TestCategory("GetPropertyInfo")]
  104. public void GetPropertyInfo_PropertyFound_Success()
  105. {
  106. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  107. Expression<Func<ClassSource, object>> exp = x => x.PropInt1;
  108. var actual = expected.GetPropertyInfoTest(exp);
  109. Assert.AreEqual(actual.Name, "PropInt1");
  110. }
  111. [TestMethod, TestCategory("GetPropertyInfo"), ExpectedException(typeof(NotImplementedException))]
  112. public void GetPropertyInfo_PropertyNotImplementedException()
  113. {
  114. PropertyInfo actual = null;
  115. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  116. Expression<Func<ClassDest, object>> exp = x => x.PropInt1 > 0;
  117. actual = expected.GetPropertyInfoTest(exp);
  118. }
  119. [TestMethod, TestCategory("GetPropertyInfo"), ExpectedException(typeof(NotImplementedException))]
  120. public void GetPropertyInfo_PropertyNotImplementedExceptionDefault()
  121. {
  122. PropertyInfo actual = null;
  123. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  124. Expression<Func<ClassDest, object>> exp = x => null;
  125. actual = expected.GetPropertyInfoTest(exp);
  126. }
  127. [TestMethod, TestCategory("CreateCommonMember")]
  128. public void CreateCommonMember_FindMapper_NotList_Success()
  129. {
  130. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  131. ExpressionMapper.Reset();
  132. ExpressionMapper.CreateMap<ClassSource2, ClassDest2>();
  133. expected.CreateMappingExpression(null);
  134. var actual = expected.GetGenericLambdaExpression();
  135. ExpressionMapper.Reset();
  136. }
  137. [TestMethod, TestCategory("CreateCommonMember")]
  138. public void CreateCommonMember_IgnoreProperty()
  139. {
  140. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  141. expected.Ignore(d => d.PropInt1);
  142. expected.CreateCommonMemberTest();
  143. }
  144. [TestMethod, TestCategory("CheckAndRemoveMemberDest")]
  145. public void CheckAndRemoveMemberDest_PropertyExist_Remove()
  146. {
  147. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  148. expected.CreateMappingExpression(null);
  149. int countOri = expected.MemberToMapForNew.Count;
  150. expected.CheckAndRemoveMemberDestTest("PropInt1");
  151. Assert.AreNotEqual(countOri, expected.MemberToMapForNew.Count);
  152. }
  153. [TestMethod, TestCategory("CreateMemberAssignementForExisting")]
  154. public void CreateMemberAssignementForExisting_Succes()
  155. {
  156. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  157. MapperConfigurationCollectionContainer.Instance.Add(expected);
  158. ExpressionMapper.CreateMap<ClassSource2, ClassDest2>();
  159. expected.CreateMappingExpression(null);
  160. Assert.IsNotNull(expected.GetDelegateForExistingTargetTest());
  161. }
  162. }
  163. }