[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System / Test / System.CodeDom.Compiler / CodeGeneratorCas.cs
1 //
2 // CodeGeneratorCas.cs 
3 //      - CAS unit tests for System.CodeDom.Compiler.CodeGenerator
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31
32 using System;
33 using System.CodeDom;
34 using System.CodeDom.Compiler;
35 using System.IO;
36 using System.Reflection;
37 using System.Security;
38 using System.Security.Permissions;
39
40 namespace MonoCasTests.System.CodeDom.Compiler {
41
42         class CodeGeneratorTest: CodeGenerator {
43
44                 public CodeGeneratorTest ()
45                 {
46                 }
47
48                 protected override string CreateEscapedIdentifier (string value)
49                 {
50                         return String.Empty;
51                 }
52
53                 protected override string CreateValidIdentifier (string value)
54                 {
55                         return String.Empty;
56                 }
57
58                 protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression e)
59                 {
60                 }
61
62                 protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression e)
63                 {
64                 }
65
66                 protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression e)
67                 {
68                 }
69
70                 protected override void GenerateAssignStatement (CodeAssignStatement e)
71                 {
72                 }
73
74                 protected override void GenerateAttachEventStatement (CodeAttachEventStatement e)
75                 {
76                 }
77
78                 protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
79                 {
80                 }
81
82                 protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
83                 {
84                 }
85
86                 protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression e)
87                 {
88                 }
89
90                 protected override void GenerateCastExpression (CodeCastExpression e)
91                 {
92                 }
93
94                 protected override void GenerateComment (CodeComment e)
95                 {
96                 }
97
98                 protected override void GenerateConditionStatement (CodeConditionStatement e)
99                 {
100                 }
101
102                 protected override void GenerateConstructor (CodeConstructor e, CodeTypeDeclaration c)
103                 {
104                 }
105
106                 protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression e)
107                 {
108                 }
109
110                 protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression e)
111                 {
112                 }
113
114                 protected override void GenerateEntryPointMethod (CodeEntryPointMethod e, CodeTypeDeclaration c)
115                 {
116                 }
117
118                 protected override void GenerateEvent (CodeMemberEvent e, CodeTypeDeclaration c)
119                 {
120                 }
121
122                 protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression e)
123                 {
124                 }
125
126                 protected override void GenerateExpressionStatement (CodeExpressionStatement e)
127                 {
128                 }
129
130                 protected override void GenerateField (CodeMemberField e)
131                 {
132                 }
133
134                 protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression e)
135                 {
136                 }
137
138                 protected override void GenerateGotoStatement (CodeGotoStatement e)
139                 {
140                 }
141
142                 protected override void GenerateIndexerExpression (CodeIndexerExpression e)
143                 {
144                 }
145
146                 protected override void GenerateIterationStatement (CodeIterationStatement e)
147                 {
148                 }
149
150                 protected override void GenerateLabeledStatement (CodeLabeledStatement e)
151                 {
152                 }
153
154                 protected override void GenerateLinePragmaEnd (CodeLinePragma e)
155                 {
156                 }
157
158                 protected override void GenerateLinePragmaStart (CodeLinePragma e)
159                 {
160                 }
161
162                 protected override void GenerateMethod (CodeMemberMethod e, CodeTypeDeclaration c)
163                 {
164                 }
165
166                 protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression e)
167                 {
168                 }
169
170                 protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression e)
171                 {
172                 }
173
174                 protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement e)
175                 {
176                 }
177
178                 protected override void GenerateNamespaceEnd (CodeNamespace e)
179                 {
180                 }
181
182                 protected override void GenerateNamespaceImport (CodeNamespaceImport e)
183                 {
184                 }
185
186                 protected override void GenerateNamespaceStart (CodeNamespace e)
187                 {
188                 }
189
190                 protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression e)
191                 {
192                 }
193
194                 protected override void GenerateProperty (CodeMemberProperty e, CodeTypeDeclaration c)
195                 {
196                 }
197
198                 protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression e)
199                 {
200                 }
201
202                 protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression e)
203                 {
204                 }
205
206                 protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement e)
207                 {
208                 }
209
210                 protected override void GenerateSnippetExpression (CodeSnippetExpression e)
211                 {
212                 }
213
214                 protected override void GenerateSnippetMember (CodeSnippetTypeMember e)
215                 {
216                 }
217
218                 protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression e)
219                 {
220                 }
221
222                 protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement e)
223                 {
224                 }
225
226                 protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement e)
227                 {
228                 }
229
230                 protected override void GenerateTypeConstructor (CodeTypeConstructor e)
231                 {
232                 }
233
234                 protected override void GenerateTypeEnd (CodeTypeDeclaration e)
235                 {
236                 }
237
238                 protected override void GenerateTypeStart (CodeTypeDeclaration e)
239                 {
240                 }
241
242                 protected override void GenerateVariableDeclarationStatement (CodeVariableDeclarationStatement e)
243                 {
244                 }
245
246                 protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression e)
247                 {
248                 }
249
250                 protected override string GetTypeOutput (CodeTypeReference value)
251                 {
252                         return String.Empty;
253                 }
254
255                 protected override bool IsValidIdentifier (string value)
256                 {
257                         return true;
258                 }
259
260                 protected override string NullToken {
261                         get {
262                                 return String.Empty;
263                         }
264                 }
265
266                 protected override void OutputType (CodeTypeReference typeRef)
267                 {
268                 }
269
270                 protected override string QuoteSnippetString (string value)
271                 {
272                         return String.Empty;
273                 }
274
275                 protected override bool Supports (GeneratorSupport support)
276                 {
277                         return true;
278                 }
279
280                 public void TestProtectedProperties ()
281                 {
282                         Assert.IsNull (CurrentClass, "CurrentClass");
283                         Assert.IsNull (CurrentMember, "CurrentMember");
284                         Assert.AreEqual ("<% unknown %>", CurrentMemberName, "CurrentMemberName");
285                         Assert.AreEqual ("<% unknown %>", CurrentTypeName, "CurrentTypeName");
286
287                         try {
288                                 Assert.AreEqual (0, Indent, "Indent");
289                         }
290                         catch (NullReferenceException) {
291                         }
292
293                         try {
294                                 Indent = Int32.MinValue;
295                         }
296                         catch (NullReferenceException) {
297                         }
298
299                         Assert.IsFalse (IsCurrentClass, "IsCurrentClass");
300                         Assert.IsFalse (IsCurrentDelegate, "IsCurrentDelegate");
301                         Assert.IsFalse (IsCurrentEnum, "IsCurrentEnum");
302                         Assert.IsFalse (IsCurrentInterface, "IsCurrentInterface");
303                         Assert.IsFalse (IsCurrentStruct, "IsCurrentStruct");
304                         Assert.AreEqual (String.Empty, NullToken, "NullToken");
305                         Assert.IsNull (Options, "Options");
306                         Assert.IsNull (Output, "Output");
307                 }
308
309                 public void TestProtectedMethods ()
310                 {
311                         try {
312                                 ContinueOnNewLine (String.Empty);
313                         }
314                         catch (NullReferenceException) {
315                         }
316                         try {
317                                 GenerateBinaryOperatorExpression (null); 
318                         }
319                         catch (NullReferenceException) {
320                         }
321                         try {
322                                 GenerateCommentStatement (null);
323                         }
324                         catch (NullReferenceException) {
325                         }
326                         try {
327                                 GenerateCommentStatements (null); 
328                         }
329                         catch (NullReferenceException) {
330                         }
331                         try {
332                                 GenerateCompileUnit (null); 
333                         }
334                         catch (NullReferenceException) {
335                         }
336                         try {
337                                 GenerateCompileUnitEnd (null); 
338                         }
339                         catch (NullReferenceException) {
340                         }
341                         try {
342                                 GenerateCompileUnitStart (null); 
343                         }
344                         catch (NullReferenceException) {
345                         }
346                         try {
347                                 GenerateDecimalValue (0); 
348                         }
349                         catch (NullReferenceException) {
350                         }
351                         try {
352                                 GenerateDefaultValueExpression (null);
353                         }
354                         catch (NotImplementedException) {
355                                 // both mono & ms
356                         }
357                         try {
358                                 GenerateDirectionExpression (null); 
359                         }
360                         catch (NullReferenceException) {
361                         }
362                         GenerateDirectives (null);
363                         try {
364                                 GenerateDoubleValue (Double.MaxValue); 
365                         }
366                         catch (NullReferenceException) {
367                         }
368                         try {
369                                 GenerateExpression (null);
370                         }
371                         catch (ArgumentNullException) {
372                         }
373                         try {
374                                 GenerateNamespace (null); 
375                         }
376                         catch (NullReferenceException) {
377                         }
378                         try {
379                                 GenerateNamespaceImports (null); 
380                         }
381                         catch (NullReferenceException) {
382                         }
383                         try {
384                                 GenerateNamespaces (null); 
385                         }
386                         catch (NullReferenceException) {
387                         }
388                         try {
389                                 GenerateParameterDeclarationExpression (null); 
390                         }
391                         catch (NullReferenceException) {
392                         }
393                         try {
394                                 GeneratePrimitiveExpression (null); 
395                         }
396                         catch (NullReferenceException) {
397                         }
398                         try {
399                                 GenerateSingleFloatValue (Single.MinValue);
400                         }
401                         catch (NullReferenceException) {
402                         }
403                         try {
404                                 GenerateSnippetCompileUnit (null); 
405                         }
406                         catch (NullReferenceException) {
407                         }
408                         try {
409                                 GenerateSnippetStatement (null); 
410                         }
411                         catch (NullReferenceException) {
412                         }
413                         try {
414                                 GenerateStatement (null); 
415                         }
416                         catch (NullReferenceException) {
417                         }
418                         try {
419                                 GenerateStatements (null); 
420                         }
421                         catch (NullReferenceException) {
422                         }
423                         try {
424                                 GenerateTypeOfExpression (null); 
425                         }
426                         catch (NullReferenceException) {
427                         }
428                         try {
429                                 GenerateTypeReferenceExpression (null); 
430                         }
431                         catch (NullReferenceException) {
432                         }
433                         try {
434                                 GenerateTypes (null);
435                         }
436                         catch (NullReferenceException) {
437                         }
438                         try {
439                                 OutputAttributeArgument (null);
440                         }
441                         catch (NullReferenceException) {
442                         }
443                         try {
444                                 OutputAttributeDeclarations (null);
445                         }
446                         catch (NullReferenceException) {
447                         }
448                         try {
449                                 OutputDirection (FieldDirection.In);
450                         }
451                         catch (NullReferenceException) {
452                         }
453                         try {
454                                 OutputExpressionList (null);
455                         }
456                         catch (NullReferenceException) {
457                         }
458                         try {
459                                 OutputExpressionList (null, true);
460                         }
461                         catch (NullReferenceException) {
462                         }
463                         try {
464                                 OutputFieldScopeModifier (MemberAttributes.Abstract);
465                         }
466                         catch (NullReferenceException) {
467                         }
468                         try {
469                                 OutputIdentifier (null);
470                         }
471                         catch (NullReferenceException) {
472                         }
473                         try {
474                                 OutputMemberAccessModifier (MemberAttributes.Abstract);
475                         }
476                         catch (NullReferenceException) {
477                         }
478                         try {
479                                 OutputMemberScopeModifier (MemberAttributes.Abstract);
480                         }
481                         catch (NullReferenceException) {
482                         }
483                         try {
484                                 OutputOperator (CodeBinaryOperatorType.Add);
485                         }
486                         catch (NullReferenceException) {
487                         }
488                         try {
489                                 OutputParameters (null);
490                         }
491                         catch (NullReferenceException) {
492                         }
493                         try {
494                                 OutputTypeAttributes (TypeAttributes.Abstract, false, false);
495                         }
496                         catch (NullReferenceException) {
497                         }
498                         try {
499                                 OutputTypeNamePair (null, null);
500                         }
501                         catch (NullReferenceException) {
502                         }
503                         try {
504                                 Supports (GeneratorSupport.ArraysOfArrays);
505                         }
506                         catch (NullReferenceException) {
507                         }
508                         try {
509                                 ValidateIdentifier (null);
510                         }
511                         catch (NullReferenceException) {
512                         }
513                 }
514         }
515
516         [TestFixture]
517         [Category ("CAS")]
518         public class CodeGeneratorCas {
519
520                 private StringWriter writer;
521
522                 [TestFixtureSetUp]
523                 public void FixtureSetUp ()
524                 {
525                         // at full trust
526                         writer = new StringWriter ();
527                 }
528
529                 [SetUp]
530                 public void SetUp ()
531                 {
532                         if (!SecurityManager.SecurityEnabled)
533                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
534                 }
535
536                 [Test]
537                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
538                 public void Public ()
539                 {
540                         CodeGeneratorTest cg = new CodeGeneratorTest ();
541                         try {
542                                 cg.GenerateCodeFromMember (new CodeTypeMember (), writer, null);
543                         }
544                         catch (NotImplementedException) {
545                                 // mono
546                         }
547                 }
548
549                 [Test]
550                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
551                 public void Protected ()
552                 {
553                         CodeGeneratorTest cg = new CodeGeneratorTest ();
554                         // test protected (but not abstract) stuff from within the class itself
555                         cg.TestProtectedProperties ();
556                         cg.TestProtectedMethods ();
557                 }
558
559                 [Test]
560                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
561                 public void StaticMethods ()
562                 {
563                         Assert.IsFalse (CodeGenerator.IsValidLanguageIndependentIdentifier ("@"), "IsValidLanguageIndependentIdentifier");
564                         try {
565                                 CodeGenerator.ValidateIdentifiers (new CodeCompileUnit ());
566                         }
567                         catch (NotImplementedException) {
568                                 // mono
569                         }
570                 }
571
572                 [Test]
573                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
574                 public void LinkDemand_Deny_Unrestricted ()
575                 {
576                         ConstructorInfo ci = typeof (CodeGeneratorTest).GetConstructor (new Type[0]);
577                         Assert.IsNotNull (ci, "default .ctor()");
578                         Assert.IsNotNull (ci.Invoke (null), "invoke");
579                 }
580         }
581 }