XmlCodeExporterTests.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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
  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. #if NET_2_0
  18. using System.Reflection;
  19. #endif
  20. using System.Xml;
  21. using System.Xml.Schema;
  22. using System.Xml.Serialization;
  23. using Microsoft.CSharp;
  24. using NUnit.Framework;
  25. using MonoTests.System.Xml.TestClasses;
  26. namespace MonoTests.System.XmlSerialization
  27. {
  28. [TestFixture]
  29. public class XmlCodeExporterTests
  30. {
  31. [Test]
  32. public void ExportTypeMapping_ArrayClass ()
  33. {
  34. CodeNamespace codeNamespace = ExportCode (typeof (ArrayClass));
  35. Assert.IsNotNull (codeNamespace, "#1");
  36. StringWriter sw = new StringWriter ();
  37. CodeDomProvider provider = new CSharpCodeProvider ();
  38. ICodeGenerator generator = provider.CreateGenerator ();
  39. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  40. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  41. "{0}{0}" +
  42. "/// <remarks/>{0}" +
  43. #if NET_2_0
  44. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  45. "[System.SerializableAttribute()]{0}" +
  46. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  47. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  48. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  49. "public partial class ArrayClass {{{0}" +
  50. " {0}" +
  51. " private object namesField;{0}" +
  52. " {0}" +
  53. " /// <remarks/>{0}" +
  54. " public object names {{{0}" +
  55. " get {{{0}" +
  56. " return this.namesField;{0}" +
  57. " }}{0}" +
  58. " set {{{0}" +
  59. " this.namesField = value;{0}" +
  60. " }}{0}" +
  61. " }}{0}" +
  62. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  63. #else
  64. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  65. "public class ArrayClass {{{0}" +
  66. " {0}" +
  67. " /// <remarks/>{0}" +
  68. " public object names;{0}" +
  69. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  70. #endif
  71. codeNamespace = ExportCode (typeof (ArrayClass[]));
  72. Assert.IsNotNull (codeNamespace, "#3");
  73. sw.GetStringBuilder ().Length = 0;
  74. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  75. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  76. "{0}{0}" +
  77. "/// <remarks/>{0}" +
  78. #if NET_2_0
  79. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  80. "[System.SerializableAttribute()]{0}" +
  81. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  82. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  83. "public partial class ArrayClass {{{0}" +
  84. " {0}" +
  85. " private object namesField;{0}" +
  86. " {0}" +
  87. " /// <remarks/>{0}" +
  88. " public object names {{{0}" +
  89. " get {{{0}" +
  90. " return this.namesField;{0}" +
  91. " }}{0}" +
  92. " set {{{0}" +
  93. " this.namesField = value;{0}" +
  94. " }}{0}" +
  95. " }}{0}" +
  96. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#4");
  97. #else
  98. "public class ArrayClass {{{0}" +
  99. " {0}" +
  100. " /// <remarks/>{0}" +
  101. " public object names;{0}" +
  102. "}}{0}", Environment.NewLine), sw.ToString (), "#4");
  103. #endif
  104. }
  105. [Test]
  106. public void ExportTypeMapping_ArrayContainer ()
  107. {
  108. CodeNamespace codeNamespace = ExportCode (typeof (ArrayContainer));
  109. Assert.IsNotNull (codeNamespace, "#1");
  110. StringWriter sw = new StringWriter ();
  111. CodeDomProvider provider = new CSharpCodeProvider ();
  112. ICodeGenerator generator = provider.CreateGenerator ();
  113. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  114. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  115. "{0}{0}" +
  116. "/// <remarks/>{0}" +
  117. #if NET_2_0
  118. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  119. "[System.SerializableAttribute()]{0}" +
  120. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  121. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  122. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  123. "public partial class ArrayContainer {{{0}" +
  124. " {0}" +
  125. " private object[] itemsField;{0}" +
  126. " {0}" +
  127. " /// <remarks/>{0}" +
  128. " public object[] items {{{0}" +
  129. " get {{{0}" +
  130. " return this.itemsField;{0}" +
  131. " }}{0}" +
  132. " set {{{0}" +
  133. " this.itemsField = value;{0}" +
  134. " }}{0}" +
  135. " }}{0}" +
  136. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  137. #else
  138. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  139. "public class ArrayContainer {{{0}" +
  140. " {0}" +
  141. " /// <remarks/>{0}" +
  142. " public object[] items;{0}" +
  143. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  144. #endif
  145. }
  146. [Test]
  147. public void ExportTypeMapping_CDataContainer ()
  148. {
  149. CodeNamespace codeNamespace = ExportCode (typeof (CDataContainer));
  150. Assert.IsNotNull (codeNamespace, "#1");
  151. StringWriter sw = new StringWriter ();
  152. CodeDomProvider provider = new CSharpCodeProvider ();
  153. ICodeGenerator generator = provider.CreateGenerator ();
  154. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  155. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  156. "{0}{0}" +
  157. "/// <remarks/>{0}" +
  158. #if NET_2_0
  159. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  160. "[System.SerializableAttribute()]{0}" +
  161. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  162. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  163. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  164. "public partial class CDataContainer {{{0}" +
  165. " {0}" +
  166. " private System.Xml.XmlCDataSection cdataField;{0}" +
  167. " {0}" +
  168. " /// <remarks/>{0}" +
  169. " public System.Xml.XmlCDataSection cdata {{{0}" +
  170. " get {{{0}" +
  171. " return this.cdataField;{0}" +
  172. " }}{0}" +
  173. " set {{{0}" +
  174. " this.cdataField = value;{0}" +
  175. " }}{0}" +
  176. " }}{0}" +
  177. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  178. #else
  179. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  180. "public class CDataContainer {{{0}" +
  181. " {0}" +
  182. " /// <remarks/>{0}" +
  183. " public System.Xml.XmlCDataSection cdata;{0}" +
  184. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  185. #endif
  186. }
  187. [Test]
  188. [Category ("NotWorking")] // order of XmlElementAttribute's does not match that of MSFT
  189. [Category ("NotDotNet")] // Mono bug ##77117 and MS.NET randomly modifies the order of the elements!
  190. public void ExportTypeMapping_Choices ()
  191. {
  192. CodeNamespace codeNamespace = ExportCode (typeof (Choices));
  193. Assert.IsNotNull (codeNamespace, "#1");
  194. StringWriter sw = new StringWriter ();
  195. CodeDomProvider provider = new CSharpCodeProvider ();
  196. ICodeGenerator generator = provider.CreateGenerator ();
  197. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  198. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  199. "{0}{0}" +
  200. "/// <remarks/>{0}" +
  201. #if NET_2_0
  202. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  203. "[System.SerializableAttribute()]{0}" +
  204. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  205. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  206. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  207. "public partial class Choices {{{0}" +
  208. " {0}" +
  209. " private string myChoiceField;{0}" +
  210. " {0}" +
  211. " /// <remarks/>{0}" +
  212. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceZero\", typeof(string))]{0}" +
  213. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceOne\", typeof(string))]{0}" +
  214. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceTwo\", typeof(string))]{0}" +
  215. " [System.Xml.Serialization.XmlChoiceIdentifierAttribute(\"ItemType\")]{0}" +
  216. " public string MyChoice {{{0}" +
  217. " get {{{0}" +
  218. " return this.myChoiceField;{0}" +
  219. " }}{0}" +
  220. " set {{{0}" +
  221. " this.myChoiceField = value;{0}" +
  222. " }}{0}" +
  223. " }}{0}" +
  224. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  225. #else
  226. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  227. "public class Choices {{{0}" +
  228. " {0}" +
  229. " /// <remarks/>{0}" +
  230. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceZero\", typeof(string))]{0}" +
  231. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceTwo\", typeof(string))]{0}" +
  232. " [System.Xml.Serialization.XmlElementAttribute(\"ChoiceOne\", typeof(string))]{0}" +
  233. " [System.Xml.Serialization.XmlChoiceIdentifierAttribute(\"ItemType\")]{0}" +
  234. " public string MyChoice;{0}" +
  235. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  236. #endif
  237. }
  238. [Test]
  239. [Category ("NotWorking")] // TODO: order of DefaultValueAttribute, ...
  240. public void ExportTypeMapping_Field ()
  241. {
  242. CodeNamespace codeNamespace = ExportCode (typeof (Field));
  243. Assert.IsNotNull (codeNamespace, "#1");
  244. StringWriter sw = new StringWriter ();
  245. CodeDomProvider provider = new CSharpCodeProvider ();
  246. ICodeGenerator generator = provider.CreateGenerator ();
  247. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  248. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  249. "{0}{0}" +
  250. "/// <remarks/>{0}" +
  251. #if NET_2_0
  252. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  253. "[System.SerializableAttribute()]{0}" +
  254. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  255. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  256. "[System.Xml.Serialization.XmlRootAttribute(\"field\", Namespace=\"\", IsNullable=true)]{0}" +
  257. "public partial class Field {{{0}" +
  258. " {0}" +
  259. " private MonoTests.System.Xml.TestClasses.FlagEnum flags1Field;{0}" +
  260. " {0}" +
  261. " private MonoTests.System.Xml.TestClasses.FlagEnum flags2Field;{0}" +
  262. " {0}" +
  263. " private MonoTests.System.Xml.TestClasses.FlagEnum flags3Field;{0}" +
  264. " {0}" +
  265. " private MonoTests.System.Xml.TestClasses.FlagEnum flags4Field;{0}" +
  266. " {0}" +
  267. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiersField;{0}" +
  268. " {0}" +
  269. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers2Field;{0}" +
  270. " {0}" +
  271. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers3Field;{0}" +
  272. " {0}" +
  273. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers4Field;{0}" +
  274. " {0}" +
  275. " private MonoTests.System.Xml.TestClasses.MapModifiers modifiers5Field;{0}" +
  276. " {0}" +
  277. " private string[] namesField;{0}" +
  278. " {0}" +
  279. " private string streetField;{0}" +
  280. " {0}" +
  281. " public Field() {{{0}" +
  282. " this.flags1Field = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  283. " this.flags2Field = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  284. " this.flags3Field = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2);{0}" +
  285. " this.modifiers3Field = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  286. " this.modifiers4Field = MonoTests.System.Xml.TestClasses.MapModifiers.Protected;{0}" +
  287. " this.modifiers5Field = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  288. " }}{0}" +
  289. " {0}" +
  290. " /// <remarks/>{0}" +
  291. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag1\")]{0}" +
  292. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags1 {{{0}" +
  293. " get {{{0}" +
  294. " return this.flags1Field;{0}" +
  295. " }}{0}" +
  296. " set {{{0}" +
  297. " this.flags1Field = value;{0}" +
  298. " }}{0}" +
  299. " }}{0}" +
  300. " {0}" +
  301. " /// <remarks/>{0}" +
  302. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag2\")]{0}" +
  303. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags2 {{{0}" +
  304. " get {{{0}" +
  305. " return this.flags2Field;{0}" +
  306. " }}{0}" +
  307. " set {{{0}" +
  308. " this.flags2Field = value;{0}" +
  309. " }}{0}" +
  310. " }}{0}" +
  311. " {0}" +
  312. " /// <remarks/>{0}" +
  313. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag3\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  314. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags3 {{{0}" +
  315. " get {{{0}" +
  316. " return this.flags3Field;{0}" +
  317. " }}{0}" +
  318. " set {{{0}" +
  319. " this.flags3Field = value;{0}" +
  320. " }}{0}" +
  321. " }}{0}" +
  322. " {0}" +
  323. " /// <remarks/>{0}" +
  324. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag4\")]{0}" +
  325. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags4 {{{0}" +
  326. " get {{{0}" +
  327. " return this.flags4Field;{0}" +
  328. " }}{0}" +
  329. " set {{{0}" +
  330. " this.flags4Field = value;{0}" +
  331. " }}{0}" +
  332. " }}{0}" +
  333. " {0}" +
  334. " /// <remarks/>{0}" +
  335. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers\")]{0}" +
  336. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers {{{0}" +
  337. " get {{{0}" +
  338. " return this.modifiersField;{0}" +
  339. " }}{0}" +
  340. " set {{{0}" +
  341. " this.modifiersField = value;{0}" +
  342. " }}{0}" +
  343. " }}{0}" +
  344. " {0}" +
  345. " /// <remarks/>{0}" +
  346. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers2\")]{0}" +
  347. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers2 {{{0}" +
  348. " get {{{0}" +
  349. " return this.modifiers2Field;{0}" +
  350. " }}{0}" +
  351. " set {{{0}" +
  352. " this.modifiers2Field = value;{0}" +
  353. " }}{0}" +
  354. " }}{0}" +
  355. " {0}" +
  356. " /// <remarks/>{0}" +
  357. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers3\")]{0}" +
  358. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers3 {{{0}" +
  359. " get {{{0}" +
  360. " return this.modifiers3Field;{0}" +
  361. " }}{0}" +
  362. " set {{{0}" +
  363. " this.modifiers3Field = value;{0}" +
  364. " }}{0}" +
  365. " }}{0}" +
  366. " {0}" +
  367. " /// <remarks/>{0}" +
  368. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers4\")]{0}" +
  369. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers4 {{{0}" +
  370. " get {{{0}" +
  371. " return this.modifiers4Field;{0}" +
  372. " }}{0}" +
  373. " set {{{0}" +
  374. " this.modifiers4Field = value;{0}" +
  375. " }}{0}" +
  376. " }}{0}" +
  377. " {0}" +
  378. " /// <remarks/>{0}" +
  379. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers5\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  380. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers5 {{{0}" +
  381. " get {{{0}" +
  382. " return this.modifiers5Field;{0}" +
  383. " }}{0}" +
  384. " set {{{0}" +
  385. " this.modifiers5Field = value;{0}" +
  386. " }}{0}" +
  387. " }}{0}" +
  388. " {0}" +
  389. " /// <remarks/>{0}" +
  390. " [System.Xml.Serialization.XmlAttributeAttribute(\"names\")]{0}" +
  391. " public string[] Names {{{0}" +
  392. " get {{{0}" +
  393. " return this.namesField;{0}" +
  394. " }}{0}" +
  395. " set {{{0}" +
  396. " this.namesField = value;{0}" +
  397. " }}{0}" +
  398. " }}{0}" +
  399. " {0}" +
  400. " /// <remarks/>{0}" +
  401. " [System.Xml.Serialization.XmlAttributeAttribute(\"street\")]{0}" +
  402. " public string Street {{{0}" +
  403. " get {{{0}" +
  404. " return this.streetField;{0}" +
  405. " }}{0}" +
  406. " set {{{0}" +
  407. " this.streetField = value;{0}" +
  408. " }}{0}" +
  409. " }}{0}" +
  410. #else
  411. "[System.Xml.Serialization.XmlRootAttribute(\"field\", Namespace=\"\", IsNullable=true)]{0}" +
  412. "public class Field {{{0}" +
  413. " {0}" +
  414. " /// <remarks/>{0}" +
  415. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag1\")]{0}" +
  416. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  417. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags1 = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  418. " {0}" +
  419. " /// <remarks/>{0}" +
  420. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag2\")]{0}" +
  421. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  422. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags2 = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  423. " {0}" +
  424. " /// <remarks/>{0}" +
  425. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag3\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  426. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2))]{0}" +
  427. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags3 = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e2);{0}" +
  428. " {0}" +
  429. " /// <remarks/>{0}" +
  430. " [System.Xml.Serialization.XmlAttributeAttribute(\"flag4\")]{0}" +
  431. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags4;{0}" +
  432. " {0}" +
  433. " /// <remarks/>{0}" +
  434. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers\")]{0}" +
  435. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers;{0}" +
  436. " {0}" +
  437. " /// <remarks/>{0}" +
  438. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers2\")]{0}" +
  439. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers2;{0}" +
  440. " {0}" +
  441. " /// <remarks/>{0}" +
  442. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers3\")]{0}" +
  443. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Public)]{0}" +
  444. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers3 = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  445. " {0}" +
  446. " /// <remarks/>{0}" +
  447. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers4\")]{0}" +
  448. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Protected)]{0}" +
  449. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers4 = MonoTests.System.Xml.TestClasses.MapModifiers.Protected;{0}" +
  450. " {0}" +
  451. " /// <remarks/>{0}" +
  452. " [System.Xml.Serialization.XmlAttributeAttribute(\"modifiers5\", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]{0}" +
  453. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.MapModifiers.Public)]{0}" +
  454. " public MonoTests.System.Xml.TestClasses.MapModifiers Modifiers5 = MonoTests.System.Xml.TestClasses.MapModifiers.Public;{0}" +
  455. " {0}" +
  456. " /// <remarks/>{0}" +
  457. " [System.Xml.Serialization.XmlAttributeAttribute(\"names\")]{0}" +
  458. " public string[] Names;{0}" +
  459. " {0}" +
  460. " /// <remarks/>{0}" +
  461. " [System.Xml.Serialization.XmlAttributeAttribute(\"street\")]{0}" +
  462. " public string Street;{0}" +
  463. #endif
  464. "}}{0}" +
  465. "{0}" +
  466. "/// <remarks/>{0}" +
  467. "[System.FlagsAttribute()]{0}" +
  468. #if NET_2_0
  469. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  470. "[System.SerializableAttribute()]{0}" +
  471. #endif
  472. "public enum FlagEnum {{{0}" +
  473. " {0}" +
  474. " /// <remarks/>{0}" +
  475. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  476. " e1 = 1,{0}" +
  477. " {0}" +
  478. " /// <remarks/>{0}" +
  479. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  480. " e2 = 2,{0}" +
  481. " {0}" +
  482. " /// <remarks/>{0}" +
  483. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  484. " e4 = 4,{0}" +
  485. "}}{0}" +
  486. "{0}" +
  487. "/// <remarks/>{0}" +
  488. "[System.FlagsAttribute()]{0}" +
  489. #if NET_2_0
  490. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  491. "[System.SerializableAttribute()]{0}" +
  492. #endif
  493. "public enum MapModifiers {{{0}" +
  494. " {0}" +
  495. " /// <remarks/>{0}" +
  496. " [System.Xml.Serialization.XmlEnumAttribute(\"public\")]{0}" +
  497. " Public = 1,{0}" +
  498. " {0}" +
  499. " /// <remarks/>{0}" +
  500. " [System.Xml.Serialization.XmlEnumAttribute(\"protected\")]{0}" +
  501. " Protected = 2,{0}" +
  502. #if NET_2_0
  503. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  504. #else
  505. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  506. #endif
  507. }
  508. [Test]
  509. public void ExportTypeMapping_ItemChoiceType ()
  510. {
  511. CodeNamespace codeNamespace = ExportCode (typeof (ItemChoiceType));
  512. Assert.IsNotNull (codeNamespace, "#1");
  513. StringWriter sw = new StringWriter ();
  514. CodeDomProvider provider = new CSharpCodeProvider ();
  515. ICodeGenerator generator = provider.CreateGenerator ();
  516. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  517. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  518. "{0}{0}" +
  519. "/// <remarks/>{0}" +
  520. #if NET_2_0
  521. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  522. "[System.SerializableAttribute()]{0}" +
  523. #endif
  524. "[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]{0}" +
  525. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=false)]{0}" +
  526. "public enum ItemChoiceType {{{0}" +
  527. " {0}" +
  528. " /// <remarks/>{0}" +
  529. " ChoiceZero,{0}" +
  530. " {0}" +
  531. " /// <remarks/>{0}" +
  532. " [System.Xml.Serialization.XmlEnumAttribute(\"ChoiceOne\")]{0}" +
  533. " StrangeOne,{0}" +
  534. " {0}" +
  535. " /// <remarks/>{0}" +
  536. " ChoiceTwo,{0}" +
  537. #if NET_2_0
  538. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  539. #else
  540. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  541. #endif
  542. codeNamespace = ExportCode (typeof (ItemChoiceType[]));
  543. Assert.IsNotNull (codeNamespace, "#3");
  544. sw.GetStringBuilder ().Length = 0;
  545. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  546. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  547. "{0}{0}" +
  548. "/// <remarks/>{0}" +
  549. #if NET_2_0
  550. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  551. "[System.SerializableAttribute()]{0}" +
  552. #endif
  553. "[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]{0}" +
  554. "public enum ItemChoiceType {{{0}" +
  555. " {0}" +
  556. " /// <remarks/>{0}" +
  557. " ChoiceZero,{0}" +
  558. " {0}" +
  559. " /// <remarks/>{0}" +
  560. " [System.Xml.Serialization.XmlEnumAttribute(\"ChoiceOne\")]{0}" +
  561. " StrangeOne,{0}" +
  562. " {0}" +
  563. " /// <remarks/>{0}" +
  564. " ChoiceTwo,{0}" +
  565. #if NET_2_0
  566. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#4");
  567. #else
  568. "}}{0}", Environment.NewLine), sw.ToString (), "#4");
  569. #endif
  570. }
  571. [Test]
  572. public void ExportTypeMapping_ClassArrayContainer ()
  573. {
  574. CodeNamespace codeNamespace = ExportCode (typeof (ClassArrayContainer));
  575. Assert.IsNotNull (codeNamespace, "#1");
  576. StringWriter sw = new StringWriter ();
  577. CodeDomProvider provider = new CSharpCodeProvider ();
  578. ICodeGenerator generator = provider.CreateGenerator ();
  579. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  580. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  581. "{0}{0}" +
  582. "/// <remarks/>{0}" +
  583. #if NET_2_0
  584. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  585. "[System.SerializableAttribute()]{0}" +
  586. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  587. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  588. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  589. "public partial class ClassArrayContainer {{{0}" +
  590. " {0}" +
  591. " private MonoTests.System.Xml.TestClasses.SimpleClass[] itemsField;{0}" +
  592. " {0}" +
  593. " /// <remarks/>{0}" +
  594. " public MonoTests.System.Xml.TestClasses.SimpleClass[] items {{{0}" +
  595. " get {{{0}" +
  596. " return this.itemsField;{0}" +
  597. " }}{0}" +
  598. " set {{{0}" +
  599. " this.itemsField = value;{0}" +
  600. " }}{0}" +
  601. " }}{0}" +
  602. "}}{0}" +
  603. "{0}" +
  604. "/// <remarks/>{0}" +
  605. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  606. "[System.SerializableAttribute()]{0}" +
  607. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  608. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  609. "public partial class SimpleClass {{{0}" +
  610. " {0}" +
  611. " private string somethingField;{0}" +
  612. " {0}" +
  613. " /// <remarks/>{0}" +
  614. " public string something {{{0}" +
  615. " get {{{0}" +
  616. " return this.somethingField;{0}" +
  617. " }}{0}" +
  618. " set {{{0}" +
  619. " this.somethingField = value;{0}" +
  620. " }}{0}" +
  621. " }}{0}" +
  622. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  623. #else
  624. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=true)]{0}" +
  625. "public class ClassArrayContainer {{{0}" +
  626. " {0}" +
  627. " /// <remarks/>{0}" +
  628. " public MonoTests.System.Xml.TestClasses.SimpleClass[] items;{0}" +
  629. "}}{0}" +
  630. "{0}" +
  631. "/// <remarks/>{0}" +
  632. "public class SimpleClass {{{0}" +
  633. " {0}" +
  634. " /// <remarks/>{0}" +
  635. " public string something;{0}" +
  636. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  637. #endif
  638. }
  639. [Test]
  640. [Category ("NotWorking")] // bug #78214
  641. public void ExportTypeMapping_Root ()
  642. {
  643. CodeNamespace codeNamespace = ExportCode (typeof (Root));
  644. Assert.IsNotNull (codeNamespace, "#1");
  645. StringWriter sw = new StringWriter ();
  646. CodeDomProvider provider = new CSharpCodeProvider ();
  647. ICodeGenerator generator = provider.CreateGenerator ();
  648. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  649. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  650. "{0}{0}" +
  651. "/// <remarks/>{0}" +
  652. #if NET_2_0
  653. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  654. "[System.SerializableAttribute()]{0}" +
  655. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  656. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  657. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:aNS\")]{0}" +
  658. "[System.Xml.Serialization.XmlRootAttribute(\"root\", Namespace=\"urn:aNS\", IsNullable=false)]{0}" +
  659. "public partial class Root {{{0}" +
  660. " {0}" +
  661. " private MonoTests.System.Xml.TestClasses.OptionalValueTypeContainer optionalValueField;{0}" +
  662. " {0}" +
  663. " private MonoTests.System.Xml.TestClasses.TestDefault defaultField;{0}" +
  664. " {0}" +
  665. " /// <remarks/>{0}" +
  666. " public MonoTests.System.Xml.TestClasses.OptionalValueTypeContainer OptionalValue {{{0}" +
  667. " get {{{0}" +
  668. " return this.optionalValueField;{0}" +
  669. " }}{0}" +
  670. " set {{{0}" +
  671. " this.optionalValueField = value;{0}" +
  672. " }}{0}" +
  673. " }}{0}" +
  674. " {0}" +
  675. " /// <remarks/>{0}" +
  676. " public MonoTests.System.Xml.TestClasses.TestDefault Default {{{0}" +
  677. " get {{{0}" +
  678. " return this.defaultField;{0}" +
  679. " }}{0}" +
  680. " set {{{0}" +
  681. " this.defaultField = value;{0}" +
  682. " }}{0}" +
  683. " }}{0}" +
  684. "}}{0}" +
  685. "{0}" +
  686. "/// <remarks/>{0}" +
  687. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  688. "[System.SerializableAttribute()]{0}" +
  689. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  690. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  691. "[System.Xml.Serialization.XmlTypeAttribute(TypeName=\"optionalValueType\", Namespace=\"some:urn\")]{0}" +
  692. "public partial class OptionalValueTypeContainer {{{0}" +
  693. " {0}" +
  694. " private MonoTests.System.Xml.TestClasses.FlagEnum attributesField;{0}" +
  695. " {0}" +
  696. " private MonoTests.System.Xml.TestClasses.FlagEnum flagsField;{0}" +
  697. " {0}" +
  698. " private bool flagsFieldSpecified;{0}" +
  699. " {0}" +
  700. " private bool isEmptyField;{0}" +
  701. " {0}" +
  702. " private bool isEmptyFieldSpecified;{0}" +
  703. " {0}" +
  704. " private bool isNullField;{0}" +
  705. " {0}" +
  706. " public OptionalValueTypeContainer() {{{0}" +
  707. " this.attributesField = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  708. " this.flagsField = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  709. " }}{0}" +
  710. " {0}" +
  711. " /// <remarks/>{0}" +
  712. " public MonoTests.System.Xml.TestClasses.FlagEnum Attributes {{{0}" +
  713. " get {{{0}" +
  714. " return this.attributesField;{0}" +
  715. " }}{0}" +
  716. " set {{{0}" +
  717. " this.attributesField = value;{0}" +
  718. " }}{0}" +
  719. " }}{0}" +
  720. " {0}" +
  721. " /// <remarks/>{0}" +
  722. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags {{{0}" +
  723. " get {{{0}" +
  724. " return this.flagsField;{0}" +
  725. " }}{0}" +
  726. " set {{{0}" +
  727. " this.flagsField = value;{0}" +
  728. " }}{0}" +
  729. " }}{0}" +
  730. " {0}" +
  731. " /// <remarks/>{0}" +
  732. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  733. " public bool FlagsSpecified {{{0}" +
  734. " get {{{0}" +
  735. " return this.flagsFieldSpecified;{0}" +
  736. " }}{0}" +
  737. " set {{{0}" +
  738. " this.flagsFieldSpecified = value;{0}" +
  739. " }}{0}" +
  740. " }}{0}" +
  741. " {0}" +
  742. " /// <remarks/>{0}" +
  743. " public bool IsEmpty {{{0}" +
  744. " get {{{0}" +
  745. " return this.isEmptyField;{0}" +
  746. " }}{0}" +
  747. " set {{{0}" +
  748. " this.isEmptyField = value;{0}" +
  749. " }}{0}" +
  750. " }}{0}" +
  751. " {0}" +
  752. " /// <remarks/>{0}" +
  753. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  754. " public bool IsEmptySpecified {{{0}" +
  755. " get {{{0}" +
  756. " return this.isEmptyFieldSpecified;{0}" +
  757. " }}{0}" +
  758. " set {{{0}" +
  759. " this.isEmptyFieldSpecified = value;{0}" +
  760. " }}{0}" +
  761. " }}{0}" +
  762. " {0}" +
  763. " /// <remarks/>{0}" +
  764. " public bool IsNull {{{0}" +
  765. " get {{{0}" +
  766. " return this.isNullField;{0}" +
  767. " }}{0}" +
  768. " set {{{0}" +
  769. " this.isNullField = value;{0}" +
  770. " }}{0}" +
  771. " }}{0}" +
  772. "}}{0}" +
  773. "{0}" +
  774. "/// <remarks/>{0}" +
  775. "[System.FlagsAttribute()]{0}" +
  776. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  777. "[System.SerializableAttribute()]{0}" +
  778. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"some:urn\")]{0}" +
  779. "public enum FlagEnum {{{0}" +
  780. " {0}" +
  781. " /// <remarks/>{0}" +
  782. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  783. " e1 = 1,{0}" +
  784. " {0}" +
  785. " /// <remarks/>{0}" +
  786. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  787. " e2 = 2,{0}" +
  788. " {0}" +
  789. " /// <remarks/>{0}" +
  790. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  791. " e4 = 4,{0}" +
  792. "}}{0}" +
  793. "{0}" +
  794. "/// <remarks/>{0}" +
  795. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  796. "[System.SerializableAttribute()]{0}" +
  797. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  798. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  799. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  800. "public partial class TestDefault {{{0}" +
  801. " {0}" +
  802. " private string strField;{0}" +
  803. " {0}" +
  804. " private string strDefaultField;{0}" +
  805. " {0}" +
  806. " private bool boolTField;{0}" +
  807. " {0}" +
  808. " private bool boolFField;{0}" +
  809. " {0}" +
  810. " private decimal decimalvalField;{0}" +
  811. " {0}" +
  812. " private MonoTests.System.Xml.TestClasses.FlagEnum flagField;{0}" +
  813. " {0}" +
  814. " private MonoTests.System.Xml.TestClasses.FlagEnum_Encoded flagencodedField;{0}" +
  815. " {0}" +
  816. " public TestDefault() {{{0}" +
  817. " this.strDefaultField = \"Default Value\";{0}" +
  818. " this.flagField = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  819. " this.flagencodedField = (MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4);{0}" +
  820. " }}{0}" +
  821. " {0}" +
  822. " /// <remarks/>{0}" +
  823. " public string str {{{0}" +
  824. " get {{{0}" +
  825. " return this.strField;{0}" +
  826. " }}{0}" +
  827. " set {{{0}" +
  828. " this.strField = value;{0}" +
  829. " }}{0}" +
  830. " }}{0}" +
  831. " {0}" +
  832. " /// <remarks/>{0}" +
  833. " public string strDefault {{{0}" +
  834. " get {{{0}" +
  835. " return this.strDefaultField;{0}" +
  836. " }}{0}" +
  837. " set {{{0}" +
  838. " this.strDefaultField = value;{0}" +
  839. " }}{0}" +
  840. " }}{0}" +
  841. " {0}" +
  842. " /// <remarks/>{0}" +
  843. " public bool boolT {{{0}" +
  844. " get {{{0}" +
  845. " return this.boolTField;{0}" +
  846. " }}{0}" +
  847. " set {{{0}" +
  848. " this.boolTField = value;{0}" +
  849. " }}{0}" +
  850. " }}{0}" +
  851. " {0}" +
  852. " /// <remarks/>{0}" +
  853. " public bool boolF {{{0}" +
  854. " get {{{0}" +
  855. " return this.boolFField;{0}" +
  856. " }}{0}" +
  857. " set {{{0}" +
  858. " this.boolFField = value;{0}" +
  859. " }}{0}" +
  860. " }}{0}" +
  861. " {0}" +
  862. " /// <remarks/>{0}" +
  863. " public decimal decimalval {{{0}" +
  864. " get {{{0}" +
  865. " return this.decimalvalField;{0}" +
  866. " }}{0}" +
  867. " set {{{0}" +
  868. " this.decimalvalField = value;{0}" +
  869. " }}{0}" +
  870. " }}{0}" +
  871. " {0}" +
  872. " /// <remarks/>{0}" +
  873. " public MonoTests.System.Xml.TestClasses.FlagEnum flag {{{0}" +
  874. " get {{{0}" +
  875. " return this.flagField;{0}" +
  876. " }}{0}" +
  877. " set {{{0}" +
  878. " this.flagField = value;{0}" +
  879. " }}{0}" +
  880. " }}{0}" +
  881. " {0}" +
  882. " /// <remarks/>{0}" +
  883. " public MonoTests.System.Xml.TestClasses.FlagEnum_Encoded flagencoded {{{0}" +
  884. " get {{{0}" +
  885. " return this.flagencodedField;{0}" +
  886. " }}{0}" +
  887. " set {{{0}" +
  888. " this.flagencodedField = value;{0}" +
  889. " }}{0}" +
  890. " }}{0}" +
  891. "}}{0}" +
  892. "{0}" +
  893. "/// <remarks/>{0}" +
  894. "[System.FlagsAttribute()]{0}" +
  895. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  896. "[System.SerializableAttribute()]{0}" +
  897. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  898. "public enum FlagEnum {{{0}" +
  899. " {0}" +
  900. " /// <remarks/>{0}" +
  901. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  902. " e1 = 1,{0}" +
  903. " {0}" +
  904. " /// <remarks/>{0}" +
  905. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  906. " e2 = 2,{0}" +
  907. " {0}" +
  908. " /// <remarks/>{0}" +
  909. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  910. " e4 = 4,{0}" +
  911. "}}{0}" +
  912. "{0}" +
  913. "/// <remarks/>{0}" +
  914. "[System.FlagsAttribute()]{0}" +
  915. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  916. "[System.SerializableAttribute()]{0}" +
  917. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  918. "public enum FlagEnum_Encoded {{{0}" +
  919. " {0}" +
  920. " /// <remarks/>{0}" +
  921. " e1 = 1,{0}" +
  922. " {0}" +
  923. " /// <remarks/>{0}" +
  924. " e2 = 2,{0}" +
  925. " {0}" +
  926. " /// <remarks/>{0}" +
  927. " e4 = 4,{0}" +
  928. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  929. #else
  930. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:aNS\")]{0}" +
  931. "[System.Xml.Serialization.XmlRootAttribute(\"root\", Namespace=\"urn:aNS\", IsNullable=false)]{0}" +
  932. "public class Root {{{0}" +
  933. " {0}" +
  934. " /// <remarks/>{0}" +
  935. " public MonoTests.System.Xml.TestClasses.OptionalValueTypeContainer OptionalValue;{0}" +
  936. " {0}" +
  937. " /// <remarks/>{0}" +
  938. " public MonoTests.System.Xml.TestClasses.TestDefault Default;{0}" +
  939. "}}{0}" +
  940. "{0}" +
  941. "/// <remarks/>{0}" +
  942. "[System.Xml.Serialization.XmlTypeAttribute(TypeName=\"optionalValueType\", Namespace=\"some:urn\")]{0}" +
  943. "public class OptionalValueTypeContainer {{{0}" +
  944. " {0}" +
  945. " /// <remarks/>{0}" +
  946. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4))]{0}" +
  947. " public MonoTests.System.Xml.TestClasses.FlagEnum Attributes = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  948. " {0}" +
  949. " /// <remarks/>{0}" +
  950. " [System.ComponentModel.DefaultValueAttribute(MonoTests.System.Xml.TestClasses.FlagEnum.e1)]{0}" +
  951. " public MonoTests.System.Xml.TestClasses.FlagEnum Flags = MonoTests.System.Xml.TestClasses.FlagEnum.e1;{0}" +
  952. " {0}" +
  953. " /// <remarks/>{0}" +
  954. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  955. " public bool FlagsSpecified;{0}" +
  956. " {0}" +
  957. " /// <remarks/>{0}" +
  958. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  959. " public bool IsEmpty = false;{0}" +
  960. " {0}" +
  961. " /// <remarks/>{0}" +
  962. " [System.Xml.Serialization.XmlIgnoreAttribute()]{0}" +
  963. " public bool IsEmptySpecified;{0}" +
  964. " {0}" +
  965. " /// <remarks/>{0}" +
  966. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  967. " public bool IsNull = false;{0}" +
  968. "}}{0}" +
  969. "{0}" +
  970. "/// <remarks/>{0}" +
  971. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"some:urn\")]{0}" +
  972. "[System.FlagsAttribute()]{0}" +
  973. "public enum FlagEnum {{{0}" +
  974. " {0}" +
  975. " /// <remarks/>{0}" +
  976. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  977. " e1 = 1,{0}" +
  978. " {0}" +
  979. " /// <remarks/>{0}" +
  980. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  981. " e2 = 2,{0}" +
  982. " {0}" +
  983. " /// <remarks/>{0}" +
  984. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  985. " e4 = 4,{0}" +
  986. "}}{0}" +
  987. "{0}" +
  988. "/// <remarks/>{0}" +
  989. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  990. "public class TestDefault {{{0}" +
  991. " {0}" +
  992. " /// <remarks/>{0}" +
  993. " public string str;{0}" +
  994. " {0}" +
  995. " /// <remarks/>{0}" +
  996. " [System.ComponentModel.DefaultValueAttribute(\"Default Value\")]{0}" +
  997. " public string strDefault = \"Default Value\";{0}" +
  998. " {0}" +
  999. " /// <remarks/>{0}" +
  1000. " [System.ComponentModel.DefaultValueAttribute(true)]{0}" +
  1001. " public bool boolT = true;{0}" +
  1002. " {0}" +
  1003. " /// <remarks/>{0}" +
  1004. " [System.ComponentModel.DefaultValueAttribute(false)]{0}" +
  1005. " public bool boolF = false;{0}" +
  1006. " {0}" +
  1007. " /// <remarks/>{0}" +
  1008. " [System.ComponentModel.DefaultValueAttribute(typeof(System.Decimal), \"10\")]{0}" +
  1009. " public System.Decimal decimalval = ((System.Decimal)(10m));{0}" +
  1010. " {0}" +
  1011. " /// <remarks/>{0}" +
  1012. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4))]{0}" +
  1013. " public MonoTests.System.Xml.TestClasses.FlagEnum flag = (MonoTests.System.Xml.TestClasses.FlagEnum.e1 | MonoTests.System.Xml.TestClasses.FlagEnum.e4);{0}" +
  1014. " {0}" +
  1015. " /// <remarks/>{0}" +
  1016. " [System.ComponentModel.DefaultValueAttribute((MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4))]{0}" +
  1017. " public MonoTests.System.Xml.TestClasses.FlagEnum_Encoded flagencoded = (MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e1 | MonoTests.System.Xml.TestClasses.FlagEnum_Encoded.e4);{0}" +
  1018. "}}{0}" +
  1019. "{0}" +
  1020. "/// <remarks/>{0}" +
  1021. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  1022. "[System.FlagsAttribute()]{0}" +
  1023. "public enum FlagEnum {{{0}" +
  1024. " {0}" +
  1025. " /// <remarks/>{0}" +
  1026. " [System.Xml.Serialization.XmlEnumAttribute(\"one\")]{0}" +
  1027. " e1 = 1,{0}" +
  1028. " {0}" +
  1029. " /// <remarks/>{0}" +
  1030. " [System.Xml.Serialization.XmlEnumAttribute(\"two\")]{0}" +
  1031. " e2 = 2,{0}" +
  1032. " {0}" +
  1033. " /// <remarks/>{0}" +
  1034. " [System.Xml.Serialization.XmlEnumAttribute(\"four\")]{0}" +
  1035. " e4 = 4,{0}" +
  1036. "}}{0}" +
  1037. "{0}" +
  1038. "/// <remarks/>{0}" +
  1039. "[System.Xml.Serialization.XmlTypeAttribute(Namespace=\"urn:myNS\")]{0}" +
  1040. "[System.FlagsAttribute()]{0}" +
  1041. "public enum FlagEnum_Encoded {{{0}" +
  1042. " {0}" +
  1043. " /// <remarks/>{0}" +
  1044. " e1 = 1,{0}" +
  1045. " {0}" +
  1046. " /// <remarks/>{0}" +
  1047. " e2 = 2,{0}" +
  1048. " {0}" +
  1049. " /// <remarks/>{0}" +
  1050. " e4 = 4,{0}" +
  1051. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  1052. #endif
  1053. }
  1054. [Test]
  1055. public void ExportTypeMapping_SimpleClassWithXmlAttributes ()
  1056. {
  1057. CodeNamespace codeNamespace = ExportCode (typeof (SimpleClassWithXmlAttributes));
  1058. Assert.IsNotNull (codeNamespace, "#1");
  1059. StringWriter sw = new StringWriter ();
  1060. CodeDomProvider provider = new CSharpCodeProvider ();
  1061. ICodeGenerator generator = provider.CreateGenerator ();
  1062. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  1063. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1064. "{0}{0}" +
  1065. "/// <remarks/>{0}" +
  1066. #if NET_2_0
  1067. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  1068. "[System.SerializableAttribute()]{0}" +
  1069. "[System.Diagnostics.DebuggerStepThroughAttribute()]{0}" +
  1070. "[System.ComponentModel.DesignerCategoryAttribute(\"code\")]{0}" +
  1071. "[System.Xml.Serialization.XmlRootAttribute(\"simple\", Namespace=\"\", IsNullable=true)]{0}" +
  1072. "public partial class SimpleClassWithXmlAttributes {{{0}" +
  1073. " {0}" +
  1074. " private string somethingField;{0}" +
  1075. " {0}" +
  1076. " /// <remarks/>{0}" +
  1077. " [System.Xml.Serialization.XmlAttributeAttribute(\"member\")]{0}" +
  1078. " public string something {{{0}" +
  1079. " get {{{0}" +
  1080. " return this.somethingField;{0}" +
  1081. " }}{0}" +
  1082. " set {{{0}" +
  1083. " this.somethingField = value;{0}" +
  1084. " }}{0}" +
  1085. " }}{0}" +
  1086. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  1087. #else
  1088. "[System.Xml.Serialization.XmlRootAttribute(\"simple\", Namespace=\"\", IsNullable=true)]{0}" +
  1089. "public class SimpleClassWithXmlAttributes {{{0}" +
  1090. " {0}" +
  1091. " /// <remarks/>{0}" +
  1092. " [System.Xml.Serialization.XmlAttributeAttribute(\"member\")]{0}" +
  1093. " public string something;{0}" +
  1094. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  1095. #endif
  1096. }
  1097. [Test]
  1098. public void ExportTypeMapping_XsdPrimitive_Arrays ()
  1099. {
  1100. ArrayList types = new ArrayList ();
  1101. types.Add (typeof (sbyte[]));
  1102. types.Add (typeof (bool[]));
  1103. types.Add (typeof (short[]));
  1104. types.Add (typeof (int[]));
  1105. types.Add (typeof (long[]));
  1106. types.Add (typeof (float[]));
  1107. types.Add (typeof (double[]));
  1108. types.Add (typeof (decimal[]));
  1109. types.Add (typeof (ushort[]));
  1110. types.Add (typeof (uint[]));
  1111. types.Add (typeof (ulong[]));
  1112. types.Add (typeof (DateTime[]));
  1113. types.Add (typeof (XmlQualifiedName[]));
  1114. types.Add (typeof (string[]));
  1115. StringWriter sw = new StringWriter ();
  1116. CodeDomProvider provider = new CSharpCodeProvider ();
  1117. ICodeGenerator generator = provider.CreateGenerator ();
  1118. foreach (Type type in types) {
  1119. CodeNamespace codeNamespace = ExportCode (type);
  1120. Assert.IsNotNull (codeNamespace, type.FullName + "#1");
  1121. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  1122. Assert.AreEqual (Environment.NewLine, sw.ToString (),
  1123. type.FullName + "#2");
  1124. sw.GetStringBuilder ().Length = 0;
  1125. }
  1126. }
  1127. [Test]
  1128. public void ExportTypeMapping_ZeroFlagEnum ()
  1129. {
  1130. CodeNamespace codeNamespace = ExportCode (typeof (ZeroFlagEnum));
  1131. Assert.IsNotNull (codeNamespace, "#1");
  1132. StringWriter sw = new StringWriter ();
  1133. CodeDomProvider provider = new CSharpCodeProvider ();
  1134. ICodeGenerator generator = provider.CreateGenerator ();
  1135. generator.GenerateCodeFromNamespace (codeNamespace, sw, new CodeGeneratorOptions ());
  1136. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1137. "{0}{0}" +
  1138. "/// <remarks/>{0}" +
  1139. "[System.FlagsAttribute()]{0}" +
  1140. #if NET_2_0
  1141. "[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Xml\", \"{1}\")]{0}" +
  1142. "[System.SerializableAttribute()]{0}" +
  1143. #endif
  1144. "[System.Xml.Serialization.XmlRootAttribute(Namespace=\"\", IsNullable=false)]{0}" +
  1145. "public enum ZeroFlagEnum {{{0}" +
  1146. " {0}" +
  1147. " /// <remarks/>{0}" +
  1148. " [System.Xml.Serialization.XmlEnumAttribute(\"zero\")]{0}" +
  1149. " e0 = 1,{0}" +
  1150. " {0}" +
  1151. " /// <remarks/>{0}" +
  1152. " [System.Xml.Serialization.XmlEnumAttribute(\"o<n>e\")]{0}" +
  1153. " e1 = 2,{0}" +
  1154. " {0}" +
  1155. " /// <remarks/>{0}" +
  1156. " [System.Xml.Serialization.XmlEnumAttribute(\"tns:t<w>o\")]{0}" +
  1157. " e2 = 4,{0}" +
  1158. #if NET_2_0
  1159. "}}{0}", Environment.NewLine, XmlFileVersion), sw.ToString (), "#2");
  1160. #else
  1161. "}}{0}", Environment.NewLine), sw.ToString (), "#2");
  1162. #endif
  1163. }
  1164. [Test]
  1165. public void DuplicateIdentifiers ()
  1166. {
  1167. XmlSchema xs = XmlSchema.Read (File.OpenText ("Test/XmlFiles/xsd/82078.xsd"), null);
  1168. XmlSchemas xss = new XmlSchemas ();
  1169. xss.Add (xs);
  1170. XmlSchemaImporter imp = new XmlSchemaImporter (xss);
  1171. CodeNamespace cns = new CodeNamespace ();
  1172. XmlCodeExporter exp = new XmlCodeExporter (cns);
  1173. XmlQualifiedName qname = new XmlQualifiedName (
  1174. "Operation", "http://tempuri.org/");
  1175. exp.ExportTypeMapping (imp.ImportTypeMapping (qname));
  1176. CodeCompileUnit ccu = new CodeCompileUnit ();
  1177. ccu.Namespaces.Add (cns);
  1178. CodeDomProvider provider = new CSharpCodeProvider ();
  1179. ICodeCompiler compiler = provider.CreateCompiler ();
  1180. CompilerParameters options = new CompilerParameters ();
  1181. options.ReferencedAssemblies.Add ("System.dll");
  1182. options.ReferencedAssemblies.Add ("System.Xml.dll");
  1183. options.GenerateInMemory = true;
  1184. CompilerResults result = compiler.CompileAssemblyFromDom (options, ccu);
  1185. Assert.AreEqual (0, result.Errors.Count, "#1");
  1186. Assert.IsNotNull (result.CompiledAssembly, "#2");
  1187. }
  1188. [Test]
  1189. public void ExportSimpleContentExtensionEnum ()
  1190. {
  1191. string xsd = @"
  1192. <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:b='urn:bar' targetNamespace='urn:bar'>
  1193. <xs:element name='Foo' type='b:DayOfWeek' />
  1194. <xs:complexType name='DayOfWeek'>
  1195. <xs:simpleContent>
  1196. <xs:extension base='b:WeekDay' />
  1197. </xs:simpleContent>
  1198. </xs:complexType>
  1199. <xs:simpleType name='WeekDay'>
  1200. <xs:restriction base='xs:string'>
  1201. <xs:enumeration value='Sunday'/>
  1202. <xs:enumeration value='Monday'/>
  1203. <xs:enumeration value='Tuesday'/>
  1204. <xs:enumeration value='Wednesday'/>
  1205. <xs:enumeration value='Thursday'/>
  1206. <xs:enumeration value='Friday'/>
  1207. <xs:enumeration value='Saturday'/>
  1208. </xs:restriction>
  1209. </xs:simpleType>
  1210. </xs:schema>";
  1211. XmlSchema xs = XmlSchema.Read (new StringReader (xsd), null);
  1212. XmlSchemas xss = new XmlSchemas ();
  1213. xss.Add (xs);
  1214. XmlSchemaImporter imp = new XmlSchemaImporter (xss);
  1215. XmlTypeMapping m = imp.ImportTypeMapping (new XmlQualifiedName ("Foo", "urn:bar"));
  1216. CodeNamespace cns = new CodeNamespace ();
  1217. XmlCodeExporter exp = new XmlCodeExporter (cns);
  1218. exp.ExportTypeMapping (m);
  1219. CodeTypeDeclaration enumType = null;
  1220. foreach (CodeTypeDeclaration ctd in cns.Types)
  1221. if (ctd.Name == "WeekDay")
  1222. enumType = ctd;
  1223. Assert.IsNotNull (enumType);
  1224. }
  1225. CodeNamespace ExportCode (Type type)
  1226. {
  1227. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1228. XmlTypeMapping map = imp.ImportTypeMapping (type);
  1229. CodeNamespace codeNamespace = new CodeNamespace ();
  1230. XmlCodeExporter exp = new XmlCodeExporter (codeNamespace);
  1231. exp.ExportTypeMapping (map);
  1232. return codeNamespace;
  1233. }
  1234. #if NET_2_0
  1235. string XmlFileVersion {
  1236. get {
  1237. Assembly xmlAsm = typeof (XmlDocument).Assembly;
  1238. AssemblyFileVersionAttribute afv = (AssemblyFileVersionAttribute)
  1239. Attribute.GetCustomAttribute (xmlAsm, typeof (AssemblyFileVersionAttribute));
  1240. return afv.Version;
  1241. }
  1242. }
  1243. #endif
  1244. [XmlRootAttribute ("root", Namespace="urn:aNS", IsNullable=false)]
  1245. public class Root
  1246. {
  1247. public OptionalValueTypeContainer OptionalValue;
  1248. public TestDefault Default;
  1249. }
  1250. }
  1251. }
  1252. #endif