renamed
[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.Compiler;\r
17 using System.ComponentModel;\r
18 using System.Collections.Specialized;\r
19 using System.Reflection;\r
20 using System.Diagnostics;\r
21 using System.IO;\r
22 \r
23 namespace MonoTests.System.Microsoft.VisualBasic\r
24 {\r
25 \r
26         enum OsType {\r
27                 Windows,\r
28                 Unix,\r
29                 Mac\r
30         }\r
31 \r
32         [TestFixture]\r
33         public class VBCodeProviderTest : Assertion {\r
34         \r
35                 CodeDomProvider MyVBCodeProvider;\r
36                 static OsType OS;\r
37                 static char DSC = Path.DirectorySeparatorChar;\r
38 \r
39                 [SetUp]\r
40                 public void GetReady() { \r
41                         if ('/' == DSC) {\r
42                                 OS = OsType.Unix;\r
43                         } else if ('\\' == DSC) {\r
44                                 OS = OsType.Windows;\r
45                         } else {\r
46                                 OS = OsType.Mac;\r
47                         }\r
48 \r
49                         MyVBCodeProvider = new VBCodeProvider(); \r
50                 }\r
51 \r
52                 [Test]\r
53                 public void FileExtension ()\r
54                 {\r
55                         AssertEquals ("#JW10", "vb", MyVBCodeProvider.FileExtension);\r
56                 }\r
57 \r
58                 [Test]\r
59                 public void LanguageOptionsTest ()\r
60                 {\r
61                         AssertEquals ("#JW20", LanguageOptions.CaseInsensitive, MyVBCodeProvider.LanguageOptions);\r
62                 }\r
63 \r
64                 [Test]\r
65                 public void CreateCompiler()\r
66                 {\r
67                         // Prepare the compilation\r
68                         //Console.WriteLine("#J30.pre1 - CreateCompiler");\r
69                         ICodeCompiler MyVBCodeCompiler;\r
70                         MyVBCodeCompiler = MyVBCodeProvider.CreateCompiler();\r
71                         AssertNotNull ("#JW30 - CreateCompiler", MyVBCodeCompiler);\r
72                         CompilerResults MyVBCodeCompilerResults;\r
73                         //Console.WriteLine("#J30.post1 - CreateCompiler");\r
74 \r
75                         CompilerParameters options = new CompilerParameters();\r
76                         options.GenerateExecutable = true;\r
77                         options.IncludeDebugInformation = true;\r
78                         options.TreatWarningsAsErrors = true;\r
79                         \r
80                         // Process compilation\r
81                         MyVBCodeCompilerResults = MyVBCodeCompiler.CompileAssemblyFromSource(options,\r
82                                 "public class TestModule" + Environment.NewLine + "public shared sub Main()" + Environment.NewLine + "System.Console.Write(\"Hello world!\")" + Environment.NewLine + "End Sub" + Environment.NewLine + "End Class" + Environment.NewLine);\r
83 \r
84                         // Analyse the compilation success/messages\r
85                         StringCollection MyOutput;\r
86                         MyOutput = MyVBCodeCompilerResults.Output;\r
87                         string MyOutStr = "";\r
88                         foreach (string MyStr in MyOutput)\r
89                         {\r
90                                 MyOutStr += MyStr + Environment.NewLine + Environment.NewLine;\r
91                         }\r
92 \r
93                         if (MyVBCodeCompilerResults.Errors.Count != 0) {
94                                 System.Console.WriteLine (MyVBCodeCompilerResults);
95                                 Assert ("#JW31 - Hello world compilation: " + MyOutStr, false);\r
96                         }
97
98                         try\r
99                         {\r
100                                 Assembly MyAss = MyVBCodeCompilerResults.CompiledAssembly;\r
101                         }\r
102                         catch (Exception ex)\r
103                         {\r
104                                 Assert ("#JW32 - MyVBCodeCompilerResults.CompiledAssembly hasn't been an expected object" + \r
105                                                 Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace, false);\r
106                         }\r
107 \r
108                         // Execute the test app\r
109                         ProcessStartInfo NewProcInfo = new ProcessStartInfo();\r
110                         if (Windows) {\r
111                                 NewProcInfo.FileName = MyVBCodeCompilerResults.CompiledAssembly.Location;\r
112                         }\r
113                         else {\r
114                                 NewProcInfo.FileName = "mono";\r
115                                 NewProcInfo.Arguments = MyVBCodeCompilerResults.CompiledAssembly.Location;\r
116                         }\r
117                         NewProcInfo.RedirectStandardOutput = true;\r
118                         NewProcInfo.UseShellExecute = false;\r
119                         NewProcInfo.CreateNoWindow = true;\r
120                         string TestAppOutput = "";\r
121                         try\r
122                         {\r
123                                 Process MyProc = Process.Start(NewProcInfo);\r
124                                 MyProc.WaitForExit();\r
125                                 TestAppOutput = MyProc.StandardOutput.ReadToEnd();\r
126                                 MyProc.Close();\r
127                                 MyProc.Dispose();\r
128                         }\r
129                         catch (Exception ex)\r
130                         {\r
131                                 Fail("#JW34 - " + ex.Message + Environment.NewLine + ex.StackTrace);\r
132                         }\r
133                         AssertEquals("#JW33 - Application output", "Hello world!", TestAppOutput);\r
134 \r
135                         // Clean up\r
136                         try\r
137                         {\r
138                                 File.Delete (NewProcInfo.FileName);\r
139                         }\r
140                         catch {}\r
141                 }\r
142 \r
143                 [Test]\r
144                 public void CreateGenerator()\r
145                 {\r
146                         ICodeGenerator MyVBCodeGen;\r
147                         MyVBCodeGen = MyVBCodeProvider.CreateGenerator();\r
148                         Assert ("#JW40 - CreateGenerator", (MyVBCodeGen != null));\r
149                         AssertEquals ("#JW41", true, MyVBCodeGen.Supports (GeneratorSupport.DeclareEnums));\r
150                 }\r
151 \r
152                 \r
153                 //TODO: [Test]\r
154                 public void     CreateParser()\r
155                 {\r
156                         //System.CodeDom.Compiler.ICodeParser CreateParser()\r
157                 }\r
158 \r
159                 //TODO: [Test]\r
160                 public void     CreateObjRef()\r
161                 {\r
162                         //System.Runtime.Remoting.ObjRef CreateObjRef(System.Type requestedType)\r
163                 }\r
164 \r
165                 bool Windows\r
166                 {\r
167                         get {\r
168                                 return OS == OsType.Windows;\r
169                         }\r
170                 }\r
171 \r
172                 bool Unix\r
173                 {\r
174                         get {\r
175                                 return OS == OsType.Unix;\r
176                         }\r
177                 }\r
178 \r
179                 bool Mac\r
180                 {\r
181                         get {\r
182                                 return OS == OsType.Mac;\r
183                         }\r
184                 }\r
185 \r
186         }\r
187 }\r