[runtime] Overwrite stacktrace for exception on re-throw. Fixes #1856.
[mono.git] / mcs / class / System / Test / Microsoft.VisualBasic / CodeGeneratorFromCompileUnitTest.cs
1 //
2 // Microsoft.VisualBasic.* Test Cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2005 Novell
8 //
9
10 using System;
11 using System.Globalization;
12 using System.IO;
13 using System.Text;
14 using System.CodeDom;
15 using System.CodeDom.Compiler;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.Microsoft.VisualBasic
20 {
21         [TestFixture]
22         public class CodeGeneratorFromCompileUnitTest : CodeGeneratorTestBase
23         {
24                 string codeUnitHeader = "";
25                 CodeCompileUnit codeUnit = null;
26
27                 public CodeGeneratorFromCompileUnitTest ()
28                 {
29                         Init();
30                         codeUnitHeader = Generate ();
31                 }
32                 
33                 [SetUp]
34                 public void Init ()
35                 {
36                         InitBase ();
37                         codeUnit = new CodeCompileUnit ();
38                 }
39
40                 protected override string Generate (CodeGeneratorOptions options)
41                 {
42                         StringWriter writer = new StringWriter ();
43                         writer.NewLine = NewLine;
44
45                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
46                         writer.Close ();
47                         return writer.ToString ().Substring (codeUnitHeader.Length);
48                 }
49
50                 [Test]
51                 public void DefaultCodeUnitTest ()
52                 {
53                         Assert.AreEqual ("", Generate ());
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (NullReferenceException))]
58                 public void NullCodeUnitTest ()
59                 {
60                         codeUnit = null;
61                         Generate();
62                 }
63
64                 [Test]
65                 public void ReferencedTest ()
66                 {
67                         codeUnit.ReferencedAssemblies.Add ("System.dll");
68                         Assert.AreEqual ("", Generate ());
69                 }
70
71                 [Test]
72                 public void SimpleNamespaceTest ()
73                 {
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 ());
78                 }
79
80                 [Test]
81                 public void ReferenceAndSimpleNamespaceTest()
82                 {
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 ());
88                 }
89
90                 [Test]
91                 public void SimpleAttributeTest ()
92                 {
93                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
94                         attrDec.Name = "A";
95
96                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
97                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
98                                 "<Assembly: A()> {0}", NewLine), Generate ());
99                 }
100
101                 [Test]
102                 public void AttributeWithValueTest ()
103                 {
104                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
105                         attrDec.Name = "A";
106
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)));
117
118                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
119                         Assert.AreEqual ("<Assembly: A(A1:=false, A2:=true, true, false)> " +
120                                 NewLine, Generate ());
121                 }
122
123                 [Test]
124                 public void MultipleAttributeTest ()
125                 {
126                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
127                         attrDec.Name = "A";
128                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
129
130                         attrDec = new CodeAttributeDeclaration ();
131                         attrDec.Name = "B";
132                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
133                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, 
134                                 "<Assembly: A(),  _{0} Assembly: B()> {0}", NewLine),
135                                 Generate ());
136                 }
137
138                 [Test]
139                 public void AttributeAndSimpleNamespaceTest ()
140                 {
141                         CodeNamespace ns = new CodeNamespace ("A");
142                         codeUnit.Namespaces.Add (ns);
143
144                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
145                         attrDec.Name = "A";
146                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
147
148                         attrDec = new CodeAttributeDeclaration ();
149                         attrDec.Name = "B";
150                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
151
152                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
153                                 "<Assembly: A(),  _{0} Assembly: B()> {0}{0}Namespace A{0}End "
154                                 + "Namespace{0}", NewLine), Generate ());
155                 }
156
157                 [Test]
158                 public void CodeSnippetTest ()
159                 {
160                         StringBuilder sb = new StringBuilder();
161                         sb.Append ("Public Class Test1");
162                         sb.Append (Environment.NewLine);
163                         sb.Append ("End Class");
164
165                         StringWriter writer = new StringWriter ();
166                         writer.NewLine = NewLine;
167
168                         codeUnit = new CodeSnippetCompileUnit (sb.ToString ());
169                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
170                         writer.Close ();
171                         Assert.AreEqual (sb.ToString () + NewLine, writer.ToString());
172                 }
173                 
174                 [Test]
175                 public void ExternalSourceTest ()
176                 {
177                         CodeSnippetCompileUnit snippet;
178                         
179                         StringBuilder sb = new StringBuilder();
180                         sb.Append ("\n");
181                         sb.Append ("#ExternalSource(\"file.vb\",123)");
182                         sb.Append ("\n");
183                         sb.Append ("\n");
184                         sb.Append ("\n");
185                         sb.Append ("#End ExternalSource");
186                         sb.Append ("\n");
187
188                         StringWriter writer = new StringWriter ();
189                         writer.NewLine = NewLine;
190
191                         codeUnit = new CodeSnippetCompileUnit ("");
192                         snippet = (CodeSnippetCompileUnit) codeUnit;
193                         snippet.LinePragma = new CodeLinePragma ("file.vb", 123);
194                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
195                         writer.Close ();
196                         Assert.AreEqual (sb.ToString (), writer.ToString());
197                 }
198         }
199 }