3 // - CAS unit tests for System.CodeDom.Compiler.CodeGenerator
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
34 using System.CodeDom.Compiler;
36 using System.Reflection;
37 using System.Security;
38 using System.Security.Permissions;
40 namespace MonoCasTests.System.CodeDom.Compiler {
42 class CodeGeneratorTest: CodeGenerator {
44 public CodeGeneratorTest ()
48 protected override string CreateEscapedIdentifier (string value)
53 protected override string CreateValidIdentifier (string value)
58 protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression e)
62 protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression e)
66 protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression e)
70 protected override void GenerateAssignStatement (CodeAssignStatement e)
74 protected override void GenerateAttachEventStatement (CodeAttachEventStatement e)
78 protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
82 protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
86 protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression e)
90 protected override void GenerateCastExpression (CodeCastExpression e)
94 protected override void GenerateComment (CodeComment e)
98 protected override void GenerateConditionStatement (CodeConditionStatement e)
102 protected override void GenerateConstructor (CodeConstructor e, CodeTypeDeclaration c)
106 protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression e)
110 protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression e)
114 protected override void GenerateEntryPointMethod (CodeEntryPointMethod e, CodeTypeDeclaration c)
118 protected override void GenerateEvent (CodeMemberEvent e, CodeTypeDeclaration c)
122 protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression e)
126 protected override void GenerateExpressionStatement (CodeExpressionStatement e)
130 protected override void GenerateField (CodeMemberField e)
134 protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression e)
138 protected override void GenerateGotoStatement (CodeGotoStatement e)
142 protected override void GenerateIndexerExpression (CodeIndexerExpression e)
146 protected override void GenerateIterationStatement (CodeIterationStatement e)
150 protected override void GenerateLabeledStatement (CodeLabeledStatement e)
154 protected override void GenerateLinePragmaEnd (CodeLinePragma e)
158 protected override void GenerateLinePragmaStart (CodeLinePragma e)
162 protected override void GenerateMethod (CodeMemberMethod e, CodeTypeDeclaration c)
166 protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression e)
170 protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression e)
174 protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement e)
178 protected override void GenerateNamespaceEnd (CodeNamespace e)
182 protected override void GenerateNamespaceImport (CodeNamespaceImport e)
186 protected override void GenerateNamespaceStart (CodeNamespace e)
190 protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression e)
194 protected override void GenerateProperty (CodeMemberProperty e, CodeTypeDeclaration c)
198 protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression e)
202 protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression e)
206 protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement e)
210 protected override void GenerateSnippetExpression (CodeSnippetExpression e)
214 protected override void GenerateSnippetMember (CodeSnippetTypeMember e)
218 protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression e)
222 protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement e)
226 protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement e)
230 protected override void GenerateTypeConstructor (CodeTypeConstructor e)
234 protected override void GenerateTypeEnd (CodeTypeDeclaration e)
238 protected override void GenerateTypeStart (CodeTypeDeclaration e)
242 protected override void GenerateVariableDeclarationStatement (CodeVariableDeclarationStatement e)
246 protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression e)
250 protected override string GetTypeOutput (CodeTypeReference value)
255 protected override bool IsValidIdentifier (string value)
260 protected override string NullToken {
266 protected override void OutputType (CodeTypeReference typeRef)
270 protected override string QuoteSnippetString (string value)
275 protected override bool Supports (GeneratorSupport support)
280 public void TestProtectedProperties ()
282 Assert.IsNull (CurrentClass, "CurrentClass");
283 Assert.IsNull (CurrentMember, "CurrentMember");
284 Assert.AreEqual ("<% unknown %>", CurrentMemberName, "CurrentMemberName");
285 Assert.AreEqual ("<% unknown %>", CurrentTypeName, "CurrentTypeName");
288 Assert.AreEqual (0, Indent, "Indent");
290 catch (NullReferenceException) {
294 Indent = Int32.MinValue;
296 catch (NullReferenceException) {
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");
309 public void TestProtectedMethods ()
312 ContinueOnNewLine (String.Empty);
314 catch (NullReferenceException) {
317 GenerateBinaryOperatorExpression (null);
319 catch (NullReferenceException) {
322 GenerateCommentStatement (null);
324 catch (NullReferenceException) {
327 GenerateCommentStatements (null);
329 catch (NullReferenceException) {
332 GenerateCompileUnit (null);
334 catch (NullReferenceException) {
337 GenerateCompileUnitEnd (null);
339 catch (NullReferenceException) {
342 GenerateCompileUnitStart (null);
344 catch (NullReferenceException) {
347 GenerateDecimalValue (0);
349 catch (NullReferenceException) {
352 GenerateDefaultValueExpression (null);
354 catch (NotImplementedException) {
358 GenerateDirectionExpression (null);
360 catch (NullReferenceException) {
362 GenerateDirectives (null);
364 GenerateDoubleValue (Double.MaxValue);
366 catch (NullReferenceException) {
369 GenerateExpression (null);
371 catch (ArgumentNullException) {
374 GenerateNamespace (null);
376 catch (NullReferenceException) {
379 GenerateNamespaceImports (null);
381 catch (NullReferenceException) {
384 GenerateNamespaces (null);
386 catch (NullReferenceException) {
389 GenerateParameterDeclarationExpression (null);
391 catch (NullReferenceException) {
394 GeneratePrimitiveExpression (null);
396 catch (NullReferenceException) {
399 GenerateSingleFloatValue (Single.MinValue);
401 catch (NullReferenceException) {
404 GenerateSnippetCompileUnit (null);
406 catch (NullReferenceException) {
409 GenerateSnippetStatement (null);
411 catch (NullReferenceException) {
414 GenerateStatement (null);
416 catch (NullReferenceException) {
419 GenerateStatements (null);
421 catch (NullReferenceException) {
424 GenerateTypeOfExpression (null);
426 catch (NullReferenceException) {
429 GenerateTypeReferenceExpression (null);
431 catch (NullReferenceException) {
434 GenerateTypes (null);
436 catch (NullReferenceException) {
439 OutputAttributeArgument (null);
441 catch (NullReferenceException) {
444 OutputAttributeDeclarations (null);
446 catch (NullReferenceException) {
449 OutputDirection (FieldDirection.In);
451 catch (NullReferenceException) {
454 OutputExpressionList (null);
456 catch (NullReferenceException) {
459 OutputExpressionList (null, true);
461 catch (NullReferenceException) {
464 OutputFieldScopeModifier (MemberAttributes.Abstract);
466 catch (NullReferenceException) {
469 OutputIdentifier (null);
471 catch (NullReferenceException) {
474 OutputMemberAccessModifier (MemberAttributes.Abstract);
476 catch (NullReferenceException) {
479 OutputMemberScopeModifier (MemberAttributes.Abstract);
481 catch (NullReferenceException) {
484 OutputOperator (CodeBinaryOperatorType.Add);
486 catch (NullReferenceException) {
489 OutputParameters (null);
491 catch (NullReferenceException) {
494 OutputTypeAttributes (TypeAttributes.Abstract, false, false);
496 catch (NullReferenceException) {
499 OutputTypeNamePair (null, null);
501 catch (NullReferenceException) {
504 Supports (GeneratorSupport.ArraysOfArrays);
506 catch (NullReferenceException) {
509 ValidateIdentifier (null);
511 catch (NullReferenceException) {
518 public class CodeGeneratorCas {
520 private StringWriter writer;
523 public void FixtureSetUp ()
526 writer = new StringWriter ();
532 if (!SecurityManager.SecurityEnabled)
533 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
537 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
538 public void Public ()
540 CodeGeneratorTest cg = new CodeGeneratorTest ();
542 cg.GenerateCodeFromMember (new CodeTypeMember (), writer, null);
544 catch (NotImplementedException) {
550 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
551 public void Protected ()
553 CodeGeneratorTest cg = new CodeGeneratorTest ();
554 // test protected (but not abstract) stuff from within the class itself
555 cg.TestProtectedProperties ();
556 cg.TestProtectedMethods ();
560 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
561 public void StaticMethods ()
563 Assert.IsFalse (CodeGenerator.IsValidLanguageIndependentIdentifier ("@"), "IsValidLanguageIndependentIdentifier");
565 CodeGenerator.ValidateIdentifiers (new CodeCompileUnit ());
567 catch (NotImplementedException) {
573 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
574 public void LinkDemand_Deny_Unrestricted ()
576 ConstructorInfo ci = typeof (CodeGeneratorTest).GetConstructor (new Type[0]);
577 Assert.IsNotNull (ci, "default .ctor()");
578 Assert.IsNotNull (ci.Invoke (null), "invoke");