/v2/src/mbunit/MbUnit.Framework/CompilerAssert.cs

http://mb-unit.googlecode.com/ · C# · 272 lines · 120 code · 19 blank · 133 comment · 8 complexity · 6e150d1519d051b8955aefad7b6b341f MD5 · raw file

  1. // MbUnit Test Framework
  2. //
  3. // Copyright (c) 2004 Jonathan de Halleux
  4. //
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from
  8. // the use of this software.
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment in the product
  16. // documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must
  19. // not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. // MbUnit HomePage: http://www.mbunit.com
  25. // Author: Jonathan de Halleux
  26. using MbUnit.Framework;
  27. using System;
  28. using System.Collections;
  29. using System.CodeDom.Compiler;
  30. using System.IO;
  31. using System.Collections.Specialized;
  32. using MbUnit.Core.Exceptions;
  33. using Microsoft.CSharp;
  34. using Microsoft.VisualBasic;
  35. namespace MbUnit.Framework {
  36. /// <summary>
  37. /// Assertion helper for compilation.
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// This class contains static helper methods to verify that snippets are compilable.
  42. /// </para>
  43. /// </remarks>
  44. public sealed class CompilerAssert {
  45. #region Private constructor and fields
  46. private static ICodeCompiler csharp = new CSharpCodeProvider().CreateCompiler();
  47. private static ICodeCompiler vb = new VBCodeProvider().CreateCompiler();
  48. private CompilerAssert() { }
  49. #endregion
  50. /// <summary>
  51. /// Gets the C# compiler from <see cref="CSharpCodeProvider"/>.
  52. /// </summary>
  53. /// <value>
  54. /// C# compiler.
  55. /// </value>
  56. public static ICodeCompiler CSharpCompiler {
  57. get {
  58. return csharp;
  59. }
  60. }
  61. /// <summary>
  62. /// Gets the VB.NET compiler from <see cref="VBCodeProvider"/>.
  63. /// </summary>
  64. /// <value>
  65. /// VB.NET compiler.
  66. /// </value>
  67. public static ICodeCompiler VBCompiler {
  68. get {
  69. return vb;
  70. }
  71. }
  72. /// <summary>
  73. /// Verifies that <paramref name="source"/> compiles using the provided compiler.
  74. /// </summary>
  75. /// <param name="compiler">Compiler instance</param>
  76. /// <param name="source">Source code to compile</param>
  77. public static void Compiles(ICodeCompiler compiler, string source) {
  78. Assert.IsNotNull(compiler);
  79. Assert.IsNotNull(source);
  80. CompilerParameters ps = new CompilerParameters();
  81. Compiles(compiler, ps, source);
  82. }
  83. /// <summary>
  84. /// Verifies that <paramref name="source"/> compiles using the provided compiler.
  85. /// </summary>
  86. /// <param name="compiler">Compiler instance</param>
  87. /// <param name="source">Source code to compile</param>
  88. public static void Compiles(ICodeCompiler compiler, Stream source) {
  89. Assert.IsNotNull(compiler);
  90. Assert.IsNotNull(source);
  91. CompilerParameters ps = new CompilerParameters();
  92. Compiles(compiler, ps, source);
  93. }
  94. /// <summary>
  95. /// Verifies that <paramref name="source"/> compiles using the provided compiler.
  96. /// </summary>
  97. /// <param name="compiler">Compiler instance</param>
  98. /// <param name="references">Referenced assemblies</param>
  99. /// <param name="source">Source code to compile</param>
  100. public static void Compiles(ICodeCompiler compiler, StringCollection references, string source) {
  101. Assert.IsNotNull(compiler);
  102. Assert.IsNotNull(references);
  103. Assert.IsNotNull(source);
  104. CompilerParameters ps = new CompilerParameters();
  105. foreach (string ra in references)
  106. ps.ReferencedAssemblies.Add(ra);
  107. Compiles(compiler, ps, source);
  108. }
  109. /// <summary>
  110. /// Verifies that <paramref name="source"/> compiles using the provided compiler.
  111. /// </summary>
  112. /// <param name="compiler">
  113. /// <see cref="ICodeCompiler"/> instance.</param>
  114. /// <param name="options">Compilation options</param>
  115. /// <param name="source">source to compile</param>
  116. public static void Compiles(ICodeCompiler compiler, CompilerParameters options, string source) {
  117. Assert.IsNotNull(compiler);
  118. Assert.IsNotNull(options);
  119. Assert.IsNotNull(source);
  120. Compiles(compiler, options, source, false);
  121. }
  122. /// <summary>
  123. /// Verifies that <paramref name="source"/> compiles using the provided compiler.
  124. /// </summary>
  125. /// <param name="compiler">
  126. /// <see cref="ICodeCompiler"/> instance.</param>
  127. /// <param name="options">Compilation options</param>
  128. /// <param name="source">Source to compile</param>
  129. /// <param name="throwOnWarning">
  130. /// true if assertion should throw if any warning.
  131. /// </param>
  132. public static void Compiles(ICodeCompiler compiler, CompilerParameters options, string source, bool throwOnWarning) {
  133. Assert.IsNotNull(compiler);
  134. Assert.IsNotNull(options);
  135. CompilerResults results = compiler.CompileAssemblyFromSource(options, source);
  136. if (results.Errors.HasErrors)
  137. throw new CompilationException(compiler, options, results, source);
  138. if (throwOnWarning && results.Errors.HasWarnings)
  139. throw new CompilationException(compiler, options, results, source);
  140. }
  141. /// <summary>
  142. /// Verifies that <paramref name="source"/> compiles using the provided compiler.
  143. /// </summary>
  144. /// <param name="compiler">
  145. /// <see cref="ICodeCompiler"/> instance.</param>
  146. /// <param name="options">Compilation options</param>
  147. /// <param name="source">Stream containing the source to compile</param>
  148. public static void Compiles(ICodeCompiler compiler, CompilerParameters options, Stream source) {
  149. Compiles(compiler, options, source, false);
  150. }
  151. /// <summary>
  152. /// Verifies that <paramref name="source"/> compiles using the provided compiler.
  153. /// </summary>
  154. /// <param name="compiler">
  155. /// <see cref="ICodeCompiler"/> instance.</param>
  156. /// <param name="options">Compilation options</param>
  157. /// <param name="source">Stream containing the source to compile</param>
  158. /// <param name="throwOnWarning">
  159. /// true if assertion should throw if any warning.
  160. /// </param>
  161. public static void Compiles(ICodeCompiler compiler, CompilerParameters options, Stream source, bool throwOnWarning) {
  162. using (StreamReader sr = new StreamReader(source)) {
  163. Compiles(compiler, options, sr.ReadToEnd(), throwOnWarning);
  164. }
  165. }
  166. /// <summary>
  167. /// Verifies that <paramref name="source"/> does not compile using the provided compiler.
  168. /// </summary>
  169. /// <param name="compiler">
  170. /// <see cref="ICodeCompiler"/> instance.</param>
  171. /// <param name="source">Source to compile</param>
  172. public static void NotCompiles(
  173. ICodeCompiler compiler,
  174. string source) {
  175. CompilerParameters options = new CompilerParameters();
  176. NotCompiles(compiler, options, source);
  177. }
  178. /// <summary>
  179. /// Verifies that <paramref name="source"/> does not compile using the provided compiler.
  180. /// </summary>
  181. /// <param name="compiler">
  182. /// <see cref="ICodeCompiler"/> instance.</param>
  183. /// <param name="source">Source to compile</param>
  184. public static void NotCompiles(
  185. ICodeCompiler compiler,
  186. Stream source) {
  187. CompilerParameters options = new CompilerParameters();
  188. NotCompiles(compiler, options, source);
  189. }
  190. /// <summary>
  191. /// Verifies that <paramref name="source"/> does not compile using the provided compiler.
  192. /// </summary>
  193. /// <param name="compiler">
  194. /// <see cref="ICodeCompiler"/> instance.</param>
  195. /// <param name="referencedAssemblies">Collection of referenced assemblies</param>
  196. /// <param name="source">Source to compile</param>
  197. public static void NotCompiles(
  198. ICodeCompiler compiler,
  199. StringCollection referencedAssemblies,
  200. string source) {
  201. CompilerParameters options = new CompilerParameters();
  202. CompilerParameters ps = new CompilerParameters();
  203. foreach (string ra in referencedAssemblies)
  204. ps.ReferencedAssemblies.Add(ra);
  205. NotCompiles(compiler, options, source);
  206. }
  207. /// <summary>
  208. /// Verifies that <paramref name="source"/> does not compile using the provided compiler.
  209. /// </summary>
  210. /// <param name="compiler">
  211. /// <see cref="ICodeCompiler"/> instance.</param>
  212. /// <param name="options">Compilation options</param>
  213. /// <param name="source">Source to compile</param>
  214. public static void NotCompiles(
  215. ICodeCompiler compiler,
  216. CompilerParameters options,
  217. string source) {
  218. Assert.IncrementAssertCount();
  219. if (compiler == null)
  220. throw new ArgumentNullException("compiler");
  221. if (options == null)
  222. throw new ArgumentNullException("options");
  223. CompilerResults results = compiler.CompileAssemblyFromSource(options, source);
  224. if (!results.Errors.HasErrors)
  225. throw new CompilationException(compiler, options, results, source);
  226. }
  227. /// <summary>
  228. /// Verifies that <paramref name="source"/> does not compile using the provided compiler.
  229. /// </summary>
  230. /// <param name="compiler">
  231. /// <see cref="ICodeCompiler"/> instance.</param>
  232. /// <param name="options">Compilation options</param>
  233. /// <param name="source">Source to compile</param>
  234. public static void NotCompiles(
  235. ICodeCompiler compiler,
  236. CompilerParameters options,
  237. Stream source) {
  238. using (StreamReader sr = new StreamReader(source)) {
  239. NotCompiles(compiler, options, sr.ReadToEnd());
  240. }
  241. }
  242. /// <summary>
  243. /// Writes the errors given in the compiler <paramref name="results"/> to the named <paramref name="writer"/>
  244. /// </summary>
  245. /// <param name="results">The <see cref="CompilerResults" /> generated by the compilation</param>
  246. /// <param name="writer">The <see cref="TextWriter"/> to write the errors out to</param>
  247. public static void DisplayErrors(CompilerResults results, TextWriter writer) {
  248. foreach (CompilerError error in results.Errors) {
  249. writer.Write(error);
  250. }
  251. }
  252. }
  253. }