PageRenderTime 3832ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/AutoFixtureUnitTest/Kernel/ConstructorInvokerTest.cs

http://autofixture.codeplex.com
C# | 256 lines | 185 code | 21 blank | 50 comment | 12 complexity | 917cb755b11d3875a82c2d109662031a MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Ploeh.AutoFixture.Kernel;
  6. using Ploeh.TestTypeFoundation;
  7. using Xunit;
  8. namespace Ploeh.AutoFixtureUnitTest.Kernel
  9. {
  10. #pragma warning disable 618
  11. public class ConstructorInvokerTest
  12. {
  13. [Fact]
  14. public void SutIsSpecimenBuilder()
  15. {
  16. // Fixture setup
  17. // Exercise system
  18. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  19. // Verify outcome
  20. Assert.IsAssignableFrom<ISpecimenBuilder>(sut);
  21. // Teardown
  22. }
  23. [Fact]
  24. public void InitializeWithNullPickerThrows()
  25. {
  26. // Fixture setup
  27. // Exercise system and verify outcome
  28. Assert.Throws<ArgumentNullException>(() =>
  29. new ConstructorInvoker(null));
  30. // Teardown
  31. }
  32. [Fact]
  33. public void QueryIsCorrect()
  34. {
  35. // Fixture setup
  36. var expectedPicker = new DelegatingConstructorQuery();
  37. var sut = new ConstructorInvoker(expectedPicker);
  38. // Exercise system
  39. IConstructorQuery result = sut.Query;
  40. // Verify outcome
  41. Assert.Equal(expectedPicker, result);
  42. // Teardown
  43. }
  44. [Fact]
  45. public void CreateWithNullRequestWillReturnNull()
  46. {
  47. // Fixture setup
  48. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  49. // Exercise system
  50. var dummyContainer = new DelegatingSpecimenContext();
  51. var result = sut.Create(null, dummyContainer);
  52. // Verify outcome
  53. var expectedResult = new NoSpecimen();
  54. Assert.Equal(expectedResult, result);
  55. // Teardown
  56. }
  57. [Fact]
  58. public void CreateWithNullContainerWillThrow()
  59. {
  60. // Fixture setup
  61. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  62. var dummyRequest = new object();
  63. // Exercise system and verify outcome
  64. Assert.Throws<ArgumentNullException>(() =>
  65. sut.Create(dummyRequest, null));
  66. // Teardown
  67. }
  68. [Fact]
  69. public void CreateFromNonTypeRequestWillReturnCorrectResult()
  70. {
  71. // Fixture setup
  72. var nonTypeRequest = new object();
  73. var dummyContainer = new DelegatingSpecimenContext();
  74. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  75. // Exercise system
  76. var result = sut.Create(nonTypeRequest, dummyContainer);
  77. // Verify outcome
  78. var expectedResult = new NoSpecimen(nonTypeRequest);
  79. Assert.Equal(expectedResult, result);
  80. // Teardown
  81. }
  82. [Fact]
  83. public void CreateReturnsCorrectResultWhenQueryReturnsEmptyConstructors()
  84. {
  85. // Fixture setup
  86. var dummyRequest = typeof(object);
  87. var query = new DelegatingConstructorQuery { OnSelectConstructors = t => Enumerable.Empty<IMethod>() };
  88. var sut = new ConstructorInvoker(query);
  89. // Exercise system
  90. var dummyContext = new DelegatingSpecimenContext();
  91. var result = sut.Create(dummyRequest, dummyContext);
  92. // Verify outcome
  93. var expectedResult = new NoSpecimen(dummyRequest);
  94. Assert.Equal(expectedResult, result);
  95. // Teardown
  96. }
  97. [Fact]
  98. public void CreateFromTypeRequestWhenContainerCannotSatisfyParameterRequestWillReturnCorrectResult()
  99. {
  100. // Fixture setup
  101. var type = typeof(string);
  102. var container = new DelegatingSpecimenContext { OnResolve = r => new NoSpecimen(type) };
  103. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  104. // Exercise system
  105. var result = sut.Create(type, container);
  106. // Verify outcome
  107. var expectedResult = new NoSpecimen(type);
  108. Assert.Equal(expectedResult, result);
  109. // Teardown
  110. }
  111. [Fact]
  112. public void CreateFromTypeWithNoPublicConstructorWhenContainerCanSatisfyRequestReturnsCorrectResult()
  113. {
  114. // Fixture setup
  115. var container = new DelegatingSpecimenContext { OnResolve = r => new object() };
  116. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  117. // Exercise system
  118. var result = sut.Create(typeof(AbstractType), container);
  119. // Verify outcome
  120. var expectedResult = new NoSpecimen(typeof(AbstractType));
  121. Assert.Equal(expectedResult, result);
  122. // Teardown
  123. }
  124. [Fact]
  125. public void CreateFromTypeWhenParentCanGenerateOneParameterButNotTheOtherWillReturnCorrectNull()
  126. {
  127. // Fixture setup
  128. var requestedType = typeof(DoubleParameterType<string, int>);
  129. var parameters = requestedType.GetConstructors().Single().GetParameters();
  130. var container = new DelegatingSpecimenContext { OnResolve = r => parameters[0] == r ? new object() : new NoSpecimen(r) };
  131. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  132. // Exercise system
  133. var result = sut.Create(requestedType, container);
  134. // Verify outcome
  135. Assert.IsAssignableFrom<NoSpecimen>(result);
  136. // Teardown
  137. }
  138. [Fact]
  139. public void CreateFromTypeWhenParentCanGenerateBothParametersWillReturnCorrectResult()
  140. {
  141. // Fixture setup
  142. var expectedParameterValues = new object[] { 1, 2m };
  143. var parameterQueue = new Queue<object>(expectedParameterValues);
  144. var requestedType = typeof(DoubleParameterType<int, decimal>);
  145. var parameters = requestedType.GetConstructors().Single().GetParameters();
  146. var container = new DelegatingSpecimenContext();
  147. container.OnResolve = r =>
  148. {
  149. if (parameters.Any(r.Equals))
  150. {
  151. return parameterQueue.Dequeue();
  152. }
  153. return null;
  154. };
  155. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  156. // Exercise system
  157. var result = sut.Create(requestedType, container);
  158. // Verify outcome
  159. var actual = (DoubleParameterType<int, decimal>)result;
  160. Assert.Equal(expectedParameterValues[0], actual.Parameter1);
  161. Assert.Equal(expectedParameterValues[1], actual.Parameter2);
  162. // Teardown
  163. }
  164. [Fact]
  165. public void CreateFromTypeWillInvokeContainerCorrectly()
  166. {
  167. // Fixture setup
  168. var requestedType = typeof(DoubleParameterType<long, short>);
  169. var parameters = requestedType.GetConstructors().Single().GetParameters();
  170. var mockVerified = false;
  171. var contextMock = new DelegatingSpecimenContext();
  172. contextMock.OnResolve = r =>
  173. {
  174. if (parameters.Any(r.Equals))
  175. {
  176. mockVerified = true;
  177. var pType = ((ParameterInfo)r).ParameterType;
  178. if (typeof(long) == pType)
  179. {
  180. return new long();
  181. }
  182. if (typeof(short) == pType)
  183. {
  184. return new short();
  185. }
  186. }
  187. throw new ArgumentException("Unexpected container request.", "r");
  188. };
  189. var sut = new ConstructorInvoker(new ModestConstructorQuery());
  190. // Exercise system
  191. sut.Create(requestedType, contextMock);
  192. // Verify outcome
  193. Assert.True(mockVerified, "Mock verification");
  194. // Teardown
  195. }
  196. [Fact]
  197. public void CreateFromTypeWillUseFirstConstructorItCanSatisfy()
  198. {
  199. // Fixture setup
  200. var requestedType = typeof(MultiUnorderedConstructorType);
  201. var ctor1 = requestedType.GetConstructor(new[] { typeof(MultiUnorderedConstructorType.ParameterObject) });
  202. var ctor2 = requestedType.GetConstructor(new[] { typeof(string), typeof(int) });
  203. var picker = new DelegatingConstructorQuery { OnSelectConstructors = t => new IMethod[] { new ConstructorMethod(ctor1), new ConstructorMethod(ctor2) } };
  204. var sut = new ConstructorInvoker(picker);
  205. var ctor2Params = ctor2.GetParameters();
  206. var expectedText = "Anonymous text";
  207. var expectedNumber = 14;
  208. var context = new DelegatingSpecimenContext();
  209. context.OnResolve = r =>
  210. {
  211. if (ctor2Params.Any(r.Equals))
  212. {
  213. var pType = ((ParameterInfo)r).ParameterType;
  214. if (typeof(string) == pType)
  215. {
  216. return expectedText;
  217. }
  218. if (typeof(int) == pType)
  219. {
  220. return expectedNumber;
  221. }
  222. }
  223. return new NoSpecimen(r);
  224. };
  225. // Exercise system
  226. var result = sut.Create(requestedType, context);
  227. // Verify outcome
  228. var actual = Assert.IsAssignableFrom<MultiUnorderedConstructorType>(result);
  229. Assert.Equal(expectedText, actual.Text);
  230. Assert.Equal(expectedNumber, actual.Number);
  231. // Teardown
  232. }
  233. }
  234. #pragma warning restore 618
  235. }