New test.
[mono.git] / mcs / class / System / Test / Microsoft.CSharp / CodeGeneratorFromStatementTest.cs
1 //
2 // Microsoft.CSharp.* Test Cases
3 //
4 // Authors:
5 //      Erik LeBel (eriklebel@yahoo.ca)
6 //
7 // (c) 2003 Erik LeBel
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.CSharp
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                                 " += EventHandler;{0}", NewLine), Generate (), "#3");
121
122                         attachEventStatement.Event = cere;
123                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
124                                 "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                                 ". += EventHandler;{0}", NewLine), Generate (), "#5");
130
131                         attachEventStatement.Listener = new CodeSnippetExpression ("");
132                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
133                                 ". += ;{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                                 "// \n", NewLine), Generate (), "#1");
146
147                         comment.Text = "a\nb";
148                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
149                                 "// a\n//b\n", 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 () {{{0}" +
191                                 "}}{0}", NewLine), Generate (), "#2");
192
193                         conditionStatement.Condition = new CodeSnippetExpression ("true == false");
194                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
195                                 "if (true == false) {{{0}" +
196                                 "}}{0}", NewLine), Generate (), "#3");
197
198                         conditionStatement.TrueStatements.AddRange (trueStatements);
199                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
200                                 "if (true == false) {{{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                                 "}}{0}", NewLine), Generate (), "#3");
212
213                         conditionStatement.FalseStatements.AddRange (falseStatements);
214                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
215                                 "if (true == false) {{{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                                 "}}{0}" +
227                                 "else {{{0}" +
228                                 "    DoD();{0}" +
229 #if NET_2_0
230                                 "B{0}" +
231 #else
232                                 "    B{0}" +
233 #endif
234                                 "    ;;{0}" +
235                                 "    DoE();{0}" +
236                                 "    ;{0}" +
237                                 "    DoF();{0}" +
238                                 "}}{0}", NewLine), Generate (), "#4");
239
240                         options.ElseOnClosing = true;
241
242                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
243                                 "if (true == false) {{{0}" +
244                                 "    DoA();{0}" +
245                                 "    ;;{0}" +
246                                 "    DoB();{0}" +
247                                 "    ;{0}" +
248 #if NET_2_0
249                                 "A{0}" +
250 #else
251                                 "    A{0}" +
252 #endif
253                                 "    DoC();{0}" +
254                                 "}} else {{{0}" +
255                                 "    DoD();{0}" +
256 #if NET_2_0
257                                 "B{0}" +
258 #else
259                                 "    B{0}" +
260 #endif
261                                 "    ;;{0}" +
262                                 "    DoE();{0}" +
263                                 "    ;{0}" +
264                                 "    DoF();{0}" +
265                                 "}}{0}", NewLine), Generate (), "#5");
266
267                         options.ElseOnClosing = false;
268
269                         conditionStatement.TrueStatements.Clear ();
270
271                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
272                                 "if (true == false) {{{0}" +
273                                 "}}{0}" +
274                                 "else {{{0}" +
275                                 "    DoD();{0}" +
276 #if NET_2_0
277                                 "B{0}" +
278 #else
279                                 "    B{0}" +
280 #endif
281                                 "    ;;{0}" +
282                                 "    DoE();{0}" +
283                                 "    ;{0}" +
284                                 "    DoF();{0}" +
285                                 "}}{0}", NewLine), Generate (), "#6");
286
287                         conditionStatement.TrueStatements.AddRange (trueStatements);
288                         conditionStatement.FalseStatements.Clear ();
289                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
290                                 "if (true == false) {{{0}" +
291                                 "    DoA();{0}" +
292                                 "    ;;{0}" +
293                                 "    DoB();{0}" +
294                                 "    ;{0}" +
295 #if NET_2_0
296                                 "A{0}" +
297 #else
298                                 "    A{0}" +
299 #endif
300                                 "    DoC();{0}" +
301                                 "}}{0}", NewLine), Generate (), "#7");
302                 }
303
304                 [Test]
305                 public void CodeExpressionStatementTest ()
306                 {
307                         CodeExpressionStatement ces = new CodeExpressionStatement ();
308                         statement = ces;
309
310                         try {
311                                 Generate ();
312                                 Assert.Fail ("#1");
313                         } catch (ArgumentNullException) {
314                         }
315
316                         ces.Expression = new CodeSnippetExpression ("something");
317                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
318                                 "something;{0}", NewLine), Generate (), "#2");
319                 }
320
321                 [Test]
322                 public void CodeGotoStatementTest ()
323                 {
324                         statement = new CodeGotoStatement ("something");
325                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
326                                 "goto something;{0}", NewLine), Generate ());
327                 }
328
329                 [Test]
330                 public void CodeIterationStatementTest ()
331                 {
332                         CodeIterationStatement cis = new CodeIterationStatement ();
333                         statement = cis;
334
335                         try {
336                                 Generate ();
337                                 Assert.Fail ("#1: null InitStatement should cause NRE");
338                         } catch (NullReferenceException) {
339                         }
340
341                         cis.InitStatement = new CodeVariableDeclarationStatement (typeof(int),
342                                 "testInt", new CodePrimitiveExpression(1));
343                         try {
344                                 Generate ();
345                                 Assert.Fail ("#2: null TestExpression should cause ArgumentNullException");
346                         } catch (ArgumentNullException) {
347                         }
348
349                         cis.TestExpression = new CodeBinaryOperatorExpression (
350                                 new CodeVariableReferenceExpression ("testInt"),
351                                 CodeBinaryOperatorType.LessThan, 
352                                 new CodePrimitiveExpression (10));
353                         try {
354                                 Generate ();
355                                 Assert.Fail ("#3: null IncrementStatement should cause NRE");
356                         } catch (NullReferenceException) {
357                         }
358
359                         cis.IncrementStatement = new CodeAssignStatement (
360                                 new CodeVariableReferenceExpression ("testInt"),
361                                 new CodeBinaryOperatorExpression (
362                                         new CodeVariableReferenceExpression ("testInt"),
363                                         CodeBinaryOperatorType.Add,
364                                         new CodePrimitiveExpression (1)));
365                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
366                                 "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
367                                 "}}{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                                 "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
379                                 "    DoA();{0}" +
380                                 "    ;;{0}" +
381                                 "    DoB();{0}" +
382                                 "test:{0}" +
383 #if NET_2_0
384                                 "C{0}" +
385 #else
386                                 "    C{0}" +
387 #endif
388                                 "    ;{0}" +
389 #if NET_2_0
390                                 "A{0}" +
391 #else
392                                 "    A{0}" +
393 #endif
394                                 "    DoC();{0}" +
395                                 "}}{0}", NewLine), Generate (), "#5");
396                 }
397
398                 [Test]
399                 public void CodeLabeledStatementTest ()
400                 {
401                         CodeLabeledStatement cls = new CodeLabeledStatement ();
402                         statement = cls;
403
404                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
405                                 ":{0}", NewLine), Generate (), "#1");
406
407                         cls.Label = "class";
408                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
409                                 "class:{0}", NewLine), Generate (), "#2");
410
411                         cls.Statement = new CodeSnippetStatement ("A");
412                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
413                                 "class:{0}" +
414 #if NET_2_0
415                                 "A{0}",
416 #else
417                                 "    A{0}",
418 #endif
419                                 NewLine), Generate (), "#3");
420                 }
421
422                 [Test]
423                 public void CodeMethodReturnStatementTest ()
424                 {
425                         CodeMethodReturnStatement cmrs = new CodeMethodReturnStatement ();
426                         statement = cmrs;
427
428                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
429                                 "return;{0}", NewLine), Generate (), "#1");
430
431                         cmrs.Expression = new CodePrimitiveExpression (1);
432                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
433                                 "return 1;{0}", NewLine), Generate (), "#2");
434                 }
435
436                 [Test]
437                 public void CodeRemoveEventStatementTest ()
438                 {
439                         CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
440                                 new CodeSnippetExpression ("A"), "class");
441                         CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
442
443                         CodeRemoveEventStatement cres = new CodeRemoveEventStatement ();
444                         statement = cres;
445
446                         try {
447                                 Generate ();
448                                 Assert.Fail ("#1");
449                         } catch (ArgumentNullException) {
450                         }
451
452                         cres.Event = cere;
453                         try {
454                                 Generate ();
455                                 Assert.Fail ("#2");
456                         } catch (ArgumentNullException) {
457                         }
458
459                         cres.Event = null;
460                         cres.Listener = handler;
461                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
462                                 " -= EventHandler;{0}", NewLine), Generate (), "#3");
463
464                         cres.Event = cere;
465                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
466                                 "A.@class -= EventHandler;{0}", NewLine), Generate (), "#4");
467
468                         cres.Event = new CodeEventReferenceExpression (
469                                 new CodeSnippetExpression ((string) null), "");
470                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
471                                 ". -= EventHandler;{0}", NewLine), Generate (), "#5");
472
473                         cres.Listener = new CodeSnippetExpression ("");
474                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
475                                 ". -= ;{0}", NewLine), Generate (), "#6");
476                 }
477
478                 [Test]
479                 public void CodeSnippetStatementTest ()
480                 {
481                         CodeSnippetStatement css = new CodeSnippetStatement ();
482                         statement = css;
483
484                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
485                                 "{0}", NewLine), Generate (), "#1");
486
487                         css.Value = "class";
488                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
489                                 "class{0}", NewLine), Generate (), "#2");
490
491                         css.Value = null;
492                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
493                                 "{0}", NewLine), Generate (), "#3");
494                 }
495
496                 [Test]
497                 [ExpectedException (typeof (ArgumentException))]
498                 public void CodeStatement ()
499                 {
500                         CodeStatement cs = new CodeStatement ();
501                         statement = cs;
502
503                         Generate ();
504                 }
505
506                 [Test]
507                 public void CodeThrowExceptionStatementTest ()
508                 {
509                         CodeThrowExceptionStatement ctes = new CodeThrowExceptionStatement ();
510                         statement = ctes;
511
512                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
513                                 "throw;{0}", NewLine), Generate (), "#1");
514
515                         ctes.ToThrow = new CodeSnippetExpression ("whatever");
516                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
517                                 "throw whatever;{0}", NewLine), Generate (), "#2");
518                 }
519
520                 [Test]
521                 public void CodeTryCatchFinallyStatementTest ()
522                 {
523                         CodeStatement cs = new CodeGotoStatement ("exit");
524                         CodeCatchClause ccc1 = new CodeCatchClause ("ex1", new CodeTypeReference ("System.ArgumentException"));
525                         CodeCatchClause ccc2 = new CodeCatchClause (null, new CodeTypeReference ("System.ApplicationException"));
526                         CodeSnippetStatement fin1 = new CodeSnippetStatement ("A");
527                         CodeSnippetStatement fin2 = new CodeSnippetStatement ("B");
528
529                         statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
530                                 new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
531
532                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
533                                 "try {{{0}" +
534                                 "    goto exit;{0}" +
535                                 "}}{0}" +
536                                 "catch (System.ArgumentException ex1) {{{0}" + 
537                                 "}}{0}" +
538                                 "catch (System.ApplicationException ) {{{0}" +
539                                 "}}{0}" +
540                                 "finally {{{0}" +
541 #if NET_2_0
542                                 "A{0}" +
543                                 "B{0}" +
544 #else
545                                 "    A{0}" +
546                                 "    B{0}" +
547 #endif
548                                 "}}{0}", NewLine), Generate (), "#1");
549
550                         options.ElseOnClosing = true;
551
552                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
553                                 "try {{{0}" +
554                                 "    goto exit;{0}" +
555                                 "}} catch (System.ArgumentException ex1) {{{0}" +
556                                 "}} catch (System.ApplicationException ) {{{0}" +
557                                 "}} finally {{{0}" +
558 #if NET_2_0
559                                 "A{0}" +
560                                 "B{0}" +
561 #else
562                                 "    A{0}" +
563                                 "    B{0}" +
564 #endif
565                                 "}}{0}", NewLine), Generate (), "#2");
566
567                         statement = new CodeTryCatchFinallyStatement ();
568
569                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
570                                 "try {{{0}" +
571                                 "}}{0}", NewLine), Generate (), "#3");
572
573                         options.ElseOnClosing = false;
574
575                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
576                                 "try {{{0}" +
577                                 "}}{0}", NewLine), Generate (), "#4");
578                 }
579
580                 [Test]
581                 public void CodeVariableDeclarationStatementTest ()
582                 {
583                         CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ();
584                         statement = cvds;
585
586                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
587                                 "void ;{0}", NewLine), Generate (), "#1");
588
589                         cvds.Name = "class";
590                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
591                                 "void @class;{0}", NewLine), Generate (), "#2");
592
593                         cvds.Name = "A";
594                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
595                                 "void A;{0}", NewLine), Generate (), "#3");
596
597                         cvds.Type = new CodeTypeReference (typeof (int));
598                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
599                                 "int A;{0}", NewLine), Generate (), "#4");
600
601                         cvds.InitExpression = new CodePrimitiveExpression (25);
602                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
603                                 "int A = 25;{0}", NewLine), Generate (), "#5");
604
605                         cvds.Name = null;
606                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
607                                 "int  = 25;{0}", NewLine), Generate (), "#5");
608                 }
609         }
610 }
611