/src/Ninject.Extensions.Factory.Test/UnitTests/ArgumentPositionCalculatorTests.cs

https://github.com/ninject/ninject.extensions.factory · C# · 199 lines · 144 code · 35 blank · 20 comment · 1 complexity · 83dec6aae458a44fbf216170d12298a8 MD5 · raw file

  1. //-------------------------------------------------------------------------------
  2. // <copyright file="ArgumentPositionCalculatorTests.cs" company="Ninject Project Contributors">
  3. // Copyright (c) 2009-2011 Ninject Project Contributors
  4. // Authors: Remo Gloor (remo.gloor@gmail.com)
  5. //
  6. // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
  7. // you may not use this file except in compliance with one of the Licenses.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. // or
  12. // http://www.microsoft.com/opensource/licenses.mspx
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS,
  16. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. // See the License for the specific language governing permissions and
  18. // limitations under the License.
  19. // </copyright>
  20. //-------------------------------------------------------------------------------
  21. #if !NO_MOQ
  22. namespace Ninject.Extensions.Factory.UnitTests
  23. {
  24. using System;
  25. using FluentAssertions;
  26. using Moq;
  27. using Ninject.Activation;
  28. using Ninject.Parameters;
  29. using Ninject.Planning.Targets;
  30. using Xunit;
  31. using Xunit.Extensions;
  32. public class ArgumentPositionCalculatorTests
  33. {
  34. private readonly ArgumentPositionCalculator testee;
  35. public ArgumentPositionCalculatorTests()
  36. {
  37. this.testee = new ArgumentPositionCalculator();
  38. }
  39. [Fact]
  40. public void GetPositionOfFuncConstructorAgumentReturnsMinusOneIfNotFound()
  41. {
  42. var funcConstructorArgument = new FuncConstructorArgument(typeof(int), 2, null);
  43. var context = CreateContext();
  44. var target = CreateTarget(typeof(int), "x");
  45. var value = this.testee.GetPositionOfFuncConstructorArgument(funcConstructorArgument, context, target);
  46. value.Should().Be(-1);
  47. }
  48. [Fact]
  49. public void GetPositionOfFuncConstructorAgumentReturnsZeroIfTheOnlyArgument()
  50. {
  51. var funcConstructorArgument = new FuncConstructorArgument(typeof(int), 2, null);
  52. var context = CreateContext(funcConstructorArgument);
  53. var target = CreateTarget(typeof(int), "x");
  54. var value = this.testee.GetPositionOfFuncConstructorArgument(funcConstructorArgument, context, target);
  55. value.Should().Be(0);
  56. }
  57. [Fact]
  58. public void GetPositionOfFuncConstructorAgumentReturnsPositionOfTheGivenInstanceWithinAllFuncConstructorArgumentsOfTheSameType()
  59. {
  60. var funcConstructorArgument = new FuncConstructorArgument(typeof(int), 2, null);
  61. var context = CreateContext(
  62. new FuncConstructorArgument(typeof(int), 2, null),
  63. new FuncConstructorArgument(typeof(int), 2, null),
  64. new FuncConstructorArgument(typeof(string), "e", null),
  65. new FuncConstructorArgument(typeof(double), 2, null),
  66. new FuncConstructorArgument(typeof(int), 2, null),
  67. funcConstructorArgument,
  68. new FuncConstructorArgument(typeof(int), 2, null),
  69. new FuncConstructorArgument(typeof(string), "w", null),
  70. new FuncConstructorArgument(typeof(int), 2, null));
  71. var target = CreateTarget(typeof(int), "x");
  72. var value = this.testee.GetPositionOfFuncConstructorArgument(funcConstructorArgument, context, target);
  73. value.Should().Be(3);
  74. }
  75. [Fact]
  76. public void GetPositionOfFuncConstructorAgumentIfNoneFuncConstructorArgumentAppliesToTargetMinusOneIsReturned()
  77. {
  78. var funcConstructorArgument = new FuncConstructorArgument(typeof(int), 2, null);
  79. var constsuctorArgumentMock = new Mock<IConstructorArgument>();
  80. var context = CreateContext(
  81. funcConstructorArgument,
  82. constsuctorArgumentMock.Object);
  83. var target = CreateTarget(typeof(int), "x");
  84. constsuctorArgumentMock.Setup(constsuctorArgument => constsuctorArgument.AppliesToTarget(context, target)).Returns(true);
  85. var value = this.testee.GetPositionOfFuncConstructorArgument(funcConstructorArgument, context, target);
  86. value.Should().Be(-1);
  87. }
  88. [Fact]
  89. public void GetPositionOfFuncConstructorAgumentNotAppliyingNoneFuncConstructorArgumentsAreIgnored()
  90. {
  91. var funcConstructorArgument = new FuncConstructorArgument(typeof(int), 2, null);
  92. var constsuctorArgumentMock = new Mock<IConstructorArgument>();
  93. var context = CreateContext(
  94. funcConstructorArgument,
  95. constsuctorArgumentMock.Object);
  96. var target = CreateTarget(typeof(int), "x");
  97. constsuctorArgumentMock.Setup(constsuctorArgument => constsuctorArgument.AppliesToTarget(context, target)).Returns(false);
  98. var value = this.testee.GetPositionOfFuncConstructorArgument(funcConstructorArgument, context, target);
  99. value.Should().Be(0);
  100. }
  101. [Theory]
  102. [InlineData("a", -1)]
  103. [InlineData("s", -1)]
  104. [InlineData("x", 0)]
  105. [InlineData("y", 1)]
  106. [InlineData("z", 2)]
  107. public void GetTargetPosition(string parameterName, int expectedPosition)
  108. {
  109. var context = CreateContext();
  110. var target = CreateTarget(typeof(int), parameterName);
  111. var position = this.testee.GetTargetPosition(context, target);
  112. position.Should().Be(expectedPosition);
  113. }
  114. [Fact]
  115. public void GetTargetPositionWhenOtherConstructorArgumentAppliesToSameParameterReturnsMinusOne()
  116. {
  117. var constructorArgumentMock = new Mock<IConstructorArgument>();
  118. var context = CreateContext(constructorArgumentMock.Object);
  119. var target = CreateTarget(typeof(int), "y");
  120. SetupAppliesToTarget(constructorArgumentMock, context, "y");
  121. var position = this.testee.GetTargetPosition(context, target);
  122. position.Should().Be(-1);
  123. }
  124. [Fact]
  125. public void GetTargetPositionWhenOtherConstructorArgumentAppliesToOtherParameterThenItIsIgnored()
  126. {
  127. var constructorArgumentMock = new Mock<IConstructorArgument>();
  128. var context = CreateContext(constructorArgumentMock.Object);
  129. var target = CreateTarget(typeof(int), "y");
  130. SetupAppliesToTarget(constructorArgumentMock, context, "x");
  131. var position = this.testee.GetTargetPosition(context, target);
  132. position.Should().Be(0);
  133. }
  134. private static void SetupAppliesToTarget(Mock<IConstructorArgument> constructorArgumentMock, IContext context, string parameterName)
  135. {
  136. constructorArgumentMock
  137. .Setup(constructorArgument => constructorArgument.AppliesToTarget(
  138. context,
  139. It.Is<ITarget>(t => t.Name == parameterName)))
  140. .Returns(true);
  141. }
  142. private static IContext CreateContext(params IParameter[] parameters)
  143. {
  144. var contextMock = new Mock<IContext>();
  145. contextMock.SetupGet(context => context.Parameters).Returns(parameters);
  146. return contextMock.Object;
  147. }
  148. private static ITarget CreateTarget(Type type, string name)
  149. {
  150. var targetMock = new Mock<ITarget>();
  151. targetMock.SetupGet(target => target.Type).Returns(type);
  152. targetMock.SetupGet(target => target.Name).Returns(name);
  153. targetMock.SetupGet(target => target.Member).Returns(typeof(TestClass).GetConstructors()[0]);
  154. return targetMock.Object;
  155. }
  156. public class TestClass
  157. {
  158. public TestClass(string s, int x, string t, string u, int y, int z)
  159. {
  160. }
  161. }
  162. }
  163. }
  164. #endif