2 // Microsoft.VisualBasic.VBCodeProvider.cs
\r
5 // Jochen Wezel (jwezel@compumaster.de) //
\r
7 // (C) 2003 Jochen Wezel (CompuMaster GmbH)
\r
9 // Last modifications:
\r
10 // 2003-12-10 JW: publishing of this file
\r
13 using NUnit.Framework;
\r
15 using Microsoft.VisualBasic;
\r
16 using System.CodeDom;
\r
17 using System.CodeDom.Compiler;
\r
18 using System.ComponentModel;
\r
19 using System.Collections.Specialized;
\r
20 using System.Reflection;
\r
21 using System.Diagnostics;
\r
24 namespace MonoTests.Microsoft.VisualBasic
\r
34 public class VBCodeProviderTest {
\r
36 CodeDomProvider MyVBCodeProvider;
\r
38 static char DSC = Path.DirectorySeparatorChar;
\r
41 public void GetReady() {
\r
44 } else if ('\\' == DSC) {
\r
45 OS = OsType.Windows;
\r
50 MyVBCodeProvider = new VBCodeProvider();
\r
54 public void FileExtension ()
\r
56 Assert.AreEqual("vb", MyVBCodeProvider.FileExtension, "#JW10");
\r
60 public void LanguageOptionsTest ()
\r
62 Assert.AreEqual(LanguageOptions.CaseInsensitive, MyVBCodeProvider.LanguageOptions, "#JW20");
\r
66 public void CreateCompiler()
\r
68 // prepare the compilation
\r
69 ICodeCompiler MyVBCodeCompiler = MyVBCodeProvider.CreateCompiler();
\r
70 Assert.IsNotNull(MyVBCodeCompiler, "#JW30 - CreateCompiler");
\r
72 CompilerParameters options = new CompilerParameters();
\r
73 options.GenerateExecutable = true;
\r
74 options.IncludeDebugInformation = true;
\r
75 options.TreatWarningsAsErrors = true;
\r
77 // process compilation
\r
78 CompilerResults MyVBCodeCompilerResults = MyVBCodeCompiler.CompileAssemblyFromSource(options,
\r
79 "public class TestModule" + Environment.NewLine + "public shared sub Main()"
\r
80 + Environment.NewLine + "System.Console.Write(\"Hello world!\")"
\r
81 + Environment.NewLine + "End Sub" + Environment.NewLine + "End Class");
\r
83 // verify outcome of compilation
\r
84 if (MyVBCodeCompilerResults.Errors.Count > 0) {
\r
85 Assert.Fail("Hello World compilation failed: " + MyVBCodeCompilerResults.Errors[0].ToString());
\r
90 Assembly MyAss = MyVBCodeCompilerResults.CompiledAssembly;
\r
92 catch (Exception ex)
\r
94 Assert.Fail("#JW32 - MyVBCodeCompilerResults.CompiledAssembly hasn't been an expected object" +
\r
95 Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
\r
98 // Execute the test app
\r
99 ProcessStartInfo NewProcInfo = new ProcessStartInfo();
\r
101 NewProcInfo.FileName = MyVBCodeCompilerResults.CompiledAssembly.Location;
\r
104 NewProcInfo.FileName = "mono";
\r
105 NewProcInfo.Arguments = MyVBCodeCompilerResults.CompiledAssembly.Location;
\r
107 NewProcInfo.RedirectStandardOutput = true;
\r
108 NewProcInfo.UseShellExecute = false;
\r
109 NewProcInfo.CreateNoWindow = true;
\r
110 string TestAppOutput = "";
\r
114 Process MyProc = Process.Start(NewProcInfo);
\r
115 MyProc.WaitForExit();
\r
116 TestAppOutput = MyProc.StandardOutput.ReadToEnd();
\r
120 catch (Exception ex)
\r
122 Assert.Fail("#JW34 - " + ex.Message + Environment.NewLine + ex.StackTrace);
\r
125 Assert.AreEqual("Hello world!", TestAppOutput, "#JW33 - Application output");
\r
130 File.Delete (NewProcInfo.FileName);
\r
135 // NOTE: This test does not clean-up the generated assemblies
\r
137 public void CompileAssembly_InMemory ()
\r
140 CompilerResults results = CompileAssembly (false);
\r
141 Assert.IsTrue(results.CompiledAssembly.Location.Length != 0, "#1");
\r
142 Assert.IsNotNull(results.PathToAssembly, "#2");
\r
145 results = CompileAssembly (true);
\r
146 Assert.AreEqual(string.Empty, results.CompiledAssembly.Location, "#3");
\r
147 Assert.IsNull(results.PathToAssembly, "#4");
\r
151 public void CreateGenerator()
\r
153 ICodeGenerator MyVBCodeGen;
\r
154 MyVBCodeGen = MyVBCodeProvider.CreateGenerator();
\r
155 Assert.IsNotNull(MyVBCodeGen, "#JW40 - CreateGenerator");
\r
156 Assert.IsTrue(MyVBCodeGen.Supports(GeneratorSupport.DeclareEnums), "#JW41");
\r
161 public void CreateParser()
\r
163 //System.CodeDom.Compiler.ICodeParser CreateParser()
\r
167 public void CreateObjRef()
\r
169 //System.Runtime.Remoting.ObjRef CreateObjRef(System.Type requestedType)
\r
175 return OS == OsType.Windows;
\r
182 return OS == OsType.Unix;
\r
189 return OS == OsType.Mac;
\r
193 private CompilerResults CompileAssembly(bool inMemory) {
\r
194 CompilerParameters options = new CompilerParameters();
\r
195 options.GenerateExecutable = false;
\r
196 options.GenerateInMemory = inMemory;
\r
198 VBCodeProvider codeProvider = new VBCodeProvider();
\r
199 ICodeCompiler compiler = codeProvider.CreateCompiler();
\r
200 return compiler.CompileAssemblyFromDom(options,
\r
201 new CodeCompileUnit());
\r