2 // Microsoft.VisualBasic.* Test Cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (c) 2005 Gert Driesen
11 using System.CodeDom.Compiler;
12 using System.Globalization;
16 using NUnit.Framework;
18 namespace MonoTests.Microsoft.VisualBasic
21 /// Test ICodeGenerator's GenerateCodeFromStatement, along with a
22 /// minimal set CodeDom components.
25 public class CodeGeneratorFromStatementTest: CodeGeneratorTestBase
27 private CodeStatement statement = null;
33 statement = new CodeStatement ();
36 protected override string Generate (CodeGeneratorOptions options)
38 StringWriter writer = new StringWriter ();
39 writer.NewLine = NewLine;
41 generator.GenerateCodeFromStatement (statement, writer, options);
43 return writer.ToString ();
47 [ExpectedException (typeof (ArgumentException))]
48 public void DefaultStatementTest ()
54 [ExpectedException (typeof (NullReferenceException))]
55 public void NullStatementTest ()
62 public void CodeCommentStatementTest ()
64 CodeCommentStatement commentStatement = new CodeCommentStatement ();
65 CodeComment comment = new CodeComment ();
66 commentStatement.Comment = comment;
67 statement = commentStatement;
69 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
70 "'{0}", NewLine), Generate (), "#1");
72 comment.Text = "a\nb";
73 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
74 "'a\n'b{0}", NewLine), Generate (), "#2");
76 comment.Text = "a\r\nb";
77 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
78 "'a\r\n'b{0}", NewLine), Generate (), "#3");
80 comment.Text = "a\rb";
81 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
82 "'a\r'b{0}", NewLine), Generate (), "#4");
86 public void ThrowExceptionStatementTest ()
88 CodeThrowExceptionStatement ctet = new CodeThrowExceptionStatement ();
90 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
91 "Throw{0}", NewLine), Generate (), "#1");
93 ctet.ToThrow = new CodeSnippetExpression ("whatever");
94 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
95 "Throw whatever{0}", NewLine), Generate (), "#2");
99 public void GotoStatementTest ()
101 statement = new CodeGotoStatement ("something");
102 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
103 "goto something{0}", NewLine), Generate ());
107 public void TryCatchFinallyStatementTest ()
109 CodeStatement cs = new CodeGotoStatement ("exit");
110 CodeCatchClause ccc1 = new CodeCatchClause ("ex1", new CodeTypeReference ("System.ArgumentException"));
111 CodeCatchClause ccc2 = new CodeCatchClause (null, new CodeTypeReference ("System.ApplicationException"));
112 CodeSnippetStatement fin1 = new CodeSnippetStatement ("A");
113 CodeSnippetStatement fin2 = new CodeSnippetStatement ("B");
115 statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
116 new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
118 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
121 "Catch ex1 As System.ArgumentException{0}" +
123 "Catch __exception As System.ApplicationException{0}" +
125 "Catch As System.ApplicationException{0}" +
135 "End Try{0}", NewLine), Generate (), "#1");
137 options.ElseOnClosing = true;
138 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
141 "Catch ex1 As System.ArgumentException{0}" +
143 "Catch __exception As System.ApplicationException{0}" +
145 "Catch As System.ApplicationException{0}" +
155 "End Try{0}", NewLine), Generate (), "#2");
157 statement = new CodeTryCatchFinallyStatement ();
159 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
161 "End Try{0}", NewLine), Generate (), "#3");
163 options.ElseOnClosing = false;
165 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
167 "End Try{0}", NewLine), Generate (), "#4");
171 public void VariableDeclarationStatementTest ()
173 statement = new CodeVariableDeclarationStatement ();
174 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
176 "Dim __exception As System.Void{0}",
178 "Dim As System.Void{0}",
180 NewLine), Generate (), "#1");
182 statement = new CodeVariableDeclarationStatement ((string) null,
184 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
186 "Dim __exception As System.Void{0}",
188 "Dim As System.Void{0}",
190 NewLine), Generate (), "#1");
193 statement = new CodeVariableDeclarationStatement ("A", (string) null);
194 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
196 "Dim __exception As A{0}",
200 NewLine), Generate (), "#1");
202 statement = new CodeVariableDeclarationStatement ((string) null, "B");
203 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
204 "Dim B As System.Void{0}", NewLine), Generate (), "#4");
206 statement = new CodeVariableDeclarationStatement ("A", "B");
207 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
208 "Dim B As A{0}", NewLine), Generate (), "#5");
210 CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ("A", "B");
211 cvds.InitExpression = new CodeSnippetExpression ("C");
213 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
214 "Dim B As A = C{0}", NewLine), Generate (), "#6");