DelegateCompositionTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.ComponentModel.Composition;
  6. using System.ComponentModel.Composition.Factories;
  7. using System.ComponentModel.Composition.Hosting;
  8. using System.ComponentModel.Composition.Primitives;
  9. using System.Linq;
  10. using System.UnitTesting;
  11. using Microsoft.VisualStudio.TestTools.UnitTesting;
  12. namespace Tests.Integration
  13. {
  14. [TestClass]
  15. public class DelegateCompositionTests
  16. {
  17. public delegate int SimpleDelegate();
  18. public delegate object DoWorkDelegate(int i, ref object o, out string s);
  19. public class MethodExporter
  20. {
  21. [Export]
  22. public int SimpleMethod()
  23. {
  24. return 1;
  25. }
  26. [Export(typeof(DoWorkDelegate))]
  27. public object DoWork(int i, ref object o, out string s)
  28. {
  29. s = "";
  30. return o;
  31. }
  32. [Export("ActionWith8Arguments")]
  33. [Export("ActionWith8Arguments", typeof(Delegate))]
  34. public void Action(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
  35. {
  36. }
  37. [Export("FunctionWith8Arguments")]
  38. [Export("FunctionWith8Arguments", typeof(Delegate))]
  39. public int Function(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
  40. {
  41. return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
  42. }
  43. [Export("ActionWith9Arguments")]
  44. [Export("ActionWith9Arguments", typeof(Delegate))]
  45. public void Action(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)
  46. {
  47. }
  48. [Export("FunctionWith9Arguments")]
  49. [Export("FunctionWith9Arguments", typeof(Delegate))]
  50. public int Function(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)
  51. {
  52. return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;
  53. }
  54. #if CLR40 && !SILVERLIGHT
  55. [Export("FunctionWithDefaultValue")]
  56. public int FunctionWithDefaultValue(int i, string s = "")
  57. {
  58. return i;
  59. }
  60. #endif
  61. }
  62. [TestMethod]
  63. public void Export_SimpleCustomDelegate_ShouldWork()
  64. {
  65. var container = ContainerFactory.CreateWithAttributedCatalog(
  66. typeof(MethodExporter));
  67. var contractName = AttributedModelServices.GetContractName(typeof(SimpleDelegate));
  68. var export1 = container.GetExportedValue<SimpleDelegate>();
  69. Assert.AreEqual(1, export1());
  70. var export2 = container.GetExportedValue<Func<int>>();
  71. Assert.AreEqual(1, export1());
  72. var export3 = (ExportedDelegate)container.GetExportedValue<object>(contractName);
  73. var export4 = (SimpleDelegate)export3.CreateDelegate(typeof(SimpleDelegate));
  74. Assert.AreEqual(1, export4());
  75. }
  76. [TestMethod]
  77. public void Export_CustomDelegateWithOutRefParams_ShouldWork()
  78. {
  79. var container = ContainerFactory.CreateWithAttributedCatalog(
  80. typeof(MethodExporter));
  81. var export1 = container.GetExportedValue<DoWorkDelegate>();
  82. int i = 0;
  83. object o = new object();
  84. string s;
  85. export1(i, ref o, out s);
  86. }
  87. [TestMethod]
  88. public void Export_FunctionWith8Arguments_ShouldWorkFine()
  89. {
  90. var container = ContainerFactory.CreateWithAttributedCatalog(
  91. typeof(MethodExporter));
  92. #if CLR40 && !SILVERLIGHT
  93. Assert.IsNotNull(container.GetExportedValue<Delegate>("FunctionWith8Arguments"));
  94. #else
  95. ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
  96. container.GetExportedValue<Delegate>("FunctionWith8Arguments"));
  97. #endif
  98. }
  99. public delegate int FuncWith9Args(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9);
  100. [TestMethod]
  101. public void Export_FuncWith9Arguments_ShouldThrowContractMismatch()
  102. {
  103. var container = ContainerFactory.CreateWithAttributedCatalog(
  104. typeof(MethodExporter));
  105. // Cannot Create a standard delegate that takes 9 arguments
  106. // so the generic Delegate type will not work
  107. ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
  108. container.GetExportedValue<Delegate>("FunctionWith9Arguments"));
  109. // If a matching custom delegate type is used then it will work
  110. Assert.IsNotNull(container.GetExportedValue<FuncWith9Args>("FunctionWith9Arguments"));
  111. }
  112. [TestMethod]
  113. public void Export_ActionWith8Arguments_ShouldWorkFine()
  114. {
  115. var container = ContainerFactory.CreateWithAttributedCatalog(
  116. typeof(MethodExporter));
  117. #if CLR40 && !SILVERLIGHT
  118. Assert.IsNotNull(container.GetExportedValue<Delegate>("ActionWith8Arguments"));
  119. #else
  120. ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
  121. container.GetExportedValue<Delegate>("ActionWith8Arguments"));
  122. #endif
  123. }
  124. public delegate void ActionWith9Args(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9);
  125. [TestMethod]
  126. public void Export_ActionWith9Arguments_ShouldThrowContractMismatch()
  127. {
  128. var container = ContainerFactory.CreateWithAttributedCatalog(
  129. typeof(MethodExporter));
  130. // Cannot Create a standard delegate that takes 9 arguments
  131. // so the generic Delegate type will not work
  132. ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
  133. container.GetExportedValue<Delegate>("ActionWith9Arguments"));
  134. // If a matching custom delegate type is used then it will work
  135. Assert.IsNotNull(container.GetExportedValue<ActionWith9Args>("ActionWith9Arguments"));
  136. }
  137. #if CLR40 && !SILVERLIGHT
  138. [TestMethod]
  139. public void Export_FunctionWithDefaultValue_ShouldWorkFine()
  140. {
  141. var container = ContainerFactory.CreateWithAttributedCatalog(
  142. typeof(MethodExporter));
  143. var export = container.GetExportedValue<Func<int, string, int>>("FunctionWithDefaultValue");
  144. Assert.AreEqual(3, export(3, "a"));
  145. // Even though the string argument is optional it still cannot be cast to Func<int, int>.
  146. var export2 = (ExportedDelegate)container.GetExportedValue<object>("FunctionWithDefaultValue");
  147. var export3 = export2.CreateDelegate(typeof(Func<int, int>));
  148. Assert.IsNull(export3);
  149. }
  150. #endif
  151. public delegate int DelegateOneArg(int i);
  152. public delegate int DelegateTwoArgs(int i, int j);
  153. public class CustomExportedDelegate : ExportedDelegate
  154. {
  155. private Func<int, int, int> _func;
  156. public CustomExportedDelegate(Func<int, int, int> func)
  157. {
  158. this._func = func;
  159. }
  160. public override Delegate CreateDelegate(Type delegateType)
  161. {
  162. if (delegateType == typeof(DelegateOneArg))
  163. {
  164. return (DelegateOneArg)((i) => this._func(i, 0));
  165. }
  166. else if (delegateType == typeof(DelegateTwoArgs))
  167. {
  168. return (DelegateTwoArgs)((i, j) => this._func(i, j));
  169. }
  170. return null;
  171. }
  172. }
  173. public class ExportCustomExportedDelegates
  174. {
  175. [Export("CustomExportedDelegate", typeof(DelegateOneArg))]
  176. [Export("CustomExportedDelegate", typeof(DelegateTwoArgs))]
  177. public ExportedDelegate MyExportedDelegate
  178. {
  179. get
  180. {
  181. return new CustomExportedDelegate(DoWork);
  182. }
  183. }
  184. public int DoWork(int i, int j)
  185. {
  186. return i + j;
  187. }
  188. }
  189. [Export]
  190. public class ImportCustomExportedDelegates
  191. {
  192. [Import("CustomExportedDelegate")]
  193. public DelegateOneArg DelegateOneArg { get; set; }
  194. [Import("CustomExportedDelegate")]
  195. public DelegateTwoArgs DelegateTwoArgs { get; set; }
  196. }
  197. [TestMethod]
  198. public void CustomExportedDelegate_ShouldWork()
  199. {
  200. var container = ContainerFactory.CreateWithAttributedCatalog(
  201. typeof(ExportCustomExportedDelegates),
  202. typeof(ImportCustomExportedDelegates));
  203. var importer = container.GetExportedValue<ImportCustomExportedDelegates>();
  204. Assert.AreEqual(1, importer.DelegateOneArg(1));
  205. Assert.AreEqual(2, importer.DelegateTwoArgs(1, 1));
  206. }
  207. public delegate void GetRef(ref int i);
  208. public delegate void GetOut(out int i);
  209. public class RefOutMethodExporter
  210. {
  211. [Export]
  212. public void MyGetOut(out int i)
  213. {
  214. i = 29;
  215. }
  216. }
  217. [TestMethod]
  218. public void MethodWithOutParam_ShouldWorkWithRefParam()
  219. {
  220. var container = ContainerFactory.CreateWithAttributedCatalog(
  221. typeof(RefOutMethodExporter));
  222. int i = 0;
  223. var export1 = container.GetExportedValue<GetRef>();
  224. export1(ref i);
  225. Assert.AreEqual(29, i);
  226. i = 0;
  227. var export2 = container.GetExportedValue<GetOut>();
  228. export2(out i);
  229. Assert.AreEqual(29, i);
  230. }
  231. }
  232. }