Merge pull request #823 from DavidKarlas/master
[mono.git] / mcs / class / System / Test / Microsoft.CSharp / CodeGeneratorFromCompileUnitTest.cs
1 //
2 // Microsoft.CSharp.* Test Cases
3 //
4 // Authors:
5 //      Erik LeBel (eriklebel@yahoo.ca)
6 //      Ilker Cetinkaya (mail@ilker.de)
7 //
8 // (c) 2003 Erik LeBel
9 //
10
11 using System;
12 using System.CodeDom;
13 using System.CodeDom.Compiler;
14 using System.Globalization;
15 using System.IO;
16 using System.Text;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.Microsoft.CSharp
21 {
22         /// <summary>
23         /// Test ICodeGenerator's GenerateCodeFromCompileUnit, along with a 
24         /// minimal set CodeDom components.
25         /// </summary>
26         [TestFixture]
27         public class CodeGeneratorFromCompileUnitTest : CodeGeneratorTestBase
28         {
29                 private string codeUnitHeader = string.Empty;
30                 private CodeCompileUnit codeUnit;
31
32                 public CodeGeneratorFromCompileUnitTest ()
33                 {
34                         Init();
35                         codeUnitHeader = Generate ();
36                 }
37                 
38                 [SetUp]
39                 public void Init ()
40                 {
41                         InitBase ();
42                         codeUnit = new CodeCompileUnit ();
43                 }
44                 
45                 protected override string Generate (CodeGeneratorOptions options)
46                 {
47                         StringWriter writer = new StringWriter ();
48                         writer.NewLine = NewLine;
49
50                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
51                         writer.Close ();
52                         return writer.ToString ().Substring (codeUnitHeader.Length);
53                 }
54                 
55                 [Test]
56                 public void DefaultCodeUnitTest ()
57                 {
58                         Assert.AreEqual (string.Empty, Generate ());
59                 }
60
61                 [Test]
62                 [ExpectedException (typeof (NullReferenceException))]
63                 public void NullCodeUnitTest ()
64                 {
65                         codeUnit = null;
66                         Generate ();
67                 }
68
69                 [Test]
70                 public void ReferencedTest ()
71                 {
72                         codeUnit.ReferencedAssemblies.Add ("System.dll");
73                         Assert.AreEqual (string.Empty, Generate ());
74                 }
75
76                 [Test]
77                 public void SimpleNamespaceTest ()
78                 {
79                         string code = null;
80
81                         CodeNamespace ns = new CodeNamespace ("A");
82                         codeUnit.Namespaces.Add (ns);
83                         code = Generate ();
84                         Assert.AreEqual ("namespace A {\n    \n}\n", code, "#1");
85
86                         CodeGeneratorOptions options = new CodeGeneratorOptions ();
87                         options.BracingStyle = "C";
88                         code = Generate (options);
89                         Assert.AreEqual ("namespace A\n{\n    \n}\n", code, "#2");
90                 }
91
92                 [Test]
93                 public void ReferenceAndSimpleNamespaceTest()
94                 {
95                         CodeNamespace ns = new CodeNamespace ("A");
96                         codeUnit.Namespaces.Add (ns);
97                         codeUnit.ReferencedAssemblies.Add ("using System;");
98                         Assert.AreEqual ("namespace A {\n    \n}\n", Generate ());
99                 }
100
101                 [Test]
102                 public void SimpleAttributeTest ()
103                 {
104                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
105                         attrDec.Name = "A";
106
107                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
108                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
109                                 "[assembly: A()]{0}{0}", NewLine), Generate ());
110                 }
111
112                 [Test]
113                 public void AttributeWithValueTest ()
114                 {
115                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
116                         attrDec.Name = "A";
117
118                         attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
119                                 new CodePrimitiveExpression (false)));
120                         attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
121                                 new CodePrimitiveExpression (true)));
122                         // null name should not be output
123                         attrDec.Arguments.Add (new CodeAttributeArgument (null,
124                                 new CodePrimitiveExpression (true)));
125                         // zero length name should not be output
126                         attrDec.Arguments.Add (new CodeAttributeArgument (string.Empty,
127                                 new CodePrimitiveExpression (false)));
128
129                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
130                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
131                                 "[assembly: A(A1=false, A2=true, true, false)]{0}{0}", NewLine), 
132                                 Generate ());
133                 }
134
135                 [Test]
136                 public void MultipleAttributeTest ()
137                 {
138                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
139                         attrDec.Name = "A";
140                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
141
142                         attrDec = new CodeAttributeDeclaration ();
143                         attrDec.Name = "B";
144                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
145                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
146                                 "[assembly: A()]{0}[assembly: B()]{0}{0}", NewLine),
147                                 Generate ());
148                 }
149
150                 [Test]
151                 public void AttributeAndSimpleNamespaceTest ()
152                 {
153                         CodeNamespace ns = new CodeNamespace ("A");
154                         codeUnit.Namespaces.Add (ns);
155
156                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
157                         attrDec.Name = "A";
158                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
159
160                         attrDec = new CodeAttributeDeclaration ();
161                         attrDec.Name = "B";
162                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
163
164                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
165                                 "[assembly: A()]{0}[assembly: B()]{0}{0}namespace A {{{0}    {0}"
166                                 + "}}{0}", NewLine), Generate ());
167                 }
168
169                 [Test]
170                 public void CodeSnippetTest ()
171                 {
172                         StringWriter writer = new StringWriter ();
173                         writer.NewLine = NewLine;
174
175                         codeUnit = new CodeSnippetCompileUnit ("public class Test1 {}");
176                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
177                         writer.Close ();
178                         Assert.AreEqual ("public class Test1 {}" + writer.NewLine, writer.ToString ());
179                 }
180
181                 [Test]
182                 public void AttributeAndGlobalNamespaceWithImportTest ()
183                 {
184                         CodeNamespace ns = new CodeNamespace ();
185                         ns.Imports.Add (new CodeNamespaceImport ("Z"));
186                         ns.Imports.Add (new CodeNamespaceImport ("A"));
187                         codeUnit.Namespaces.Add (ns);
188
189                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
190                         attrDec.Name = "A";
191                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
192
193                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
194                                 "using A;{0}using Z;{0}{0}[assembly: A()]{0}{0}{0}", NewLine), Generate ());
195                 }
196
197                 [Test]
198                 public void GlobalAttributeBeforeType ()
199                 {
200                         StringWriter writer = new StringWriter ();
201                         writer.NewLine = NewLine;
202
203                         codeUnit = new CodeCompileUnit () {
204                                 AssemblyCustomAttributes = {
205                                         new CodeAttributeDeclaration (
206                                                 new CodeTypeReference (typeof (CLSCompliantAttribute)),
207                                                 new CodeAttributeArgument (new CodePrimitiveExpression (false))),
208                                 },
209                                 Namespaces = {
210                                         new CodeNamespace () {
211                                                 Types = {
212                                                         new CodeTypeDeclaration ("Resources"),
213                                                 },
214                                         }
215                                 },
216                         };
217
218                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
219                         writer.Close ();
220
221                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
222                                 "[assembly: System.CLSCompliantAttribute(false)]{0}{0}{0}{0}public class Resources {{{0}}}{0}", NewLine), Generate ());
223                 }
224         }
225 }