CodeGeneratorFromCompileUnitTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // Microsoft.VisualBasic.* Test Cases
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (c) 2005 Novell
  8. //
  9. using System;
  10. using System.Globalization;
  11. using System.Text;
  12. using System.CodeDom;
  13. using System.CodeDom.Compiler;
  14. using NUnit.Framework;
  15. namespace MonoTests.Microsoft.VisualBasic
  16. {
  17. [TestFixture]
  18. public class CodeGeneratorFromCompileUnitTest : CodeGeneratorTestBase
  19. {
  20. string codeUnitHeader = "";
  21. CodeCompileUnit codeUnit = null;
  22. public CodeGeneratorFromCompileUnitTest ()
  23. {
  24. Init();
  25. Generate();
  26. codeUnitHeader = Code;
  27. }
  28. [SetUp]
  29. public void Init ()
  30. {
  31. InitBase ();
  32. codeUnit = new CodeCompileUnit ();
  33. }
  34. protected override string Code {
  35. get { return base.Code.Substring (codeUnitHeader.Length); }
  36. }
  37. protected override void Generate ()
  38. {
  39. generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
  40. writer.Close ();
  41. }
  42. [Test]
  43. public void DefaultCodeUnitTest ()
  44. {
  45. Generate ();
  46. Assert.AreEqual ("", Code);
  47. }
  48. [Test]
  49. [ExpectedException (typeof (NullReferenceException))]
  50. public void NullCodeUnitTest ()
  51. {
  52. codeUnit = null;
  53. Generate();
  54. }
  55. [Test]
  56. public void ReferencedTest ()
  57. {
  58. codeUnit.ReferencedAssemblies.Add ("System.dll");
  59. Generate();
  60. Assert.AreEqual ("", Code);
  61. }
  62. [Test]
  63. public void SimpleNamespaceTest ()
  64. {
  65. CodeNamespace ns = new CodeNamespace ("A");
  66. codeUnit.Namespaces.Add (ns);
  67. Generate ();
  68. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  69. "{0}Namespace A{0}End Namespace{0}", writer.NewLine), Code);
  70. }
  71. [Test]
  72. public void ReferenceAndSimpleNamespaceTest()
  73. {
  74. CodeNamespace ns = new CodeNamespace ("A");
  75. codeUnit.Namespaces.Add (ns);
  76. codeUnit.ReferencedAssemblies.Add ("using System;");
  77. Generate ();
  78. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  79. "{0}Namespace A{0}End Namespace{0}", writer.NewLine), Code);
  80. }
  81. [Test]
  82. public void SimpleAttributeTest ()
  83. {
  84. CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
  85. attrDec.Name = "A";
  86. codeUnit.AssemblyCustomAttributes.Add (attrDec);
  87. Generate ();
  88. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  89. "<Assembly: A()> {0}", writer.NewLine), Code);
  90. }
  91. [Test]
  92. public void AttributeWithValueTest ()
  93. {
  94. CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
  95. attrDec.Name = "A";
  96. attrDec.Arguments.Add (new CodeAttributeArgument("A1",
  97. new CodePrimitiveExpression(false)));
  98. attrDec.Arguments.Add (new CodeAttributeArgument("A2",
  99. new CodePrimitiveExpression(true)));
  100. // null name should not be output
  101. attrDec.Arguments.Add (new CodeAttributeArgument (null,
  102. new CodePrimitiveExpression (true)));
  103. // zero length name should not be output
  104. attrDec.Arguments.Add (new CodeAttributeArgument (string.Empty,
  105. new CodePrimitiveExpression (false)));
  106. codeUnit.AssemblyCustomAttributes.Add (attrDec);
  107. Generate ();
  108. Assert.AreEqual ("<Assembly: A(A1:=false, A2:=true, true, false)> " +
  109. writer.NewLine, Code);
  110. }
  111. [Test]
  112. public void MultipleAttributeTest ()
  113. {
  114. CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
  115. attrDec.Name = "A";
  116. codeUnit.AssemblyCustomAttributes.Add (attrDec);
  117. attrDec = new CodeAttributeDeclaration ();
  118. attrDec.Name = "B";
  119. codeUnit.AssemblyCustomAttributes.Add (attrDec);
  120. Generate ();
  121. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  122. "<Assembly: A(), _{0} Assembly: B()> {0}", writer.NewLine),
  123. Code);
  124. }
  125. [Test]
  126. public void AttributeAndSimpleNamespaceTest ()
  127. {
  128. CodeNamespace ns = new CodeNamespace ("A");
  129. codeUnit.Namespaces.Add (ns);
  130. CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
  131. attrDec.Name = "A";
  132. codeUnit.AssemblyCustomAttributes.Add (attrDec);
  133. attrDec = new CodeAttributeDeclaration ();
  134. attrDec.Name = "B";
  135. codeUnit.AssemblyCustomAttributes.Add (attrDec);
  136. Generate ();
  137. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  138. "<Assembly: A(), _{0} Assembly: B()> {0}{0}Namespace A{0}End "
  139. + "Namespace{0}",writer.NewLine), Code);
  140. }
  141. [Test]
  142. public void CodeSnippetTest ()
  143. {
  144. StringBuilder sb = new StringBuilder();
  145. sb.Append ("Public Class Test1");
  146. sb.Append (Environment.NewLine);
  147. sb.Append ("End Class");
  148. codeUnit = new CodeSnippetCompileUnit (sb.ToString ());
  149. generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
  150. writer.Close ();
  151. Assert.AreEqual (sb.ToString () + writer.NewLine,
  152. writer.ToString());
  153. }
  154. }
  155. }