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
9 using System.Reflection;
\r
10 using System.Collections;
\r
11 using System.CodeDom.Compiler;
\r
15 namespace NUnit.Fixtures
\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
23 public class SnippetRunner : TestLoadFixture
\r
27 private static readonly string testAssembly = "test.dll";
\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
33 base.doCell (cell, columnNumber);
\r
35 FieldInfo field = columnBindings[columnNumber].field;
\r
36 if ( field != null && field.Name == "Code" && CompileCodeSnippet( cell, Code ) )
\r
37 LoadAndRunTestAssembly( cell, testAssembly );
\r
40 private bool CompileCodeSnippet( fit.Parse cell, string code )
\r
42 TestCompiler compiler = new TestCompiler(
\r
43 new string[] { "system.dll", "nunit.framework.dll" },
\r
46 CompilerResults results = compiler.CompileCode( code );
\r
47 if ( results.NativeCompilerReturnValue == 0 )
\r
50 cell.addToBody( "<font size=-1 color=\"#c08080\"><i>Compiler errors</i></font>" );
\r
53 cell.addToBody( "<hr>" );
\r
55 foreach( string line in results.Output )
\r
56 cell.addToBody( line + "<br>" );
\r
61 public TestTree Tree()
\r
63 if ( testRunner.Test == null )
\r
64 return new TestTree( "NULL" );
\r
66 if ( testRunner.Test.Tests.Count == 0 )
\r
67 return new TestTree( "EMPTY" );
\r
69 StringBuilder sb = new StringBuilder();
\r
70 AppendTests( sb, "", testRunner.Test.Tests );
\r
72 return new TestTree( sb.ToString() );
\r
75 private void AppendTests( StringBuilder sb, string prefix, IList tests )
\r
77 foreach( TestNode test in tests )
\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