Merge pull request #2346 from xmcclure/proxy-load-fail
[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 CodeAssignStatementTest ()
63                 {
64                         CodeSnippetExpression cse1 = new CodeSnippetExpression ("A");
65                         CodeSnippetExpression cse2 = new CodeSnippetExpression ("B");
66
67                         CodeAssignStatement assignStatement = new CodeAssignStatement (cse1, cse2);
68                         statement = assignStatement;
69
70                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
71                                 "A = B{0}", NewLine), Generate (), "#1");
72
73                         assignStatement.Left = null;
74                         try {
75                                 Generate ();
76                                 Assert.Fail ("#2");
77                         } catch (ArgumentNullException) {
78                         }
79
80                         assignStatement.Left = cse1;
81                         Generate ();
82
83                         assignStatement.Right = null;
84                         try {
85                                 Generate ();
86                                 Assert.Fail ("#3");
87                         } catch (ArgumentNullException) {
88                         }
89
90                         assignStatement.Right = cse2;
91                         Generate ();
92                 }
93
94                 [Test]
95                 public void CodeAttachEventStatementTest ()
96                 {
97                         CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
98                                 new CodeSnippetExpression ("A"), "class");
99                         CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
100
101                         CodeAttachEventStatement attachEventStatement = new CodeAttachEventStatement ();
102                         statement = attachEventStatement;
103
104                         try {
105                                 Generate ();
106                                 Assert.Fail ("#1");
107                         } catch (ArgumentNullException) {
108                         }
109
110                         attachEventStatement.Event = cere;
111                         try {
112                                 Generate ();
113                                 Assert.Fail ("#2");
114                         } catch (ArgumentNullException) {
115                         }
116
117                         attachEventStatement.Event = null;
118                         attachEventStatement.Listener = handler;
119                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
120                                 "AddHandler , EventHandler{0}", NewLine), Generate (), "#3");
121
122                         attachEventStatement.Event = cere;
123                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
124                                 "AddHandler A.[class], EventHandler{0}", NewLine), Generate (), "#4");
125
126                         attachEventStatement.Event = new CodeEventReferenceExpression (
127                                 new CodeSnippetExpression ((string) null), "");
128                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
129                                 "AddHandler ., EventHandler{0}", NewLine), Generate (), "#5");
130
131                         attachEventStatement.Listener = new CodeSnippetExpression ("");
132                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
133                                 "AddHandler ., {0}", NewLine), Generate (), "#6");
134                 }
135                 
136                 [Test]
137                 public void CodeAttachEventStatementKeywordTest ()
138                 {
139                         CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
140                                 new CodeSnippetExpression ("Set"), "Event");
141                         CodeSnippetExpression handler = new CodeSnippetExpression ("Handles");
142
143                         CodeAttachEventStatement attachEventStatement = new CodeAttachEventStatement ();
144                         statement = attachEventStatement;
145
146                         try {
147                                 Generate ();
148                                 Assert.Fail ("#1");
149                         } catch (ArgumentNullException) {
150                         }
151
152                         attachEventStatement.Event = cere;
153                         try {
154                                 Generate ();
155                                 Assert.Fail ("#2");
156                         } catch (ArgumentNullException) {
157                         }
158
159                         attachEventStatement.Event = null;
160                         attachEventStatement.Listener = handler;
161                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
162                                 "AddHandler , Handles{0}", NewLine), Generate (), "#3");
163
164                         attachEventStatement.Event = cere;
165                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
166                                 "AddHandler Set.[Event], Handles{0}", NewLine), Generate (), "#4");
167
168                         attachEventStatement.Event = new CodeEventReferenceExpression (
169                                 new CodeSnippetExpression ((string) null), "");
170                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
171                                 "AddHandler ., Handles{0}", NewLine), Generate (), "#5");
172
173                         attachEventStatement.Listener = new CodeSnippetExpression ("");
174                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
175                                 "AddHandler ., {0}", NewLine), Generate (), "#6");
176                 }
177                 [Test]
178                 public void CodeCommentStatementTest ()
179                 {
180                         CodeCommentStatement commentStatement = new CodeCommentStatement ();
181                         CodeComment comment = new CodeComment ();
182                         commentStatement.Comment = comment;
183                         statement = commentStatement;
184
185                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
186                                 "'{0}", NewLine), Generate (), "#1");
187
188                         comment.Text = "a\nb";
189                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
190                                 "'a\n'b{0}", NewLine), Generate (), "#2");
191
192                         comment.Text = "a\r\nb";
193                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
194                                 "'a\r\n'b{0}", NewLine), Generate (), "#3");
195
196                         comment.Text = "a\rb";
197                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
198                                 "'a\r'b{0}", NewLine), Generate (), "#4");
199                 }
200
201                 [Test]
202                 public void CodeConditionStatementTest ()
203                 {
204                         CodeStatement[] trueStatements = new CodeStatement[] {
205                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoA()")),
206                                 new CodeExpressionStatement (new CodeSnippetExpression (";")),
207                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoB()")),
208                                 new CodeExpressionStatement (new CodeSnippetExpression ("")),
209                                 new CodeSnippetStatement ("A"),
210                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoC()")) };
211
212                         CodeStatement[] falseStatements = new CodeStatement[] {
213                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoD()")),
214                                 new CodeSnippetStatement ("B"),
215                                 new CodeExpressionStatement (new CodeSnippetExpression (";")),
216                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoE()")),
217                                 new CodeExpressionStatement (new CodeSnippetExpression ("")),
218                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoF()")) };
219
220                         CodeConditionStatement conditionStatement = new CodeConditionStatement ();
221                         statement = conditionStatement;
222
223                         try {
224                                 Generate ();
225                                 Assert.Fail ("#1");
226                         } catch (ArgumentNullException) {
227                         }
228
229                         conditionStatement.Condition = new CodeSnippetExpression ("");
230                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
231                                 "If  Then{0}" +
232                                 "End If{0}", NewLine), Generate (), "#2");
233
234                         conditionStatement.Condition = new CodeSnippetExpression ("true == false");
235                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
236                                 "If true == false Then{0}" +
237                                 "End If{0}", NewLine), Generate (), "#3");
238
239                         conditionStatement.TrueStatements.AddRange (trueStatements);
240                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
241                                 "If true == false Then{0}" +
242                                 "    DoA(){0}" +
243                                 "    ;{0}" +
244                                 "    DoB(){0}" +
245                                 "    {0}" +
246                                 "A{0}" +
247                                 "    DoC(){0}" +
248                                 "End If{0}", NewLine), Generate (), "#3");
249
250                         conditionStatement.FalseStatements.AddRange (falseStatements);
251                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
252                                 "If true == false Then{0}" +
253                                 "    DoA(){0}" +
254                                 "    ;{0}" +
255                                 "    DoB(){0}" +
256                                 "    {0}" +
257                                 "A{0}" +
258                                 "    DoC(){0}" +
259                                 "Else{0}" +
260                                 "    DoD(){0}" +
261                                 "B{0}" +
262                                 "    ;{0}" +
263                                 "    DoE(){0}" +
264                                 "    {0}" +
265                                 "    DoF(){0}" +
266                                 "End If{0}", NewLine), Generate (), "#4");
267
268                         options.ElseOnClosing = true;
269
270                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
271                                 "If true == false Then{0}" +
272                                 "    DoA(){0}" +
273                                 "    ;{0}" +
274                                 "    DoB(){0}" +
275                                 "    {0}" +
276                                 "A{0}" +
277                                 "    DoC(){0}" +
278                                 "Else{0}" +
279                                 "    DoD(){0}" +
280                                 "B{0}" +
281                                 "    ;{0}" +
282                                 "    DoE(){0}" +
283                                 "    {0}" +
284                                 "    DoF(){0}" +
285                                 "End If{0}", NewLine), Generate (), "#5");
286
287                         options.ElseOnClosing = false;
288
289                         conditionStatement.TrueStatements.Clear ();
290
291                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
292                                 "If true == false Then{0}" +
293                                 "Else{0}" +
294                                 "    DoD(){0}" +
295                                 "B{0}" +
296                                 "    ;{0}" +
297                                 "    DoE(){0}" +
298                                 "    {0}" +
299                                 "    DoF(){0}" +
300                                 "End If{0}", NewLine), Generate (), "#6");
301
302                         conditionStatement.TrueStatements.AddRange (trueStatements);
303                         conditionStatement.FalseStatements.Clear ();
304                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
305                                 "If true == false Then{0}" +
306                                 "    DoA(){0}" +
307                                 "    ;{0}" +
308                                 "    DoB(){0}" +
309                                 "    {0}" +
310                                 "A{0}" +
311                                 "    DoC(){0}" +
312                                 "End If{0}", NewLine), Generate (), "#7");
313                 }
314
315                 [Test]
316                 public void CodeExpressionStatementTest ()
317                 {
318                         CodeExpressionStatement ces = new CodeExpressionStatement ();
319                         statement = ces;
320
321                         try {
322                                 Generate ();
323                                 Assert.Fail ("#1");
324                         } catch (ArgumentNullException) {
325                         }
326
327                         ces.Expression = new CodeSnippetExpression ("something");
328                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
329                                 "something{0}", NewLine), Generate (), "#2");
330                 }
331
332                 [Test]
333                 public void CodeGotoStatementTest ()
334                 {
335                         statement = new CodeGotoStatement ("something");
336                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
337                                 "goto something{0}", NewLine), Generate ());
338                 }
339
340                 [Test]
341                 public void CodeIterationStatementTest ()
342                 {
343                         CodeIterationStatement cis = new CodeIterationStatement ();
344                         statement = cis;
345
346                         try {
347                                 Generate ();
348                                 Assert.Fail ("#1: null InitStatement should cause NRE");
349                         } catch (NullReferenceException) {
350                         }
351
352                         cis.InitStatement = new CodeVariableDeclarationStatement (typeof (int),
353                                 "testInt", new CodePrimitiveExpression (1));
354                         try {
355                                 Generate ();
356                                 Assert.Fail ("#2: null TestExpression should cause ArgumentNullException");
357                         } catch (ArgumentNullException) {
358                         }
359
360                         cis.TestExpression = new CodeBinaryOperatorExpression (
361                                 new CodeVariableReferenceExpression ("testInt"),
362                                 CodeBinaryOperatorType.LessThan,
363                                 new CodePrimitiveExpression (10));
364                         try {
365                                 Generate ();
366                                 Assert.Fail ("#3: null IncrementStatement should cause NRE");
367                         } catch (NullReferenceException) {
368                         }
369
370                         cis.IncrementStatement = new CodeAssignStatement (
371                                 new CodeVariableReferenceExpression ("testInt"),
372                                 new CodeBinaryOperatorExpression (
373                                         new CodeVariableReferenceExpression ("testInt"),
374                                         CodeBinaryOperatorType.Add,
375                                         new CodePrimitiveExpression (1)));
376                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
377                                 "Dim testInt As Integer = 1{0}" +
378                                 "Do While (testInt < 10){0}" +
379                                 "    testInt = (testInt + 1){0}" +
380                                 "Loop{0}", NewLine), Generate (), "#4");
381
382                         cis.Statements.AddRange (new CodeStatement[] {
383                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoA()")),
384                                 new CodeExpressionStatement (new CodeSnippetExpression (";")),
385                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoB()")),
386                                 new CodeLabeledStatement ("test", new CodeSnippetStatement ("C")),
387                                 new CodeExpressionStatement (new CodeSnippetExpression ("")),
388                                 new CodeSnippetStatement ("A"),
389                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoC()")) });
390                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
391                                 "Dim testInt As Integer = 1{0}" +
392                                 "Do While (testInt < 10){0}" +
393                                 "    DoA(){0}" +
394                                 "    ;{0}" +
395                                 "    DoB(){0}" +
396                                 "test:{0}" +
397                                 "C{0}" +
398                                 "    {0}" +
399                                 "A{0}" +
400                                 "    DoC(){0}" +
401                                 "    testInt = (testInt + 1){0}" +
402                                 "Loop{0}", NewLine), Generate (), "#5");
403                 }
404
405                 [Test]
406                 public void CodeLabeledStatementTest ()
407                 {
408                         CodeLabeledStatement cls = new CodeLabeledStatement ();
409                         statement = cls;
410
411                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
412                                 ":{0}", NewLine), Generate (), "#1");
413
414                         cls.Label = "class";
415                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
416                                 "class:{0}", NewLine), Generate (), "#2");
417
418                         cls.Statement = new CodeSnippetStatement ("A");
419                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
420                                 "class:{0}" +
421                                 "A{0}",
422                                 NewLine), Generate (), "#3");
423                 }
424
425                 [Test]
426                 public void CodeMethodReturnStatementTest ()
427                 {
428                         CodeMethodReturnStatement cmrs = new CodeMethodReturnStatement ();
429                         statement = cmrs;
430
431                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
432                                 "Return{0}", NewLine), Generate (), "#1");
433
434                         cmrs.Expression = new CodePrimitiveExpression (1);
435                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
436                                 "Return 1{0}", NewLine), Generate (), "#2");
437                 }
438
439                 [Test]
440                 public void CodeRemoveEventStatementTest ()
441                 {
442                         CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
443                                 new CodeSnippetExpression ("A"), "class");
444                         CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
445
446                         CodeRemoveEventStatement cres = new CodeRemoveEventStatement ();
447                         statement = cres;
448
449                         try {
450                                 Generate ();
451                                 Assert.Fail ("#1");
452                         } catch (ArgumentNullException) {
453                         }
454
455                         cres.Event = cere;
456                         try {
457                                 Generate ();
458                                 Assert.Fail ("#2");
459                         } catch (ArgumentNullException) {
460                         }
461
462                         cres.Event = null;
463                         cres.Listener = handler;
464                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
465                                 "RemoveHandler , EventHandler{0}", NewLine), Generate (), "#3");
466
467                         cres.Event = cere;
468                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
469                                 "RemoveHandler A.[class], EventHandler{0}", NewLine), Generate (), "#4");
470
471                         cres.Event = new CodeEventReferenceExpression (
472                                 new CodeSnippetExpression ((string) null), "");
473                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
474                                 "RemoveHandler ., EventHandler{0}", NewLine), Generate (), "#5");
475
476                         cres.Listener = new CodeSnippetExpression ("");
477                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
478                                 "RemoveHandler ., {0}", NewLine), Generate (), "#6");
479                 }
480
481                 [Test]
482                 public void CodeSnippetStatementTest ()
483                 {
484                         CodeSnippetStatement css = new CodeSnippetStatement ();
485                         statement = css;
486
487                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
488                                 "{0}", NewLine), Generate (), "#1");
489
490                         css.Value = "class";
491                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
492                                 "class{0}", NewLine), Generate (), "#2");
493
494                         css.Value = null;
495                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
496                                 "{0}", NewLine), Generate (), "#3");
497                 }
498
499                 [Test]
500                 [ExpectedException (typeof (ArgumentException))]
501                 public void CodeStatement ()
502                 {
503                         CodeStatement cs = new CodeStatement ();
504                         statement = cs;
505
506                         Generate ();
507                 }
508
509                 [Test]
510                 public void CodeThrowExceptionStatementTest ()
511                 {
512                         CodeThrowExceptionStatement ctet = new CodeThrowExceptionStatement ();
513                         statement = ctet;
514                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
515                                 "Throw{0}", NewLine), Generate (), "#1");
516
517                         ctet.ToThrow = new CodeSnippetExpression ("whatever");
518                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
519                                 "Throw whatever{0}", NewLine), Generate (), "#2");
520                 }
521
522                 [Test]
523                 public void CodeTryCatchFinallyStatementTest ()
524                 {
525                         CodeStatement cs = new CodeGotoStatement ("exit");
526                         CodeCatchClause ccc1 = new CodeCatchClause ("ex1", new CodeTypeReference ("System.ArgumentException"));
527                         CodeCatchClause ccc2 = new CodeCatchClause (null, new CodeTypeReference ("System.ApplicationException"));
528                         CodeSnippetStatement fin1 = new CodeSnippetStatement ("A");
529                         CodeSnippetStatement fin2 = new CodeSnippetStatement ("B");
530
531                         statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
532                                 new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
533
534                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
535                                 "Try {0}" +
536                                 "    goto exit{0}" +
537                                 "Catch ex1 As System.ArgumentException{0}" + 
538                                 "Catch __exception As System.ApplicationException{0}" +
539                                 "Finally{0}" +
540                                 "A{0}" +
541                                 "B{0}" +
542                                 "End Try{0}", NewLine), Generate (), "#1");
543
544                         options.ElseOnClosing = true;
545                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
546                                 "Try {0}" +
547                                 "    goto exit{0}" +
548                                 "Catch ex1 As System.ArgumentException{0}" +
549                                 "Catch __exception As System.ApplicationException{0}" +
550                                 "Finally{0}" +
551                                 "A{0}" +
552                                 "B{0}" +
553                                 "End Try{0}", NewLine), Generate (), "#2");
554
555                         statement = new CodeTryCatchFinallyStatement ();
556
557                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
558                                 "Try {0}" +
559                                 "End Try{0}", NewLine), Generate (), "#3");
560
561                         options.ElseOnClosing = false;
562
563                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
564                                 "Try {0}" +
565                                 "End Try{0}", NewLine), Generate (), "#4");
566                 }
567
568                 [Test]
569                 public void CodeVariableDeclarationStatementTest ()
570                 {
571                         CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ();
572                         statement = cvds;
573
574                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
575                                 "Dim __exception As System.Void{0}", 
576                                 NewLine), Generate (), "#1");
577
578                         cvds.Name = "class";
579                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
580                                 "Dim [class] As System.Void{0}", NewLine), Generate (), "#2");
581
582                         cvds.Name = "A";
583                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
584                                 "Dim A As System.Void{0}", NewLine), Generate (), "#3");
585
586                         cvds.Type = new CodeTypeReference (typeof (int));
587                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
588                                 "Dim A As Integer{0}", NewLine), Generate (), "#4");
589
590                         cvds.InitExpression = new CodePrimitiveExpression (25);
591                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
592                                 "Dim A As Integer = 25{0}", NewLine), Generate (), "#5");
593
594                         cvds.Name = null;
595                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
596                                 "Dim __exception As Integer = 25{0}", 
597                                 NewLine), Generate (), "#6");
598                 }
599         }
600 }