PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Xaml/Test/System.Xaml/XamlXmlWriterTest.cs

https://bitbucket.org/danipen/mono
C# | 1075 lines | 951 code | 77 blank | 47 comment | 3 complexity | 4ba626c1ea7b8a816f45da7d4d1e707a MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-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.ComponentModel;
  27. using System.IO;
  28. using System.Linq;
  29. using System.Reflection;
  30. using System.Windows.Markup;
  31. using System.Xaml;
  32. using System.Xaml.Schema;
  33. using NUnit.Framework;
  34. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  35. namespace MonoTests.System.Xaml
  36. {
  37. [TestFixture]
  38. public class XamlXmlWriterTest
  39. {
  40. PropertyInfo str_len = typeof (string).GetProperty ("Length");
  41. XamlSchemaContext sctx = new XamlSchemaContext (null, null);
  42. XamlType xt, xt2;
  43. XamlMember xm;
  44. public XamlXmlWriterTest ()
  45. {
  46. xt = new XamlType (typeof (string), sctx);
  47. xt2 = new XamlType (typeof (List<int>), sctx);
  48. xm = new XamlMember (str_len, sctx);
  49. }
  50. public class Foo : List<int>
  51. {
  52. public List<string> Bar { get; set; }
  53. }
  54. [Test]
  55. [ExpectedException (typeof (ArgumentNullException))]
  56. public void SchemaContextNull ()
  57. {
  58. new XamlXmlWriter (new MemoryStream (), null);
  59. }
  60. [Test]
  61. public void SettingsNull ()
  62. {
  63. // allowed.
  64. var w = new XamlXmlWriter (new MemoryStream (), sctx, null);
  65. Assert.AreEqual (sctx, w.SchemaContext, "#1");
  66. Assert.IsNotNull (w.Settings, "#2");
  67. }
  68. [Test]
  69. [ExpectedException (typeof (XamlXmlWriterException))]
  70. public void InitWriteEndMember ()
  71. {
  72. new XamlXmlWriter (new MemoryStream (), sctx, null).WriteEndMember ();
  73. }
  74. [Test]
  75. [ExpectedException (typeof (XamlXmlWriterException))]
  76. public void InitWriteEndObject ()
  77. {
  78. new XamlXmlWriter (new MemoryStream (), sctx, null).WriteEndObject ();
  79. }
  80. [Test]
  81. [ExpectedException (typeof (XamlXmlWriterException))]
  82. public void InitWriteGetObject ()
  83. {
  84. new XamlXmlWriter (new MemoryStream (), sctx, null).WriteGetObject ();
  85. }
  86. [Test]
  87. [ExpectedException (typeof (XamlXmlWriterException))]
  88. public void InitWriteValue ()
  89. {
  90. new XamlXmlWriter (new StringWriter (), sctx, null).WriteValue ("foo");
  91. }
  92. [Test]
  93. [ExpectedException (typeof (XamlXmlWriterException))]
  94. public void InitWriteStartMember ()
  95. {
  96. new XamlXmlWriter (new StringWriter (), sctx, null).WriteStartMember (new XamlMember (str_len, sctx));
  97. }
  98. [Test]
  99. public void InitWriteNamespace ()
  100. {
  101. var sw = new StringWriter ();
  102. var xw = new XamlXmlWriter (sw, sctx, null);
  103. xw.WriteNamespace (new NamespaceDeclaration ("urn:foo", "x")); // ignored.
  104. xw.Close ();
  105. Assert.AreEqual ("", sw.ToString (), "#1");
  106. }
  107. [Test]
  108. [ExpectedException (typeof (ArgumentNullException))]
  109. public void WriteNamespaceNull ()
  110. {
  111. new XamlXmlWriter (new StringWriter (), sctx, null).WriteNamespace (null);
  112. }
  113. [Test]
  114. public void InitWriteStartObject ()
  115. {
  116. string xml = @"<?xml version='1.0' encoding='utf-16'?><Int32 xmlns='http://schemas.microsoft.com/winfx/2006/xaml' />";
  117. var sw = new StringWriter ();
  118. var xw = new XamlXmlWriter (sw, sctx, null);
  119. xw.WriteStartObject (new XamlType (typeof (int), sctx));
  120. xw.Close ();
  121. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  122. }
  123. [Test]
  124. [ExpectedException (typeof (XamlXmlWriterException))]
  125. public void GetObjectAfterStartObject ()
  126. {
  127. var sw = new StringWriter ();
  128. var xw = new XamlXmlWriter (sw, sctx, null);
  129. xw.WriteStartObject (xt);
  130. xw.WriteGetObject ();
  131. }
  132. [Test]
  133. [ExpectedException (typeof (XamlXmlWriterException))]
  134. public void WriteStartObjectAfterTopLevel ()
  135. {
  136. var sw = new StringWriter ();
  137. var xw = new XamlXmlWriter (sw, sctx, null);
  138. xw.WriteStartObject (xt);
  139. xw.WriteEndObject ();
  140. // writing another root is not allowed.
  141. xw.WriteStartObject (xt);
  142. }
  143. [Test]
  144. [ExpectedException (typeof (XamlXmlWriterException))]
  145. public void WriteEndObjectExcess ()
  146. {
  147. var sw = new StringWriter ();
  148. var xw = new XamlXmlWriter (sw, sctx, null);
  149. xw.WriteStartObject (xt);
  150. xw.WriteEndObject ();
  151. xw.WriteEndObject ();
  152. }
  153. [Test]
  154. [ExpectedException (typeof (XamlXmlWriterException))]
  155. public void StartObjectWriteEndMember ()
  156. {
  157. var sw = new StringWriter ();
  158. var xw = new XamlXmlWriter (sw, sctx, null);
  159. xw.WriteStartObject (xt);
  160. xw.WriteEndMember ();
  161. }
  162. [Test]
  163. public void WriteObjectAndMember ()
  164. {
  165. string xml = @"<?xml version='1.0' encoding='utf-16'?><String Length='foo' xmlns='http://schemas.microsoft.com/winfx/2006/xaml' />";
  166. var sw = new StringWriter ();
  167. var xw = new XamlXmlWriter (sw, sctx, null);
  168. xw.WriteStartObject (xt);
  169. xw.WriteStartMember (xm);
  170. xw.WriteValue ("foo");
  171. xw.WriteEndMember ();
  172. xw.Close ();
  173. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  174. }
  175. [Test]
  176. [ExpectedException (typeof (XamlXmlWriterException))]
  177. public void StartMemberWriteEndMember ()
  178. {
  179. var sw = new StringWriter ();
  180. var xw = new XamlXmlWriter (sw, sctx, null);
  181. xw.WriteStartObject (xt);
  182. xw.WriteStartMember (xm);
  183. xw.WriteEndMember (); // wow, really?
  184. }
  185. [Test]
  186. [ExpectedException (typeof (XamlXmlWriterException))]
  187. public void StartMemberWriteStartMember ()
  188. {
  189. var sw = new StringWriter ();
  190. var xw = new XamlXmlWriter (sw, sctx, null);
  191. xw.WriteStartObject (xt);
  192. xw.WriteStartMember (xm);
  193. xw.WriteStartMember (xm);
  194. }
  195. [Test]
  196. public void WriteObjectInsideMember ()
  197. {
  198. string xml = @"<?xml version='1.0' encoding='utf-16'?><String xmlns='http://schemas.microsoft.com/winfx/2006/xaml'><String.Length><String /></String.Length></String>";
  199. var sw = new StringWriter ();
  200. var xw = new XamlXmlWriter (sw, sctx, null);
  201. xw.WriteStartObject (xt);
  202. xw.WriteStartMember (xm);
  203. xw.WriteStartObject (xt);
  204. xw.WriteEndObject ();
  205. xw.WriteEndMember ();
  206. xw.Close ();
  207. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  208. }
  209. [Test]
  210. public void ValueAfterObject ()
  211. {
  212. string xml = @"<?xml version='1.0' encoding='utf-16'?><String xmlns='http://schemas.microsoft.com/winfx/2006/xaml'><String.Length><String />foo</String.Length></String>";
  213. var sw = new StringWriter ();
  214. var xw = new XamlXmlWriter (sw, sctx, null);
  215. xw.WriteStartObject (xt);
  216. xw.WriteStartMember (xm);
  217. xw.WriteStartObject (xt);
  218. xw.WriteEndObject ();
  219. // allowed.
  220. xw.WriteValue ("foo");
  221. xw.WriteEndMember ();
  222. xw.Close ();
  223. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  224. }
  225. [Test]
  226. [Category ("NotWorking")] // This is an abnormal operation and I cannot completely care about such operations.
  227. public void ValueAfterObject2 ()
  228. {
  229. string xml = @"<?xml version='1.0' encoding='utf-16'?><String xmlns='http://schemas.microsoft.com/winfx/2006/xaml'><String.Length>foo<String />foo</String.Length></String>";
  230. var sw = new StringWriter ();
  231. var xw = new XamlXmlWriter (sw, sctx, null);
  232. xw.WriteStartObject (xt);
  233. xw.WriteStartMember (xm);
  234. xw.WriteValue ("foo");
  235. xw.WriteStartObject (xt);
  236. xw.WriteEndObject ();
  237. // allowed.
  238. xw.WriteValue ("foo");
  239. xw.WriteEndMember ();
  240. xw.Close ();
  241. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  242. }
  243. [Test]
  244. public void ValueAfterObject3 ()
  245. {
  246. string xml = @"<?xml version='1.0' encoding='utf-16'?><String xmlns='http://schemas.microsoft.com/winfx/2006/xaml'><String.Length><String />foo<String />foo</String.Length></String>";
  247. var sw = new StringWriter ();
  248. var xw = new XamlXmlWriter (sw, sctx, null);
  249. xw.WriteStartObject (xt);
  250. xw.WriteStartMember (xm);
  251. xw.WriteStartObject (xt);
  252. xw.WriteEndObject ();
  253. xw.WriteValue ("foo");
  254. xw.WriteStartObject (xt);
  255. xw.WriteEndObject ();
  256. xw.WriteValue ("foo");
  257. xw.WriteEndMember ();
  258. xw.Close ();
  259. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  260. }
  261. [Test]
  262. [ExpectedException (typeof (ArgumentException))]
  263. public void WriteValueTypeNonString ()
  264. {
  265. var sw = new StringWriter ();
  266. var xw = new XamlXmlWriter (sw, sctx, null);
  267. xw.WriteStartObject (xt);
  268. xw.WriteStartMember (xm);
  269. xw.WriteValue (5); // even the type matches the member type, writing non-string value is rejected.
  270. }
  271. [Test]
  272. [ExpectedException (typeof (XamlXmlWriterException))]
  273. public void WriteValueAfterValue ()
  274. {
  275. var sw = new StringWriter ();
  276. var xw = new XamlXmlWriter (sw, sctx, null);
  277. xw.WriteStartObject (xt);
  278. xw.WriteValue ("foo");
  279. xw.WriteValue ("bar");
  280. }
  281. [Test]
  282. [ExpectedException (typeof (XamlXmlWriterException))]
  283. public void WriteValueAfterNullValue ()
  284. {
  285. var sw = new StringWriter ();
  286. var xw = new XamlXmlWriter (sw, sctx, null);
  287. xw.WriteStartObject (xt);
  288. xw.WriteValue (null);
  289. xw.WriteValue ("bar");
  290. }
  291. [Test]
  292. [ExpectedException (typeof (XamlXmlWriterException))]
  293. public void WriteValueList ()
  294. {
  295. var sw = new StringWriter ();
  296. var xw = new XamlXmlWriter (sw, sctx, null);
  297. xw.WriteStartObject (new XamlType (typeof (List<string>), sctx));
  298. xw.WriteStartMember (XamlLanguage.Items);
  299. xw.WriteValue ("foo");
  300. xw.WriteValue ("bar");
  301. }
  302. [ExpectedException (typeof (XamlXmlWriterException))]
  303. public void StartMemberWriteEndObject ()
  304. {
  305. var sw = new StringWriter ();
  306. var xw = new XamlXmlWriter (sw, sctx, null);
  307. xw.WriteStartObject (xt);
  308. xw.WriteStartMember (xm);
  309. xw.WriteEndObject ();
  310. }
  311. [Test]
  312. public void WriteNamespace ()
  313. {
  314. string xml = @"<?xml version='1.0' encoding='utf-16'?><x:String xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:y='urn:foo' />";
  315. var sw = new StringWriter ();
  316. var xw = new XamlXmlWriter (sw, sctx, null);
  317. xw.WriteNamespace (new NamespaceDeclaration (XamlLanguage.Xaml2006Namespace, "x"));
  318. xw.WriteNamespace (new NamespaceDeclaration ("urn:foo", "y"));
  319. xw.WriteStartObject (xt);
  320. xw.WriteEndObject ();
  321. xw.Close ();
  322. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  323. }
  324. [Test]
  325. [ExpectedException (typeof (XamlXmlWriterException))]
  326. public void StartObjectStartObject ()
  327. {
  328. var sw = new StringWriter ();
  329. var xw = new XamlXmlWriter (sw, sctx, null);
  330. xw.WriteStartObject (xt);
  331. xw.WriteStartObject (xt);
  332. }
  333. [Test]
  334. [ExpectedException (typeof (XamlXmlWriterException))]
  335. public void StartObjectValue ()
  336. {
  337. var sw = new StringWriter ();
  338. var xw = new XamlXmlWriter (sw, sctx, null);
  339. xw.WriteStartObject (xt);
  340. xw.WriteValue ("foo");
  341. }
  342. [Test]
  343. public void ObjectThenNamespaceThenObjectThenObject ()
  344. {
  345. string xml = @"<?xml version='1.0' encoding='utf-16'?><String xmlns='http://schemas.microsoft.com/winfx/2006/xaml'><String.Length><String /><String /></String.Length></String>";
  346. var sw = new StringWriter ();
  347. var xw = new XamlXmlWriter (sw, sctx, null);
  348. xw.WriteStartObject (xt); // <String>
  349. xw.WriteStartMember (xm); // <String.Length>
  350. xw.WriteStartObject (xt); // <String />
  351. xw.WriteEndObject ();
  352. xw.WriteStartObject (xt); // <String />
  353. xw.WriteEndObject ();
  354. xw.Close ();
  355. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  356. }
  357. // This doesn't result in XamlXmlWriterException. Instead,
  358. // IOE is thrown. WriteValueAfterNamespace() too.
  359. // It is probably because namespaces are verified independently
  360. // from state transition (and borks when the next write is not
  361. // appropriate).
  362. [Test]
  363. [ExpectedException (typeof (InvalidOperationException))]
  364. public void EndObjectAfterNamespace ()
  365. {
  366. var sw = new StringWriter ();
  367. var xw = new XamlXmlWriter (sw, sctx, null);
  368. xw.WriteStartObject (xt);
  369. xw.WriteNamespace (new NamespaceDeclaration ("urn:foo", "y"));
  370. xw.WriteEndObject ();
  371. }
  372. [Test]
  373. [ExpectedException (typeof (InvalidOperationException))] // ... shouldn't it be XamlXmlWriterException?
  374. public void WriteValueAfterNamespace ()
  375. {
  376. var sw = new StringWriter ();
  377. var xw = new XamlXmlWriter (sw, sctx, null);
  378. xw.WriteStartObject (xt);
  379. xw.WriteStartMember (XamlLanguage.Initialization);
  380. xw.WriteNamespace (new NamespaceDeclaration ("urn:foo", "y"));
  381. xw.WriteValue ("foo");
  382. }
  383. [Test]
  384. [Category ("NotWorking")]
  385. public void ValueThenStartObject ()
  386. {
  387. string xml = @"<?xml version='1.0' encoding='utf-16'?><String xmlns='http://schemas.microsoft.com/winfx/2006/xaml'><String.Length>foo<String /></String.Length></String>";
  388. var sw = new StringWriter ();
  389. var xw = new XamlXmlWriter (sw, sctx, null);
  390. xw.WriteStartObject (xt);
  391. xw.WriteStartMember (xm);
  392. xw.WriteValue ("foo");
  393. xw.WriteStartObject (xt); // looks like it is ignored. It is weird input anyways.
  394. xw.Close ();
  395. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  396. }
  397. [Test]
  398. public void ValueThenNamespace ()
  399. {
  400. var sw = new StringWriter ();
  401. var xw = new XamlXmlWriter (sw, sctx, null);
  402. xw.WriteStartObject (xt);
  403. xw.WriteStartMember (xm);
  404. xw.WriteValue ("foo");
  405. xw.WriteNamespace (new NamespaceDeclaration ("y", "urn:foo")); // this does not raise an error (since it might start another object)
  406. }
  407. [Test]
  408. [ExpectedException (typeof (XamlXmlWriterException))] // strange, this does *not* result in IOE...
  409. public void ValueThenNamespaceThenEndMember ()
  410. {
  411. var sw = new StringWriter ();
  412. var xw = new XamlXmlWriter (sw, sctx, null);
  413. xw.WriteStartObject (xt);
  414. xw.WriteStartMember (xm);
  415. xw.WriteValue ("foo");
  416. xw.WriteNamespace (new NamespaceDeclaration ("y", "urn:foo"));
  417. xw.WriteEndMember ();
  418. }
  419. [Test]
  420. public void StartMemberAfterNamespace ()
  421. {
  422. // This test shows:
  423. // 1) StartMember after NamespaceDeclaration is valid
  424. // 2) Member is written as an element (not attribute)
  425. // if there is a NamespaceDeclaration in the middle.
  426. string xml = @"<?xml version='1.0' encoding='utf-16'?><String xmlns='http://schemas.microsoft.com/winfx/2006/xaml'><String.Length xmlns:y='urn:foo'>foo</String.Length></String>";
  427. var sw = new StringWriter ();
  428. var xw = new XamlXmlWriter (sw, sctx, null);
  429. xw.WriteStartObject (xt);
  430. xw.WriteNamespace (new NamespaceDeclaration ("urn:foo", "y"));
  431. xw.WriteStartMember (xm);
  432. xw.WriteValue ("foo");
  433. xw.Close ();
  434. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  435. }
  436. [Test]
  437. [ExpectedException (typeof (XamlXmlWriterException))]
  438. public void EndMemberThenStartObject ()
  439. {
  440. var sw = new StringWriter ();
  441. var xw = new XamlXmlWriter (sw, sctx, null);
  442. xw.WriteStartObject (xt);
  443. xw.WriteStartMember (xm);
  444. xw.WriteValue ("foo");
  445. xw.WriteEndMember ();
  446. xw.WriteStartObject (xt);
  447. }
  448. [Test]
  449. [ExpectedException (typeof (InvalidOperationException))]
  450. public void GetObjectOnNonCollection ()
  451. {
  452. var sw = new StringWriter ();
  453. var xw = new XamlXmlWriter (sw, sctx, null);
  454. xw.WriteStartObject (xt);
  455. xw.WriteStartMember (xm);
  456. xw.WriteGetObject ();
  457. }
  458. [Test]
  459. [ExpectedException (typeof (InvalidOperationException))]
  460. public void GetObjectOnNonCollection2 ()
  461. {
  462. var sw = new StringWriter ();
  463. var xw = new XamlXmlWriter (sw, sctx, null);
  464. xw.WriteStartObject (xt);
  465. xw.WriteStartMember (new XamlMember (typeof (string).GetProperty ("Length"), sctx)); // Length is of type int, which is not a collection
  466. xw.WriteGetObject ();
  467. }
  468. [Test]
  469. public void GetObjectOnCollection ()
  470. {
  471. string xml = @"<?xml version='1.0' encoding='utf-16'?><List xmlns='clr-namespace:System.Collections.Generic;assembly=mscorlib'><x:TypeArguments xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>x:Int32</x:TypeArguments><List.Bar /></List>";
  472. var sw = new StringWriter ();
  473. var xw = new XamlXmlWriter (sw, sctx, null);
  474. xw.WriteStartObject (xt2);
  475. xw.WriteStartMember (new XamlMember (typeof (Foo).GetProperty ("Bar"), sctx));
  476. xw.WriteGetObject ();
  477. xw.Close ();
  478. // FIXME: enable it once we got generic type output fixed.
  479. //Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  480. }
  481. [Test]
  482. [ExpectedException (typeof (XamlXmlWriterException))]
  483. public void ValueAfterGetObject ()
  484. {
  485. var sw = new StringWriter ();
  486. var xw = new XamlXmlWriter (sw, sctx, null);
  487. xw.WriteStartObject (xt2);
  488. xw.WriteStartMember (new XamlMember (typeof (Foo).GetProperty ("Bar"), sctx));
  489. xw.WriteGetObject ();
  490. xw.WriteValue ("foo");
  491. }
  492. [Test]
  493. [ExpectedException (typeof (XamlXmlWriterException))]
  494. public void StartObjectAfterGetObject ()
  495. {
  496. var sw = new StringWriter ();
  497. var xw = new XamlXmlWriter (sw, sctx, null);
  498. xw.WriteStartObject (xt2);
  499. xw.WriteStartMember (new XamlMember (typeof (Foo).GetProperty ("Bar"), sctx));
  500. xw.WriteGetObject ();
  501. xw.WriteStartObject (xt);
  502. }
  503. [Test]
  504. [ExpectedException (typeof (XamlXmlWriterException))]
  505. public void EndMemberAfterGetObject ()
  506. {
  507. var sw = new StringWriter ();
  508. var xw = new XamlXmlWriter (sw, sctx, null);
  509. xw.WriteStartObject (xt2);
  510. xw.WriteStartMember (new XamlMember (typeof (Foo).GetProperty ("Bar"), sctx));
  511. xw.WriteGetObject ();
  512. xw.WriteEndMember (); // ...!?
  513. }
  514. [Test]
  515. public void StartMemberAfterGetObject ()
  516. {
  517. string xml = @"<?xml version='1.0' encoding='utf-16'?><List xmlns='clr-namespace:System.Collections.Generic;assembly=mscorlib'><x:TypeArguments xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>x:Int32</x:TypeArguments><List.Bar><List.Length /></List.Bar></List>";
  518. var sw = new StringWriter ();
  519. var xw = new XamlXmlWriter (sw, sctx, null);
  520. xw.WriteStartObject (xt2); // <List
  521. xw.WriteStartMember (new XamlMember (typeof (Foo).GetProperty ("Bar"), sctx)); // <List.Bar>
  522. xw.WriteGetObject ();
  523. xw.WriteStartMember (xm); // <List.Length /> . Note that the corresponding member is String.Length(!)
  524. xw.Close ();
  525. // FIXME: enable it once we got generic type output fixed.
  526. //Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  527. }
  528. [Test]
  529. public void EndObjectAfterGetObject ()
  530. {
  531. var sw = new StringWriter ();
  532. var xw = new XamlXmlWriter (sw, sctx, null);
  533. xw.WriteStartObject (xt2);
  534. xw.WriteStartMember (new XamlMember (typeof (Foo).GetProperty ("Bar"), sctx));
  535. xw.WriteGetObject ();
  536. xw.WriteEndObject ();
  537. }
  538. [Test]
  539. public void WriteNode ()
  540. {
  541. string xml = @"<?xml version='1.0' encoding='utf-16'?><x:String xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>foo</x:String>";
  542. var r = new XamlObjectReader ("foo", sctx);
  543. var sw = new StringWriter ();
  544. var w = new XamlXmlWriter (sw, sctx, null);
  545. while (r.Read ())
  546. w.WriteNode (r);
  547. w.Close ();
  548. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  549. }
  550. [Test]
  551. public void WriteNode2 ()
  552. {
  553. var r = new XamlObjectReader ("foo", sctx);
  554. var w = new XamlObjectWriter (sctx, null);
  555. while (r.Read ())
  556. w.WriteNode (r);
  557. w.Close ();
  558. Assert.AreEqual ("foo", w.Result, "#1");
  559. }
  560. [Test]
  561. public void ConstructorArguments ()
  562. {
  563. string xml = String.Format (@"<?xml version='1.0' encoding='utf-16'?><ArgumentAttributed xmlns='clr-namespace:MonoTests.System.Xaml;assembly={0}' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><x:Arguments><x:String>xxx</x:String><x:String>yyy</x:String></x:Arguments></ArgumentAttributed>", GetType ().Assembly.GetName ().Name);
  564. Assert.IsFalse (sctx.FullyQualifyAssemblyNamesInClrNamespaces, "premise0");
  565. var r = new XamlObjectReader (new ArgumentAttributed ("xxx", "yyy"), sctx);
  566. var sw = new StringWriter ();
  567. var w = new XamlXmlWriter (sw, sctx, null);
  568. XamlServices.Transform (r, w);
  569. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  570. }
  571. [Test]
  572. public void WriteValueAsString ()
  573. {
  574. var sw = new StringWriter ();
  575. var xw = new XamlXmlWriter (sw, sctx, null);
  576. var xt = sctx.GetXamlType (typeof (TestXmlWriterClass1));
  577. xw.WriteStartObject (xt);
  578. xw.WriteStartMember (xt.GetMember ("Foo"));
  579. xw.WriteValue ("50");
  580. xw.Close ();
  581. string xml = String.Format (@"<?xml version='1.0' encoding='utf-16'?><TestXmlWriterClass1 xmlns='clr-namespace:MonoTests.System.Xaml;assembly={0}' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'></TestXmlWriterClass1>", GetType ().Assembly.GetName ().Name);
  582. }
  583. string ReadXml (string name)
  584. {
  585. #if NET_4_5
  586. string ver = "net_4_5";
  587. #else
  588. string ver = "net_4_0";
  589. #endif
  590. return File.ReadAllText ("Test/XmlFiles/" + name).Trim ().Replace (">\n", ">\r\n").Replace ("net_4_0", ver);
  591. }
  592. [Test]
  593. public void Write_String ()
  594. {
  595. Assert.AreEqual (ReadXml ("String.xml"), XamlServices.Save ("foo"), "#1");
  596. }
  597. [Test]
  598. public void Write_Int32 ()
  599. {
  600. Assert.AreEqual (ReadXml ("Int32.xml"), XamlServices.Save (5), "#1");
  601. }
  602. [Test]
  603. public void Write_DateTime ()
  604. {
  605. Assert.AreEqual (ReadXml ("DateTime.xml"), XamlServices.Save (new DateTime (2010, 4, 14)), "#1");
  606. }
  607. [Test]
  608. public void Write_TimeSpan ()
  609. {
  610. Assert.AreEqual (ReadXml ("TimeSpan.xml"), XamlServices.Save (TimeSpan.FromMinutes (7)), "#1");
  611. }
  612. [Test]
  613. public void Write_Uri ()
  614. {
  615. Assert.AreEqual (ReadXml ("Uri.xml"), XamlServices.Save (new Uri ("urn:foo")), "#1");
  616. }
  617. [Test]
  618. public void Write_Null ()
  619. {
  620. Assert.AreEqual (ReadXml ("NullExtension.xml"), XamlServices.Save (null), "#1");
  621. }
  622. [Test]
  623. public void Write_NullExtension ()
  624. {
  625. Assert.AreEqual (ReadXml ("NullExtension.xml"), XamlServices.Save (new NullExtension ()), "#1");
  626. }
  627. [Test]
  628. public void Write_Type ()
  629. {
  630. Assert.AreEqual (ReadXml ("Type.xml").Trim (), XamlServices.Save (typeof (int)), "#1");
  631. }
  632. [Test]
  633. public void Write_Type2 ()
  634. {
  635. Assert.AreEqual (ReadXml ("Type2.xml").Trim (), XamlServices.Save (typeof (TestClass1)), "#1");
  636. }
  637. [Test]
  638. public void Write_Guid ()
  639. {
  640. Assert.AreEqual (ReadXml ("Guid.xml").Trim (), XamlServices.Save (Guid.Parse ("9c3345ec-8922-4662-8e8d-a4e41f47cf09")), "#1");
  641. }
  642. [Test]
  643. public void Write_StaticExtension ()
  644. {
  645. Assert.AreEqual (ReadXml ("StaticExtension.xml").Trim (), XamlServices.Save (new StaticExtension ("FooBar")), "#1");
  646. }
  647. [Test]
  648. public void Write_StaticExtension2 ()
  649. {
  650. Assert.AreEqual (ReadXml ("StaticExtension.xml").Trim (), XamlServices.Save (new StaticExtension () { Member = "FooBar"}), "#1");
  651. }
  652. [Test]
  653. public void Write_Reference ()
  654. {
  655. Assert.AreEqual (ReadXml ("Reference.xml").Trim (), XamlServices.Save (new Reference ("FooBar")), "#1");
  656. }
  657. [Test]
  658. public void Write_ArrayInt32 ()
  659. {
  660. Assert.AreEqual (ReadXml ("Array_Int32.xml").Trim (), XamlServices.Save (new int [] {4, -5, 0, 255, int.MaxValue}), "#1");
  661. }
  662. [Test]
  663. public void Write_ListInt32 ()
  664. {
  665. Assert.AreEqual (ReadXml ("List_Int32.xml").Trim (), XamlServices.Save (new int [] {5, -3, int.MaxValue, 0}.ToList ()), "#1");
  666. }
  667. [Test]
  668. public void Write_ListInt32_2 ()
  669. {
  670. var obj = new List<int> (new int [0]) { Capacity = 0 }; // set explicit capacity for trivial implementation difference
  671. Assert.AreEqual (ReadXml ("List_Int32_2.xml").Trim (), XamlServices.Save (obj), "#1");
  672. }
  673. [Test]
  674. public void Write_ListType ()
  675. {
  676. var obj = new List<Type> (new Type [] {typeof (int), typeof (Dictionary<Type, XamlType>)}) { Capacity = 2 };
  677. Assert.AreEqual (ReadXml ("List_Type.xml").Trim (), XamlServices.Save (obj), "#1");
  678. }
  679. [Test]
  680. public void Write_ListArray ()
  681. {
  682. var obj = new List<Array> (new Array [] { new int [] { 1,2,3}, new string [] { "foo", "bar", "baz" }}) { Capacity = 2 };
  683. Assert.AreEqual (ReadXml ("List_Array.xml").Trim (), XamlServices.Save (obj), "#1");
  684. }
  685. [Test]
  686. public void Write_DictionaryInt32String ()
  687. {
  688. var dic = new Dictionary<int,string> ();
  689. dic.Add (0, "foo");
  690. dic.Add (5, "bar");
  691. dic.Add (-2, "baz");
  692. Assert.AreEqual (ReadXml ("Dictionary_Int32_String.xml").Trim (), XamlServices.Save (dic), "#1");
  693. }
  694. [Test]
  695. public void Write_DictionaryStringType ()
  696. {
  697. var dic = new Dictionary<string,Type> ();
  698. dic.Add ("t1", typeof (int));
  699. dic.Add ("t2", typeof (int []));
  700. dic.Add ("t3", typeof (int?));
  701. dic.Add ("t4", typeof (List<int>));
  702. dic.Add ("t5", typeof (Dictionary<int,DateTime>));
  703. dic.Add ("t6", typeof (List<KeyValuePair<int,DateTime>>));
  704. Assert.AreEqual (ReadXml ("Dictionary_String_Type.xml").Trim (), XamlServices.Save (dic), "#1");
  705. }
  706. [Test]
  707. [ExpectedException (typeof (XamlXmlWriterException))]
  708. public void Write_PositionalParameters1 ()
  709. {
  710. // PositionalParameters can only be written when the
  711. // instance is NOT the root object.
  712. //
  713. // A single positional parameter can be written as an
  714. // attribute, but there are two in PositionalParameters1.
  715. //
  716. // A default constructor could be used to not use
  717. // PositionalParameters, but there isn't in this type.
  718. var obj = new PositionalParametersClass1 ("foo", 5);
  719. XamlServices.Save (obj);
  720. }
  721. [Test]
  722. public void Write_PositionalParameters1Wrapper ()
  723. {
  724. // Unlike the above case, this has the wrapper object and hence PositionalParametersClass1 can be written as an attribute (markup extension)
  725. var obj = new PositionalParametersWrapper ("foo", 5);
  726. Assert.AreEqual (ReadXml ("PositionalParametersWrapper.xml").Trim (), XamlServices.Save (obj), "#1");
  727. }
  728. [Test]
  729. public void Write_ArgumentAttributed ()
  730. {
  731. var obj = new ArgumentAttributed ("foo", "bar");
  732. Assert.AreEqual (ReadXml ("ArgumentAttributed.xml").Trim (), XamlServices.Save (obj), "#1");
  733. }
  734. [Test]
  735. public void Write_ArrayExtension2 ()
  736. {
  737. var obj = new ArrayExtension (typeof (int));
  738. Assert.AreEqual (ReadXml ("ArrayExtension2.xml").Trim (), XamlServices.Save (obj), "#1");
  739. }
  740. [Test]
  741. public void Write_ArrayList ()
  742. {
  743. var obj = new ArrayList (new int [] {5, -3, 0});
  744. Assert.AreEqual (ReadXml ("ArrayList.xml").Trim (), XamlServices.Save (obj), "#1");
  745. }
  746. [Test]
  747. public void ComplexPositionalParameterWrapper ()
  748. {
  749. var obj = new ComplexPositionalParameterWrapper () { Param = new ComplexPositionalParameterClass (new ComplexPositionalParameterValue () { Foo = "foo" })};
  750. Assert.AreEqual (ReadXml ("ComplexPositionalParameterWrapper.xml").Trim (), XamlServices.Save (obj), "#1");
  751. }
  752. [Test]
  753. public void Write_ListWrapper ()
  754. {
  755. var obj = new ListWrapper (new List<int> (new int [] {5, -3, 0}) { Capacity = 3}); // set explicit capacity for trivial implementation difference
  756. Assert.AreEqual (ReadXml ("ListWrapper.xml").Trim (), XamlServices.Save (obj), "#1");
  757. }
  758. [Test]
  759. public void Write_ListWrapper2 ()
  760. {
  761. var obj = new ListWrapper2 (new List<int> (new int [] {5, -3, 0}) { Capacity = 3}); // set explicit capacity for trivial implementation difference
  762. Assert.AreEqual (ReadXml ("ListWrapper2.xml").Trim (), XamlServices.Save (obj), "#1");
  763. }
  764. [Test]
  765. public void Write_MyArrayExtension ()
  766. {
  767. var obj = new MyArrayExtension (new int [] {5, -3, 0});
  768. Assert.AreEqual (ReadXml ("MyArrayExtension.xml").Trim (), XamlServices.Save (obj), "#1");
  769. }
  770. [Test]
  771. public void Write_MyArrayExtensionA ()
  772. {
  773. var obj = new MyArrayExtensionA (new int [] {5, -3, 0});
  774. Assert.AreEqual (ReadXml ("MyArrayExtensionA.xml").Trim (), XamlServices.Save (obj), "#1");
  775. }
  776. [Test]
  777. public void Write_MyExtension ()
  778. {
  779. var obj = new MyExtension () { Foo = typeof (int), Bar = "v2", Baz = "v7"};
  780. Assert.AreEqual (ReadXml ("MyExtension.xml").Trim (), XamlServices.Save (obj), "#1");
  781. }
  782. [Test]
  783. public void Write_MyExtension2 ()
  784. {
  785. var obj = new MyExtension2 () { Foo = typeof (int), Bar = "v2"};
  786. Assert.AreEqual (ReadXml ("MyExtension2.xml").Trim (), XamlServices.Save (obj), "#1");
  787. }
  788. [Test]
  789. public void Write_MyExtension3 ()
  790. {
  791. var obj = new MyExtension3 () { Foo = typeof (int), Bar = "v2"};
  792. Assert.AreEqual (ReadXml ("MyExtension3.xml").Trim (), XamlServices.Save (obj), "#1");
  793. }
  794. [Test]
  795. public void Write_MyExtension4 ()
  796. {
  797. var obj = new MyExtension4 () { Foo = typeof (int), Bar = "v2"};
  798. Assert.AreEqual (ReadXml ("MyExtension4.xml").Trim (), XamlServices.Save (obj), "#1");
  799. }
  800. [Test]
  801. public void Write_MyExtension6 ()
  802. {
  803. var obj = new MyExtension6 ("foo");
  804. Assert.AreEqual (ReadXml ("MyExtension6.xml").Trim (), XamlServices.Save (obj), "#1");
  805. }
  806. [Test]
  807. public void Write_PropertyDefinition ()
  808. {
  809. var obj = new PropertyDefinition () { Modifier = "protected", Name = "foo", Type = XamlLanguage.String };
  810. Assert.AreEqual (ReadXml ("PropertyDefinition.xml").Trim (), XamlServices.Save (obj), "#1");
  811. }
  812. [Test]
  813. public void Write_StaticExtensionWrapper ()
  814. {
  815. var obj = new StaticExtensionWrapper () { Param = new StaticExtension ("StaticExtensionWrapper.Foo") };
  816. Assert.AreEqual (ReadXml ("StaticExtensionWrapper.xml").Trim (), XamlServices.Save (obj), "#1");
  817. }
  818. [Test]
  819. public void Write_TypeExtensionWrapper ()
  820. {
  821. var obj = new TypeExtensionWrapper () { Param = new TypeExtension ("Foo") };
  822. Assert.AreEqual (ReadXml ("TypeExtensionWrapper.xml").Trim (), XamlServices.Save (obj), "#1");
  823. }
  824. [Test]
  825. public void Write_NamedItems ()
  826. {
  827. // foo
  828. // - bar
  829. // -- foo
  830. // - baz
  831. var obj = new NamedItem ("foo");
  832. var obj2 = new NamedItem ("bar");
  833. obj.References.Add (obj2);
  834. obj.References.Add (new NamedItem ("baz"));
  835. obj2.References.Add (obj);
  836. Assert.AreEqual (ReadXml ("NamedItems.xml").Trim (), XamlServices.Save (obj), "#1");
  837. }
  838. [Test]
  839. public void Write_NamedItems2 ()
  840. {
  841. // i1
  842. // - i2
  843. // -- i3
  844. // - i4
  845. // -- i3
  846. var obj = new NamedItem2 ("i1");
  847. var obj2 = new NamedItem2 ("i2");
  848. var obj3 = new NamedItem2 ("i3");
  849. var obj4 = new NamedItem2 ("i4");
  850. obj.References.Add (obj2);
  851. obj.References.Add (obj4);
  852. obj2.References.Add (obj3);
  853. obj4.References.Add (obj3);
  854. Assert.AreEqual (ReadXml ("NamedItems2.xml").Trim (), XamlServices.Save (obj), "#1");
  855. }
  856. [Test]
  857. public void Write_XmlSerializableWrapper ()
  858. {
  859. var obj = new XmlSerializableWrapper (new XmlSerializable ("<root/>"));
  860. Assert.AreEqual (ReadXml ("XmlSerializableWrapper.xml").Trim (), XamlServices.Save (obj), "#1");
  861. }
  862. [Test]
  863. public void Write_XmlSerializable ()
  864. {
  865. var obj = new XmlSerializable ("<root/>");
  866. Assert.AreEqual (ReadXml ("XmlSerializable.xml").Trim (), XamlServices.Save (obj), "#1");
  867. }
  868. [Test]
  869. public void Write_ListXmlSerializable ()
  870. {
  871. var obj = new List<XmlSerializable> ();
  872. obj.Add (new XmlSerializable ("<root/>"));
  873. Assert.AreEqual (ReadXml ("List_XmlSerializable.xml").Trim (), XamlServices.Save (obj), "#1");
  874. }
  875. [Test]
  876. public void Write_AttachedProperty ()
  877. {
  878. var obj = new AttachedWrapper ();
  879. Attachable.SetFoo (obj, "x");
  880. Attachable.SetFoo (obj.Value, "y");
  881. try {
  882. Assert.AreEqual (ReadXml ("AttachedProperty.xml").Trim (), XamlServices.Save (obj), "#1");
  883. } finally {
  884. Attachable.SetFoo (obj, null);
  885. Attachable.SetFoo (obj.Value, null);
  886. }
  887. }
  888. [Test]
  889. [Category ("NotWorking")] // cosmetic attribute order difference
  890. public void Write_AbstractWrapper ()
  891. {
  892. var obj = new AbstractContainer () { Value2 = new DerivedObject () { Foo = "x" } };
  893. Assert.AreEqual (ReadXml ("AbstractContainer.xml").Trim (), XamlServices.Save (obj), "#1");
  894. }
  895. [Test]
  896. public void Write_ReadOnlyPropertyContainer ()
  897. {
  898. var obj = new ReadOnlyPropertyContainer () { Foo = "x" };
  899. Assert.AreEqual (ReadXml ("ReadOnlyPropertyContainer.xml").Trim (), XamlServices.Save (obj), "#1");
  900. var sw = new StringWriter ();
  901. var xw = new XamlXmlWriter (sw, new XamlSchemaContext ());
  902. var xt = xw.SchemaContext.GetXamlType (obj.GetType ());
  903. xw.WriteStartObject (xt);
  904. xw.WriteStartMember (xt.GetMember ("Bar"));
  905. xw.WriteValue ("x");
  906. xw.WriteEndMember ();
  907. xw.WriteEndObject ();
  908. xw.Close ();
  909. Assert.IsTrue (sw.ToString ().IndexOf ("Bar") > 0, "#2"); // it is not rejected by XamlXmlWriter. But XamlServices does not write it.
  910. }
  911. [Test]
  912. public void Write_TypeConverterOnListMember ()
  913. {
  914. var obj = new SecondTest.TypeOtherAssembly ();
  915. obj.Values.AddRange (new uint? [] {1, 2, 3});
  916. Assert.AreEqual (ReadXml ("TypeConverterOnListMember.xml").Trim (), XamlServices.Save (obj), "#1");
  917. }
  918. [Test]
  919. public void Write_EnumContainer ()
  920. {
  921. var obj = new EnumContainer () { EnumProperty = EnumValueType.Two };
  922. Assert.AreEqual (ReadXml ("EnumContainer.xml").Trim (), XamlServices.Save (obj), "#1");
  923. }
  924. [Test]
  925. public void Write_CollectionContentProperty ()
  926. {
  927. var obj = new CollectionContentProperty ();
  928. for (int i = 0; i < 4; i++)
  929. obj.ListOfItems.Add (new SimpleClass ());
  930. Assert.AreEqual (ReadXml ("CollectionContentProperty.xml").Trim (), XamlServices.Save (obj), "#1");
  931. }
  932. [Test]
  933. public void Write_CollectionContentPropertyX ()
  934. {
  935. var obj = new CollectionContentPropertyX ();
  936. var l = new List<object> ();
  937. obj.ListOfItems.Add (l);
  938. for (int i = 0; i < 4; i++)
  939. l.Add (new SimpleClass ());
  940. Assert.AreEqual (ReadXml ("CollectionContentPropertyX.xml").Trim (), XamlServices.Save (obj), "#1");
  941. }
  942. [Test]
  943. [Category ("NotWorking")] // TestProperty is written as element so far.
  944. public void Write_AmbientPropertyContainer ()
  945. {
  946. var obj = new SecondTest.ResourcesDict ();
  947. var t1 = new SecondTest.TestObject ();
  948. obj.Add ("TestDictItem", t1);
  949. var t2 = new SecondTest.TestObject ();
  950. t2.TestProperty = t1;
  951. obj.Add ("okay", t2);
  952. Assert.AreEqual (ReadXml ("AmbientPropertyContainer.xml").Trim (), XamlServices.Save (obj), "#1");
  953. }
  954. [Test]
  955. public void Write_NullableContainer ()
  956. {
  957. var obj = new NullableContainer () { TestProp = 5 };
  958. Assert.AreEqual (ReadXml ("NullableContainer.xml").Trim (), XamlServices.Save (obj), "#1");
  959. }
  960. }
  961. public class TestXmlWriterClass1
  962. {
  963. public int Foo { get; set; }
  964. }
  965. }