* MainsoftWebApp20.Tomcat.vmwcsproj: converted to csproj
[mono.git] / mcs / class / System / Test / Microsoft.CSharp / CodeGeneratorFromCompileUnitTest.cs
index 1b2f1fa7dec8833a908e3f22087ea9e95cfe888d..84e15c9dcfa754fac51c23c5ee54f99996c4f100 100644 (file)
@@ -2,36 +2,35 @@
 // Microsoft.CSharp.* Test Cases
 //
 // Authors:
-//     Eric Lindvall (eric@5stops.com)
+//     Erik LeBel (eriklebel@yahoo.ca)
 //
-// (c) 2003 Eric Lindvall
+// (c) 2003 Erik LeBel
 //
 using System;
-using System.Text;
 using System.CodeDom;
 using System.CodeDom.Compiler;
+using System.Globalization;
+using System.IO;
+using System.Text;
 
 using NUnit.Framework;
 
 namespace MonoTests.Microsoft.CSharp
 {
-       ///
        /// <summary>
-       ///     Test ICodeGenerator's GenerateCodeFromCompileUnit, along with a 
-       ///     minimal set CodeDom components.
+       /// Test ICodeGenerator's GenerateCodeFromCompileUnit, along with a 
+       /// minimal set CodeDom components.
        /// </summary>
-       ///
        [TestFixture]
        public class CodeGeneratorFromCompileUnitTest : CodeGeneratorTestBase
        {
-               string codeUnitHeader = "";
-               CodeCompileUnit codeUnit = null;
+               private string codeUnitHeader = "";
+               private CodeCompileUnit codeUnit = null;
 
                public CodeGeneratorFromCompileUnitTest ()
                {
                        Init();
-                       Generate();
-                       codeUnitHeader = Code;
+                       codeUnitHeader = Generate ();
                }
                
                [SetUp]
@@ -41,21 +40,20 @@ namespace MonoTests.Microsoft.CSharp
                        codeUnit = new CodeCompileUnit ();
                }
                
-               protected override string Code {
-                       get { return base.Code.Substring (codeUnitHeader.Length); }
-               }
-               
-               protected override void Generate ()
+               protected override string Generate (CodeGeneratorOptions options)
                {
+                       StringWriter writer = new StringWriter ();
+                       writer.NewLine = NewLine;
+
                        generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
                        writer.Close ();
+                       return writer.ToString ().Substring (codeUnitHeader.Length);
                }
                
                [Test]
                public void DefaultCodeUnitTest ()
                {
-                       Generate ();
-                       Assertion.AssertEquals ("", Code);
+                       Assert.AreEqual ("", Generate ());
                }
 
                [Test]
@@ -63,36 +61,39 @@ namespace MonoTests.Microsoft.CSharp
                public void NullCodeUnitTest ()
                {
                        codeUnit = null;
-                       Generate();
+                       Generate ();
                }
 
                [Test]
                public void ReferencedTest ()
                {
                        codeUnit.ReferencedAssemblies.Add ("System.dll");
-                       Generate();
-                       Assertion.AssertEquals ("", Code);
+                       Assertion.AssertEquals ("", Generate ());
                }
 
                [Test]
-               [Ignore ("This only differs in 4 spaces")]
                public void SimpleNamespaceTest ()
                {
+                       string code = null;
+
                        CodeNamespace ns = new CodeNamespace ("A");
                        codeUnit.Namespaces.Add (ns);
-                       Generate ();
-                       Assertion.AssertEquals ("namespace A {\n    \n}\n", Code);
+                       code = Generate ();
+                       Assert.AreEqual ("namespace A {\n    \n}\n", code, "#1");
+
+                       CodeGeneratorOptions options = new CodeGeneratorOptions ();
+                       options.BracingStyle = "C";
+                       code = Generate (options);
+                       Assert.AreEqual ("namespace A\n{\n    \n}\n", code, "#2");
                }
 
                [Test]
-               [Ignore ("This only differs in 4 spaces")]
                public void ReferenceAndSimpleNamespaceTest()
                {
                        CodeNamespace ns = new CodeNamespace ("A");
                        codeUnit.Namespaces.Add (ns);
                        codeUnit.ReferencedAssemblies.Add ("using System;");
-                       Generate ();
-                       Assertion.AssertEquals ("namespace A {\n    \n}\n", Code);
+                       Assert.AreEqual ("namespace A {\n    \n}\n", Generate ());
                }
 
                [Test]
@@ -102,22 +103,77 @@ namespace MonoTests.Microsoft.CSharp
                        attrDec.Name = "A";
 
                        codeUnit.AssemblyCustomAttributes.Add (attrDec);
-                       Generate ();
-                       Assertion.AssertEquals ("[assembly: A()]\n\n", Code);
+                       Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
+                               "[assembly: A()]{0}{0}", NewLine), Generate ());
                }
 
-               /* FIXME
                [Test]
                public void AttributeWithValueTest ()
                {
                        CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
                        attrDec.Name = "A";
-                       
+
+                       attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
+                               new CodePrimitiveExpression (false)));
+                       attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
+                               new CodePrimitiveExpression (true)));
+                       // null name should not be output
+                       attrDec.Arguments.Add (new CodeAttributeArgument (null,
+                               new CodePrimitiveExpression (true)));
+                       // zero length name should not be output
+                       attrDec.Arguments.Add (new CodeAttributeArgument (string.Empty,
+                               new CodePrimitiveExpression (false)));
 
                        codeUnit.AssemblyCustomAttributes.Add (attrDec);
-                       Generate ();
-                       Assertion.AssertEquals ("[assembly: A()]\n\n", Code);
-               }*/
+                       Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
+                               "[assembly: A(A1=false, A2=true, true, false)]{0}{0}", NewLine), 
+                               Generate ());
+               }
 
+               [Test]
+               public void MultipleAttributeTest ()
+               {
+                       CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
+                       attrDec.Name = "A";
+                       codeUnit.AssemblyCustomAttributes.Add (attrDec);
+
+                       attrDec = new CodeAttributeDeclaration ();
+                       attrDec.Name = "B";
+                       codeUnit.AssemblyCustomAttributes.Add (attrDec);
+                       Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
+                               "[assembly: A()]{0}[assembly: B()]{0}{0}", NewLine),
+                               Generate ());
+               }
+
+               [Test]
+               public void AttributeAndSimpleNamespaceTest ()
+               {
+                       CodeNamespace ns = new CodeNamespace ("A");
+                       codeUnit.Namespaces.Add (ns);
+
+                       CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
+                       attrDec.Name = "A";
+                       codeUnit.AssemblyCustomAttributes.Add (attrDec);
+
+                       attrDec = new CodeAttributeDeclaration ();
+                       attrDec.Name = "B";
+                       codeUnit.AssemblyCustomAttributes.Add (attrDec);
+
+                       Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
+                               "[assembly: A()]{0}[assembly: B()]{0}{0}namespace A {{{0}    {0}"
+                               + "}}{0}", NewLine), Generate ());
+               }
+
+               [Test]
+               public void CodeSnippetTest ()
+               {
+                       StringWriter writer = new StringWriter ();
+                       writer.NewLine = NewLine;
+
+                       codeUnit = new CodeSnippetCompileUnit ("public class Test1 {}");
+                       generator.GenerateCodeFromCompileUnit (codeUnit, writer, options);
+                       writer.Close ();
+                       Assert.AreEqual ("public class Test1 {}" + writer.NewLine, writer.ToString ());
+               }
        }
 }