* VBCodeProviderTest.cs: moved to using NUNit 2.x Assert class, improved
[mono.git] / mcs / class / System / Test / Microsoft.VisualBasic / VBCodeProviderTest.cs
1 //\r
2 // Microsoft.VisualBasic.VBCodeProvider.cs\r
3 //\r
4 // Author:\r
5 //   Jochen Wezel (jwezel@compumaster.de) //\r
6 //\r
7 // (C) 2003 Jochen Wezel (CompuMaster GmbH)\r
8 //\r
9 // Last modifications:\r
10 // 2003-12-10 JW: publishing of this file\r
11 //\r
12 \r
13 using NUnit.Framework;\r
14 using System;\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
22 using System.IO;\r
23 \r
24 namespace MonoTests.Microsoft.VisualBasic\r
25 {\r
26 \r
27         enum OsType {\r
28                 Windows,\r
29                 Unix,\r
30                 Mac\r
31         }\r
32 \r
33         [TestFixture]\r
34         public class VBCodeProviderTest {\r
35         \r
36                 CodeDomProvider MyVBCodeProvider;\r
37                 static OsType OS;\r
38                 static char DSC = Path.DirectorySeparatorChar;\r
39 \r
40                 [SetUp]\r
41                 public void GetReady() { \r
42                         if ('/' == DSC) {\r
43                                 OS = OsType.Unix;\r
44                         } else if ('\\' == DSC) {\r
45                                 OS = OsType.Windows;\r
46                         } else {\r
47                                 OS = OsType.Mac;\r
48                         }\r
49 \r
50                         MyVBCodeProvider = new VBCodeProvider(); \r
51                 }\r
52 \r
53                 [Test]\r
54                 public void FileExtension ()\r
55                 {\r
56                         Assert.AreEqual("vb", MyVBCodeProvider.FileExtension, "#JW10");\r
57                 }\r
58 \r
59                 [Test]\r
60                 public void LanguageOptionsTest ()\r
61                 {\r
62                         Assert.AreEqual(LanguageOptions.CaseInsensitive, MyVBCodeProvider.LanguageOptions, "#JW20");\r
63                 }\r
64 \r
65                 [Test]\r
66                 public void CreateCompiler()\r
67                 {\r
68                         // prepare the compilation\r
69                         ICodeCompiler MyVBCodeCompiler = MyVBCodeProvider.CreateCompiler();\r
70                         Assert.IsNotNull(MyVBCodeCompiler, "#JW30 - CreateCompiler");\r
71                         \r
72                         CompilerParameters options = new CompilerParameters();\r
73                         options.GenerateExecutable = true;\r
74                         options.IncludeDebugInformation = true;\r
75                         options.TreatWarningsAsErrors = true;\r
76                         \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
82 \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
86                         }\r
87 \r
88                         try\r
89                         {\r
90                                 Assembly MyAss = MyVBCodeCompilerResults.CompiledAssembly;\r
91                         }\r
92                         catch (Exception ex)\r
93                         {\r
94                                 Assert.Fail("#JW32 - MyVBCodeCompilerResults.CompiledAssembly hasn't been an expected object" + \r
95                                                 Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);\r
96                         }\r
97 \r
98                         // Execute the test app\r
99                         ProcessStartInfo NewProcInfo = new ProcessStartInfo();\r
100                         if (Windows) {\r
101                                 NewProcInfo.FileName = MyVBCodeCompilerResults.CompiledAssembly.Location;\r
102                         }\r
103                         else {\r
104                                 NewProcInfo.FileName = "mono";\r
105                                 NewProcInfo.Arguments = MyVBCodeCompilerResults.CompiledAssembly.Location;\r
106                         }\r
107                         NewProcInfo.RedirectStandardOutput = true;\r
108                         NewProcInfo.UseShellExecute = false;\r
109                         NewProcInfo.CreateNoWindow = true;\r
110                         string TestAppOutput = "";\r
111 \r
112                         try\r
113                         {\r
114                                 Process MyProc = Process.Start(NewProcInfo);\r
115                                 MyProc.WaitForExit();\r
116                                 TestAppOutput = MyProc.StandardOutput.ReadToEnd();\r
117                                 MyProc.Close();\r
118                                 MyProc.Dispose();\r
119                         }\r
120                         catch (Exception ex)\r
121                         {\r
122                                 Assert.Fail("#JW34 - " + ex.Message + Environment.NewLine + ex.StackTrace);\r
123                         }\r
124                         \r
125                         Assert.AreEqual("Hello world!", TestAppOutput, "#JW33 - Application output");\r
126 \r
127                         // Clean up\r
128                         try\r
129                         {\r
130                                 File.Delete (NewProcInfo.FileName);\r
131                         }\r
132                         catch {}\r
133                 }\r
134 \r
135                 // NOTE: This test does not clean-up the generated assemblies\r
136                 [Test]\r
137                 public void CompileAssembly_InMemory ()\r
138                 {\r
139                         // NOT in memory\r
140                         CompilerResults results = CompileAssembly (false);\r
141                         Assert.IsTrue(results.CompiledAssembly.Location.Length != 0, "#1");\r
142                         Assert.IsNotNull(results.PathToAssembly, "#2");\r
143 \r
144                         // in memory\r
145                         results = CompileAssembly (true);\r
146                         Assert.AreEqual(string.Empty, results.CompiledAssembly.Location, "#3");\r
147                         Assert.IsNull(results.PathToAssembly, "#4");\r
148                 }\r
149 \r
150                 [Test]\r
151                 public void CreateGenerator()\r
152                 {\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
157                 }\r
158 \r
159                 \r
160                 //TODO: [Test]\r
161                 public void     CreateParser()\r
162                 {\r
163                         //System.CodeDom.Compiler.ICodeParser CreateParser()\r
164                 }\r
165 \r
166                 //TODO: [Test]\r
167                 public void     CreateObjRef()\r
168                 {\r
169                         //System.Runtime.Remoting.ObjRef CreateObjRef(System.Type requestedType)\r
170                 }\r
171 \r
172                 bool Windows\r
173                 {\r
174                         get {\r
175                                 return OS == OsType.Windows;\r
176                         }\r
177                 }\r
178 \r
179                 bool Unix\r
180                 {\r
181                         get {\r
182                                 return OS == OsType.Unix;\r
183                         }\r
184                 }\r
185 \r
186                 bool Mac\r
187                 {\r
188                         get {\r
189                                 return OS == OsType.Mac;\r
190                         }\r
191                 }\r
192 \r
193                 private CompilerResults CompileAssembly(bool inMemory) {\r
194                         CompilerParameters options = new CompilerParameters();\r
195                         options.GenerateExecutable = false;\r
196                         options.GenerateInMemory = inMemory;\r
197 \r
198                         VBCodeProvider codeProvider = new VBCodeProvider();\r
199                         ICodeCompiler compiler = codeProvider.CreateCompiler();\r
200                         return compiler.CompileAssemblyFromDom(options,\r
201                                                                 new CodeCompileUnit());\r
202                 }\r
203         }\r
204 }\r