Add this for backwards compatibility
[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 //
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 GenerateCodeFromCompileUnit, along with a 
22         /// minimal set CodeDom components.
23         /// </summary>
24         [TestFixture]
25         public class CodeGeneratorFromCompileUnitTest : CodeGeneratorTestBase
26         {
27                 private string codeUnitHeader = "";
28                 private CodeCompileUnit codeUnit = null;
29
30                 public CodeGeneratorFromCompileUnitTest ()
31                 {
32                         Init();
33                         codeUnitHeader = Generate ();
34                 }
35                 
36                 [SetUp]
37                 public void Init ()
38                 {
39                         InitBase ();
40                         codeUnit = new CodeCompileUnit ();
41                 }
42                 
43                 protected override string Generate (CodeGeneratorOptions options)
44                 {
45                         StringWriter writer = new StringWriter ();
46                         writer.NewLine = NewLine;
47
48                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
49                         writer.Close ();
50                         return writer.ToString ().Substring (codeUnitHeader.Length);
51                 }
52                 
53                 [Test]
54                 public void DefaultCodeUnitTest ()
55                 {
56                         Assert.AreEqual ("", Generate ());
57                 }
58
59                 [Test]
60                 [ExpectedException (typeof (NullReferenceException))]
61                 public void NullCodeUnitTest ()
62                 {
63                         codeUnit = null;
64                         Generate ();
65                 }
66
67                 [Test]
68                 public void ReferencedTest ()
69                 {
70                         codeUnit.ReferencedAssemblies.Add ("System.dll");
71                         Assertion.AssertEquals ("", Generate ());
72                 }
73
74                 [Test]
75                 public void SimpleNamespaceTest ()
76                 {
77                         string code = null;
78
79                         CodeNamespace ns = new CodeNamespace ("A");
80                         codeUnit.Namespaces.Add (ns);
81                         code = Generate ();
82                         Assert.AreEqual ("namespace A {\n    \n}\n", code, "#1");
83
84                         CodeGeneratorOptions options = new CodeGeneratorOptions ();
85                         options.BracingStyle = "C";
86                         code = Generate (options);
87                         Assert.AreEqual ("namespace A\n{\n    \n}\n", code, "#2");
88                 }
89
90                 [Test]
91                 public void ReferenceAndSimpleNamespaceTest()
92                 {
93                         CodeNamespace ns = new CodeNamespace ("A");
94                         codeUnit.Namespaces.Add (ns);
95                         codeUnit.ReferencedAssemblies.Add ("using System;");
96                         Assert.AreEqual ("namespace A {\n    \n}\n", Generate ());
97                 }
98
99                 [Test]
100                 public void SimpleAttributeTest ()
101                 {
102                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
103                         attrDec.Name = "A";
104
105                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
106                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
107                                 "[assembly: A()]{0}{0}", NewLine), Generate ());
108                 }
109
110                 [Test]
111                 public void AttributeWithValueTest ()
112                 {
113                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
114                         attrDec.Name = "A";
115
116                         attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
117                                 new CodePrimitiveExpression (false)));
118                         attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
119                                 new CodePrimitiveExpression (true)));
120                         // null name should not be output
121                         attrDec.Arguments.Add (new CodeAttributeArgument (null,
122                                 new CodePrimitiveExpression (true)));
123                         // zero length name should not be output
124                         attrDec.Arguments.Add (new CodeAttributeArgument (string.Empty,
125                                 new CodePrimitiveExpression (false)));
126
127                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
128                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
129                                 "[assembly: A(A1=false, A2=true, true, false)]{0}{0}", NewLine), 
130                                 Generate ());
131                 }
132
133                 [Test]
134                 public void MultipleAttributeTest ()
135                 {
136                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
137                         attrDec.Name = "A";
138                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
139
140                         attrDec = new CodeAttributeDeclaration ();
141                         attrDec.Name = "B";
142                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
143                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
144                                 "[assembly: A()]{0}[assembly: B()]{0}{0}", NewLine),
145                                 Generate ());
146                 }
147
148                 [Test]
149                 public void AttributeAndSimpleNamespaceTest ()
150                 {
151                         CodeNamespace ns = new CodeNamespace ("A");
152                         codeUnit.Namespaces.Add (ns);
153
154                         CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
155                         attrDec.Name = "A";
156                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
157
158                         attrDec = new CodeAttributeDeclaration ();
159                         attrDec.Name = "B";
160                         codeUnit.AssemblyCustomAttributes.Add (attrDec);
161
162                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
163                                 "[assembly: A()]{0}[assembly: B()]{0}{0}namespace A {{{0}    {0}"
164                                 + "}}{0}", NewLine), Generate ());
165                 }
166
167                 [Test]
168                 public void CodeSnippetTest ()
169                 {
170                         StringWriter writer = new StringWriter ();
171                         writer.NewLine = NewLine;
172
173                         codeUnit = new CodeSnippetCompileUnit ("public class Test1 {}");
174                         generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
175                         writer.Close ();
176                         Assert.AreEqual ("public class Test1 {}" + writer.NewLine, writer.ToString ());
177                 }
178         }
179 }