PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System.Xaml/Test/System.Xaml.Schema/XamlTypeInvokerTest.cs

https://bitbucket.org/foobar22/mono
C# | 400 lines | 316 code | 46 blank | 38 comment | 2 complexity | 0076333f1cc58411f1b62697376fced9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0
  1. //
  2. // Copyright (C) 2010 Novell Inc. http://novell.com
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Reflection;
  28. using System.Text;
  29. using System.Windows.Markup;
  30. using System.Xaml;
  31. using System.Xaml.Schema;
  32. using System.Xml;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Xaml.Schema
  35. {
  36. [TestFixture]
  37. public class XamlTypeInvokerTest
  38. {
  39. XamlSchemaContext sctx = new XamlSchemaContext (new XamlSchemaContextSettings ());
  40. [Test]
  41. [ExpectedException (typeof (ArgumentNullException))]
  42. public void ConstructorTypeNull ()
  43. {
  44. new XamlTypeInvoker (null);
  45. }
  46. [Test]
  47. public void DefaultValues ()
  48. {
  49. var i = new XamlTypeInvoker (new XamlType (typeof (object), sctx));
  50. Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
  51. Assert.IsNull (i.SetTypeConverterHandler, "#2");
  52. }
  53. [XamlSetMarkupExtension ("HandleMarkupExtension")]
  54. public class TestClassMarkupExtension1
  55. {
  56. }
  57. // SetMarkupExtensionHandler
  58. [Test]
  59. [ExpectedException (typeof (ArgumentException))]
  60. public void SetHandleMarkupExtensionInvalid ()
  61. {
  62. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassMarkupExtension1), sctx));
  63. Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
  64. }
  65. [XamlSetMarkupExtension ("HandleMarkupExtension")]
  66. public class TestClassMarkupExtension2
  67. {
  68. // delegate type mismatch
  69. void HandleMarkupExtension ()
  70. {
  71. }
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentException))]
  75. public void SetHandleMarkupExtensionInvalid2 ()
  76. {
  77. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassMarkupExtension2), sctx));
  78. Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
  79. }
  80. [XamlSetMarkupExtension ("HandleMarkupExtension")]
  81. public class TestClassMarkupExtension3
  82. {
  83. // must be static
  84. public void HandleMarkupExtension (object o, XamlSetMarkupExtensionEventArgs a)
  85. {
  86. }
  87. }
  88. [Test]
  89. [ExpectedException (typeof (ArgumentException))]
  90. public void SetHandleMarkupExtensionInvalid3 ()
  91. {
  92. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassMarkupExtension3), sctx));
  93. Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
  94. }
  95. [XamlSetMarkupExtension ("HandleMarkupExtension")]
  96. public class TestClassMarkupExtension4
  97. {
  98. // can be private.
  99. static void HandleMarkupExtension (object o, XamlSetMarkupExtensionEventArgs a)
  100. {
  101. }
  102. }
  103. [Test]
  104. public void SetHandleMarkupExtension ()
  105. {
  106. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassMarkupExtension4), sctx));
  107. Assert.IsNotNull (i.SetMarkupExtensionHandler, "#1");
  108. }
  109. // SetTypeConverterHandler
  110. [XamlSetTypeConverter ("HandleTypeConverter")]
  111. public class TestClassTypeConverter1
  112. {
  113. }
  114. [Test]
  115. [ExpectedException (typeof (ArgumentException))]
  116. public void SetHandleTypeConverterInvalid ()
  117. {
  118. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassTypeConverter1), sctx));
  119. Assert.IsNull (i.SetTypeConverterHandler, "#1");
  120. }
  121. [XamlSetTypeConverter ("HandleTypeConverter")]
  122. public class TestClassTypeConverter2
  123. {
  124. // delegate type mismatch
  125. void HandleTypeConverter ()
  126. {
  127. }
  128. }
  129. [Test]
  130. [ExpectedException (typeof (ArgumentException))]
  131. public void SetHandleTypeConverterInvalid2 ()
  132. {
  133. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassTypeConverter2), sctx));
  134. Assert.IsNull (i.SetTypeConverterHandler, "#1");
  135. }
  136. [XamlSetTypeConverter ("HandleTypeConverter")]
  137. public class TestClassTypeConverter3
  138. {
  139. // must be static
  140. public void HandleTypeConverter (object o, XamlSetTypeConverterEventArgs a)
  141. {
  142. }
  143. }
  144. [Test]
  145. [ExpectedException (typeof (ArgumentException))]
  146. public void SetHandleTypeConverterInvalid3 ()
  147. {
  148. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassTypeConverter3), sctx));
  149. Assert.IsNull (i.SetTypeConverterHandler, "#1");
  150. }
  151. [XamlSetTypeConverter ("HandleTypeConverter")]
  152. public class TestClassTypeConverter4
  153. {
  154. // can be private.
  155. static void HandleTypeConverter (object o, XamlSetTypeConverterEventArgs a)
  156. {
  157. }
  158. }
  159. [Test]
  160. public void SetHandleTypeConverter ()
  161. {
  162. var i = new XamlTypeInvoker (new XamlType (typeof (TestClassTypeConverter4), sctx));
  163. Assert.IsNotNull (i.SetTypeConverterHandler, "#1");
  164. }
  165. // AddToCollection
  166. [Test]
  167. public void AddToCollectionNoUnderlyingType ()
  168. {
  169. var i = new XamlTypeInvoker (new XamlType ("urn:foo", "FooType", null, sctx));
  170. i.AddToCollection (new List<int> (), 5); // ... passes.
  171. }
  172. [Test]
  173. [ExpectedException (typeof (NotSupportedException))]
  174. public void AddToCollectionArrayExtension ()
  175. {
  176. var i = XamlLanguage.Array.Invoker;
  177. var ax = new ArrayExtension ();
  178. i.AddToCollection (ax, 5);
  179. }
  180. [Test]
  181. [ExpectedException (typeof (NotSupportedException))]
  182. public void AddToCollectionArrayInstance ()
  183. {
  184. var i = new XamlTypeInvoker (new XamlType (typeof (int []), sctx));
  185. var ax = new ArrayExtension ();
  186. i.AddToCollection (ax, 5);
  187. }
  188. [Test]
  189. public void AddToCollectionList_ObjectTypeMismatch ()
  190. {
  191. var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
  192. try {
  193. i.AddToCollection (new ArrayExtension (), 5);
  194. Assert.Fail ("not supported operation.");
  195. } catch (NotSupportedException) {
  196. } catch (TargetException) {
  197. // .NET throws this, but the difference should not really matter.
  198. }
  199. }
  200. [Test]
  201. public void AddToCollectionList_ObjectTypeMismatch2 ()
  202. {
  203. var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
  204. i.AddToCollection (new List<object> (), 5); // it is allowed.
  205. }
  206. [Test]
  207. public void AddToCollectionList_ObjectTypeMismatch3 ()
  208. {
  209. var i = new XamlTypeInvoker (new XamlType (typeof (List<object>), sctx));
  210. i.AddToCollection (new List<int> (), 5); // it is allowed too.
  211. }
  212. [Test]
  213. public void AddToCollectionList_ObjectTypeMismatch4 ()
  214. {
  215. var i = new XamlTypeInvoker (new XamlType (typeof (List<Uri>), sctx));
  216. i.AddToCollection (new List<TimeSpan> (), TimeSpan.Zero); // it is allowed too.
  217. }
  218. [Test]
  219. public void AddToCollectionList_NonCollectionType ()
  220. {
  221. // so, the source collection type is not checked at all.
  222. var i = new XamlTypeInvoker (new XamlType (typeof (Uri), sctx));
  223. i.AddToCollection (new List<TimeSpan> (), TimeSpan.Zero); // it is allowed too.
  224. }
  225. [Test]
  226. public void AddToCollectionList ()
  227. {
  228. var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
  229. var l = new List<int> ();
  230. i.AddToCollection (l, 5);
  231. i.AddToCollection (l, 3);
  232. i.AddToCollection (l, -12);
  233. Assert.AreEqual (3, l.Count, "#1");
  234. Assert.AreEqual (-12, l [2], "#2");
  235. }
  236. [Test]
  237. [ExpectedException (typeof (ArgumentException))]
  238. public void AddToCollectionTypeMismatch ()
  239. {
  240. var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
  241. var l = new List<int> ();
  242. i.AddToCollection (l, "5");
  243. }
  244. // CreateInstance
  245. [Test]
  246. [ExpectedException (typeof (NotSupportedException))]
  247. public void CreateInstanceNoUnderlyingType ()
  248. {
  249. var i = new XamlTypeInvoker (new XamlType ("urn:foo", "FooType", null, sctx));
  250. i.CreateInstance (new object [0]); // unkown type is not supported
  251. }
  252. [Test]
  253. public void CreateInstanceArrayExtension ()
  254. {
  255. var i = XamlLanguage.Array.Invoker;
  256. i.CreateInstance (new object [0]);
  257. }
  258. [Test]
  259. [ExpectedException (typeof (MissingMethodException))]
  260. public void CreateInstanceArray ()
  261. {
  262. var i = new XamlTypeInvoker (new XamlType (typeof (int []), sctx));
  263. i.CreateInstance (new object [0]); // no default constructor.
  264. }
  265. [Test]
  266. [ExpectedException (typeof (MissingMethodException))]
  267. public void CreateInstanceList_ArgumentMismatch ()
  268. {
  269. var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
  270. i.CreateInstance (new object [] {"foo"});
  271. }
  272. [Test]
  273. public void CreateInstanceList ()
  274. {
  275. var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
  276. i.CreateInstance (new object [0]);
  277. }
  278. [Test]
  279. public void GetItems ()
  280. {
  281. var i = new XamlType (typeof (List<int>), sctx).Invoker;
  282. var list = new int [] {5, -3, 0}.ToList ();
  283. var items = i.GetItems (list);
  284. var arr = new List<object> ();
  285. while (items.MoveNext ())
  286. arr.Add (items.Current);
  287. Assert.AreEqual (5, arr [0], "#1");
  288. Assert.AreEqual (0, arr [2], "#2");
  289. }
  290. [Test]
  291. public void GetItems2 ()
  292. {
  293. // GetItems() returns IEnumerable<KeyValuePair<,>>
  294. var i = new XamlType (typeof (Dictionary<int,string>), sctx).Invoker;
  295. var dic = new Dictionary<int,string> ();
  296. dic [5] = "foo";
  297. dic [-3] = "bar";
  298. dic [0] = "baz";
  299. var items = i.GetItems (dic);
  300. var arr = new List<object> ();
  301. while (items.MoveNext ())
  302. arr.Add (items.Current);
  303. Assert.AreEqual (new KeyValuePair<int,string> (5, "foo"), arr [0], "#1");
  304. Assert.AreEqual (new KeyValuePair<int,string> (0, "baz"), arr [2], "#1");
  305. }
  306. [Test]
  307. [ExpectedException (typeof (NotSupportedException))]
  308. public void UnknownInvokerCreateInstance ()
  309. {
  310. XamlTypeInvoker.UnknownInvoker.CreateInstance (new object [0]);
  311. }
  312. [Test]
  313. public void UnknownInvokerGetItems ()
  314. {
  315. var items = XamlTypeInvoker.UnknownInvoker.GetItems (new object [] {1});
  316. Assert.IsNotNull (items, "#1");
  317. Assert.IsTrue (items.MoveNext (), "#2");
  318. Assert.AreEqual (1, items.Current, "#3");
  319. Assert.IsFalse (items.MoveNext (), "#4");
  320. }
  321. [Test]
  322. public void UnknownInvokerAddToCollection ()
  323. {
  324. // this does not check Unknown-ness.
  325. var c = new List<object> ();
  326. XamlTypeInvoker.UnknownInvoker.AddToCollection (c, 1);
  327. Assert.AreEqual (1, c.Count, "#1");
  328. }
  329. [Test]
  330. public void UnknownInvokerAddToDictionary ()
  331. {
  332. var dic = new Dictionary<object,object> ();
  333. // this does not check Unknown-ness.
  334. XamlTypeInvoker.UnknownInvoker.AddToDictionary (dic, 1, 2);
  335. Assert.AreEqual (1, dic.Count, "#1");
  336. }
  337. [Test]
  338. public void UnknownInvokerGetEnumeratorMethod ()
  339. {
  340. try {
  341. Assert.IsNull (XamlTypeInvoker.UnknownInvoker.GetEnumeratorMethod (), "#1");
  342. } catch (Exception) {
  343. // .NET is buggy, returns NRE.
  344. }
  345. }
  346. [Test]
  347. public void UnknownInvoker ()
  348. {
  349. Assert.IsNull (XamlTypeInvoker.UnknownInvoker.SetMarkupExtensionHandler, "#1");
  350. Assert.IsNull (XamlTypeInvoker.UnknownInvoker.SetTypeConverterHandler, "#2");
  351. Assert.IsNull (XamlTypeInvoker.UnknownInvoker.GetAddMethod (XamlLanguage.Object), "#3");
  352. }
  353. }
  354. }