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 GenerateCodeFromCompileUnit, along with a
22 /// minimal set CodeDom components.
25 public class CodeGeneratorFromCompileUnitTest : CodeGeneratorTestBase
27 private string codeUnitHeader = "";
28 private CodeCompileUnit codeUnit = null;
30 public CodeGeneratorFromCompileUnitTest ()
33 codeUnitHeader = Generate ();
40 codeUnit = new CodeCompileUnit ();
43 protected override string Generate (CodeGeneratorOptions options)
45 StringWriter writer = new StringWriter ();
46 writer.NewLine = NewLine;
48 generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
50 return writer.ToString ().Substring (codeUnitHeader.Length);
54 public void DefaultCodeUnitTest ()
56 Assert.AreEqual ("", Generate ());
60 [ExpectedException (typeof (NullReferenceException))]
61 public void NullCodeUnitTest ()
68 public void ReferencedTest ()
70 codeUnit.ReferencedAssemblies.Add ("System.dll");
71 Assertion.AssertEquals ("", Generate ());
75 public void SimpleNamespaceTest ()
79 CodeNamespace ns = new CodeNamespace ("A");
80 codeUnit.Namespaces.Add (ns);
82 Assert.AreEqual ("namespace A {\n \n}\n", code, "#1");
84 CodeGeneratorOptions options = new CodeGeneratorOptions ();
85 options.BracingStyle = "C";
86 code = Generate (options);
87 Assert.AreEqual ("namespace A\n{\n \n}\n", code, "#2");
91 public void ReferenceAndSimpleNamespaceTest()
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 ());
100 public void SimpleAttributeTest ()
102 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
105 codeUnit.AssemblyCustomAttributes.Add (attrDec);
106 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
107 "[assembly: A()]{0}{0}", NewLine), Generate ());
111 public void AttributeWithValueTest ()
113 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
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)));
127 codeUnit.AssemblyCustomAttributes.Add (attrDec);
128 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
129 "[assembly: A(A1=false, A2=true, true, false)]{0}{0}", NewLine),
134 public void MultipleAttributeTest ()
136 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
138 codeUnit.AssemblyCustomAttributes.Add (attrDec);
140 attrDec = new CodeAttributeDeclaration ();
142 codeUnit.AssemblyCustomAttributes.Add (attrDec);
143 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
144 "[assembly: A()]{0}[assembly: B()]{0}{0}", NewLine),
149 public void AttributeAndSimpleNamespaceTest ()
151 CodeNamespace ns = new CodeNamespace ("A");
152 codeUnit.Namespaces.Add (ns);
154 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
156 codeUnit.AssemblyCustomAttributes.Add (attrDec);
158 attrDec = new CodeAttributeDeclaration ();
160 codeUnit.AssemblyCustomAttributes.Add (attrDec);
162 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
163 "[assembly: A()]{0}[assembly: B()]{0}{0}namespace A {{{0} {0}"
164 + "}}{0}", NewLine), Generate ());
168 public void CodeSnippetTest ()
170 StringWriter writer = new StringWriter ();
171 writer.NewLine = NewLine;
173 codeUnit = new CodeSnippetCompileUnit ("public class Test1 {}");
174 generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
176 Assert.AreEqual ("public class Test1 {}" + writer.NewLine, writer.ToString ());