In Test/System.Diagnostics:
[mono.git] / mcs / class / System / Test / Microsoft.VisualBasic / VBCodeProviderTest.cs
index c97927e67dc6044c45c13207e93662374e067fb5..44b703e6337c852f8ca5c2f3f5905fce4d97f7a5 100644 (file)
@@ -32,6 +32,7 @@ namespace MonoTests.Microsoft.VisualBasic
        }
 
        [TestFixture]
+       [Category ("NotWorking")] // we cannot rely on vbnc being available
        public class VBCodeProviderTest
        {
                private string _tempDir;
@@ -39,10 +40,15 @@ namespace MonoTests.Microsoft.VisualBasic
                private static OsType OS;
                private static char DSC = Path.DirectorySeparatorChar;
 
-               private static readonly string _sourceTest1 = "Public Class Test1" +
+               private static readonly string _sourceLibrary1 = "Public Class Test1" +
                        Environment.NewLine + "End Class";
-               private static readonly string _sourceTest2 = "Public Class Test2" +
+               private static readonly string _sourceLibrary2 = "Public Class Test2" +
                        Environment.NewLine + "End Class";
+               private static readonly string _sourceExecutable = @"
+                       Public Class Program
+                       Public Sub Main
+                       End Sub
+                       End Class";
 
                [SetUp]
                public void GetReady ()
@@ -113,12 +119,7 @@ namespace MonoTests.Microsoft.VisualBasic
                }
 
                [Test]
-#if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
-#else
-               [Ignore ("Bug #75223")]
-#endif
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CreateCompiler ()
                {
                        // Prepare the compilation
@@ -193,17 +194,14 @@ namespace MonoTests.Microsoft.VisualBasic
                }
 
                [Test]
-#if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
-#endif
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CompileFromFile_InMemory ()
                {
                        // create vb source file
                        string sourceFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
                        using (FileStream f = new FileStream (sourceFile, FileMode.Create)) {
                                using (StreamWriter s = new StreamWriter (f)) {
-                                       s.Write (_sourceTest1);
+                                       s.Write (_sourceLibrary1);
                                        s.Close ();
                                }
                                f.Close ();
@@ -213,6 +211,9 @@ namespace MonoTests.Microsoft.VisualBasic
                        options.GenerateExecutable = false;
                        options.GenerateInMemory = true;
                        options.TempFiles = new TempFileCollection (_tempDir);
+#if NET_2_0
+                       options.EmbeddedResources.Add (sourceFile);
+#endif
 
                        ICodeCompiler compiler = _codeProvider.CreateCompiler ();
                        CompilerResults results = compiler.CompileAssemblyFromFile (options,
@@ -221,28 +222,130 @@ namespace MonoTests.Microsoft.VisualBasic
                        // verify compilation was successful
                        AssertCompileResults (results, true);
 
-                       Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
-                       Assert.IsNull (results.PathToAssembly, "#2");
-                       Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");
+                       Assembly compiledAssembly = results.CompiledAssembly;
+
+                       Assert.IsNotNull (compiledAssembly, "#1");
+                       Assert.AreEqual (string.Empty, compiledAssembly.Location, "#2");
+                       Assert.IsNull (results.PathToAssembly, "#3");
+                       Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#4");
 
                        // verify we don't cleanup files in temp directory too agressively
                        string[] tempFiles = Directory.GetFiles (_tempDir);
-                       Assert.AreEqual (1, tempFiles.Length, "#4");
-                       Assert.AreEqual (sourceFile, tempFiles[0], "#5");
+                       Assert.AreEqual (1, tempFiles.Length, "#5");
+                       Assert.AreEqual (sourceFile, tempFiles[0], "#6");
+
+#if NET_2_0
+                       string[] resources = compiledAssembly.GetManifestResourceNames();
+                       Assert.IsNotNull (resources, "#7");
+                       Assert.AreEqual (1, resources.Length, "#8");
+                       Assert.AreEqual ("file.vb", resources[0], "#9");
+                       Assert.IsNull (compiledAssembly.GetFile ("file.vb"), "#10");
+                       Assert.IsNotNull (compiledAssembly.GetManifestResourceStream  ("file.vb"), "#11");
+#endif
                }
 
                [Test]
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
+               public void CompileFromFileBatch_Executable_InMemory ()
+               {
+                       // create vb source file
+                       string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
+                       using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
+                               using (StreamWriter s = new StreamWriter (f)) {
+                                       s.Write (_sourceLibrary1);
+                                       s.Close ();
+                               }
+                               f.Close ();
+                       }
+
+                       string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
+                       using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
+                               using (StreamWriter s = new StreamWriter (f)) {
+                                       s.Write (_sourceExecutable);
+                                       s.Close ();
+                               }
+                               f.Close ();
+                       }
+
+                       CompilerParameters options = new CompilerParameters ();
+                       options.GenerateExecutable = false;
+                       options.GenerateInMemory = true;
+                       options.OutputAssembly = string.Empty;
+                       options.TempFiles = new TempFileCollection (_tempDir);
 #if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
+                       options.EmbeddedResources.Add (sourceFile1);
+                       options.LinkedResources.Add (sourceFile2);
 #endif
-               public void CompileFromFileBatch_InMemory ()
+
+                       ICodeCompiler compiler = _codeProvider.CreateCompiler ();
+                       CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
+                               new string [] { sourceFile1, sourceFile2 });
+
+                       // verify compilation was successful
+                       AssertCompileResults (results, true);
+
+                       Assembly compiledAssembly = results.CompiledAssembly;
+
+                       Assert.IsNotNull (compiledAssembly, "#A1");
+                       Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
+                       Assert.IsNull (results.PathToAssembly, "#A3");
+                       Assert.IsNotNull (options.OutputAssembly, "#A4");
+                       Assert.AreEqual (".dll", Path.GetExtension (options.OutputAssembly), "#A5");
+                       Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
+                       Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
+
+                       Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
+                       Assert.IsNotNull (compiledAssembly.GetType ("Program"), "#B2");
+
+                       // verify we don't cleanup files in temp directory too agressively
+                       string [] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (2, tempFiles.Length, "#C1");
+                       Assert.IsTrue (File.Exists (sourceFile1), "#C2");
+                       Assert.IsTrue (File.Exists (sourceFile2), "#C3");
+
+#if NET_2_0
+                       string[] resources = compiledAssembly.GetManifestResourceNames();
+                       Assert.IsNotNull (resources, "#D1");
+                       Assert.AreEqual (2, resources.Length, "#D2");
+
+                       Assert.AreEqual ("file1.vb", resources[0], "#E1");
+                       Assert.IsNull (compiledAssembly.GetFile ("file1.vb"), "#E2");
+                       Assert.IsNotNull (compiledAssembly.GetManifestResourceStream  ("file1.vb"), "#E3");
+                       ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.vb");
+                       Assert.IsNotNull (info, "#E4");
+                       Assert.IsNull (info.FileName, "#E5");
+                       Assert.IsNull (info.ReferencedAssembly, "#E6");
+                       Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
+
+                       Assert.AreEqual ("file2.vb", resources[1], "#F1");
+                       try {
+                               compiledAssembly.GetFile ("file2.vb");
+                               Assert.Fail ("#F2");
+                       } catch (FileNotFoundException) {
+                       }
+                       try {
+                               compiledAssembly.GetManifestResourceStream  ("file2.vb");
+                               Assert.Fail ("#F3");
+                       } catch (FileNotFoundException) {
+                       }
+                       info = compiledAssembly.GetManifestResourceInfo ("file2.vb");
+                       Assert.IsNotNull (info, "#F4");
+                       Assert.IsNotNull (info.FileName, "#F5");
+                       Assert.AreEqual ("file2.vb", info.FileName, "#F6");
+                       Assert.IsNull (info.ReferencedAssembly, "#F7");
+                       Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
+#endif
+               }
+
+               [Test]
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
+               public void CompileFromFileBatch_Library_InMemory ()
                {
                        // create vb source file
-                       string sourceFile1 = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
+                       string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
                        using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
                                using (StreamWriter s = new StreamWriter (f)) {
-                                       s.Write (_sourceTest1);
+                                       s.Write (_sourceLibrary1);
                                        s.Close ();
                                }
                                f.Close ();
@@ -251,7 +354,7 @@ namespace MonoTests.Microsoft.VisualBasic
                        string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
                        using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
                                using (StreamWriter s = new StreamWriter (f)) {
-                                       s.Write (_sourceTest2);
+                                       s.Write (_sourceLibrary2);
                                        s.Close ();
                                }
                                f.Close ();
@@ -261,6 +364,10 @@ namespace MonoTests.Microsoft.VisualBasic
                        options.GenerateExecutable = false;
                        options.GenerateInMemory = true;
                        options.TempFiles = new TempFileCollection (_tempDir);
+#if NET_2_0
+                       options.EmbeddedResources.Add (sourceFile1);
+                       options.LinkedResources.Add (sourceFile2);
+#endif
 
                        ICodeCompiler compiler = _codeProvider.CreateCompiler ();
                        CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
@@ -269,24 +376,61 @@ namespace MonoTests.Microsoft.VisualBasic
                        // verify compilation was successful
                        AssertCompileResults (results, true);
 
-                       Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
-                       Assert.IsNull (results.PathToAssembly, "#2");
+                       Assembly compiledAssembly = results.CompiledAssembly;
 
-                       Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");
-                       Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#4");
+                       Assert.IsNotNull (compiledAssembly, "#A1");
+                       Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
+                       Assert.IsNull (results.PathToAssembly, "#A3");
+                       Assert.IsNotNull (options.OutputAssembly, "#A4");
+                       Assert.AreEqual (".dll", Path.GetExtension (options.OutputAssembly), "#A5");
+                       Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
+                       Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
+
+                       Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
+                       Assert.IsNotNull (compiledAssembly.GetType ("Test2"), "#B2");
 
                        // verify we don't cleanup files in temp directory too agressively
                        string[] tempFiles = Directory.GetFiles (_tempDir);
-                       Assert.AreEqual (2, tempFiles.Length, "#5");
-                       Assert.IsTrue (File.Exists (sourceFile1), "#6");
-                       Assert.IsTrue (File.Exists (sourceFile2), "#7");
-               }
+                       Assert.AreEqual (2, tempFiles.Length, "#C1");
+                       Assert.IsTrue (File.Exists (sourceFile1), "#C2");
+                       Assert.IsTrue (File.Exists (sourceFile2), "#C3");
 
-               [Test]
 #if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
+                       string[] resources = compiledAssembly.GetManifestResourceNames();
+                       Assert.IsNotNull (resources, "#D1");
+                       Assert.AreEqual (2, resources.Length, "#D2");
+
+                       Assert.AreEqual ("file1.vb", resources[0], "#E1");
+                       Assert.IsNull (compiledAssembly.GetFile ("file1.vb"), "#E2");
+                       Assert.IsNotNull (compiledAssembly.GetManifestResourceStream  ("file1.vb"), "#E3");
+                       ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.vb");
+                       Assert.IsNotNull (info, "#E4");
+                       Assert.IsNull (info.FileName, "#E5");
+                       Assert.IsNull (info.ReferencedAssembly, "#E6");
+                       Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
+
+                       Assert.AreEqual ("file2.vb", resources[1], "#F1");
+                       try {
+                               compiledAssembly.GetFile ("file2.vb");
+                               Assert.Fail ("#F2");
+                       } catch (FileNotFoundException) {
+                       }
+                       try {
+                               compiledAssembly.GetManifestResourceStream  ("file2.vb");
+                               Assert.Fail ("#F3");
+                       } catch (FileNotFoundException) {
+                       }
+                       info = compiledAssembly.GetManifestResourceInfo ("file2.vb");
+                       Assert.IsNotNull (info, "#F4");
+                       Assert.IsNotNull (info.FileName, "#F5");
+                       Assert.AreEqual ("file2.vb", info.FileName, "#F6");
+                       Assert.IsNull (info.ReferencedAssembly, "#F7");
+                       Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
 #endif
+               }
+
+               [Test]
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CompileFromSource_InMemory ()
                {
                        // create a file in temp directory to ensure that compiler is not removing
@@ -303,7 +447,7 @@ namespace MonoTests.Microsoft.VisualBasic
 
                        ICodeCompiler compiler = _codeProvider.CreateCompiler ();
                        CompilerResults results = compiler.CompileAssemblyFromSource (options,
-                               _sourceTest1);
+                               _sourceLibrary1);
 
                        // verify compilation was successful
                        AssertCompileResults (results, true);
@@ -319,10 +463,7 @@ namespace MonoTests.Microsoft.VisualBasic
                }
 
                [Test]
-#if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
-#endif
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CompileFromSourceBatch_InMemory ()
                {
                        // create a file in temp directory to ensure that compiler is not removing
@@ -332,35 +473,39 @@ namespace MonoTests.Microsoft.VisualBasic
                                fs.Close ();
                        }
 
+                       string outputAssembly = Path.Combine (_tempDir, "sourcebatch.dll");
+
                        CompilerParameters options = new CompilerParameters ();
                        options.GenerateExecutable = false;
                        options.GenerateInMemory = true;
+                       options.OutputAssembly = outputAssembly;
                        options.TempFiles = new TempFileCollection (_tempDir);
 
                        ICodeCompiler compiler = _codeProvider.CreateCompiler ();
                        CompilerResults results = compiler.CompileAssemblyFromSourceBatch (options,
-                               new string[] { _sourceTest1, _sourceTest2 });
+                               new string[] { _sourceLibrary1, _sourceLibrary2 });
 
                        // verify compilation was successful
                        AssertCompileResults (results, true);
 
-                       Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
-                       Assert.IsNull (results.PathToAssembly, "#2");
+                       Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#A1");
+                       Assert.IsNull (results.PathToAssembly, "#A2");
+                       Assert.IsNotNull (options.OutputAssembly, "#A3");
+                       Assert.AreEqual (outputAssembly, options.OutputAssembly, "#A4");
+                       Assert.IsTrue (File.Exists (outputAssembly), "#A5");
 
-                       Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");
-                       Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#4");
+                       Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#B1");
+                       Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#B2");
 
                        // verify we don't cleanup files in temp directory too agressively
                        string[] tempFiles = Directory.GetFiles (_tempDir);
-                       Assert.AreEqual (1, tempFiles.Length, "#5");
-                       Assert.AreEqual (tempFile, tempFiles[0], "#6");
+                       Assert.AreEqual (2, tempFiles.Length, "#C1");
+                       Assert.AreEqual (tempFile, tempFiles[0], "#C2");
+                       Assert.AreEqual (outputAssembly, tempFiles [1], "#C3");
                }
 
                [Test]
-#if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
-#endif
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CompileFromDom_NotInMemory ()
                {
                        // create a file in temp directory to ensure that compiler is not removing
@@ -390,10 +535,7 @@ namespace MonoTests.Microsoft.VisualBasic
                }
 
                [Test]
-#if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
-#endif
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CompileFromDomBatch_NotInMemory ()
                {
                        // create a file in temp directory to ensure that compiler is not removing
@@ -423,10 +565,7 @@ namespace MonoTests.Microsoft.VisualBasic
                }
 
                [Test]
-#if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
-#endif
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CompileFromDom_InMemory ()
                {
                        // create a file in temp directory to ensure that compiler is not removing
@@ -457,10 +596,7 @@ namespace MonoTests.Microsoft.VisualBasic
                }
 
                [Test]
-#if NET_2_0
-               // there's not yet an mbas for the 2.0 profile
-               [Category ("NotWorking")]
-#endif
+               [Category ("NotWorking")] // we cannot rely on vbnc being available
                public void CompileFromDomBatch_InMemory ()
                {
                        // create a file in temp directory to ensure that compiler is not removing
@@ -562,7 +698,7 @@ namespace MonoTests.Microsoft.VisualBasic
                                        continue;
                                }
 
-                               Assert.Fail (compilerError.ToString ());
+                               throw new Exception (compilerError.ToString ());
                        }
                }
 
@@ -579,9 +715,11 @@ namespace MonoTests.Microsoft.VisualBasic
                        return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
                                testerType.Assembly.FullName, testerType.FullName, false,
                                BindingFlags.Public | BindingFlags.Instance, null, new object[0],
-                               CultureInfo.InvariantCulture, new object[0], domain.Evidence);
+                               CultureInfo.InvariantCulture, new object[0], null);
                }
 
+               // do not use the Assert class as this will introduce failures if the
+               // nunit.framework assembly is not in the GAC
                private class CrossDomainTester : MarshalByRefObject
                {
                        public string CompileAssemblyFromDom (string tempDir)
@@ -598,9 +736,10 @@ namespace MonoTests.Microsoft.VisualBasic
                                // verify compilation was successful
                                AssertCompileResults (results, true);
 
-                               Assert.IsTrue (results.CompiledAssembly.Location.Length != 0,
-                                       "Location should not be empty string");
-                               Assert.IsNotNull (results.PathToAssembly, "PathToAssembly should not be null");
+                               if (results.CompiledAssembly.Location.Length == 0)
+                                       throw new Exception ("Location should not be empty string");
+                               if (results.PathToAssembly == null)
+                                       throw new Exception ("PathToAssembly should not be null");
 
                                return results.PathToAssembly;
                        }
@@ -619,9 +758,10 @@ namespace MonoTests.Microsoft.VisualBasic
                                // verify compilation was successful
                                AssertCompileResults (results, true);
 
-                               Assert.IsTrue (results.CompiledAssembly.Location.Length != 0,
-                                       "Location should not be empty string");
-                               Assert.IsNotNull (results.PathToAssembly, "PathToAssembly should not be null");
+                               if (results.CompiledAssembly.Location.Length == 0)
+                                       throw new Exception ("Location should not be empty string");
+                               if (results.PathToAssembly == null)
+                                       throw new Exception ("PathToAssembly should not be null");
 
                                return results.PathToAssembly;
                        }