-//\r
-// Microsoft.VisualBasic.VBCodeProvider.cs\r
-//\r
-// Author:\r
-// Jochen Wezel (jwezel@compumaster.de)\r
-//\r
-// (C) 2003 Jochen Wezel (CompuMaster GmbH)\r
-//\r
-// Last modifications:\r
-// 2003-12-10 JW: publishing of this file\r
-//\r
-\r
-using System;\r
-using System.CodeDom;\r
-using System.CodeDom.Compiler;\r
-using System.ComponentModel;\r
-using System.Collections.Specialized;\r
-using System.Globalization;\r
-using System.Reflection;\r
-using System.Diagnostics;\r
-using System.IO;\r
-using Microsoft.VisualBasic;\r
-using NUnit.Framework;\r
-\r
-namespace MonoTests.Microsoft.VisualBasic\r
-{\r
- enum OsType\r
- {\r
- Windows,\r
- Unix,\r
- Mac\r
- }\r
-\r
- [TestFixture]\r
- public class VBCodeProviderTest\r
- {\r
- private string _tempDir;\r
- private CodeDomProvider _codeProvider;\r
- private static OsType OS;\r
- private static char DSC = Path.DirectorySeparatorChar;\r
-\r
- private static readonly string _sourceTest1 = "Public Class Test1" +\r
- Environment.NewLine + "End Class";\r
- private static readonly string _sourceTest2 = "Public Class Test2" +\r
- Environment.NewLine + "End Class";\r
-\r
- [SetUp]\r
- public void GetReady ()\r
- {\r
- if ('/' == DSC) {\r
- OS = OsType.Unix;\r
- } else if ('\\' == DSC) {\r
- OS = OsType.Windows;\r
- } else {\r
- OS = OsType.Mac;\r
- }\r
-\r
- _codeProvider = new VBCodeProvider ();\r
- _tempDir = CreateTempDirectory ();\r
- }\r
-\r
- [TearDown]\r
- public void TearDown ()\r
- {\r
- RemoveDirectory (_tempDir);\r
- }\r
-\r
- [Test]\r
- public void FileExtension ()\r
- {\r
- Assert.AreEqual ("vb", _codeProvider.FileExtension, "#JW10");\r
- }\r
-\r
- [Test]\r
- public void LanguageOptionsTest ()\r
- {\r
- Assert.AreEqual (LanguageOptions.CaseInsensitive, _codeProvider.LanguageOptions, "#JW20");\r
- }\r
-\r
- [Test]\r
- public void CreateCompiler ()\r
- {\r
- // Prepare the compilation\r
- ICodeCompiler codeCompiler = _codeProvider.CreateCompiler ();\r
- Assert.IsNotNull (codeCompiler, "#JW30 - CreateCompiler");\r
-\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = true;\r
- options.IncludeDebugInformation = true;\r
- options.TreatWarningsAsErrors = true;\r
-\r
- // process compilation\r
- CompilerResults compilerResults = codeCompiler.CompileAssemblyFromSource (options,\r
- "public class TestModule" + Environment.NewLine + "public shared sub Main()"\r
- + Environment.NewLine + "System.Console.Write(\"Hello world!\")"\r
- + Environment.NewLine + "End Sub" + Environment.NewLine + "End Class");\r
-\r
- // Analyse the compilation success/messages\r
- StringCollection MyOutput = compilerResults.Output;\r
- string MyOutStr = "";\r
- foreach (string MyStr in MyOutput) {\r
- MyOutStr += MyStr + Environment.NewLine + Environment.NewLine;\r
- }\r
-\r
- if (compilerResults.Errors.Count != 0) {\r
- Assert.Fail ("#JW31 - Hello world compilation: " + MyOutStr);\r
- }\r
-\r
- try {\r
- Assembly MyAss = compilerResults.CompiledAssembly;\r
- } catch (Exception ex) {\r
- Assert.Fail ("#JW32 - compilerResults.CompiledAssembly hasn't been an expected object" +\r
- Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);\r
- }\r
-\r
- // Execute the test app\r
- ProcessStartInfo NewProcInfo = new ProcessStartInfo ();\r
- if (Windows) {\r
- NewProcInfo.FileName = compilerResults.CompiledAssembly.Location;\r
- } else {\r
- NewProcInfo.FileName = "mono";\r
- NewProcInfo.Arguments = compilerResults.CompiledAssembly.Location;\r
- }\r
- NewProcInfo.RedirectStandardOutput = true;\r
- NewProcInfo.UseShellExecute = false;\r
- NewProcInfo.CreateNoWindow = true;\r
- string TestAppOutput = "";\r
- try {\r
- Process MyProc = Process.Start (NewProcInfo);\r
- MyProc.WaitForExit ();\r
- TestAppOutput = MyProc.StandardOutput.ReadToEnd ();\r
- MyProc.Close ();\r
- MyProc.Dispose ();\r
- } catch (Exception ex) {\r
- Assert.Fail ("#JW34 - " + ex.Message + Environment.NewLine + ex.StackTrace);\r
- }\r
- Assert.AreEqual ("Hello world!", TestAppOutput, "#JW33 - Application output");\r
-\r
- // Clean up\r
- try {\r
- File.Delete (NewProcInfo.FileName);\r
- } catch { }\r
- }\r
-\r
- [Test]\r
- public void CreateGenerator ()\r
- {\r
- ICodeGenerator MyVBCodeGen;\r
- MyVBCodeGen = _codeProvider.CreateGenerator ();\r
- Assert.IsNotNull (MyVBCodeGen, "#JW40 - CreateGenerator");\r
- Assert.IsTrue (MyVBCodeGen.Supports (GeneratorSupport.DeclareEnums), "#JW41");\r
- }\r
-\r
- [Test]\r
- public void CompileFromFile_InMemory ()\r
- {\r
- // create vb source file\r
- string sourceFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream f = new FileStream (sourceFile, FileMode.Create)) {\r
- using (StreamWriter s = new StreamWriter (f)) {\r
- s.Write (_sourceTest1);\r
- s.Close ();\r
- }\r
- f.Close ();\r
- }\r
-\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = true;\r
- options.TempFiles = new TempFileCollection (_tempDir);\r
-\r
- ICodeCompiler compiler = _codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromFile (options,\r
- sourceFile);\r
-\r
- Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");\r
- Assert.IsNull (results.PathToAssembly, "#2");\r
- Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");\r
-\r
- // verify we don't cleanup files in temp directory too agressively\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
-\r
- foreach (string file in tempFiles) {\r
- if (file != sourceFile) {\r
- Assert.Fail ("TEMPFILE: " + file);\r
- }\r
- }\r
-\r
- Assert.AreEqual (1, tempFiles.Length, "#4");\r
- Assert.AreEqual (sourceFile, tempFiles[0], "#5");\r
-\r
- }\r
-\r
- [Test]\r
- public void CompileFromFileBatch_InMemory ()\r
- {\r
- // create vb source file\r
- string sourceFile1 = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {\r
- using (StreamWriter s = new StreamWriter (f)) {\r
- s.Write (_sourceTest1);\r
- s.Close ();\r
- }\r
- f.Close ();\r
- }\r
-\r
- string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);\r
- using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {\r
- using (StreamWriter s = new StreamWriter (f)) {\r
- s.Write (_sourceTest2);\r
- s.Close ();\r
- }\r
- f.Close ();\r
- }\r
-\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = true;\r
- options.TempFiles = new TempFileCollection (_tempDir);\r
-\r
- ICodeCompiler compiler = _codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,\r
- new string[] { sourceFile1, sourceFile2 });\r
-\r
- Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");\r
- Assert.IsNull (results.PathToAssembly, "#2");\r
-\r
- Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");\r
- Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#4");\r
-\r
- // verify we don't cleanup files in temp directory too agressively\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
- Assert.AreEqual (2, tempFiles.Length, "#5");\r
- Assert.IsTrue (File.Exists (sourceFile1), "#6");\r
- Assert.IsTrue (File.Exists (sourceFile2), "#7");\r
- }\r
-\r
- [Test]\r
- public void CompileFromSource_InMemory ()\r
- {\r
- // create a file in temp directory to ensure that compiler is not removing\r
- // too much (temporary) files\r
- string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream fs = File.Create (tempFile)) {\r
- fs.Close ();\r
- }\r
-\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = true;\r
- options.TempFiles = new TempFileCollection (_tempDir);\r
-\r
- ICodeCompiler compiler = _codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromSource (options,\r
- _sourceTest1);\r
-\r
- Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");\r
- Assert.IsNull (results.PathToAssembly, "#2");\r
- Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");\r
-\r
- // verify we don't cleanup files in temp directory too agressively\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
- Assert.AreEqual (1, tempFiles.Length, "#4");\r
- Assert.AreEqual (tempFile, tempFiles[0], "#5");\r
- }\r
-\r
- [Test]\r
- public void CompileFromSourceBatch_InMemory ()\r
- {\r
- // create a file in temp directory to ensure that compiler is not removing\r
- // too much (temporary) files\r
- string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream fs = File.Create (tempFile)) {\r
- fs.Close ();\r
- }\r
-\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = true;\r
- options.TempFiles = new TempFileCollection (_tempDir);\r
-\r
- ICodeCompiler compiler = _codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromSourceBatch (options,\r
- new string[] { _sourceTest1, _sourceTest2 });\r
-\r
- Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");\r
- Assert.IsNull (results.PathToAssembly, "#2");\r
-\r
- Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");\r
- Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#4");\r
-\r
- // verify we don't cleanup files in temp directory too agressively\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
- Assert.AreEqual (1, tempFiles.Length, "#5");\r
- Assert.AreEqual (tempFile, tempFiles[0], "#6");\r
- }\r
-\r
- [Test]\r
- public void CompileFromDom_NotInMemory ()\r
- {\r
- // create a file in temp directory to ensure that compiler is not removing\r
- // too much (temporary) files\r
- string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream fs = File.Create (tempFile)) {\r
- fs.Close ();\r
- }\r
-\r
- // compile and verify result in separate appdomain to avoid file locks\r
- AppDomain testDomain = CreateTestDomain ();\r
- CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);\r
-\r
- string outputAssembly = null;\r
-\r
- try {\r
- outputAssembly = compileTester.CompileAssemblyFromDom (_tempDir);\r
- } finally {\r
- AppDomain.Unload (testDomain);\r
- }\r
-\r
- // there should be two files in temp dir: temp file and output assembly\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
- Assert.AreEqual (2, tempFiles.Length, "#1");\r
- Assert.IsTrue (File.Exists (outputAssembly), "#2");\r
- Assert.IsTrue (File.Exists (tempFile), "#3");\r
- }\r
-\r
- [Test]\r
- public void CompileFromDomBatch_NotInMemory ()\r
- {\r
- // create a file in temp directory to ensure that compiler is not removing\r
- // too much (temporary) files\r
- string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream fs = File.Create (tempFile)) {\r
- fs.Close ();\r
- }\r
-\r
- // compile and verify result in separate appdomain to avoid file locks\r
- AppDomain testDomain = CreateTestDomain ();\r
- CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);\r
-\r
- string outputAssembly = null;\r
-\r
- try {\r
- outputAssembly = compileTester.CompileAssemblyFromDomBatch (_tempDir);\r
- } finally {\r
- AppDomain.Unload (testDomain);\r
- }\r
-\r
- // there should be two files in temp dir: temp file and output assembly\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
- Assert.AreEqual (2, tempFiles.Length, "#1");\r
- Assert.IsTrue (File.Exists (outputAssembly), "#2");\r
- Assert.IsTrue (File.Exists (tempFile), "#3");\r
- }\r
-\r
- [Test]\r
- public void CompileFromDom_InMemory ()\r
- {\r
- // create a file in temp directory to ensure that compiler is not removing\r
- // too much (temporary) files\r
- string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream fs = File.Create (tempFile)) {\r
- fs.Close ();\r
- }\r
-\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = true;\r
- options.TempFiles = new TempFileCollection (_tempDir);\r
-\r
- ICodeCompiler compiler = _codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());\r
-\r
- Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");\r
- Assert.IsNull (results.PathToAssembly, "#2");\r
-\r
- // verify we don't cleanup files in temp directory too agressively\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
- Assert.AreEqual (1, tempFiles.Length, "#3");\r
- Assert.AreEqual (tempFile, tempFiles[0], "#4");\r
- }\r
-\r
- [Test]\r
- public void CompileFromDomBatch_InMemory ()\r
- {\r
- // create a file in temp directory to ensure that compiler is not removing\r
- // too much (temporary) files\r
- string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);\r
- using (FileStream fs = File.Create (tempFile)) {\r
- fs.Close ();\r
- }\r
-\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = true;\r
- options.TempFiles = new TempFileCollection (_tempDir);\r
-\r
- ICodeCompiler compiler = _codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromDomBatch (options,\r
- new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });\r
-\r
- Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");\r
- Assert.IsNull (results.PathToAssembly, "#2");\r
-\r
- // verify we don't cleanup files in temp directory too agressively\r
- string[] tempFiles = Directory.GetFiles (_tempDir);\r
- Assert.AreEqual (1, tempFiles.Length, "#3");\r
- Assert.AreEqual (tempFile, tempFiles[0], "#4");\r
- }\r
-\r
- //TODO: [Test]\r
- public void CreateParser ()\r
- {\r
- //System.CodeDom.Compiler.ICodeParser CreateParser()\r
- }\r
-\r
- //TODO: [Test]\r
- public void CreateObjRef ()\r
- {\r
- //System.Runtime.Remoting.ObjRef CreateObjRef(System.Type requestedType)\r
- }\r
-\r
- bool Windows\r
- {\r
- get {\r
- return OS == OsType.Windows;\r
- }\r
- }\r
-\r
- bool Unix\r
- {\r
- get {\r
- return OS == OsType.Unix;\r
- }\r
- }\r
-\r
- bool Mac\r
- {\r
- get {\r
- return OS == OsType.Mac;\r
- }\r
- }\r
-\r
- private static string CreateTempDirectory ()\r
- {\r
- // create a uniquely named zero-byte file\r
- string tempFile = Path.GetTempFileName ();\r
- // remove the temporary file\r
- File.Delete (tempFile);\r
- // create a directory named after the unique temporary file\r
- Directory.CreateDirectory (tempFile);\r
- // return the path to the temporary directory\r
- return tempFile;\r
- }\r
-\r
- private static void RemoveDirectory (string path)\r
- {\r
- try {\r
- if (Directory.Exists (path)) {\r
- string[] directoryNames = Directory.GetDirectories (path);\r
- foreach (string directoryName in directoryNames) {\r
- RemoveDirectory (directoryName);\r
- }\r
- string[] fileNames = Directory.GetFiles (path);\r
- foreach (string fileName in fileNames) {\r
- File.Delete (fileName);\r
- }\r
- Directory.Delete (path, true);\r
- }\r
- } catch (Exception ex) {\r
- throw new AssertionException ("Unable to cleanup '" + path + "'.", ex);\r
- }\r
- }\r
-\r
- private static AppDomain CreateTestDomain ()\r
- {\r
- return AppDomain.CreateDomain ("CompileFromDom", AppDomain.CurrentDomain.Evidence,\r
- AppDomain.CurrentDomain.SetupInformation);\r
- }\r
-\r
- private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)\r
- {\r
- Type testerType = typeof (CrossDomainTester);\r
-\r
- return (CrossDomainTester) domain.CreateInstanceAndUnwrap (\r
- testerType.Assembly.FullName, testerType.FullName, false,\r
- BindingFlags.Public | BindingFlags.Instance, null, new object[0],\r
- CultureInfo.InvariantCulture, new object[0], domain.Evidence);\r
- }\r
-\r
- private class CrossDomainTester : MarshalByRefObject\r
- {\r
- public string CompileAssemblyFromDom (string tempDir)\r
- {\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = false;\r
- options.TempFiles = new TempFileCollection (tempDir);\r
-\r
- VBCodeProvider codeProvider = new VBCodeProvider ();\r
- ICodeCompiler compiler = codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());\r
-\r
- Assert.IsTrue (results.CompiledAssembly.Location.Length != 0,\r
- "Location should not be empty string");\r
- Assert.IsNotNull (results.PathToAssembly, "PathToAssembly should not be null");\r
-\r
- return results.PathToAssembly;\r
- }\r
-\r
- public string CompileAssemblyFromDomBatch (string tempDir)\r
- {\r
- CompilerParameters options = new CompilerParameters ();\r
- options.GenerateExecutable = false;\r
- options.GenerateInMemory = false;\r
- options.TempFiles = new TempFileCollection (tempDir);\r
-\r
- VBCodeProvider codeProvider = new VBCodeProvider ();\r
- ICodeCompiler compiler = codeProvider.CreateCompiler ();\r
- CompilerResults results = compiler.CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });\r
-\r
- Assert.IsTrue (results.CompiledAssembly.Location.Length != 0,\r
- "Location should not be empty string");\r
- Assert.IsNotNull (results.PathToAssembly, "PathToAssembly should not be null");\r
-\r
- return results.PathToAssembly;\r
- }\r
- }\r
- }\r
-}\r
+//
+// Microsoft.VisualBasic.VBCodeProvider.cs
+//
+// Author:
+// Jochen Wezel (jwezel@compumaster.de)
+//
+// (C) 2003 Jochen Wezel (CompuMaster GmbH)
+//
+// Last modifications:
+// 2003-12-10 JW: publishing of this file
+//
+
+using System;
+using System.CodeDom;
+using System.CodeDom.Compiler;
+using System.Collections.Specialized;
+using System.Globalization;
+using System.Reflection;
+using System.Diagnostics;
+using System.IO;
+using System.Text;
+using Microsoft.VisualBasic;
+using NUnit.Framework;
+
+namespace MonoTests.Microsoft.VisualBasic
+{
+ enum OsType
+ {
+ Windows,
+ Unix,
+ Mac
+ }
+
+ [TestFixture]
+ [Category ("NotWorking")] // we cannot rely on vbnc being available
+ public class VBCodeProviderTest
+ {
+ private string _tempDir;
+ private CodeDomProvider _codeProvider;
+ private static OsType OS;
+ private static char DSC = Path.DirectorySeparatorChar;
+
+ private static readonly string _sourceLibrary1 = "Public Class Test1" +
+ Environment.NewLine + "End Class";
+ 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 ()
+ {
+ if ('/' == DSC) {
+ OS = OsType.Unix;
+ } else if ('\\' == DSC) {
+ OS = OsType.Windows;
+ } else {
+ OS = OsType.Mac;
+ }
+
+ _codeProvider = new VBCodeProvider ();
+ _tempDir = CreateTempDirectory ();
+ }
+
+ [TearDown]
+ public void TearDown ()
+ {
+ RemoveDirectory (_tempDir);
+ }
+
+ [Test]
+ public void FileExtension ()
+ {
+ Assert.AreEqual ("vb", _codeProvider.FileExtension, "#JW10");
+ }
+
+ [Test]
+ public void LanguageOptionsTest ()
+ {
+ Assert.AreEqual (LanguageOptions.CaseInsensitive, _codeProvider.LanguageOptions, "#JW20");
+ }
+
+ [Test]
+ public void GeneratorSupports ()
+ {
+ ICodeGenerator codeGenerator = _codeProvider.CreateGenerator ();
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#1");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ArraysOfArrays), "#2");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.AssemblyAttributes), "#3");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ChainedConstructorArguments), "#4");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ComplexExpressions), "#5");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareDelegates), "#6");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#7");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEvents), "#8");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareInterfaces), "#9");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareValueTypes), "#10");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.EntryPointMethod), "#11");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GotoStatements), "#12");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultidimensionalArrays), "#13");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultipleInterfaceMembers), "#14");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.NestedTypes), "#15");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ParameterAttributes), "#16");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PublicStaticMembers), "#17");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReferenceParameters), "#18");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReturnTypeAttributes), "#19");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.StaticConstructors), "#20");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.TryCatchStatements), "#21");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Win32Resources), "#22");
+#if NET_2_0
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareIndexerProperties), "#23");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeDeclaration), "#24");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeReference), "#25");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PartialTypes), "#26");
+ Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Resources), "#27");
+#endif
+ }
+
+ [Test]
+ [Category ("NotWorking")] // we cannot rely on vbnc being available
+ public void CreateCompiler ()
+ {
+ // Prepare the compilation
+ ICodeCompiler codeCompiler = _codeProvider.CreateCompiler ();
+ Assert.IsNotNull (codeCompiler, "#JW30 - CreateCompiler");
+
+ CompilerParameters options = new CompilerParameters ();
+ options.GenerateExecutable = true;
+ options.IncludeDebugInformation = true;
+ options.TreatWarningsAsErrors = true;
+
+ // process compilation
+ CompilerResults compilerResults = codeCompiler.CompileAssemblyFromSource (options,
+ "public class TestModule" + Environment.NewLine + "public shared sub Main()"
+ + Environment.NewLine + "System.Console.Write(\"Hello world!\")"
+ + Environment.NewLine + "End Sub" + Environment.NewLine + "End Class");
+
+ // Analyse the compilation success/messages
+ StringCollection MyOutput = compilerResults.Output;
+ string MyOutStr = "";
+ foreach (string MyStr in MyOutput) {
+ MyOutStr += MyStr + Environment.NewLine + Environment.NewLine;
+ }
+
+ if (compilerResults.Errors.Count != 0) {
+ Assert.Fail ("#JW31 - Hello world compilation: " + MyOutStr);
+ }
+
+ try {
+ Assembly MyAss = compilerResults.CompiledAssembly;
+ } catch (Exception ex) {
+ Assert.Fail ("#JW32 - compilerResults.CompiledAssembly hasn't been an expected object" +
+ Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
+ }
+
+ // Execute the test app
+ ProcessStartInfo NewProcInfo = new ProcessStartInfo ();
+ if (Windows) {
+ NewProcInfo.FileName = compilerResults.CompiledAssembly.Location;
+ } else {
+ NewProcInfo.FileName = "mono";
+ NewProcInfo.Arguments = compilerResults.CompiledAssembly.Location;
+ }
+ NewProcInfo.RedirectStandardOutput = true;
+ NewProcInfo.UseShellExecute = false;
+ NewProcInfo.CreateNoWindow = true;
+ string TestAppOutput = "";
+ try {
+ Process MyProc = Process.Start (NewProcInfo);
+ MyProc.WaitForExit ();
+ TestAppOutput = MyProc.StandardOutput.ReadToEnd ();
+ MyProc.Close ();
+ MyProc.Dispose ();
+ } catch (Exception ex) {
+ Assert.Fail ("#JW34 - " + ex.Message + Environment.NewLine + ex.StackTrace);
+ }
+ Assert.AreEqual ("Hello world!", TestAppOutput, "#JW33 - Application output");
+
+ // Clean up
+ try {
+ File.Delete (NewProcInfo.FileName);
+ } catch { }
+ }
+
+ [Test]
+ public void CreateGenerator ()
+ {
+ ICodeGenerator MyVBCodeGen;
+ MyVBCodeGen = _codeProvider.CreateGenerator ();
+ Assert.IsNotNull (MyVBCodeGen, "#JW40 - CreateGenerator");
+ Assert.IsTrue (MyVBCodeGen.Supports (GeneratorSupport.DeclareEnums), "#JW41");
+ }
+
+ [Test]
+ [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 (_sourceLibrary1);
+ s.Close ();
+ }
+ f.Close ();
+ }
+
+ CompilerParameters options = new CompilerParameters ();
+ 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,
+ sourceFile);
+
+ // verify compilation was successful
+ AssertCompileResults (results, true);
+
+ 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, "#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
+ options.EmbeddedResources.Add (sourceFile1);
+ options.LinkedResources.Add (sourceFile2);
+#endif
+
+ 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, "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 (_sourceLibrary2);
+ s.Close ();
+ }
+ f.Close ();
+ }
+
+ CompilerParameters options = new CompilerParameters ();
+ 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,
+ 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 ("Test2"), "#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 CompileFromSource_InMemory ()
+ {
+ // create a file in temp directory to ensure that compiler is not removing
+ // too much (temporary) files
+ string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
+ using (FileStream fs = File.Create (tempFile)) {
+ fs.Close ();
+ }
+
+ CompilerParameters options = new CompilerParameters ();
+ options.GenerateExecutable = false;
+ options.GenerateInMemory = true;
+ options.TempFiles = new TempFileCollection (_tempDir);
+
+ ICodeCompiler compiler = _codeProvider.CreateCompiler ();
+ CompilerResults results = compiler.CompileAssemblyFromSource (options,
+ _sourceLibrary1);
+
+ // 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");
+
+ // verify we don't cleanup files in temp directory too agressively
+ string[] tempFiles = Directory.GetFiles (_tempDir);
+ Assert.AreEqual (1, tempFiles.Length, "#4");
+ Assert.AreEqual (tempFile, tempFiles[0], "#5");
+ }
+
+ [Test]
+ [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
+ // too much (temporary) files
+ string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
+ using (FileStream fs = File.Create (tempFile)) {
+ 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[] { _sourceLibrary1, _sourceLibrary2 });
+
+ // verify compilation was successful
+ AssertCompileResults (results, true);
+
+ 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"), "#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 (2, tempFiles.Length, "#C1");
+ Assert.AreEqual (tempFile, tempFiles[0], "#C2");
+ Assert.AreEqual (outputAssembly, tempFiles [1], "#C3");
+ }
+
+ [Test]
+ [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
+ // too much (temporary) files
+ string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
+ using (FileStream fs = File.Create (tempFile)) {
+ fs.Close ();
+ }
+
+ // compile and verify result in separate appdomain to avoid file locks
+ AppDomain testDomain = CreateTestDomain ();
+ CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
+
+ string outputAssembly = null;
+
+ try {
+ outputAssembly = compileTester.CompileAssemblyFromDom (_tempDir);
+ } finally {
+ AppDomain.Unload (testDomain);
+ }
+
+ // there should be two files in temp dir: temp file and output assembly
+ string[] tempFiles = Directory.GetFiles (_tempDir);
+ Assert.AreEqual (2, tempFiles.Length, "#1");
+ Assert.IsTrue (File.Exists (outputAssembly), "#2");
+ Assert.IsTrue (File.Exists (tempFile), "#3");
+ }
+
+ [Test]
+ [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
+ // too much (temporary) files
+ string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
+ using (FileStream fs = File.Create (tempFile)) {
+ fs.Close ();
+ }
+
+ // compile and verify result in separate appdomain to avoid file locks
+ AppDomain testDomain = CreateTestDomain ();
+ CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
+
+ string outputAssembly = null;
+
+ try {
+ outputAssembly = compileTester.CompileAssemblyFromDomBatch (_tempDir);
+ } finally {
+ AppDomain.Unload (testDomain);
+ }
+
+ // there should be two files in temp dir: temp file and output assembly
+ string[] tempFiles = Directory.GetFiles (_tempDir);
+ Assert.AreEqual (2, tempFiles.Length, "#1");
+ Assert.IsTrue (File.Exists (outputAssembly), "#2");
+ Assert.IsTrue (File.Exists (tempFile), "#3");
+ }
+
+ [Test]
+ [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
+ // too much (temporary) files
+ string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
+ using (FileStream fs = File.Create (tempFile)) {
+ fs.Close ();
+ }
+
+ CompilerParameters options = new CompilerParameters ();
+ options.GenerateExecutable = false;
+ options.GenerateInMemory = true;
+ options.TempFiles = new TempFileCollection (_tempDir);
+
+ ICodeCompiler compiler = _codeProvider.CreateCompiler ();
+ CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
+
+ // verify compilation was successful
+ AssertCompileResults (results, true);
+
+ Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
+ Assert.IsNull (results.PathToAssembly, "#2");
+
+ // verify we don't cleanup files in temp directory too agressively
+ string[] tempFiles = Directory.GetFiles (_tempDir);
+ Assert.AreEqual (1, tempFiles.Length, "#3");
+ Assert.AreEqual (tempFile, tempFiles[0], "#4");
+ }
+
+ [Test]
+ [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
+ // too much (temporary) files
+ string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
+ using (FileStream fs = File.Create (tempFile)) {
+ fs.Close ();
+ }
+
+ CompilerParameters options = new CompilerParameters ();
+ options.GenerateExecutable = false;
+ options.GenerateInMemory = true;
+ options.TempFiles = new TempFileCollection (_tempDir);
+
+ ICodeCompiler compiler = _codeProvider.CreateCompiler ();
+ CompilerResults results = compiler.CompileAssemblyFromDomBatch (options,
+ new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
+
+ // verify compilation was successful
+ AssertCompileResults (results, true);
+
+ Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
+ Assert.IsNull (results.PathToAssembly, "#2");
+
+ // verify we don't cleanup files in temp directory too agressively
+ string[] tempFiles = Directory.GetFiles (_tempDir);
+ Assert.AreEqual (1, tempFiles.Length, "#3");
+ Assert.AreEqual (tempFile, tempFiles[0], "#4");
+ }
+
+ //TODO: [Test]
+ public void CreateParser ()
+ {
+ //System.CodeDom.Compiler.ICodeParser CreateParser()
+ }
+
+ //TODO: [Test]
+ public void CreateObjRef ()
+ {
+ //System.Runtime.Remoting.ObjRef CreateObjRef(System.Type requestedType)
+ }
+
+ bool Windows
+ {
+ get {
+ return OS == OsType.Windows;
+ }
+ }
+
+ bool Unix
+ {
+ get {
+ return OS == OsType.Unix;
+ }
+ }
+
+ bool Mac
+ {
+ get {
+ return OS == OsType.Mac;
+ }
+ }
+
+ private static string CreateTempDirectory ()
+ {
+ // create a uniquely named zero-byte file
+ string tempFile = Path.GetTempFileName ();
+ // remove the temporary file
+ File.Delete (tempFile);
+ // create a directory named after the unique temporary file
+ Directory.CreateDirectory (tempFile);
+ // return the path to the temporary directory
+ return tempFile;
+ }
+
+ private static void RemoveDirectory (string path)
+ {
+ try {
+ if (Directory.Exists (path)) {
+ string[] directoryNames = Directory.GetDirectories (path);
+ foreach (string directoryName in directoryNames) {
+ RemoveDirectory (directoryName);
+ }
+ string[] fileNames = Directory.GetFiles (path);
+ foreach (string fileName in fileNames) {
+ File.Delete (fileName);
+ }
+ Directory.Delete (path, true);
+ }
+ } catch (Exception ex) {
+ throw new AssertionException ("Unable to cleanup '" + path + "'.", ex);
+ }
+ }
+
+ private static void AssertCompileResults (CompilerResults results, bool allowWarnings)
+ {
+ foreach (CompilerError compilerError in results.Errors) {
+ if (allowWarnings && compilerError.IsWarning) {
+ continue;
+ }
+
+ throw new Exception (compilerError.ToString ());
+ }
+ }
+
+ private static AppDomain CreateTestDomain ()
+ {
+ return AppDomain.CreateDomain ("CompileFromDom", AppDomain.CurrentDomain.Evidence,
+ AppDomain.CurrentDomain.SetupInformation);
+ }
+
+ private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
+ {
+ Type testerType = typeof (CrossDomainTester);
+
+ return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
+ testerType.Assembly.FullName, testerType.FullName, false,
+ BindingFlags.Public | BindingFlags.Instance, null, new object[0],
+ 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)
+ {
+ CompilerParameters options = new CompilerParameters ();
+ options.GenerateExecutable = false;
+ options.GenerateInMemory = false;
+ options.TempFiles = new TempFileCollection (tempDir);
+
+ VBCodeProvider codeProvider = new VBCodeProvider ();
+ ICodeCompiler compiler = codeProvider.CreateCompiler ();
+ CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
+
+ // verify compilation was successful
+ AssertCompileResults (results, true);
+
+ 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;
+ }
+
+ public string CompileAssemblyFromDomBatch (string tempDir)
+ {
+ CompilerParameters options = new CompilerParameters ();
+ options.GenerateExecutable = false;
+ options.GenerateInMemory = false;
+ options.TempFiles = new TempFileCollection (tempDir);
+
+ VBCodeProvider codeProvider = new VBCodeProvider ();
+ ICodeCompiler compiler = codeProvider.CreateCompiler ();
+ CompilerResults results = compiler.CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
+
+ // verify compilation was successful
+ AssertCompileResults (results, true);
+
+ 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;
+ }
+ }
+ }
+}