XmlCodeExporterTests.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. [Category ("NotWorking")]
  915. public void ExportSimpleContentExtensionEnum ()
  916. {
  917. string xsd = @"
  918. <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:b='urn:bar' targetNamespace='urn:bar'>
  919. <xs:element name='Foo' type='b:DayOfWeek' />
  920. <xs:complexType name='DayOfWeek'>
  921. <xs:simpleContent>
  922. <xs:extension base='b:WeekDay' />
  923. </xs:simpleContent>
  924. </xs:complexType>
  925. <xs:simpleType name='WeekDay'>
  926. <xs:restriction base='xs:string'>
  927. <xs:enumeration value='Sunday'/>
  928. <xs:enumeration value='Monday'/>
  929. <xs:enumeration value='Tuesday'/>
  930. <xs:enumeration value='Wednesday'/>
  931. <xs:enumeration value='Thursday'/>
  932. <xs:enumeration value='Friday'/>
  933. <xs:enumeration value='Saturday'/>
  934. </xs:restriction>
  935. </xs:simpleType>
  936. </xs:schema>";
  937. XmlSchema xs = XmlSchema.Read (new StringReader (xsd), null);
  938. XmlSchemas xss = new XmlSchemas ();
  939. xss.Add (xs);
  940. XmlSchemaImporter imp = new XmlSchemaImporter (xss);
  941. XmlTypeMapping m = imp.ImportTypeMapping (new XmlQualifiedName ("Foo", "urn:bar"));
  942. CodeNamespace cns = new CodeNamespace ();
  943. XmlCodeExporter exp = new XmlCodeExporter (cns);
  944. exp.ExportTypeMapping (m);
  945. CodeTypeDeclaration enumType = null;
  946. foreach (CodeTypeDeclaration ctd in cns.Types)
  947. if (ctd.Name == "WeekDay")
  948. enumType = ctd;
  949. Assert.IsNotNull (enumType);
  950. }
  951. CodeNamespace ExportCode (Type type)
  952. {
  953. XmlReflectionImporter imp = new XmlReflectionImporter ();
  954. XmlTypeMapping map = imp.ImportTypeMapping (type);
  955. CodeNamespace codeNamespace = new CodeNamespace ();
  956. XmlCodeExporter exp = new XmlCodeExporter (codeNamespace);
  957. exp.ExportTypeMapping (map);
  958. return codeNamespace;
  959. }
  960. #if NET_2_0
  961. string XmlFileVersion {
  962. get {
  963. Assembly xmlAsm = typeof (XmlDocument).Assembly;
  964. AssemblyFileVersionAttribute afv = (AssemblyFileVersionAttribute)
  965. Attribute.GetCustomAttribute (xmlAsm, typeof (AssemblyFileVersionAttribute));
  966. return afv.Version;
  967. }
  968. }
  969. #endif
  970. [XmlRootAttribute ("root", Namespace="urn:aNS", IsNullable=false)]
  971. public class Root
  972. {
  973. public OptionalValueTypeContainer OptionalValue;
  974. public TestDefault Default;
  975. }
  976. }
  977. }