2 // Microsoft.VisualBasic.* Test Cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
11 using System.Globalization;
15 using System.CodeDom.Compiler;
17 using NUnit.Framework;
19 namespace MonoTests.Microsoft.VisualBasic
22 public class CodeGeneratorFromCompileUnitTest : CodeGeneratorTestBase
24 string codeUnitHeader = "";
25 CodeCompileUnit codeUnit = null;
27 public CodeGeneratorFromCompileUnitTest ()
30 codeUnitHeader = Generate ();
37 codeUnit = new CodeCompileUnit ();
40 protected override string Generate (CodeGeneratorOptions options)
42 StringWriter writer = new StringWriter ();
43 writer.NewLine = NewLine;
45 generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
47 return writer.ToString ().Substring (codeUnitHeader.Length);
51 public void DefaultCodeUnitTest ()
53 Assert.AreEqual ("", Generate ());
57 [ExpectedException (typeof (NullReferenceException))]
58 public void NullCodeUnitTest ()
65 public void ReferencedTest ()
67 codeUnit.ReferencedAssemblies.Add ("System.dll");
68 Assert.AreEqual ("", Generate ());
72 public void SimpleNamespaceTest ()
74 CodeNamespace ns = new CodeNamespace ("A");
75 codeUnit.Namespaces.Add (ns);
76 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
77 "{0}Namespace A{0}End Namespace{0}", NewLine), Generate ());
81 public void ReferenceAndSimpleNamespaceTest()
83 CodeNamespace ns = new CodeNamespace ("A");
84 codeUnit.Namespaces.Add (ns);
85 codeUnit.ReferencedAssemblies.Add ("using System;");
86 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
87 "{0}Namespace A{0}End Namespace{0}", NewLine), Generate ());
91 public void SimpleAttributeTest ()
93 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
96 codeUnit.AssemblyCustomAttributes.Add (attrDec);
97 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
98 "<Assembly: A()> {0}", NewLine), Generate ());
102 public void AttributeWithValueTest ()
104 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
107 attrDec.Arguments.Add (new CodeAttributeArgument("A1",
108 new CodePrimitiveExpression(false)));
109 attrDec.Arguments.Add (new CodeAttributeArgument("A2",
110 new CodePrimitiveExpression(true)));
111 // null name should not be output
112 attrDec.Arguments.Add (new CodeAttributeArgument (null,
113 new CodePrimitiveExpression (true)));
114 // zero length name should not be output
115 attrDec.Arguments.Add (new CodeAttributeArgument (string.Empty,
116 new CodePrimitiveExpression (false)));
118 codeUnit.AssemblyCustomAttributes.Add (attrDec);
119 Assert.AreEqual ("<Assembly: A(A1:=false, A2:=true, true, false)> " +
120 NewLine, Generate ());
124 public void MultipleAttributeTest ()
126 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
128 codeUnit.AssemblyCustomAttributes.Add (attrDec);
130 attrDec = new CodeAttributeDeclaration ();
132 codeUnit.AssemblyCustomAttributes.Add (attrDec);
133 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
134 "<Assembly: A(), _{0} Assembly: B()> {0}", NewLine),
139 public void AttributeAndSimpleNamespaceTest ()
141 CodeNamespace ns = new CodeNamespace ("A");
142 codeUnit.Namespaces.Add (ns);
144 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
146 codeUnit.AssemblyCustomAttributes.Add (attrDec);
148 attrDec = new CodeAttributeDeclaration ();
150 codeUnit.AssemblyCustomAttributes.Add (attrDec);
152 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
153 "<Assembly: A(), _{0} Assembly: B()> {0}{0}Namespace A{0}End "
154 + "Namespace{0}", NewLine), Generate ());
158 public void CodeSnippetTest ()
160 StringBuilder sb = new StringBuilder();
161 sb.Append ("Public Class Test1");
162 sb.Append (Environment.NewLine);
163 sb.Append ("End Class");
165 StringWriter writer = new StringWriter ();
166 writer.NewLine = NewLine;
168 codeUnit = new CodeSnippetCompileUnit (sb.ToString ());
169 generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
171 Assert.AreEqual (sb.ToString () + NewLine, writer.ToString());
175 public void ExternalSourceTest ()
177 CodeSnippetCompileUnit snippet;
179 StringBuilder sb = new StringBuilder();
181 sb.Append ("#ExternalSource(\"file.vb\",123)");
185 sb.Append ("#End ExternalSource");
188 StringWriter writer = new StringWriter ();
189 writer.NewLine = NewLine;
191 codeUnit = new CodeSnippetCompileUnit ("");
192 snippet = (CodeSnippetCompileUnit) codeUnit;
193 snippet.LinePragma = new CodeLinePragma ("file.vb", 123);
194 generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
196 Assert.AreEqual (sb.ToString (), writer.ToString());