* CodeGeneratorFromExpressionTest.cs: Added test for
[mono.git] / mcs / class / System / Test / Microsoft.VisualBasic / CodeGeneratorFromStatementTest.cs
1 //
2 // Microsoft.VisualBasic.* Test Cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2005 Gert Driesen
8 //
9 using System;
10 using System.CodeDom;
11 using System.CodeDom.Compiler;
12 using System.Globalization;
13 using System.IO;
14 using System.Text;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.Microsoft.VisualBasic
19 {
20         /// <summary>
21         /// Test ICodeGenerator's GenerateCodeFromStatement, along with a 
22         /// minimal set CodeDom components.
23         /// </summary>
24         [TestFixture]
25         public class CodeGeneratorFromStatementTest: CodeGeneratorTestBase
26         {
27                 private CodeStatement statement = null;
28
29                 [SetUp]
30                 public void Init ()
31                 {
32                         InitBase ();
33                         statement = new CodeStatement ();
34                 }
35                 
36                 protected override string Generate (CodeGeneratorOptions options)
37                 {
38                         StringWriter writer = new StringWriter ();
39                         writer.NewLine = NewLine;
40
41                         generator.GenerateCodeFromStatement (statement, writer, options);
42                         writer.Close ();
43                         return writer.ToString ();
44                 }
45                 
46                 [Test]
47                 [ExpectedException (typeof (ArgumentException))]
48                 public void DefaultStatementTest ()
49                 {
50                         Generate ();
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (NullReferenceException))]
55                 public void NullStatementTest ()
56                 {
57                         statement = null;
58                         Generate ();
59                 }
60
61                 [Test]
62                 public void CodeCommentStatementTest ()
63                 {
64                         CodeCommentStatement commentStatement = new CodeCommentStatement ();
65                         CodeComment comment = new CodeComment ();
66                         commentStatement.Comment = comment;
67                         statement = commentStatement;
68
69                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
70                                 "'{0}", NewLine), Generate (), "#1");
71
72                         comment.Text = "a\nb";
73                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
74                                 "'a\n'b{0}", NewLine), Generate (), "#2");
75
76                         comment.Text = "a\r\nb";
77                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
78                                 "'a\r\n'b{0}", NewLine), Generate (), "#3");
79
80                         comment.Text = "a\rb";
81                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
82                                 "'a\r'b{0}", NewLine), Generate (), "#4");
83                 }
84
85                 [Test]
86                 public void ThrowExceptionStatementTest ()
87                 {
88                         CodeThrowExceptionStatement ctet = new CodeThrowExceptionStatement ();
89                         statement = ctet;
90                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
91                                 "Throw{0}", NewLine), Generate (), "#1");
92
93                         ctet.ToThrow = new CodeSnippetExpression ("whatever");
94                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
95                                 "Throw whatever{0}", NewLine), Generate (), "#2");
96                 }
97
98                 [Test]
99                 public void GotoStatementTest ()
100                 {
101                         statement = new CodeGotoStatement ("something");
102                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
103                                 "goto something{0}", NewLine), Generate ());
104                 }
105
106                 [Test]
107                 public void TryCatchFinallyStatementTest ()
108                 {
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");
114
115                         statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
116                                 new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
117
118                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
119                                 "Try {0}" +
120                                 "    goto exit{0}" +
121                                 "Catch ex1 As System.ArgumentException{0}" + 
122 #if NET_2_0
123                                 "Catch __exception As System.ApplicationException{0}" +
124 #else
125                                 "Catch  As System.ApplicationException{0}" +
126 #endif
127                                 "Finally{0}" +
128 #if NET_2_0
129                                 "A{0}" +
130                                 "B{0}" +
131 #else
132                                 "    A{0}" +
133                                 "    B{0}" +
134 #endif
135                                 "End Try{0}", NewLine), Generate (), "#1");
136
137                         options.ElseOnClosing = true;
138                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
139                                 "Try {0}" +
140                                 "    goto exit{0}" +
141                                 "Catch ex1 As System.ArgumentException{0}" +
142 #if NET_2_0
143                                 "Catch __exception As System.ApplicationException{0}" +
144 #else
145                                 "Catch  As System.ApplicationException{0}" +
146 #endif
147                                 "Finally{0}" +
148 #if NET_2_0
149                                 "A{0}" +
150                                 "B{0}" +
151 #else
152                                 "    A{0}" +
153                                 "    B{0}" +
154 #endif
155                                 "End Try{0}", NewLine), Generate (), "#2");
156
157                         statement = new CodeTryCatchFinallyStatement ();
158
159                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
160                                 "Try {0}" +
161                                 "End Try{0}", NewLine), Generate (), "#3");
162
163                         options.ElseOnClosing = false;
164
165                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
166                                 "Try {0}" +
167                                 "End Try{0}", NewLine), Generate (), "#4");
168                 }
169
170                 [Test]
171                 public void VariableDeclarationStatementTest ()
172                 {
173                         statement = new CodeVariableDeclarationStatement ();
174                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
175 #if NET_2_0
176                                 "Dim __exception As System.Void{0}",
177 #else
178                                 "Dim  As System.Void{0}",
179 #endif
180                                 NewLine), Generate (), "#1");
181
182                         statement = new CodeVariableDeclarationStatement ((string) null,
183                                 (string) null);
184                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
185 #if NET_2_0
186                                 "Dim __exception As System.Void{0}",
187 #else
188                                 "Dim  As System.Void{0}",
189 #endif
190                                 NewLine), Generate (), "#1");
191
192
193                         statement = new CodeVariableDeclarationStatement ("A", (string) null);
194                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
195 #if NET_2_0
196                                 "Dim __exception As A{0}",
197 #else
198                                 "Dim  As A{0}",
199 #endif
200                                 NewLine), Generate (), "#1");
201
202                         statement = new CodeVariableDeclarationStatement ((string) null, "B");
203                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
204                                 "Dim B As System.Void{0}", NewLine), Generate (), "#4");
205
206                         statement = new CodeVariableDeclarationStatement ("A", "B");
207                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
208                                 "Dim B As A{0}", NewLine), Generate (), "#5");
209
210                         CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ("A", "B");
211                         cvds.InitExpression = new CodeSnippetExpression ("C");
212                         statement = cvds;
213                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
214                                 "Dim B As A = C{0}", NewLine), Generate (), "#6");
215                 }
216         }
217 }