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