XmlCodeExporterTests.cs 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. [Category ("NotWorking")] // TODO: order of DefaultValueAttribute, ...
  239. public void ExportTypeMapping_Field ()
  240. {
  241. CodeNamespace codeNamespace = ExportCode (typeof (Field));
  242. Assert.IsNotNull (codeNamespace, "#1");
  243. StringWriter sw = new StringWriter ();
  244. CodeDomProvider provider = new CSharpCodeProvider ();
  245. ICodeGenerator generator = provider.CreateGenerator ();
  246. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  247. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  248. "{0}{0}" +
  249. "/// <remarks/>{0}" +
  250. #if NET_2_0
  251. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  252. "[System.SerializableAttribute()]{0}" +
  253. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  254. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  255. "[System.Xml.Serialization.XmlRootAttribute(\"field\", Namespace=\"\", IsNullable=true)]{0}" +
  256. "public partial class Field {{{0}" +
  257. " {0}" +
  258. " private MonoTests.System.Xml.TestClasses.FlagEnum flags1Field;{0}" +
  259. " {0}" +
  260. " private MonoTests.System.Xml.TestClasses.FlagEnum flags2Field;{0}" +
  261. " {0}" +
  262. " private MonoTests.System.Xml.TestClasses.FlagEnum flags3Field;{0}" +
  263. " {0}" +
  264. " private MonoTests.System.Xml.TestClasses.FlagEnum flags4Field;{0}" +
  265. " {0}" +
  266. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiersField;{0}" +
  267. " {0}" +
  268. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers2Field;{0}" +
  269. " {0}" +
  270. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers3Field;{0}" +
  271. " {0}" +
  272. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers4Field;{0}" +
  273. " {0}" +
  274. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers5Field;{0}" +
  275. " {0}" +
  276. " private string[] namesField;{0}" +
  277. " {0}" +
  278. " private string streetField;{0}" +
  279. " {0}" +
  280. " public Field() {{{0}" +
  281. " this.flags1Field = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  282. " this.flags2Field = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  283. " this.flags3Field = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2);{0}" +
  284. " this.modifiers3Field = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  285. " this.modifiers4Field = MonoTests.System.Xml.TestClasses.MapModifiers.Protected;{0}" +
  286. " this.modifiers5Field = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  287. " }}{0}" +
  288. " {0}" +
  289. " /// <remarks/>{0}" +
  290. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag1\")]{0}" +
  291. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags1 {{{0}" +
  292. " get {{{0}" +
  293. " return this.flags1Field;{0}" +
  294. " }}{0}" +
  295. " set {{{0}" +
  296. " this.flags1Field = value;{0}" +
  297. " }}{0}" +
  298. " }}{0}" +
  299. " {0}" +
  300. " /// <remarks/>{0}" +
  301. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag2\")]{0}" +
  302. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags2 {{{0}" +
  303. " get {{{0}" +
  304. " return this.flags2Field;{0}" +
  305. " }}{0}" +
  306. " set {{{0}" +
  307. " this.flags2Field = value;{0}" +
  308. " }}{0}" +
  309. " }}{0}" +
  310. " {0}" +
  311. " /// <remarks/>{0}" +
  312. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag3\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  313. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags3 {{{0}" +
  314. " get {{{0}" +
  315. " return this.flags3Field;{0}" +
  316. " }}{0}" +
  317. " set {{{0}" +
  318. " this.flags3Field = value;{0}" +
  319. " }}{0}" +
  320. " }}{0}" +
  321. " {0}" +
  322. " /// <remarks/>{0}" +
  323. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag4\")]{0}" +
  324. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags4 {{{0}" +
  325. " get {{{0}" +
  326. " return this.flags4Field;{0}" +
  327. " }}{0}" +
  328. " set {{{0}" +
  329. " this.flags4Field = value;{0}" +
  330. " }}{0}" +
  331. " }}{0}" +
  332. " {0}" +
  333. " /// <remarks/>{0}" +
  334. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers\")]{0}" +
  335. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers {{{0}" +
  336. " get {{{0}" +
  337. " return this.modifiersField;{0}" +
  338. " }}{0}" +
  339. " set {{{0}" +
  340. " this.modifiersField = value;{0}" +
  341. " }}{0}" +
  342. " }}{0}" +
  343. " {0}" +
  344. " /// <remarks/>{0}" +
  345. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers2\")]{0}" +
  346. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers2 {{{0}" +
  347. " get {{{0}" +
  348. " return this.modifiers2Field;{0}" +
  349. " }}{0}" +
  350. " set {{{0}" +
  351. " this.modifiers2Field = value;{0}" +
  352. " }}{0}" +
  353. " }}{0}" +
  354. " {0}" +
  355. " /// <remarks/>{0}" +
  356. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers3\")]{0}" +
  357. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers3 {{{0}" +
  358. " get {{{0}" +
  359. " return this.modifiers3Field;{0}" +
  360. " }}{0}" +
  361. " set {{{0}" +
  362. " this.modifiers3Field = value;{0}" +
  363. " }}{0}" +
  364. " }}{0}" +
  365. " {0}" +
  366. " /// <remarks/>{0}" +
  367. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers4\")]{0}" +
  368. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers4 {{{0}" +
  369. " get {{{0}" +
  370. " return this.modifiers4Field;{0}" +
  371. " }}{0}" +
  372. " set {{{0}" +
  373. " this.modifiers4Field = value;{0}" +
  374. " }}{0}" +
  375. " }}{0}" +
  376. " {0}" +
  377. " /// <remarks/>{0}" +
  378. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers5\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{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. [Category ("NotWorking")] // bug #78214
  640. public void ExportTypeMapping_Root ()
  641. {
  642. CodeNamespace codeNamespace = ExportCode (typeof (Root));
  643. Assert.IsNotNull (codeNamespace, "#1");
  644. StringWriter sw = new StringWriter ();
  645. CodeDomProvider provider = new CSharpCodeProvider ();
  646. ICodeGenerator generator = provider.CreateGenerator ();
  647. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  648. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  649. "{0}{0}" +
  650. "/// <remarks/>{0}" +
  651. #if NET_2_0
  652. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  653. "[System.SerializableAttribute()]{0}" +
  654. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  655. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  656. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:aNS\")]{0}" +
  657. "[System.Xml.Serialization.XmlRootAttribute(\"root\", Namespace=\"urn:aNS\", IsNullable=false)]{0}" +
  658. "public partial class Root {{{0}" +
  659. " {0}" +
  660. " private MonoTests.System.Xml.TestClasses.OptionalValueTypeContainer optionalValueField;{0}" +
  661. " {0}" +
  662. " private MonoTests.System.Xml.TestClasses.TestDefault defaultField;{0}" +
  663. " {0}" +
  664. " /// <remarks/>{0}" +
  665. " public MonoTests.System.Xml.TestClasses.OptionalValueTypeContainer OptionalValue {{{0}" +
  666. " get {{{0}" +
  667. " return this.optionalValueField;{0}" +
  668. " }}{0}" +
  669. " set {{{0}" +
  670. " this.optionalValueField = value;{0}" +
  671. " }}{0}" +
  672. " }}{0}" +
  673. " {0}" +
  674. " /// <remarks/>{0}" +
  675. " public MonoTests.System.Xml.TestClasses.TestDefault Default {{{0}" +
  676. " get {{{0}" +
  677. " return this.defaultField;{0}" +
  678. " }}{0}" +
  679. " set {{{0}" +
  680. " this.defaultField = value;{0}" +
  681. " }}{0}" +
  682. " }}{0}" +
  683. "}}{0}" +
  684. "{0}" +
  685. "/// <remarks/>{0}" +
  686. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  687. "[System.SerializableAttribute()]{0}" +
  688. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  689. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  690. "[System.Xml.Serialization.XmlTypeAttribute(TypeName=\"optionalValueType\", Namespace=\"some:urn\")]{0}" +
  691. "public partial class OptionalValueTypeContainer {{{0}" +
  692. " {0}" +
  693. " private MonoTests.System.Xml.TestClasses.FlagEnum attributesField;{0}" +
  694. " {0}" +
  695. " private MonoTests.System.Xml.TestClasses.FlagEnum flagsField;{0}" +
  696. " {0}" +
  697. " private bool flagsFieldSpecified;{0}" +
  698. " {0}" +
  699. " private bool isEmptyField;{0}" +
  700. " {0}" +
  701. " private bool isEmptyFieldSpecified;{0}" +
  702. " {0}" +
  703. " private bool isNullField;{0}" +
  704. " {0}" +
  705. " public OptionalValueTypeContainer() {{{0}" +
  706. " this.attributesField = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  707. " this.flagsField = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  708. " }}{0}" +
  709. " {0}" +
  710. " /// <remarks/>{0}" +
  711. " public MonoTests.System.Xml.TestClasses.FlagEnum Attributes {{{0}" +
  712. " get {{{0}" +
  713. " return this.attributesField;{0}" +
  714. " }}{0}" +
  715. " set {{{0}" +
  716. " this.attributesField = value;{0}" +
  717. " }}{0}" +
  718. " }}{0}" +
  719. " {0}" +
  720. " /// <remarks/>{0}" +
  721. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags {{{0}" +
  722. " get {{{0}" +
  723. " return this.flagsField;{0}" +
  724. " }}{0}" +
  725. " set {{{0}" +
  726. " this.flagsField = value;{0}" +
  727. " }}{0}" +
  728. " }}{0}" +
  729. " {0}" +
  730. " /// <remarks/>{0}" +
  731. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  732. " public bool FlagsSpecified {{{0}" +
  733. " get {{{0}" +
  734. " return this.flagsFieldSpecified;{0}" +
  735. " }}{0}" +
  736. " set {{{0}" +
  737. " this.flagsFieldSpecified = value;{0}" +
  738. " }}{0}" +
  739. " }}{0}" +
  740. " {0}" +
  741. " /// <remarks/>{0}" +
  742. " public bool IsEmpty {{{0}" +
  743. " get {{{0}" +
  744. " return this.isEmptyField;{0}" +
  745. " }}{0}" +
  746. " set {{{0}" +
  747. " this.isEmptyField = value;{0}" +
  748. " }}{0}" +
  749. " }}{0}" +
  750. " {0}" +
  751. " /// <remarks/>{0}" +
  752. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  753. " public bool IsEmptySpecified {{{0}" +
  754. " get {{{0}" +
  755. " return this.isEmptyFieldSpecified;{0}" +
  756. " }}{0}" +
  757. " set {{{0}" +
  758. " this.isEmptyFieldSpecified = value;{0}" +
  759. " }}{0}" +
  760. " }}{0}" +
  761. " {0}" +
  762. " /// <remarks/>{0}" +
  763. " public bool IsNull {{{0}" +
  764. " get {{{0}" +
  765. " return this.isNullField;{0}" +
  766. " }}{0}" +
  767. " set {{{0}" +
  768. " this.isNullField = value;{0}" +
  769. " }}{0}" +
  770. " }}{0}" +
  771. "}}{0}" +
  772. "{0}" +
  773. "/// <remarks/>{0}" +
  774. "[System.FlagsAttribute()]{0}" +
  775. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  776. "[System.SerializableAttribute()]{0}" +
  777. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"some:urn\")]{0}" +
  778. "public enum FlagEnum {{{0}" +
  779. " {0}" +
  780. " /// <remarks/>{0}" +
  781. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  782. " e1 = 1,{0}" +
  783. " {0}" +
  784. " /// <remarks/>{0}" +
  785. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  786. " e2 = 2,{0}" +
  787. " {0}" +
  788. " /// <remarks/>{0}" +
  789. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  790. " e4 = 4,{0}" +
  791. "}}{0}" +
  792. "{0}" +
  793. "/// <remarks/>{0}" +
  794. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  795. "[System.SerializableAttribute()]{0}" +
  796. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  797. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  798. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  799. "public partial class TestDefault {{{0}" +
  800. " {0}" +
  801. " private string strField;{0}" +
  802. " {0}" +
  803. " private string strDefaultField;{0}" +
  804. " {0}" +
  805. " private bool boolTField;{0}" +
  806. " {0}" +
  807. " private bool boolFField;{0}" +
  808. " {0}" +
  809. " private decimal decimalvalField;{0}" +
  810. " {0}" +
  811. " private MonoTests.System.Xml.TestClasses.FlagEnum flagField;{0}" +
  812. " {0}" +
  813. " private MonoTests.System.Xml.TestClasses.FlagEnum_Encoded flagencodedField;{0}" +
  814. " {0}" +
  815. " public TestDefault() {{{0}" +
  816. " this.strDefaultField = \"Default Value\";{0}" +
  817. " this.flagField = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  818. " this.flagencodedField = (MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4);{0}" +
  819. " }}{0}" +
  820. " {0}" +
  821. " /// <remarks/>{0}" +
  822. " public string str {{{0}" +
  823. " get {{{0}" +
  824. " return this.strField;{0}" +
  825. " }}{0}" +
  826. " set {{{0}" +
  827. " this.strField = value;{0}" +
  828. " }}{0}" +
  829. " }}{0}" +
  830. " {0}" +
  831. " /// <remarks/>{0}" +
  832. " public string strDefault {{{0}" +
  833. " get {{{0}" +
  834. " return this.strDefaultField;{0}" +
  835. " }}{0}" +
  836. " set {{{0}" +
  837. " this.strDefaultField = value;{0}" +
  838. " }}{0}" +
  839. " }}{0}" +
  840. " {0}" +
  841. " /// <remarks/>{0}" +
  842. " public bool boolT {{{0}" +
  843. " get {{{0}" +
  844. " return this.boolTField;{0}" +
  845. " }}{0}" +
  846. " set {{{0}" +
  847. " this.boolTField = value;{0}" +
  848. " }}{0}" +
  849. " }}{0}" +
  850. " {0}" +
  851. " /// <remarks/>{0}" +
  852. " public bool boolF {{{0}" +
  853. " get {{{0}" +
  854. " return this.boolFField;{0}" +
  855. " }}{0}" +
  856. " set {{{0}" +
  857. " this.boolFField = value;{0}" +
  858. " }}{0}" +
  859. " }}{0}" +
  860. " {0}" +
  861. " /// <remarks/>{0}" +
  862. " public decimal decimalval {{{0}" +
  863. " get {{{0}" +
  864. " return this.decimalvalField;{0}" +
  865. " }}{0}" +
  866. " set {{{0}" +
  867. " this.decimalvalField = value;{0}" +
  868. " }}{0}" +
  869. " }}{0}" +
  870. " {0}" +
  871. " /// <remarks/>{0}" +
  872. " public MonoTests.System.Xml.TestClasses.FlagEnum flag {{{0}" +
  873. " get {{{0}" +
  874. " return this.flagField;{0}" +
  875. " }}{0}" +
  876. " set {{{0}" +
  877. " this.flagField = value;{0}" +
  878. " }}{0}" +
  879. " }}{0}" +
  880. " {0}" +
  881. " /// <remarks/>{0}" +
  882. " public MonoTests.System.Xml.TestClasses.FlagEnum_Encoded flagencoded {{{0}" +
  883. " get {{{0}" +
  884. " return this.flagencodedField;{0}" +
  885. " }}{0}" +
  886. " set {{{0}" +
  887. " this.flagencodedField = value;{0}" +
  888. " }}{0}" +
  889. " }}{0}" +
  890. "}}{0}" +
  891. "{0}" +
  892. "/// <remarks/>{0}" +
  893. "[System.FlagsAttribute()]{0}" +
  894. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  895. "[System.SerializableAttribute()]{0}" +
  896. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  897. "public enum FlagEnum {{{0}" +
  898. " {0}" +
  899. " /// <remarks/>{0}" +
  900. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  901. " e1 = 1,{0}" +
  902. " {0}" +
  903. " /// <remarks/>{0}" +
  904. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  905. " e2 = 2,{0}" +
  906. " {0}" +
  907. " /// <remarks/>{0}" +
  908. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  909. " e4 = 4,{0}" +
  910. "}}{0}" +
  911. "{0}" +
  912. "/// <remarks/>{0}" +
  913. "[System.FlagsAttribute()]{0}" +
  914. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  915. "[System.SerializableAttribute()]{0}" +
  916. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  917. "public enum FlagEnum_Encoded {{{0}" +
  918. " {0}" +
  919. " /// <remarks/>{0}" +
  920. " e1 = 1,{0}" +
  921. " {0}" +
  922. " /// <remarks/>{0}" +
  923. " e2 = 2,{0}" +
  924. " {0}" +
  925. " /// <remarks/>{0}" +
  926. " e4 = 4,{0}" +
  927. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  928. #else
  929. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:aNS\")]{0}" +
  930. "[System.Xml.Serialization.XmlRootAttribute(\"root\", Namespace=\"urn:aNS\", IsNullable=false)]{0}" +
  931. "public class Root {{{0}" +
  932. " {0}" +
  933. " /// <remarks/>{0}" +
  934. " public MonoTests.System.Xml.TestClasses.OptionalValueTypeContainer OptionalValue;{0}" +
  935. " {0}" +
  936. " /// <remarks/>{0}" +
  937. " public MonoTests.System.Xml.TestClasses.TestDefault Default;{0}" +
  938. "}}{0}" +
  939. "{0}" +
  940. "/// <remarks/>{0}" +
  941. "[System.Xml.Serialization.XmlTypeAttribute(TypeName=\"optionalValueType\", Namespace=\"some:urn\")]{0}" +
  942. "public class OptionalValueTypeContainer {{{0}" +
  943. " {0}" +
  944. " /// <remarks/>{0}" +
  945. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4))]{0}" +
  946. " public MonoTests.System.Xml.TestClasses.FlagEnum Attributes = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  947. " {0}" +
  948. " /// <remarks/>{0}" +
  949. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  950. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  951. " {0}" +
  952. " /// <remarks/>{0}" +
  953. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  954. " public bool FlagsSpecified;{0}" +
  955. " {0}" +
  956. " /// <remarks/>{0}" +
  957. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  958. " public bool IsEmpty = false;{0}" +
  959. " {0}" +
  960. " /// <remarks/>{0}" +
  961. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  962. " public bool IsEmptySpecified;{0}" +
  963. " {0}" +
  964. " /// <remarks/>{0}" +
  965. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  966. " public bool IsNull = false;{0}" +
  967. "}}{0}" +
  968. "{0}" +
  969. "/// <remarks/>{0}" +
  970. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"some:urn\")]{0}" +
  971. "[System.FlagsAttribute()]{0}" +
  972. "public enum FlagEnum {{{0}" +
  973. " {0}" +
  974. " /// <remarks/>{0}" +
  975. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  976. " e1 = 1,{0}" +
  977. " {0}" +
  978. " /// <remarks/>{0}" +
  979. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  980. " e2 = 2,{0}" +
  981. " {0}" +
  982. " /// <remarks/>{0}" +
  983. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  984. " e4 = 4,{0}" +
  985. "}}{0}" +
  986. "{0}" +
  987. "/// <remarks/>{0}" +
  988. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  989. "public class TestDefault {{{0}" +
  990. " {0}" +
  991. " /// <remarks/>{0}" +
  992. " public string str;{0}" +
  993. " {0}" +
  994. " /// <remarks/>{0}" +
  995. " [System.ComponentModel.DefaultValueAttribute(\"Default Value\")]{0}" +
  996. " public string strDefault = \"Default Value\";{0}" +
  997. " {0}" +
  998. " /// <remarks/>{0}" +
  999. " [System.ComponentModel.DefaultValueAttribute(true)]{0}" +
  1000. " public bool boolT = true;{0}" +
  1001. " {0}" +
  1002. " /// <remarks/>{0}" +
  1003. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  1004. " public bool boolF = false;{0}" +
  1005. " {0}" +
  1006. " /// <remarks/>{0}" +
  1007. " [System.ComponentModel.DefaultValueAttribute(typeof(System.Decimal), \"10\")]{0}" +
  1008. " public System.Decimal decimalval = ((System.Decimal)(10m));{0}" +
  1009. " {0}" +
  1010. " /// <remarks/>{0}" +
  1011. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4))]{0}" +
  1012. " public MonoTests.System.Xml.TestClasses.FlagEnum flag = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  1013. " {0}" +
  1014. " /// <remarks/>{0}" +
  1015. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4))]{0}" +
  1016. " public MonoTests.System.Xml.TestClasses.FlagEnum_Encoded flagencoded = (MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4);{0}" +
  1017. "}}{0}" +
  1018. "{0}" +
  1019. "/// <remarks/>{0}" +
  1020. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  1021. "[System.FlagsAttribute()]{0}" +
  1022. "public enum FlagEnum {{{0}" +
  1023. " {0}" +
  1024. " /// <remarks/>{0}" +
  1025. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  1026. " e1 = 1,{0}" +
  1027. " {0}" +
  1028. " /// <remarks/>{0}" +
  1029. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  1030. " e2 = 2,{0}" +
  1031. " {0}" +
  1032. " /// <remarks/>{0}" +
  1033. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  1034. " e4 = 4,{0}" +
  1035. "}}{0}" +
  1036. "{0}" +
  1037. "/// <remarks/>{0}" +
  1038. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  1039. "[System.FlagsAttribute()]{0}" +
  1040. "public enum FlagEnum_Encoded {{{0}" +
  1041. " {0}" +
  1042. " /// <remarks/>{0}" +
  1043. " e1 = 1,{0}" +
  1044. " {0}" +
  1045. " /// <remarks/>{0}" +
  1046. " e2 = 2,{0}" +
  1047. " {0}" +
  1048. " /// <remarks/>{0}" +
  1049. " e4 = 4,{0}" +
  1050. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  1051. #endif
  1052. }
  1053. [Test]
  1054. public void ExportTypeMapping_SimpleClassWithXmlAttributes ()
  1055. {
  1056. CodeNamespace codeNamespace = ExportCode (typeof (SimpleClassWithXmlAttributes));
  1057. Assert.IsNotNull (codeNamespace, "#1");
  1058. StringWriter sw = new StringWriter ();
  1059. CodeDomProvider provider = new CSharpCodeProvider ();
  1060. ICodeGenerator generator = provider.CreateGenerator ();
  1061. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  1062. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1063. "{0}{0}" +
  1064. "/// <remarks/>{0}" +
  1065. #if NET_2_0
  1066. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  1067. "[System.SerializableAttribute()]{0}" +
  1068. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  1069. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  1070. "[System.Xml.Serialization.XmlRootAttribute(\"simple\", Namespace=\"\", IsNullable=true)]{0}" +
  1071. "public partial class SimpleClassWithXmlAttributes {{{0}" +
  1072. " {0}" +
  1073. " private string somethingField;{0}" +
  1074. " {0}" +
  1075. " /// <remarks/>{0}" +
  1076. " [System.Xml.Serialization.XmlAttributeAttribute(\"member\")]{0}" +
  1077. " public string something {{{0}" +
  1078. " get {{{0}" +
  1079. " return this.somethingField;{0}" +
  1080. " }}{0}" +
  1081. " set {{{0}" +
  1082. " this.somethingField = value;{0}" +
  1083. " }}{0}" +
  1084. " }}{0}" +
  1085. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  1086. #else
  1087. "[System.Xml.Serialization.XmlRootAttribute(\"simple\", Namespace=\"\", IsNullable=true)]{0}" +
  1088. "public class SimpleClassWithXmlAttributes {{{0}" +
  1089. " {0}" +
  1090. " /// <remarks/>{0}" +
  1091. " [System.Xml.Serialization.XmlAttributeAttribute(\"member\")]{0}" +
  1092. " public string something;{0}" +
  1093. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  1094. #endif
  1095. }
  1096. [Test]
  1097. public void ExportTypeMapping_XsdPrimitive_Arrays ()
  1098. {
  1099. ArrayList types = new ArrayList ();
  1100. types.Add (typeof (sbyte[]));
  1101. types.Add (typeof (bool[]));
  1102. types.Add (typeof (short[]));
  1103. types.Add (typeof (int[]));
  1104. types.Add (typeof (long[]));
  1105. types.Add (typeof (float[]));
  1106. types.Add (typeof (double[]));
  1107. types.Add (typeof (decimal[]));
  1108. types.Add (typeof (ushort[]));
  1109. types.Add (typeof (uint[]));
  1110. types.Add (typeof (ulong[]));
  1111. types.Add (typeof (DateTime[]));
  1112. types.Add (typeof (XmlQualifiedName[]));
  1113. types.Add (typeof (string[]));
  1114. StringWriter sw = new StringWriter ();
  1115. CodeDomProvider provider = new CSharpCodeProvider ();
  1116. ICodeGenerator generator = provider.CreateGenerator ();
  1117. foreach (Type type in types) {
  1118. CodeNamespace codeNamespace = ExportCode (type);
  1119. Assert.IsNotNull (codeNamespace, type.FullName + "#1");
  1120. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  1121. Assert.AreEqual (Environment.NewLine, sw.ToString (),
  1122. type.FullName + "#2");
  1123. sw.GetStringBuilder ().Length = 0;
  1124. }
  1125. }
  1126. [Test]
  1127. public void ExportTypeMapping_ZeroFlagEnum ()
  1128. {
  1129. CodeNamespace codeNamespace = ExportCode (typeof (ZeroFlagEnum));
  1130. Assert.IsNotNull (codeNamespace, "#1");
  1131. StringWriter sw = new StringWriter ();
  1132. CodeDomProvider provider = new CSharpCodeProvider ();
  1133. ICodeGenerator generator = provider.CreateGenerator ();
  1134. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  1135. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1136. "{0}{0}" +
  1137. "/// <remarks/>{0}" +
  1138. "[System.FlagsAttribute()]{0}" +
  1139. #if NET_2_0
  1140. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  1141. "[System.SerializableAttribute()]{0}" +
  1142. #endif
  1143. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=false)]{0}" +
  1144. "public enum ZeroFlagEnum {{{0}" +
  1145. " {0}" +
  1146. " /// <remarks/>{0}" +
  1147. " [System.Xml.Serialization.XmlEnumAttribute(\"zero\")]{0}" +
  1148. " e0 = 1,{0}" +
  1149. " {0}" +
  1150. " /// <remarks/>{0}" +
  1151. " [System.Xml.Serialization.XmlEnumAttribute(\"o<n>e\")]{0}" +
  1152. " e1 = 2,{0}" +
  1153. " {0}" +
  1154. " /// <remarks/>{0}" +
  1155. " [System.Xml.Serialization.XmlEnumAttribute(\"tns:t<w>o\")]{0}" +
  1156. " e2 = 4,{0}" +
  1157. #if NET_2_0
  1158. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  1159. #else
  1160. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  1161. #endif
  1162. }
  1163. [Test]
  1164. public void DuplicateIdentifiers ()
  1165. {
  1166. XmlSchema xs = XmlSchema.Read (File.OpenText ("Test/XmlFiles/xsd/82078.xsd"), null);
  1167. XmlSchemas xss = new XmlSchemas ();
  1168. xss.Add (xs);
  1169. XmlSchemaImporter imp = new XmlSchemaImporter (xss);
  1170. CodeNamespace cns = new CodeNamespace ();
  1171. XmlCodeExporter exp = new XmlCodeExporter (cns);
  1172. XmlQualifiedName qname = new XmlQualifiedName (
  1173. "Operation", "http://tempuri.org/");
  1174. exp.ExportTypeMapping (imp.ImportTypeMapping (qname));
  1175. CodeCompileUnit ccu = new CodeCompileUnit ();
  1176. ccu.Namespaces.Add (cns);
  1177. CodeDomProvider provider = new CSharpCodeProvider ();
  1178. ICodeCompiler compiler = provider.CreateCompiler ();
  1179. CompilerParameters options = new CompilerParameters ();
  1180. options.ReferencedAssemblies.Add ("System.dll");
  1181. options.ReferencedAssemblies.Add ("System.Xml.dll");
  1182. options.GenerateInMemory = true;
  1183. CompilerResults result = compiler.CompileAssemblyFromDom (options, ccu);
  1184. Assert.AreEqual (0, result.Errors.Count, "#1");
  1185. Assert.IsNotNull (result.CompiledAssembly, "#2");
  1186. }
  1187. [Test]
  1188. public void ExportSimpleContentExtensionEnum ()
  1189. {
  1190. string xsd = @"
  1191. <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:b='urn:bar' targetNamespace='urn:bar'>
  1192. <xs:element name='Foo' type='b:DayOfWeek' />
  1193. <xs:complexType name='DayOfWeek'>
  1194. <xs:simpleContent>
  1195. <xs:extension base='b:WeekDay' />
  1196. </xs:simpleContent>
  1197. </xs:complexType>
  1198. <xs:simpleType name='WeekDay'>
  1199. <xs:restriction base='xs:string'>
  1200. <xs:enumeration value='Sunday'/>
  1201. <xs:enumeration value='Monday'/>
  1202. <xs:enumeration value='Tuesday'/>
  1203. <xs:enumeration value='Wednesday'/>
  1204. <xs:enumeration value='Thursday'/>
  1205. <xs:enumeration value='Friday'/>
  1206. <xs:enumeration value='Saturday'/>
  1207. </xs:restriction>
  1208. </xs:simpleType>
  1209. </xs:schema>";
  1210. XmlSchema xs = XmlSchema.Read (new StringReader (xsd), null);
  1211. XmlSchemas xss = new XmlSchemas ();
  1212. xss.Add (xs);
  1213. XmlSchemaImporter imp = new XmlSchemaImporter (xss);
  1214. XmlTypeMapping m = imp.ImportTypeMapping (new XmlQualifiedName ("Foo", "urn:bar"));
  1215. CodeNamespace cns = new CodeNamespace ();
  1216. XmlCodeExporter exp = new XmlCodeExporter (cns);
  1217. exp.ExportTypeMapping (m);
  1218. CodeTypeDeclaration enumType = null;
  1219. foreach (CodeTypeDeclaration ctd in cns.Types)
  1220. if (ctd.Name == "WeekDay")
  1221. enumType = ctd;
  1222. Assert.IsNotNull (enumType);
  1223. }
  1224. CodeNamespace ExportCode (Type type)
  1225. {
  1226. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1227. XmlTypeMapping map = imp.ImportTypeMapping (type);
  1228. CodeNamespace codeNamespace = new CodeNamespace ();
  1229. XmlCodeExporter exp = new XmlCodeExporter (codeNamespace);
  1230. exp.ExportTypeMapping (map);
  1231. return codeNamespace;
  1232. }
  1233. #if NET_2_0
  1234. string XmlFileVersion {
  1235. get {
  1236. Assembly xmlAsm = typeof (XmlDocument).Assembly;
  1237. AssemblyFileVersionAttribute afv = (AssemblyFileVersionAttribute)
  1238. Attribute.GetCustomAttribute (xmlAsm, typeof (AssemblyFileVersionAttribute));
  1239. return afv.Version;
  1240. }
  1241. }
  1242. #endif
  1243. [XmlRootAttribute ("root", Namespace="urn:aNS", IsNullable=false)]
  1244. public class Root
  1245. {
  1246. public OptionalValueTypeContainer OptionalValue;
  1247. public TestDefault Default;
  1248. }
  1249. }
  1250. }