2 // Microsoft.CSharp.* Test Cases
5 // Erik LeBel (eriklebel@yahoo.ca)
11 using System.CodeDom.Compiler;
12 using System.Globalization;
16 using NUnit.Framework;
18 namespace MonoTests.Microsoft.CSharp
21 /// Test ICodeGenerator's GenerateCodeFromNamespace, along with a
22 /// minimal set CodeDom components.
25 public class CodeGeneratorFromNamespaceTest : CodeGeneratorTestBase
27 CodeNamespace codeNamespace = null;
33 codeNamespace = new CodeNamespace ();
36 protected override string Generate (CodeGeneratorOptions options)
38 StringWriter writer = new StringWriter ();
39 writer.NewLine = NewLine;
41 generator.GenerateCodeFromNamespace (codeNamespace, writer, options);
43 return writer.ToString ();
47 [ExpectedException (typeof (NullReferenceException))]
48 public void NullNamespaceTest ()
55 public void NullNamespaceNameTest ()
57 codeNamespace.Name = null;
58 Assert.AreEqual ("\n", Generate ());
63 public void DefaultNamespaceTest ()
65 Assert.AreEqual ("\n", Generate ());
69 public void SimpleNamespaceTest ()
73 codeNamespace.Name = "A";
75 Assert.AreEqual ("namespace A {\n \n}\n", code, "#1");
77 CodeGeneratorOptions options = new CodeGeneratorOptions ();
78 options.BracingStyle = "C";
79 code = Generate (options);
80 Assert.AreEqual ("namespace A\n{\n \n}\n", code, "#2");
84 public void InvalidNamespaceTest ()
86 codeNamespace.Name = "A,B";
87 Assert.AreEqual ("namespace A,B {\n \n}\n", Generate ());
91 public void CommentOnlyNamespaceTest ()
93 CodeCommentStatement comment = new CodeCommentStatement ("a");
94 codeNamespace.Comments.Add (comment);
95 Assert.AreEqual ("// a\n\n", Generate ());
99 public void ImportsTest ()
101 codeNamespace.Imports.Add (new CodeNamespaceImport ("System"));
102 codeNamespace.Imports.Add (new CodeNamespaceImport ("System.Collections"));
104 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
106 "using System.Collections;{0}" +
107 "{0}", NewLine), Generate (), "#1");
109 codeNamespace.Name = "A";
111 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
112 "namespace A {{{0}" +
113 " using System;{0}" +
114 " using System.Collections;{0}" +
116 "}}{0}", NewLine), Generate (), "#2");
118 codeNamespace.Name = null;
119 codeNamespace.Comments.Add (new CodeCommentStatement ("a"));
121 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
124 "using System.Collections;{0}" +
125 "{0}", NewLine), Generate (), "#3");
127 codeNamespace.Name = "A";
129 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
131 "namespace A {{{0}" +
132 " using System;{0}" +
133 " using System.Collections;{0}" +
135 "}}{0}", NewLine), Generate (), "#4");
139 public void TypeTest ()
141 codeNamespace.Types.Add (new CodeTypeDeclaration ("Person"));
142 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
145 "public class Person {{{0}" +
146 "}}{0}", NewLine), Generate (), "#A1");
148 CodeGeneratorOptions options = new CodeGeneratorOptions ();
149 options.BlankLinesBetweenMembers = false;
150 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
152 "public class Person {{{0}" +
153 "}}{0}", NewLine), Generate (options), "#A2");
155 codeNamespace.Name = "A";
156 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
157 "namespace A {{{0}" +
160 " public class Person {{{0}" +
162 "}}{0}", NewLine), Generate (), "#B1");
164 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
165 "namespace A {{{0}" +
167 " public class Person {{{0}" +
169 "}}{0}", NewLine), Generate (options), "#B2");