[bcl] Remove more NET_2_0 checks from class libs
[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                                 "A{0}" +
206                                 "    DoC();{0}" +
207                                 "}}{0}", NewLine), Generate (), "#3");
208
209                         conditionStatement.FalseStatements.AddRange (falseStatements);
210                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
211                                 "if (true == false) {{{0}" +
212                                 "    DoA();{0}" +
213                                 "    ;;{0}" +
214                                 "    DoB();{0}" +
215                                 "    ;{0}" +
216                                 "A{0}" +
217                                 "    DoC();{0}" +
218                                 "}}{0}" +
219                                 "else {{{0}" +
220                                 "    DoD();{0}" +
221                                 "B{0}" +
222                                 "    ;;{0}" +
223                                 "    DoE();{0}" +
224                                 "    ;{0}" +
225                                 "    DoF();{0}" +
226                                 "}}{0}", NewLine), Generate (), "#4");
227
228                         options.ElseOnClosing = true;
229
230                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
231                                 "if (true == false) {{{0}" +
232                                 "    DoA();{0}" +
233                                 "    ;;{0}" +
234                                 "    DoB();{0}" +
235                                 "    ;{0}" +
236                                 "A{0}" +
237                                 "    DoC();{0}" +
238                                 "}} else {{{0}" +
239                                 "    DoD();{0}" +
240                                 "B{0}" +
241                                 "    ;;{0}" +
242                                 "    DoE();{0}" +
243                                 "    ;{0}" +
244                                 "    DoF();{0}" +
245                                 "}}{0}", NewLine), Generate (), "#5");
246
247                         options.ElseOnClosing = false;
248
249                         conditionStatement.TrueStatements.Clear ();
250
251                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
252                                 "if (true == false) {{{0}" +
253                                 "}}{0}" +
254                                 "else {{{0}" +
255                                 "    DoD();{0}" +
256                                 "B{0}" +
257                                 "    ;;{0}" +
258                                 "    DoE();{0}" +
259                                 "    ;{0}" +
260                                 "    DoF();{0}" +
261                                 "}}{0}", NewLine), Generate (), "#6");
262
263                         conditionStatement.TrueStatements.AddRange (trueStatements);
264                         conditionStatement.FalseStatements.Clear ();
265                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
266                                 "if (true == false) {{{0}" +
267                                 "    DoA();{0}" +
268                                 "    ;;{0}" +
269                                 "    DoB();{0}" +
270                                 "    ;{0}" +
271                                 "A{0}" +
272                                 "    DoC();{0}" +
273                                 "}}{0}", NewLine), Generate (), "#7");
274                 }
275
276                 [Test]
277                 public void CodeExpressionStatementTest ()
278                 {
279                         CodeExpressionStatement ces = new CodeExpressionStatement ();
280                         statement = ces;
281
282                         try {
283                                 Generate ();
284                                 Assert.Fail ("#1");
285                         } catch (ArgumentNullException) {
286                         }
287
288                         ces.Expression = new CodeSnippetExpression ("something");
289                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
290                                 "something;{0}", NewLine), Generate (), "#2");
291                 }
292
293                 [Test]
294                 public void CodeGotoStatementTest ()
295                 {
296                         statement = new CodeGotoStatement ("something");
297                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
298                                 "goto something;{0}", NewLine), Generate ());
299                 }
300
301                 [Test]
302                 public void CodeIterationStatementTest ()
303                 {
304                         CodeIterationStatement cis = new CodeIterationStatement ();
305                         statement = cis;
306
307                         try {
308                                 Generate ();
309                                 Assert.Fail ("#1: null InitStatement should cause NRE");
310                         } catch (NullReferenceException) {
311                         }
312
313                         cis.InitStatement = new CodeVariableDeclarationStatement (typeof(int),
314                                 "testInt", new CodePrimitiveExpression(1));
315                         try {
316                                 Generate ();
317                                 Assert.Fail ("#2: null TestExpression should cause ArgumentNullException");
318                         } catch (ArgumentNullException) {
319                         }
320
321                         cis.TestExpression = new CodeBinaryOperatorExpression (
322                                 new CodeVariableReferenceExpression ("testInt"),
323                                 CodeBinaryOperatorType.LessThan, 
324                                 new CodePrimitiveExpression (10));
325                         try {
326                                 Generate ();
327                                 Assert.Fail ("#3: null IncrementStatement should cause NRE");
328                         } catch (NullReferenceException) {
329                         }
330
331                         cis.IncrementStatement = new CodeAssignStatement (
332                                 new CodeVariableReferenceExpression ("testInt"),
333                                 new CodeBinaryOperatorExpression (
334                                         new CodeVariableReferenceExpression ("testInt"),
335                                         CodeBinaryOperatorType.Add,
336                                         new CodePrimitiveExpression (1)));
337                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
338                                 "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
339                                 "}}{0}", NewLine), Generate (), "#4");
340
341                         cis.Statements.AddRange (new CodeStatement[] {
342                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoA()")),
343                                 new CodeExpressionStatement (new CodeSnippetExpression (";")),
344                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoB()")),
345                                 new CodeLabeledStatement ("test", new CodeSnippetStatement ("C")),
346                                 new CodeExpressionStatement (new CodeSnippetExpression ("")),
347                                 new CodeSnippetStatement ("A"),
348                                 new CodeExpressionStatement (new CodeSnippetExpression ("DoC()")) });
349                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
350                                 "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
351                                 "    DoA();{0}" +
352                                 "    ;;{0}" +
353                                 "    DoB();{0}" +
354                                 "test:{0}" +
355                                 "C{0}" +
356                                 "    ;{0}" +
357                                 "A{0}" +
358                                 "    DoC();{0}" +
359                                 "}}{0}", NewLine), Generate (), "#5");
360                 }
361
362                 [Test]
363                 public void CodeLabeledStatementTest ()
364                 {
365                         CodeLabeledStatement cls = new CodeLabeledStatement ();
366                         statement = cls;
367
368                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
369                                 ":{0}", NewLine), Generate (), "#1");
370
371                         cls.Label = "class";
372                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
373                                 "class:{0}", NewLine), Generate (), "#2");
374
375                         cls.Statement = new CodeSnippetStatement ("A");
376                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
377                                 "class:{0}" +
378                                 "A{0}",
379                                 NewLine), Generate (), "#3");
380                 }
381
382                 [Test]
383                 public void CodeMethodReturnStatementTest ()
384                 {
385                         CodeMethodReturnStatement cmrs = new CodeMethodReturnStatement ();
386                         statement = cmrs;
387
388                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
389                                 "return;{0}", NewLine), Generate (), "#1");
390
391                         cmrs.Expression = new CodePrimitiveExpression (1);
392                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
393                                 "return 1;{0}", NewLine), Generate (), "#2");
394                 }
395
396                 [Test]
397                 public void CodeRemoveEventStatementTest ()
398                 {
399                         CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
400                                 new CodeSnippetExpression ("A"), "class");
401                         CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
402
403                         CodeRemoveEventStatement cres = new CodeRemoveEventStatement ();
404                         statement = cres;
405
406                         try {
407                                 Generate ();
408                                 Assert.Fail ("#1");
409                         } catch (ArgumentNullException) {
410                         }
411
412                         cres.Event = cere;
413                         try {
414                                 Generate ();
415                                 Assert.Fail ("#2");
416                         } catch (ArgumentNullException) {
417                         }
418
419                         cres.Event = null;
420                         cres.Listener = handler;
421                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
422                                 " -= EventHandler;{0}", NewLine), Generate (), "#3");
423
424                         cres.Event = cere;
425                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
426                                 "A.@class -= EventHandler;{0}", NewLine), Generate (), "#4");
427
428                         cres.Event = new CodeEventReferenceExpression (
429                                 new CodeSnippetExpression ((string) null), "");
430                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
431                                 ". -= EventHandler;{0}", NewLine), Generate (), "#5");
432
433                         cres.Listener = new CodeSnippetExpression ("");
434                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
435                                 ". -= ;{0}", NewLine), Generate (), "#6");
436                 }
437
438                 [Test]
439                 public void CodeSnippetStatementTest ()
440                 {
441                         CodeSnippetStatement css = new CodeSnippetStatement ();
442                         statement = css;
443
444                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
445                                 "{0}", NewLine), Generate (), "#1");
446
447                         css.Value = "class";
448                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
449                                 "class{0}", NewLine), Generate (), "#2");
450
451                         css.Value = null;
452                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
453                                 "{0}", NewLine), Generate (), "#3");
454                 }
455
456                 [Test]
457                 [ExpectedException (typeof (ArgumentException))]
458                 public void CodeStatement ()
459                 {
460                         CodeStatement cs = new CodeStatement ();
461                         statement = cs;
462
463                         Generate ();
464                 }
465
466                 [Test]
467                 public void CodeThrowExceptionStatementTest ()
468                 {
469                         CodeThrowExceptionStatement ctes = new CodeThrowExceptionStatement ();
470                         statement = ctes;
471
472                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
473                                 "throw;{0}", NewLine), Generate (), "#1");
474
475                         ctes.ToThrow = new CodeSnippetExpression ("whatever");
476                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
477                                 "throw whatever;{0}", NewLine), Generate (), "#2");
478                 }
479
480                 [Test]
481                 public void CodeTryCatchFinallyStatementTest ()
482                 {
483                         CodeStatement cs = new CodeGotoStatement ("exit");
484                         CodeCatchClause ccc1 = new CodeCatchClause ("ex1", new CodeTypeReference ("System.ArgumentException"));
485                         CodeCatchClause ccc2 = new CodeCatchClause (null, new CodeTypeReference ("System.ApplicationException"));
486                         CodeSnippetStatement fin1 = new CodeSnippetStatement ("A");
487                         CodeSnippetStatement fin2 = new CodeSnippetStatement ("B");
488
489                         statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
490                                 new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
491
492                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
493                                 "try {{{0}" +
494                                 "    goto exit;{0}" +
495                                 "}}{0}" +
496                                 "catch (System.ArgumentException ex1) {{{0}" + 
497                                 "}}{0}" +
498                                 "catch (System.ApplicationException ) {{{0}" +
499                                 "}}{0}" +
500                                 "finally {{{0}" +
501                                 "A{0}" +
502                                 "B{0}" +
503                                 "}}{0}", NewLine), Generate (), "#1");
504
505                         options.ElseOnClosing = true;
506
507                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
508                                 "try {{{0}" +
509                                 "    goto exit;{0}" +
510                                 "}} catch (System.ArgumentException ex1) {{{0}" +
511                                 "}} catch (System.ApplicationException ) {{{0}" +
512                                 "}} finally {{{0}" +
513                                 "A{0}" +
514                                 "B{0}" +
515                                 "}}{0}", NewLine), Generate (), "#2");
516
517                         statement = new CodeTryCatchFinallyStatement ();
518
519                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
520                                 "try {{{0}" +
521                                 "}}{0}", NewLine), Generate (), "#3");
522
523                         options.ElseOnClosing = false;
524
525                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
526                                 "try {{{0}" +
527                                 "}}{0}", NewLine), Generate (), "#4");
528                 }
529
530                 [Test]
531                 public void CodeVariableDeclarationStatementTest ()
532                 {
533                         CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ();
534                         statement = cvds;
535
536                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
537                                 "void ;{0}", NewLine), Generate (), "#1");
538
539                         cvds.Name = "class";
540                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
541                                 "void @class;{0}", NewLine), Generate (), "#2");
542
543                         cvds.Name = "A";
544                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
545                                 "void A;{0}", NewLine), Generate (), "#3");
546
547                         cvds.Type = new CodeTypeReference (typeof (int));
548                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
549                                 "int A;{0}", NewLine), Generate (), "#4");
550
551                         cvds.InitExpression = new CodePrimitiveExpression (25);
552                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
553                                 "int A = 25;{0}", NewLine), Generate (), "#5");
554
555                         cvds.Name = null;
556                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
557                                 "int  = 25;{0}", NewLine), Generate (), "#5");
558                 }
559         }
560 }
561