XmlCodeExporterTests.cs 40 KB

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