Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / nunit24 / NUnitFixtures / fixtures / SnippetRunner.cs
1 // ****************************************************************\r
2 // Copyright 2007, Charlie Poole\r
3 // This is free software licensed under the NUnit license. You may\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4\r
5 // ****************************************************************\r
6 \r
7 using System;\r
8 using System.Text;\r
9 using System.Reflection;\r
10 using System.Collections;\r
11 using System.CodeDom.Compiler;\r
12 using NUnit.Core;\r
13 using NUnit.Util;\r
14 \r
15 namespace NUnit.Fixtures\r
16 {\r
17         /// <summary>\r
18         /// Abstract base class for fixtures that compile a snippet of code.\r
19         /// The fixture is basically a column fixture with one input column\r
20         /// dedicated to containing the code that is to be compiled. This\r
21         /// will normally be the first column\r
22         /// </summary>\r
23         public class SnippetRunner : TestLoadFixture\r
24         {\r
25                 public string Code;\r
26 \r
27                 private static readonly string testAssembly = "test.dll";\r
28 \r
29                 // Override doCell to handle the 'Code' column. We compile\r
30                 // the code and optionally load and run the tests.\r
31                 public override void doCell(fit.Parse cell, int columnNumber)\r
32                 {\r
33                         base.doCell (cell, columnNumber);\r
34 \r
35                         FieldInfo field = columnBindings[columnNumber].field;\r
36                         if ( field != null && field.Name == "Code" && CompileCodeSnippet( cell, Code ) )\r
37                                 LoadAndRunTestAssembly( cell, testAssembly );\r
38                 }\r
39 \r
40                 private bool CompileCodeSnippet( fit.Parse cell, string code )\r
41                 {\r
42                         TestCompiler compiler = new TestCompiler( \r
43                                 new string[] { "system.dll", "nunit.framework.dll" }, \r
44                                 testAssembly );\r
45 \r
46                         CompilerResults results = compiler.CompileCode( code );\r
47                         if ( results.NativeCompilerReturnValue == 0 )\r
48                                 return true;\r
49 \r
50                         cell.addToBody( "<font size=-1 color=\"#c08080\"><i>Compiler errors</i></font>" );\r
51 \r
52                         wrong( cell );\r
53                         cell.addToBody( "<hr>" );\r
54                                 \r
55                         foreach( string line in results.Output )\r
56                                 cell.addToBody( line + "<br>" );\r
57 \r
58                         return true;\r
59                 }\r
60 \r
61                 public TestTree Tree()\r
62                 {\r
63                         if ( testRunner.Test == null )\r
64                                 return new TestTree( "NULL" );\r
65 \r
66                         if ( testRunner.Test.Tests.Count == 0 )\r
67                                 return new TestTree( "EMPTY" );\r
68 \r
69                         StringBuilder sb = new StringBuilder();\r
70                         AppendTests( sb, "", testRunner.Test.Tests );\r
71 \r
72                         return new TestTree( sb.ToString() );\r
73                 }\r
74 \r
75                 private void AppendTests( StringBuilder sb, string prefix, IList tests )\r
76                 {\r
77                         foreach( TestNode test in tests )\r
78                         {\r
79                                 sb.Append( prefix );\r
80                                 sb.Append( test.TestName.Name );\r
81                                 sb.Append( Environment.NewLine );\r
82                                 if ( test.Tests != null )\r
83                                         AppendTests( sb, prefix + ">", test.Tests );\r
84                         }\r
85                 }\r
86         }\r
87 }\r