CodeGeneratorFromCompileUnitTest.cs 4.5 KB

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