XmlCodeExporterTests.cs 38 KB

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