XmlCodeExporterTests.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. //
  2. // System.Xml.Serialization.XmlCodeExporterTests
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (C) 2006 Novell
  8. //
  9. using System;
  10. using System.CodeDom;
  11. using System.CodeDom.Compiler;
  12. using System.Collections;
  13. using System.Globalization;
  14. using System.IO;
  15. #if NET_2_0
  16. using System.Reflection;
  17. #endif
  18. using System.Xml;
  19. using System.Xml.Schema;
  20. using System.Xml.Serialization;
  21. using Microsoft.CSharp;
  22. using NUnit.Framework;
  23. using MonoTests.System.Xml.TestClasses;
  24. namespace MonoTests.System.XmlSerialization
  25. {
  26. [TestFixture]
  27. public class XmlCodeExporterTests
  28. {
  29. [Test]
  30. public void ExportTypeMapping_ArrayClass ()
  31. {
  32. CodeNamespace codeNamespace = ExportCode (typeof (ArrayClass));
  33. Assert.IsNotNull (codeNamespace, "#1");
  34. StringWriter sw = new StringWriter ();
  35. CodeDomProvider provider = new CSharpCodeProvider ();
  36. ICodeGenerator generator = provider.CreateGenerator ();
  37. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  38. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  39. "{0}{0}" +
  40. "/// <remarks/>{0}" +
  41. #if NET_2_0
  42. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  43. "[System.SerializableAttribute()]{0}" +
  44. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  45. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  46. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  47. "public partial class ArrayClass {{{0}" +
  48. " {0}" +
  49. " private object namesField;{0}" +
  50. " {0}" +
  51. " /// <remarks/>{0}" +
  52. " public object names {{{0}" +
  53. " get {{{0}" +
  54. " return this.namesField;{0}" +
  55. " }}{0}" +
  56. " set {{{0}" +
  57. " this.namesField = value;{0}" +
  58. " }}{0}" +
  59. " }}{0}" +
  60. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  61. #else
  62. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  63. "public class ArrayClass {{{0}" +
  64. " {0}" +
  65. " /// <remarks/>{0}" +
  66. " public object names;{0}" +
  67. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  68. #endif
  69. codeNamespace = ExportCode (typeof (ArrayClass[]));
  70. Assert.IsNotNull (codeNamespace, "#3");
  71. sw.GetStringBuilder ().Length = 0;
  72. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  73. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  74. "{0}{0}" +
  75. "/// <remarks/>{0}" +
  76. #if NET_2_0
  77. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  78. "[System.SerializableAttribute()]{0}" +
  79. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  80. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  81. "public partial class ArrayClass {{{0}" +
  82. " {0}" +
  83. " private object namesField;{0}" +
  84. " {0}" +
  85. " /// <remarks/>{0}" +
  86. " public object names {{{0}" +
  87. " get {{{0}" +
  88. " return this.namesField;{0}" +
  89. " }}{0}" +
  90. " set {{{0}" +
  91. " this.namesField = value;{0}" +
  92. " }}{0}" +
  93. " }}{0}" +
  94. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#4");
  95. #else
  96. "public class ArrayClass {{{0}" +
  97. " {0}" +
  98. " /// <remarks/>{0}" +
  99. " public object names;{0}" +
  100. "}}{0}", Environment.NewLine), sw.ToString (), "#4");
  101. #endif
  102. }
  103. [Test]
  104. public void ExportTypeMapping_ArrayContainer ()
  105. {
  106. CodeNamespace codeNamespace = ExportCode (typeof (ArrayContainer));
  107. Assert.IsNotNull (codeNamespace, "#1");
  108. StringWriter sw = new StringWriter ();
  109. CodeDomProvider provider = new CSharpCodeProvider ();
  110. ICodeGenerator generator = provider.CreateGenerator ();
  111. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  112. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  113. "{0}{0}" +
  114. "/// <remarks/>{0}" +
  115. #if NET_2_0
  116. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  117. "[System.SerializableAttribute()]{0}" +
  118. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  119. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  120. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  121. "public partial class ArrayContainer {{{0}" +
  122. " {0}" +
  123. " private object[] itemsField;{0}" +
  124. " {0}" +
  125. " /// <remarks/>{0}" +
  126. " public object[] items {{{0}" +
  127. " get {{{0}" +
  128. " return this.itemsField;{0}" +
  129. " }}{0}" +
  130. " set {{{0}" +
  131. " this.itemsField = value;{0}" +
  132. " }}{0}" +
  133. " }}{0}" +
  134. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  135. #else
  136. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  137. "public class ArrayContainer {{{0}" +
  138. " {0}" +
  139. " /// <remarks/>{0}" +
  140. " public object[] items;{0}" +
  141. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  142. #endif
  143. }
  144. [Test]
  145. public void ExportTypeMapping_CDataContainer ()
  146. {
  147. CodeNamespace codeNamespace = ExportCode (typeof (CDataContainer));
  148. Assert.IsNotNull (codeNamespace, "#1");
  149. StringWriter sw = new StringWriter ();
  150. CodeDomProvider provider = new CSharpCodeProvider ();
  151. ICodeGenerator generator = provider.CreateGenerator ();
  152. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  153. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  154. "{0}{0}" +
  155. "/// <remarks/>{0}" +
  156. #if NET_2_0
  157. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  158. "[System.SerializableAttribute()]{0}" +
  159. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  160. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  161. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  162. "public partial class CDataContainer {{{0}" +
  163. " {0}" +
  164. " private System.Xml.XmlCDataSection cdataField;{0}" +
  165. " {0}" +
  166. " /// <remarks/>{0}" +
  167. " public System.Xml.XmlCDataSection cdata {{{0}" +
  168. " get {{{0}" +
  169. " return this.cdataField;{0}" +
  170. " }}{0}" +
  171. " set {{{0}" +
  172. " this.cdataField = value;{0}" +
  173. " }}{0}" +
  174. " }}{0}" +
  175. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  176. #else
  177. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  178. "public class CDataContainer {{{0}" +
  179. " {0}" +
  180. " /// <remarks/>{0}" +
  181. " public System.Xml.XmlCDataSection cdata;{0}" +
  182. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  183. #endif
  184. }
  185. [Test]
  186. [Category ("NotWorking")] // order of XmlElementAttribute's does not match that of MSFT
  187. [Category ("NotDotNet")] // Mono bug ##77117 and MS.NET randomly modifies the order of the elements!
  188. public void ExportTypeMapping_Choices ()
  189. {
  190. CodeNamespace codeNamespace = ExportCode (typeof (Choices));
  191. Assert.IsNotNull (codeNamespace, "#1");
  192. StringWriter sw = new StringWriter ();
  193. CodeDomProvider provider = new CSharpCodeProvider ();
  194. ICodeGenerator generator = provider.CreateGenerator ();
  195. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  196. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  197. "{0}{0}" +
  198. "/// <remarks/>{0}" +
  199. #if NET_2_0
  200. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  201. "[System.SerializableAttribute()]{0}" +
  202. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  203. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  204. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  205. "public partial class Choices {{{0}" +
  206. " {0}" +
  207. " private string myChoiceField;{0}" +
  208. " {0}" +
  209. " /// <remarks/>{0}" +
  210. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceZero\", typeof(string))]{0}" +
  211. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceOne\", typeof(string))]{0}" +
  212. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceTwo\", typeof(string))]{0}" +
  213. " [System.Xml.Serialization.XmlChoiceIdentifierAttribute(\"ItemType\")]{0}" +
  214. " public string MyChoice {{{0}" +
  215. " get {{{0}" +
  216. " return this.myChoiceField;{0}" +
  217. " }}{0}" +
  218. " set {{{0}" +
  219. " this.myChoiceField = value;{0}" +
  220. " }}{0}" +
  221. " }}{0}" +
  222. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  223. #else
  224. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  225. "public class Choices {{{0}" +
  226. " {0}" +
  227. " /// <remarks/>{0}" +
  228. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceZero\", typeof(string))]{0}" +
  229. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceTwo\", typeof(string))]{0}" +
  230. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceOne\", typeof(string))]{0}" +
  231. " [System.Xml.Serialization.XmlChoiceIdentifierAttribute(\"ItemType\")]{0}" +
  232. " public string MyChoice;{0}" +
  233. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  234. #endif
  235. }
  236. [Test]
  237. #if NET_2_0
  238. [Category ("NotDotNet")] // regression in MS.NET 2.0
  239. #endif
  240. [Category ("NotWorking")] // TODO: order of DefaultValueAttribute, ...
  241. public void ExportTypeMapping_Field ()
  242. {
  243. CodeNamespace codeNamespace = ExportCode (typeof (Field));
  244. Assert.IsNotNull (codeNamespace, "#1");
  245. StringWriter sw = new StringWriter ();
  246. CodeDomProvider provider = new CSharpCodeProvider ();
  247. ICodeGenerator generator = provider.CreateGenerator ();
  248. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  249. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  250. "{0}{0}" +
  251. "/// <remarks/>{0}" +
  252. #if NET_2_0
  253. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  254. "[System.SerializableAttribute()]{0}" +
  255. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  256. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  257. "[System.Xml.Serialization.XmlRootAttribute(\"field\", Namespace=\"\", IsNullable=true)]{0}" +
  258. "public partial class Field {{{0}" +
  259. " {0}" +
  260. " private MonoTests.System.Xml.TestClasses.FlagEnum flags1Field = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  261. " {0}" +
  262. " private MonoTests.System.Xml.TestClasses.FlagEnum flags2Field = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  263. " {0}" +
  264. " private MonoTests.System.Xml.TestClasses.FlagEnum flags3Field = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2);{0}" +
  265. " {0}" +
  266. " private MonoTests.System.Xml.TestClasses.FlagEnum flags4Field;{0}" +
  267. " {0}" +
  268. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiersField;{0}" +
  269. " {0}" +
  270. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers2Field;{0}" +
  271. " {0}" +
  272. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers3Field = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  273. " {0}" +
  274. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers4Field = MonoTests.System.Xml.TestClasses.MapModifiers.Protected;{0}" +
  275. " {0}" +
  276. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers5Field = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  277. " {0}" +
  278. " private string[] namesField;{0}" +
  279. " {0}" +
  280. " private string streetField;{0}" +
  281. " {0}" +
  282. " /// <remarks/>{0}" +
  283. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag1\")]{0}" +
  284. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  285. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags1 {{{0}" +
  286. " get {{{0}" +
  287. " return this.flags1Field;{0}" +
  288. " }}{0}" +
  289. " set {{{0}" +
  290. " this.flags1Field = value;{0}" +
  291. " }}{0}" +
  292. " }}{0}" +
  293. " {0}" +
  294. " /// <remarks/>{0}" +
  295. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag2\")]{0}" +
  296. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  297. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags2 {{{0}" +
  298. " get {{{0}" +
  299. " return this.flags2Field;{0}" +
  300. " }}{0}" +
  301. " set {{{0}" +
  302. " this.flags2Field = value;{0}" +
  303. " }}{0}" +
  304. " }}{0}" +
  305. " {0}" +
  306. " /// <remarks/>{0}" +
  307. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag3\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  308. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2))]{0}" +
  309. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags3 {{{0}" +
  310. " get {{{0}" +
  311. " return this.flags3Field;{0}" +
  312. " }}{0}" +
  313. " set {{{0}" +
  314. " this.flags3Field = value;{0}" +
  315. " }}{0}" +
  316. " }}{0}" +
  317. " {0}" +
  318. " /// <remarks/>{0}" +
  319. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag4\")]{0}" +
  320. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags4 {{{0}" +
  321. " get {{{0}" +
  322. " return this.flags4Field;{0}" +
  323. " }}{0}" +
  324. " set {{{0}" +
  325. " this.flags4Field = value;{0}" +
  326. " }}{0}" +
  327. " }}{0}" +
  328. " {0}" +
  329. " /// <remarks/>{0}" +
  330. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers\")]{0}" +
  331. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers {{{0}" +
  332. " get {{{0}" +
  333. " return this.modifiersField;{0}" +
  334. " }}{0}" +
  335. " set {{{0}" +
  336. " this.modifiersField = value;{0}" +
  337. " }}{0}" +
  338. " }}{0}" +
  339. " {0}" +
  340. " /// <remarks/>{0}" +
  341. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers2\")]{0}" +
  342. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers2 {{{0}" +
  343. " get {{{0}" +
  344. " return this.modifiers2Field;{0}" +
  345. " }}{0}" +
  346. " set {{{0}" +
  347. " this.modifiers2Field = value;{0}" +
  348. " }}{0}" +
  349. " }}{0}" +
  350. " {0}" +
  351. " /// <remarks/>{0}" +
  352. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers3\")]{0}" +
  353. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Public)]{0}" +
  354. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers3 {{{0}" +
  355. " get {{{0}" +
  356. " return this.modifiers3Field;{0}" +
  357. " }}{0}" +
  358. " set {{{0}" +
  359. " this.modifiers3Field = value;{0}" +
  360. " }}{0}" +
  361. " }}{0}" +
  362. " {0}" +
  363. " /// <remarks/>{0}" +
  364. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers4\")]{0}" +
  365. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Protected)]{0}" +
  366. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers4 {{{0}" +
  367. " get {{{0}" +
  368. " return this.modifiers4Field;{0}" +
  369. " }}{0}" +
  370. " set {{{0}" +
  371. " this.modifiers4Field = value;{0}" +
  372. " }}{0}" +
  373. " }}{0}" +
  374. " {0}" +
  375. " /// <remarks/>{0}" +
  376. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers5\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  377. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Public)]{0}" +
  378. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers5 {{{0}" +
  379. " get {{{0}" +
  380. " return this.modifiers5Field;{0}" +
  381. " }}{0}" +
  382. " set {{{0}" +
  383. " this.modifiers5Field = value;{0}" +
  384. " }}{0}" +
  385. " }}{0}" +
  386. " {0}" +
  387. " /// <remarks/>{0}" +
  388. " [System.Xml.Serialization.XmlAttributeAttribute(\"names\")]{0}" +
  389. " public string[] Names {{{0}" +
  390. " get {{{0}" +
  391. " return this.namesField;{0}" +
  392. " }}{0}" +
  393. " set {{{0}" +
  394. " this.namesField = value;{0}" +
  395. " }}{0}" +
  396. " }}{0}" +
  397. " {0}" +
  398. " /// <remarks/>{0}" +
  399. " [System.Xml.Serialization.XmlAttributeAttribute(\"street\")]{0}" +
  400. " public string Street {{{0}" +
  401. " get {{{0}" +
  402. " return this.streetField;{0}" +
  403. " }}{0}" +
  404. " set {{{0}" +
  405. " this.streetField = value;{0}" +
  406. " }}{0}" +
  407. " }}{0}" +
  408. #else
  409. "[System.Xml.Serialization.XmlRootAttribute(\"field\", Namespace=\"\", IsNullable=true)]{0}" +
  410. "public class Field {{{0}" +
  411. " {0}" +
  412. " /// <remarks/>{0}" +
  413. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag1\")]{0}" +
  414. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  415. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags1 = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  416. " {0}" +
  417. " /// <remarks/>{0}" +
  418. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag2\")]{0}" +
  419. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  420. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags2 = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  421. " {0}" +
  422. " /// <remarks/>{0}" +
  423. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag3\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  424. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2))]{0}" +
  425. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags3 = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2);{0}" +
  426. " {0}" +
  427. " /// <remarks/>{0}" +
  428. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag4\")]{0}" +
  429. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags4;{0}" +
  430. " {0}" +
  431. " /// <remarks/>{0}" +
  432. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers\")]{0}" +
  433. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers;{0}" +
  434. " {0}" +
  435. " /// <remarks/>{0}" +
  436. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers2\")]{0}" +
  437. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers2;{0}" +
  438. " {0}" +
  439. " /// <remarks/>{0}" +
  440. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers3\")]{0}" +
  441. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Public)]{0}" +
  442. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers3 = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  443. " {0}" +
  444. " /// <remarks/>{0}" +
  445. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers4\")]{0}" +
  446. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Protected)]{0}" +
  447. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers4 = MonoTests.System.Xml.TestClasses.MapModifiers.Protected;{0}" +
  448. " {0}" +
  449. " /// <remarks/>{0}" +
  450. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers5\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  451. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Public)]{0}" +
  452. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers5 = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  453. " {0}" +
  454. " /// <remarks/>{0}" +
  455. " [System.Xml.Serialization.XmlAttributeAttribute(\"names\")]{0}" +
  456. " public string[] Names;{0}" +
  457. " {0}" +
  458. " /// <remarks/>{0}" +
  459. " [System.Xml.Serialization.XmlAttributeAttribute(\"street\")]{0}" +
  460. " public string Street;{0}" +
  461. #endif
  462. "}}{0}" +
  463. "{0}" +
  464. "/// <remarks/>{0}" +
  465. "[System.FlagsAttribute()]{0}" +
  466. #if NET_2_0
  467. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  468. "[System.SerializableAttribute()]{0}" +
  469. #endif
  470. "public enum FlagEnum {{{0}" +
  471. " {0}" +
  472. " /// <remarks/>{0}" +
  473. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  474. " e1 = 1,{0}" +
  475. " {0}" +
  476. " /// <remarks/>{0}" +
  477. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  478. " e2 = 2,{0}" +
  479. " {0}" +
  480. " /// <remarks/>{0}" +
  481. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  482. " e4 = 4,{0}" +
  483. "}}{0}" +
  484. "{0}" +
  485. "/// <remarks/>{0}" +
  486. "[System.FlagsAttribute()]{0}" +
  487. #if NET_2_0
  488. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  489. "[System.SerializableAttribute()]{0}" +
  490. #endif
  491. "public enum MapModifiers {{{0}" +
  492. " {0}" +
  493. " /// <remarks/>{0}" +
  494. " [System.Xml.Serialization.XmlEnumAttribute(\"public\")]{0}" +
  495. " Public = 1,{0}" +
  496. " {0}" +
  497. " /// <remarks/>{0}" +
  498. " [System.Xml.Serialization.XmlEnumAttribute(\"protected\")]{0}" +
  499. " Protected = 2,{0}" +
  500. #if NET_2_0
  501. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  502. #else
  503. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  504. #endif
  505. }
  506. [Test]
  507. public void ExportTypeMapping_ItemChoiceType ()
  508. {
  509. CodeNamespace codeNamespace = ExportCode (typeof (ItemChoiceType));
  510. Assert.IsNotNull (codeNamespace, "#1");
  511. StringWriter sw = new StringWriter ();
  512. CodeDomProvider provider = new CSharpCodeProvider ();
  513. ICodeGenerator generator = provider.CreateGenerator ();
  514. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  515. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  516. "{0}{0}" +
  517. "/// <remarks/>{0}" +
  518. #if NET_2_0
  519. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  520. "[System.SerializableAttribute()]{0}" +
  521. #endif
  522. "[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]{0}" +
  523. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=false)]{0}" +
  524. "public enum ItemChoiceType {{{0}" +
  525. " {0}" +
  526. " /// <remarks/>{0}" +
  527. " ChoiceZero,{0}" +
  528. " {0}" +
  529. " /// <remarks/>{0}" +
  530. " [System.Xml.Serialization.XmlEnumAttribute(\"ChoiceOne\")]{0}" +
  531. " StrangeOne,{0}" +
  532. " {0}" +
  533. " /// <remarks/>{0}" +
  534. " ChoiceTwo,{0}" +
  535. #if NET_2_0
  536. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  537. #else
  538. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  539. #endif
  540. codeNamespace = ExportCode (typeof (ItemChoiceType[]));
  541. Assert.IsNotNull (codeNamespace, "#3");
  542. sw.GetStringBuilder ().Length = 0;
  543. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  544. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  545. "{0}{0}" +
  546. "/// <remarks/>{0}" +
  547. #if NET_2_0
  548. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  549. "[System.SerializableAttribute()]{0}" +
  550. #endif
  551. "[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]{0}" +
  552. "public enum ItemChoiceType {{{0}" +
  553. " {0}" +
  554. " /// <remarks/>{0}" +
  555. " ChoiceZero,{0}" +
  556. " {0}" +
  557. " /// <remarks/>{0}" +
  558. " [System.Xml.Serialization.XmlEnumAttribute(\"ChoiceOne\")]{0}" +
  559. " StrangeOne,{0}" +
  560. " {0}" +
  561. " /// <remarks/>{0}" +
  562. " ChoiceTwo,{0}" +
  563. #if NET_2_0
  564. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#4");
  565. #else
  566. "}}{0}", Environment.NewLine), sw.ToString (), "#4");
  567. #endif
  568. }
  569. [Test]
  570. public void ExportTypeMapping_ClassArrayContainer ()
  571. {
  572. CodeNamespace codeNamespace = ExportCode (typeof (ClassArrayContainer));
  573. Assert.IsNotNull (codeNamespace, "#1");
  574. StringWriter sw = new StringWriter ();
  575. CodeDomProvider provider = new CSharpCodeProvider ();
  576. ICodeGenerator generator = provider.CreateGenerator ();
  577. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  578. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  579. "{0}{0}" +
  580. "/// <remarks/>{0}" +
  581. #if NET_2_0
  582. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  583. "[System.SerializableAttribute()]{0}" +
  584. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  585. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  586. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  587. "public partial class ClassArrayContainer {{{0}" +
  588. " {0}" +
  589. " private MonoTests.System.Xml.TestClasses.SimpleClass[] itemsField;{0}" +
  590. " {0}" +
  591. " /// <remarks/>{0}" +
  592. " public MonoTests.System.Xml.TestClasses.SimpleClass[] items {{{0}" +
  593. " get {{{0}" +
  594. " return this.itemsField;{0}" +
  595. " }}{0}" +
  596. " set {{{0}" +
  597. " this.itemsField = value;{0}" +
  598. " }}{0}" +
  599. " }}{0}" +
  600. "}}{0}" +
  601. "{0}" +
  602. "/// <remarks/>{0}" +
  603. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  604. "[System.SerializableAttribute()]{0}" +
  605. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  606. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  607. "public partial class SimpleClass {{{0}" +
  608. " {0}" +
  609. " private string somethingField;{0}" +
  610. " {0}" +
  611. " /// <remarks/>{0}" +
  612. " public string something {{{0}" +
  613. " get {{{0}" +
  614. " return this.somethingField;{0}" +
  615. " }}{0}" +
  616. " set {{{0}" +
  617. " this.somethingField = value;{0}" +
  618. " }}{0}" +
  619. " }}{0}" +
  620. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  621. #else
  622. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  623. "public class ClassArrayContainer {{{0}" +
  624. " {0}" +
  625. " /// <remarks/>{0}" +
  626. " public MonoTests.System.Xml.TestClasses.SimpleClass[] items;{0}" +
  627. "}}{0}" +
  628. "{0}" +
  629. "/// <remarks/>{0}" +
  630. "public class SimpleClass {{{0}" +
  631. " {0}" +
  632. " /// <remarks/>{0}" +
  633. " public string something;{0}" +
  634. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  635. #endif
  636. }
  637. [Test]
  638. #if NET_2_0
  639. [Category ("NotDotNet")] // regression in MS.NET 2.0
  640. #endif
  641. [Category ("NotWorking")] // bug #78214
  642. public void ExportTypeMapping_Root ()
  643. {
  644. CodeNamespace codeNamespace = ExportCode (typeof (Root));
  645. Assert.IsNotNull (codeNamespace, "#1");
  646. StringWriter sw = new StringWriter ();
  647. CodeDomProvider provider = new CSharpCodeProvider ();
  648. ICodeGenerator generator = provider.CreateGenerator ();
  649. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  650. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  651. "{0}{0}" +
  652. "/// <remarks/>{0}" +
  653. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:aNS\")]{0}" +
  654. "[System.Xml.Serialization.XmlRootAttribute(\"root\", Namespace=\"urn:aNS\", IsNullable=false)]{0}" +
  655. "public class Root {{{0}" +
  656. " {0}" +
  657. " /// <remarks/>{0}" +
  658. " public MonoTests.System.Xml.TestClasses.OptionalValueTypeContainer OptionalValue;{0}" +
  659. " {0}" +
  660. " /// <remarks/>{0}" +
  661. " public MonoTests.System.Xml.TestClasses.TestDefault Default;{0}" +
  662. "}}{0}" +
  663. "{0}" +
  664. "/// <remarks/>{0}" +
  665. "[System.Xml.Serialization.XmlTypeAttribute(TypeName=\"optionalValueType\", Namespace=\"some:urn\")]{0}" +
  666. "public class OptionalValueTypeContainer {{{0}" +
  667. " {0}" +
  668. " /// <remarks/>{0}" +
  669. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4))]{0}" +
  670. " public MonoTests.System.Xml.TestClasses.FlagEnum Attributes = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  671. " {0}" +
  672. " /// <remarks/>{0}" +
  673. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  674. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  675. " {0}" +
  676. " /// <remarks/>{0}" +
  677. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  678. " public bool FlagsSpecified;{0}" +
  679. " {0}" +
  680. " /// <remarks/>{0}" +
  681. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  682. " public bool IsEmpty = false;{0}" +
  683. " {0}" +
  684. " /// <remarks/>{0}" +
  685. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  686. " public bool IsEmptySpecified;{0}" +
  687. " {0}" +
  688. " /// <remarks/>{0}" +
  689. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  690. " public bool IsNull = false;{0}" +
  691. "}}{0}" +
  692. "{0}" +
  693. "/// <remarks/>{0}" +
  694. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"some:urn\")]{0}" +
  695. "[System.FlagsAttribute()]{0}" +
  696. "public enum FlagEnum {{{0}" +
  697. " {0}" +
  698. " /// <remarks/>{0}" +
  699. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  700. " e1 = 1,{0}" +
  701. " {0}" +
  702. " /// <remarks/>{0}" +
  703. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  704. " e2 = 2,{0}" +
  705. " {0}" +
  706. " /// <remarks/>{0}" +
  707. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  708. " e4 = 4,{0}" +
  709. "}}{0}" +
  710. "{0}" +
  711. "/// <remarks/>{0}" +
  712. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  713. "public class TestDefault {{{0}" +
  714. " {0}" +
  715. " /// <remarks/>{0}" +
  716. " public string str;{0}" +
  717. " {0}" +
  718. " /// <remarks/>{0}" +
  719. " [System.ComponentModel.DefaultValueAttribute(\"Default Value\")]{0}" +
  720. " public string strDefault = \"Default Value\";{0}" +
  721. " {0}" +
  722. " /// <remarks/>{0}" +
  723. " [System.ComponentModel.DefaultValueAttribute(true)]{0}" +
  724. " public bool boolT = true;{0}" +
  725. " {0}" +
  726. " /// <remarks/>{0}" +
  727. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  728. " public bool boolF = false;{0}" +
  729. " {0}" +
  730. " /// <remarks/>{0}" +
  731. " [System.ComponentModel.DefaultValueAttribute(typeof(System.Decimal), \"10\")]{0}" +
  732. " public System.Decimal decimalval = ((System.Decimal)(10m));{0}" +
  733. " {0}" +
  734. " /// <remarks/>{0}" +
  735. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4))]{0}" +
  736. " public MonoTests.System.Xml.TestClasses.FlagEnum flag = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  737. " {0}" +
  738. " /// <remarks/>{0}" +
  739. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4))]{0}" +
  740. " public MonoTests.System.Xml.TestClasses.FlagEnum_Encoded flagencoded = (MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4);{0}" +
  741. "}}{0}" +
  742. "{0}" +
  743. "/// <remarks/>{0}" +
  744. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  745. "[System.FlagsAttribute()]{0}" +
  746. "public enum FlagEnum {{{0}" +
  747. " {0}" +
  748. " /// <remarks/>{0}" +
  749. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  750. " e1 = 1,{0}" +
  751. " {0}" +
  752. " /// <remarks/>{0}" +
  753. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  754. " e2 = 2,{0}" +
  755. " {0}" +
  756. " /// <remarks/>{0}" +
  757. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  758. " e4 = 4,{0}" +
  759. "}}{0}" +
  760. "{0}" +
  761. "/// <remarks/>{0}" +
  762. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  763. "[System.FlagsAttribute()]{0}" +
  764. "public enum FlagEnum_Encoded {{{0}" +
  765. " {0}" +
  766. " /// <remarks/>{0}" +
  767. " e1 = 1,{0}" +
  768. " {0}" +
  769. " /// <remarks/>{0}" +
  770. " e2 = 2,{0}" +
  771. " {0}" +
  772. " /// <remarks/>{0}" +
  773. " e4 = 4,{0}" +
  774. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  775. }
  776. [Test]
  777. public void ExportTypeMapping_SimpleClassWithXmlAttributes ()
  778. {
  779. CodeNamespace codeNamespace = ExportCode (typeof (SimpleClassWithXmlAttributes));
  780. Assert.IsNotNull (codeNamespace, "#1");
  781. StringWriter sw = new StringWriter ();
  782. CodeDomProvider provider = new CSharpCodeProvider ();
  783. ICodeGenerator generator = provider.CreateGenerator ();
  784. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  785. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  786. "{0}{0}" +
  787. "/// <remarks/>{0}" +
  788. #if NET_2_0
  789. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  790. "[System.SerializableAttribute()]{0}" +
  791. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  792. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  793. "[System.Xml.Serialization.XmlRootAttribute(\"simple\", Namespace=\"\", IsNullable=true)]{0}" +
  794. "public partial class SimpleClassWithXmlAttributes {{{0}" +
  795. " {0}" +
  796. " private string somethingField;{0}" +
  797. " {0}" +
  798. " /// <remarks/>{0}" +
  799. " [System.Xml.Serialization.XmlAttributeAttribute(\"member\")]{0}" +
  800. " public string something {{{0}" +
  801. " get {{{0}" +
  802. " return this.somethingField;{0}" +
  803. " }}{0}" +
  804. " set {{{0}" +
  805. " this.somethingField = value;{0}" +
  806. " }}{0}" +
  807. " }}{0}" +
  808. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  809. #else
  810. "[System.Xml.Serialization.XmlRootAttribute(\"simple\", Namespace=\"\", IsNullable=true)]{0}" +
  811. "public class SimpleClassWithXmlAttributes {{{0}" +
  812. " {0}" +
  813. " /// <remarks/>{0}" +
  814. " [System.Xml.Serialization.XmlAttributeAttribute(\"member\")]{0}" +
  815. " public string something;{0}" +
  816. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  817. #endif
  818. }
  819. [Test]
  820. public void ExportTypeMapping_XsdPrimitive_Arrays ()
  821. {
  822. ArrayList types = new ArrayList ();
  823. types.Add (typeof (sbyte[]));
  824. types.Add (typeof (bool[]));
  825. types.Add (typeof (short[]));
  826. types.Add (typeof (int[]));
  827. types.Add (typeof (long[]));
  828. types.Add (typeof (float[]));
  829. types.Add (typeof (double[]));
  830. types.Add (typeof (decimal[]));
  831. types.Add (typeof (ushort[]));
  832. types.Add (typeof (uint[]));
  833. types.Add (typeof (ulong[]));
  834. types.Add (typeof (DateTime[]));
  835. types.Add (typeof (XmlQualifiedName[]));
  836. types.Add (typeof (string[]));
  837. StringWriter sw = new StringWriter ();
  838. CodeDomProvider provider = new CSharpCodeProvider ();
  839. ICodeGenerator generator = provider.CreateGenerator ();
  840. foreach (Type type in types) {
  841. CodeNamespace codeNamespace = ExportCode (type);
  842. Assert.IsNotNull (codeNamespace, type.FullName + "#1");
  843. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  844. Assert.AreEqual (Environment.NewLine, sw.ToString (),
  845. type.FullName + "#2");
  846. sw.GetStringBuilder ().Length = 0;
  847. }
  848. }
  849. [Test]
  850. public void ExportTypeMapping_ZeroFlagEnum ()
  851. {
  852. CodeNamespace codeNamespace = ExportCode (typeof (ZeroFlagEnum));
  853. Assert.IsNotNull (codeNamespace, "#1");
  854. StringWriter sw = new StringWriter ();
  855. CodeDomProvider provider = new CSharpCodeProvider ();
  856. ICodeGenerator generator = provider.CreateGenerator ();
  857. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  858. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  859. "{0}{0}" +
  860. "/// <remarks/>{0}" +
  861. "[System.FlagsAttribute()]{0}" +
  862. #if NET_2_0
  863. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  864. "[System.SerializableAttribute()]{0}" +
  865. #endif
  866. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=false)]{0}" +
  867. "public enum ZeroFlagEnum {{{0}" +
  868. " {0}" +
  869. " /// <remarks/>{0}" +
  870. " [System.Xml.Serialization.XmlEnumAttribute(\"zero\")]{0}" +
  871. " e0 = 1,{0}" +
  872. " {0}" +
  873. " /// <remarks/>{0}" +
  874. " [System.Xml.Serialization.XmlEnumAttribute(\"o<n>e\")]{0}" +
  875. " e1 = 2,{0}" +
  876. " {0}" +
  877. " /// <remarks/>{0}" +
  878. " [System.Xml.Serialization.XmlEnumAttribute(\"tns:t<w>o\")]{0}" +
  879. " e2 = 4,{0}" +
  880. #if NET_2_0
  881. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  882. #else
  883. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  884. #endif
  885. }
  886. CodeNamespace ExportCode (Type type)
  887. {
  888. XmlReflectionImporter imp = new XmlReflectionImporter ();
  889. XmlTypeMapping map = imp.ImportTypeMapping (type);
  890. CodeNamespace codeNamespace = new CodeNamespace ();
  891. XmlCodeExporter exp = new XmlCodeExporter (codeNamespace);
  892. exp.ExportTypeMapping (map);
  893. return codeNamespace;
  894. }
  895. #if NET_2_0
  896. string XmlFileVersion {
  897. get {
  898. Assembly xmlAsm = typeof (XmlDocument).Assembly;
  899. AssemblyFileVersionAttribute afv = (AssemblyFileVersionAttribute)
  900. Attribute.GetCustomAttribute (xmlAsm, typeof (AssemblyFileVersionAttribute));
  901. return afv.Version;
  902. }
  903. }
  904. #endif
  905. [XmlRootAttribute ("root", Namespace="urn:aNS", IsNullable=false)]
  906. public class Root
  907. {
  908. public OptionalValueTypeContainer OptionalValue;
  909. public TestDefault Default;
  910. }
  911. }
  912. }