MapperConfigurationBaseTests.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. namespace Masuit.Tools.UnitTest.Mapping.Core
  9. {
  10. [TestClass]
  11. public class MapperConfigurationBaseTests
  12. {
  13. [TestMethod, TestCategory("Constructor")]
  14. public void NewMapperConfigurationBase_SetProperties()
  15. {
  16. MapperConfigurationBase actual = new MapperConfiguration<ClassSource, ClassDest>("sourceTest");
  17. Assert.IsNotNull(actual.MemberToMapForNew);
  18. Assert.AreEqual(actual.TargetType, typeof(ClassDest));
  19. Assert.AreEqual(actual.SourceType, typeof(ClassSource));
  20. }
  21. [TestMethod, TestCategory("GetDestinationType")]
  22. public void GetDestinationType_WithoutServiceConstructor()
  23. {
  24. var mapper = new MapperConfiguration<ClassSource, ClassDest>("sourceTest");
  25. var actual = mapper.GetDestinationType();
  26. Assert.AreEqual(actual, typeof(ClassDest));
  27. }
  28. [TestMethod, TestCategory("GetDestinationType")]
  29. public void GetDestinationType_WithServiceConstructor()
  30. {
  31. ExpressionMapper.ConstructServicesUsing((x) => new ClassDest2());
  32. var mapper = ExpressionMapper.CreateMap<ClassSource2, IClassDest2>().ConstructUsingServiceLocator();
  33. ExpressionMapper.Initialize();
  34. var actual = mapper.GetDestinationType();
  35. Assert.AreEqual(actual, typeof(ClassDest2));
  36. ExpressionMapper.Reset();
  37. }
  38. [TestMethod, TestCategory("GetDelegate"), ExpectedException(typeof(MapperNotInitializedException))]
  39. public void GetDelegate_MapperNotInitialise_Exception()
  40. {
  41. MapperConfigurationBase mapper = new MapperConfiguration<ClassSource, ClassDest>("sourceTest");
  42. mapper.GetDelegate();
  43. }
  44. [TestMethod, TestCategory("CheckAndConfigureTuple")]
  45. public void CheckAndConfigureMappingTest_List_NotSameType_Success()
  46. {
  47. ExpressionMapper.Reset();
  48. ExpressionMapper.CreateMap<ClassSource2, ClassDest2>();
  49. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  50. MapperConfigurationCollectionContainer.Instance.Add(expected);
  51. ExpressionMapper.Initialize();
  52. Expression<Func<ClassSource, object>> source = s => s.ListProp;
  53. Expression<Func<ClassDest, object>> target = d => d.ListProp;
  54. Tuple<Expression, Expression, bool, string> tuple = Tuple.Create(source.Body, target.Body, true, string.Empty);
  55. expected.CheckAndConfigureMappingTest(tuple);
  56. Assert.IsNotNull(expected.GetDelegate());
  57. }
  58. [TestMethod, TestCategory("CheckAndConfigureTuple")]
  59. public void CheckAndConfigureMappingTest_List_SameType_Success()
  60. {
  61. ExpressionMapper.Reset();
  62. ExpressionMapper.CreateMap<ClassSource2, ClassDest2>();
  63. MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();
  64. MapperConfigurationCollectionContainer.Instance.Add(expected);
  65. ExpressionMapper.Initialize();
  66. Expression<Func<ClassSource, object>> source = s => s.ListString;
  67. Expression<Func<ClassDest, object>> target = d => d.ListString;
  68. Tuple<Expression, Expression, bool, string> tuple = Tuple.Create(source.Body, target.Body, false, string.Empty);
  69. expected.CheckAndConfigureMappingTest(tuple);
  70. Assert.IsNotNull(expected.GetDelegate());
  71. }
  72. }
  73. }