PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/UnitTests/ConfigurationValidation.cs

https://github.com/ngbrown/AutoMapper
C# | 497 lines | 417 code | 80 blank | 0 comment | 0 complexity | b9c7aabee34e2b4872abdd45b7cee5d5 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using Should;
  4. using Xunit;
  5. namespace AutoMapper.UnitTests
  6. {
  7. namespace ConfigurationValidation
  8. {
  9. public class When_testing_a_dto_with_mismatched_members : NonValidatingSpecBase
  10. {
  11. public class ModelObject
  12. {
  13. public string Foo { get; set; }
  14. public string Barr { get; set; }
  15. }
  16. public class ModelDto
  17. {
  18. public string Foo { get; set; }
  19. public string Bar { get; set; }
  20. }
  21. public class ModelObject2
  22. {
  23. public string Foo { get; set; }
  24. public string Barr { get; set; }
  25. }
  26. public class ModelDto2
  27. {
  28. public string Foo { get; set; }
  29. public string Bar { get; set; }
  30. public string Bar1 { get; set; }
  31. public string Bar2 { get; set; }
  32. public string Bar3 { get; set; }
  33. public string Bar4 { get; set; }
  34. }
  35. public class ModelObject3
  36. {
  37. public string Foo { get; set; }
  38. public string Bar { get; set; }
  39. public string Bar1 { get; set; }
  40. public string Bar2 { get; set; }
  41. public string Bar3 { get; set; }
  42. public string Bar4 { get; set; }
  43. }
  44. public class ModelDto3
  45. {
  46. public string Foo { get; set; }
  47. public string Bar { get; set; }
  48. }
  49. protected override void Establish_context()
  50. {
  51. Mapper.Initialize(cfg =>
  52. {
  53. cfg.CreateMap<ModelObject, ModelDto>();
  54. cfg.CreateMap<ModelObject2, ModelDto2>();
  55. cfg.CreateMap<ModelObject3, ModelDto3>(MemberList.Source);
  56. });
  57. }
  58. [Fact]
  59. public void Should_fail_a_configuration_check()
  60. {
  61. typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
  62. }
  63. }
  64. public class When_testing_a_dto_with_fully_mapped_and_custom_matchers : NonValidatingSpecBase
  65. {
  66. public class ModelObject
  67. {
  68. public string Foo { get; set; }
  69. public string Barr { get; set; }
  70. }
  71. public class ModelDto
  72. {
  73. public string Foo { get; set; }
  74. public string Bar { get; set; }
  75. }
  76. protected override void Establish_context()
  77. {
  78. Mapper
  79. .CreateMap<ModelObject, ModelDto>()
  80. .ForMember(dto => dto.Bar, opt => opt.MapFrom(m => m.Barr));
  81. }
  82. [Fact]
  83. public void Should_pass_an_inspection_of_missing_mappings()
  84. {
  85. Mapper.AssertConfigurationIsValid();
  86. }
  87. }
  88. public class When_testing_a_dto_with_matching_member_names_but_mismatched_types : NonValidatingSpecBase
  89. {
  90. public class Source
  91. {
  92. public decimal Value { get; set; }
  93. }
  94. public class Destination
  95. {
  96. public Type Value { get; set; }
  97. }
  98. protected override void Establish_context()
  99. {
  100. Mapper.CreateMap<Source, Destination>();
  101. }
  102. [Fact]
  103. public void Should_fail_a_configuration_check()
  104. {
  105. typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
  106. }
  107. }
  108. public class When_testing_a_dto_with_member_type_mapped_mappings : AutoMapperSpecBase
  109. {
  110. private AutoMapperConfigurationException _exception;
  111. public class Source
  112. {
  113. public int Value { get; set; }
  114. public OtherSource Other { get; set; }
  115. }
  116. public class OtherSource
  117. {
  118. public int Value { get; set; }
  119. }
  120. public class Destination
  121. {
  122. public int Value { get; set; }
  123. public OtherDest Other { get; set; }
  124. }
  125. public class OtherDest
  126. {
  127. public int Value { get; set; }
  128. }
  129. protected override void Establish_context()
  130. {
  131. Mapper.CreateMap<Source, Destination>();
  132. Mapper.CreateMap<OtherSource, OtherDest>();
  133. }
  134. protected override void Because_of()
  135. {
  136. try
  137. {
  138. Mapper.AssertConfigurationIsValid();
  139. }
  140. catch (AutoMapperConfigurationException ex)
  141. {
  142. _exception = ex;
  143. }
  144. }
  145. [Fact]
  146. public void Should_pass_a_configuration_check()
  147. {
  148. _exception.ShouldBeNull();
  149. }
  150. }
  151. public class When_testing_a_dto_with_matched_members_but_mismatched_types_that_are_ignored : AutoMapperSpecBase
  152. {
  153. private AutoMapperConfigurationException _exception;
  154. public class ModelObject
  155. {
  156. public string Foo { get; set; }
  157. public string Bar { get; set; }
  158. }
  159. public class ModelDto
  160. {
  161. public string Foo { get; set; }
  162. public int Bar { get; set; }
  163. }
  164. protected override void Establish_context()
  165. {
  166. Mapper.CreateMap<ModelObject, ModelDto>()
  167. .ForMember(dest => dest.Bar, opt => opt.Ignore());
  168. }
  169. protected override void Because_of()
  170. {
  171. try
  172. {
  173. Mapper.AssertConfigurationIsValid();
  174. }
  175. catch (AutoMapperConfigurationException ex)
  176. {
  177. _exception = ex;
  178. }
  179. }
  180. [Fact]
  181. public void Should_pass_a_configuration_check()
  182. {
  183. _exception.ShouldBeNull();
  184. }
  185. }
  186. public class When_testing_a_dto_with_array_types_with_mismatched_element_types : NonValidatingSpecBase
  187. {
  188. public class Source
  189. {
  190. public SourceItem[] Items;
  191. }
  192. public class Destination
  193. {
  194. public DestinationItem[] Items;
  195. }
  196. public class SourceItem
  197. {
  198. }
  199. public class DestinationItem
  200. {
  201. }
  202. protected override void Establish_context()
  203. {
  204. Mapper.CreateMap<Source, Destination>();
  205. }
  206. [Fact]
  207. public void Should_fail_a_configuration_check()
  208. {
  209. typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
  210. }
  211. }
  212. public class When_testing_a_dto_with_list_types_with_mismatched_element_types : NonValidatingSpecBase
  213. {
  214. public class Source
  215. {
  216. public List<SourceItem> Items;
  217. }
  218. public class Destination
  219. {
  220. public List<DestinationItem> Items;
  221. }
  222. public class SourceItem
  223. {
  224. }
  225. public class DestinationItem
  226. {
  227. }
  228. protected override void Establish_context()
  229. {
  230. Mapper.CreateMap<Source, Destination>();
  231. }
  232. [Fact]
  233. public void Should_fail_a_configuration_check()
  234. {
  235. typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
  236. }
  237. }
  238. public class When_testing_a_dto_with_readonly_members : NonValidatingSpecBase
  239. {
  240. public class Source
  241. {
  242. public int Value { get; set; }
  243. }
  244. public class Destination
  245. {
  246. public int Value { get; set; }
  247. public string ValuePlusOne { get { return (Value + 1).ToString(); } }
  248. public int ValuePlusTwo { get { return Value + 2; } }
  249. }
  250. protected override void Establish_context()
  251. {
  252. Mapper.CreateMap<Source, Destination>();
  253. }
  254. protected override void Because_of()
  255. {
  256. Mapper.Map<Source, Destination>(new Source { Value = 5 });
  257. }
  258. [Fact]
  259. public void Should_be_valid()
  260. {
  261. typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(Mapper.AssertConfigurationIsValid);
  262. }
  263. }
  264. public class When_testing_a_dto_in_a_specfic_profile : NonValidatingSpecBase
  265. {
  266. public class GoodSource
  267. {
  268. public int Value { get; set; }
  269. }
  270. public class GoodDest
  271. {
  272. public int Value { get; set; }
  273. }
  274. public class BadDest
  275. {
  276. public int Valufffff { get; set; }
  277. }
  278. protected override void Because_of()
  279. {
  280. Mapper.Initialize(cfg =>
  281. {
  282. cfg.CreateProfile("Good", profile =>
  283. {
  284. profile.CreateMap<GoodSource, GoodDest>();
  285. });
  286. cfg.CreateProfile("Bad", profile =>
  287. {
  288. profile.CreateMap<GoodSource, BadDest>();
  289. });
  290. });
  291. }
  292. [Fact]
  293. public void Should_ignore_bad_dtos_in_other_profiles()
  294. {
  295. typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(() => Mapper.AssertConfigurationIsValid("Good"));
  296. }
  297. }
  298. public class When_testing_a_dto_with_mismatched_custom_member_mapping : NonValidatingSpecBase
  299. {
  300. public class SubBarr { }
  301. public class SubBar { }
  302. public class ModelObject
  303. {
  304. public string Foo { get; set; }
  305. public SubBarr Barr { get; set; }
  306. }
  307. public class ModelDto
  308. {
  309. public string Foo { get; set; }
  310. public SubBar Bar { get; set; }
  311. }
  312. protected override void Establish_context()
  313. {
  314. Mapper.CreateMap<ModelObject, ModelDto>()
  315. .ForMember(dest => dest.Bar, opt => opt.MapFrom(src => src.Barr));
  316. }
  317. [Fact]
  318. public void Should_fail_a_configuration_check()
  319. {
  320. typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
  321. }
  322. }
  323. public class When_testing_a_dto_with_value_specified_members : NonValidatingSpecBase
  324. {
  325. public class Source { }
  326. public class Destination
  327. {
  328. public int Value { get; set; }
  329. }
  330. protected override void Establish_context()
  331. {
  332. object i = 7;
  333. Mapper.Initialize(cfg =>
  334. {
  335. cfg.CreateMap<Source, Destination>()
  336. .ForMember(dest => dest.Value, opt => opt.UseValue(i));
  337. });
  338. }
  339. [Fact]
  340. public void Should_validate_successfully()
  341. {
  342. typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(Mapper.AssertConfigurationIsValid);
  343. }
  344. }
  345. public class When_testing_a_dto_with_setter_only_peroperty_member : NonValidatingSpecBase
  346. {
  347. public class Source
  348. {
  349. public string Value { set { } }
  350. }
  351. public class Destination
  352. {
  353. public string Value { get; set; }
  354. }
  355. protected override void Establish_context()
  356. {
  357. Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());
  358. }
  359. [Fact]
  360. public void Should_fail_a_configuration_check()
  361. {
  362. typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
  363. }
  364. }
  365. public class When_testing_a_dto_with_matching_void_method_member : NonValidatingSpecBase
  366. {
  367. public class Source
  368. {
  369. public void Method()
  370. {
  371. }
  372. }
  373. public class Destination
  374. {
  375. public string Method { get; set; }
  376. }
  377. protected override void Establish_context()
  378. {
  379. Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());
  380. }
  381. [Fact]
  382. public void Should_fail_a_configuration_check()
  383. {
  384. typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
  385. }
  386. }
  387. public class When_redirecting_types : NonValidatingSpecBase
  388. {
  389. protected override void Establish_context()
  390. {
  391. Mapper.Initialize(cfg =>
  392. {
  393. cfg.CreateMap<ConcreteSource, ConcreteDest>()
  394. .ForMember(d => d.DifferentName, opt => opt.MapFrom(s => s.Name));
  395. cfg.CreateMap<ConcreteSource, IAbstractDest>().As<ConcreteDest>();
  396. });
  397. }
  398. [Fact]
  399. public void Should_pass_configuration_check()
  400. {
  401. typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(Mapper.AssertConfigurationIsValid);
  402. }
  403. class ConcreteSource
  404. {
  405. public string Name { get; set; }
  406. }
  407. class ConcreteDest : IAbstractDest
  408. {
  409. public string DifferentName { get; set; }
  410. }
  411. interface IAbstractDest
  412. {
  413. string DifferentName { get; set; }
  414. }
  415. }
  416. }
  417. }