VBCodeProviderTest.cs 4.5 KB

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