PageRenderTime 32ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Unity/Tests/Unity.Tests/RegistrationByConventionHelpersFixture.cs

http://unity.codeplex.com
C# | 225 lines | 187 code | 34 blank | 4 comment | 0 complexity | e5ae3c6758a33ee8cdfb6a9e55ebdacd MD5 | raw file
  1. // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Microsoft.Practices.Unity.TestSupport;
  6. #if NETFX_CORE
  7. using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
  8. #elif __IOS__
  9. using NUnit.Framework;
  10. using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
  11. using TestMethodAttribute = NUnit.Framework.TestAttribute;
  12. using TestInitializeAttribute = NUnit.Framework.TestFixtureSetUpAttribute;
  13. #else
  14. using Microsoft.VisualStudio.TestTools.UnitTesting;
  15. #endif
  16. namespace Microsoft.Practices.Unity.Tests
  17. {
  18. [TestClass]
  19. public class RegistrationByConventionHelpersFixture
  20. {
  21. [TestMethod]
  22. public void GetsNoTypes()
  23. {
  24. WithMappings.None(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
  25. WithMappings.None(typeof(DisposableType)).AssertHasNoItems();
  26. WithMappings.None(typeof(TestObject)).AssertHasNoItems();
  27. WithMappings.None(typeof(AnotherTestObject)).AssertHasNoItems();
  28. }
  29. [TestMethod]
  30. public void GetsAllInterfaces()
  31. {
  32. WithMappings.FromAllInterfaces(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
  33. WithMappings.FromAllInterfaces(typeof(DisposableType)).AssertHasNoItems();
  34. WithMappings.FromAllInterfaces(typeof(TestObject)).AssertContainsInAnyOrder(typeof(IAnotherInterface), typeof(ITestObject), typeof(IComparable));
  35. WithMappings.FromAllInterfaces(typeof(AnotherTestObject)).AssertContainsInAnyOrder(typeof(IAnotherInterface), typeof(ITestObject), typeof(IComparable));
  36. // Generics
  37. WithMappings.FromAllInterfaces(typeof(GenericTestObject<,>)).AssertContainsInAnyOrder(typeof(IGenericTestObject<,>));
  38. WithMappings.FromAllInterfaces(typeof(GenericTestObjectAlt<,>)).AssertHasNoItems();
  39. WithMappings.FromAllInterfaces(typeof(GenericTestObject<>)).AssertContainsInAnyOrder(typeof(IComparable<>));
  40. WithMappings.FromAllInterfaces(typeof(GenericTestObject)).AssertContainsInAnyOrder(typeof(IGenericTestObject<string, int>), typeof(IComparable<int>), typeof(IEnumerable<IList<string>>), typeof(IEnumerable));
  41. }
  42. [TestMethod]
  43. public void GetsMatchingInterface()
  44. {
  45. WithMappings.FromMatchingInterface(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
  46. WithMappings.FromMatchingInterface(typeof(DisposableType)).AssertHasNoItems();
  47. WithMappings.FromMatchingInterface(typeof(TestObject)).AssertContainsInAnyOrder(typeof(ITestObject));
  48. WithMappings.FromMatchingInterface(typeof(AnotherTestObject)).AssertHasNoItems();
  49. // Generics
  50. WithMappings.FromMatchingInterface(typeof(GenericTestObject<,>)).AssertContainsExactly(typeof(IGenericTestObject<,>));
  51. WithMappings.FromMatchingInterface(typeof(GenericTestObjectAlt<,>)).AssertHasNoItems();
  52. WithMappings.FromMatchingInterface(typeof(GenericTestObject<>)).AssertHasNoItems();
  53. WithMappings.FromMatchingInterface(typeof(GenericTestObject)).AssertHasNoItems();
  54. }
  55. [TestMethod]
  56. public void GetsAllInterfacesInSameAssembly()
  57. {
  58. WithMappings.FromAllInterfacesInSameAssembly(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
  59. WithMappings.FromAllInterfacesInSameAssembly(typeof(DisposableType)).AssertHasNoItems();
  60. WithMappings.FromAllInterfacesInSameAssembly(typeof(TestObject)).AssertContainsInAnyOrder(typeof(ITestObject), typeof(IAnotherInterface));
  61. WithMappings.FromAllInterfacesInSameAssembly(typeof(AnotherTestObject)).AssertContainsInAnyOrder(typeof(ITestObject), typeof(IAnotherInterface));
  62. // Generics
  63. WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObject<,>)).AssertContainsExactly(typeof(IGenericTestObject<,>));
  64. WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObjectAlt<,>)).AssertHasNoItems();
  65. WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObject<>)).AssertHasNoItems();
  66. WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObject)).AssertContainsExactly(typeof(IGenericTestObject<string, int>));
  67. }
  68. [TestMethod]
  69. public void GetsNames()
  70. {
  71. Assert.AreEqual("MockLogger", WithName.TypeName(typeof(MockLogger)));
  72. Assert.AreEqual("List`1", WithName.TypeName(typeof(List<>)));
  73. Assert.IsNull(WithName.Default(typeof(MockLogger)));
  74. Assert.IsNull(WithName.Default(typeof(List<>)));
  75. }
  76. [TestMethod]
  77. public void GetsLifetimeManagers()
  78. {
  79. Assert.IsInstanceOfType(WithLifetime.ContainerControlled(typeof(MockLogger)), typeof(ContainerControlledLifetimeManager));
  80. Assert.IsInstanceOfType(WithLifetime.ExternallyControlled(typeof(MockLogger)), typeof(ExternallyControlledLifetimeManager));
  81. Assert.IsInstanceOfType(WithLifetime.Hierarchical(typeof(MockLogger)), typeof(HierarchicalLifetimeManager));
  82. Assert.IsNull(WithLifetime.None(typeof(MockLogger)));
  83. Assert.IsInstanceOfType(WithLifetime.PerResolve(typeof(MockLogger)), typeof(PerResolveLifetimeManager));
  84. Assert.IsInstanceOfType(WithLifetime.Transient(typeof(MockLogger)), typeof(TransientLifetimeManager));
  85. Assert.IsInstanceOfType(WithLifetime.Custom<CustomLifetimeManager>(typeof(MockLogger)), typeof(CustomLifetimeManager));
  86. #if !NETFX_CORE
  87. Assert.IsInstanceOfType(WithLifetime.PerThread(typeof(MockLogger)), typeof(PerThreadLifetimeManager));
  88. #endif
  89. }
  90. public class CustomLifetimeManager : LifetimeManager
  91. {
  92. public override object GetValue()
  93. {
  94. throw new NotImplementedException();
  95. }
  96. public override void SetValue(object newValue)
  97. {
  98. throw new NotImplementedException();
  99. }
  100. public override void RemoveValue()
  101. {
  102. throw new NotImplementedException();
  103. }
  104. }
  105. public class TypeWithoutInterfaces { }
  106. public class DisposableType : IDisposable
  107. {
  108. public void Dispose()
  109. {
  110. throw new NotImplementedException();
  111. }
  112. }
  113. public class TestObject : IAnotherInterface, ITestObject, IDisposable, IComparable
  114. {
  115. public void Dispose()
  116. {
  117. throw new NotImplementedException();
  118. }
  119. public int CompareTo(object obj)
  120. {
  121. throw new NotImplementedException();
  122. }
  123. }
  124. public class AnotherTestObject : IAnotherInterface, ITestObject, IDisposable, IComparable
  125. {
  126. public void Dispose()
  127. {
  128. throw new NotImplementedException();
  129. }
  130. public int CompareTo(object obj)
  131. {
  132. throw new NotImplementedException();
  133. }
  134. }
  135. public interface ITestObject { }
  136. public interface IAnotherInterface { }
  137. public interface IGenericTestObject<T, U> { }
  138. public class GenericTestObject<T, U> : IGenericTestObject<T, U>, IComparable<T>, IEnumerable<IList<T>>
  139. {
  140. public int CompareTo(T other)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. public IEnumerator<IList<T>> GetEnumerator()
  145. {
  146. throw new NotImplementedException();
  147. }
  148. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  149. {
  150. throw new NotImplementedException();
  151. }
  152. }
  153. public class GenericTestObjectAlt<T, U> : IGenericTestObject<U, T>
  154. {
  155. }
  156. public class GenericTestObject<T> : IGenericTestObject<T, int>, IComparable, IEnumerable<IList<int>>, IComparable<T>
  157. {
  158. public int CompareTo(object obj)
  159. {
  160. throw new NotImplementedException();
  161. }
  162. public int CompareTo(T other)
  163. {
  164. throw new NotImplementedException();
  165. }
  166. public IEnumerator<IList<int>> GetEnumerator()
  167. {
  168. throw new NotImplementedException();
  169. }
  170. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  171. {
  172. throw new NotImplementedException();
  173. }
  174. }
  175. public class GenericTestObject : IGenericTestObject<string, int>, IComparable<int>, IEnumerable<IList<string>>
  176. {
  177. public int CompareTo(int other)
  178. {
  179. throw new NotImplementedException();
  180. }
  181. public IEnumerator<IList<string>> GetEnumerator()
  182. {
  183. throw new NotImplementedException();
  184. }
  185. IEnumerator IEnumerable.GetEnumerator()
  186. {
  187. throw new NotImplementedException();
  188. }
  189. }
  190. }
  191. }