2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / nunit24 / NUnitCore / core / TestFixtureBuilder.cs
blob79bfc9c7f44877624fc7b588d94eef2aeadedb7e
1 // ****************************************************************
2 // This is free software licensed under the NUnit license. You
3 // may obtain a copy of the license as well as information regarding
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
5 // ****************************************************************
7 using System;
8 using System.Reflection;
10 namespace NUnit.Core
12 /// <summary>
13 /// TestFixtureBuilder contains static methods for building
14 /// TestFixtures from types. It uses builtin SuiteBuilders
15 /// and any installed extensions to do it.
16 /// </summary>
17 public class TestFixtureBuilder
19 public static bool CanBuildFrom( Type type )
21 return CoreExtensions.Host.SuiteBuilders.CanBuildFrom( type );
24 /// <summary>
25 /// Build a test fixture from a given type.
26 /// </summary>
27 /// <param name="type">The type to be used for the fixture</param>
28 /// <returns>A TestSuite if the fixture can be built, null if not</returns>
29 public static Test BuildFrom( Type type )
31 Test suite = CoreExtensions.Host.SuiteBuilders.BuildFrom( type );
33 if ( suite != null )
34 suite = CoreExtensions.Host.TestDecorators.Decorate( suite, type );
36 return suite;
39 /// <summary>
40 /// Build a fixture from an object.
41 /// </summary>
42 /// <param name="fixture">The object to be used for the fixture</param>
43 /// <returns>A TestSuite if fixture type can be built, null if not</returns>
44 public static Test BuildFrom( object fixture )
46 Test suite = BuildFrom( fixture.GetType() );
47 if( suite != null)
48 suite.Fixture = fixture;
49 return suite;
52 public static string GetAssemblyPath( Type fixtureType )
54 return GetAssemblyPath( fixtureType.Assembly );
57 // TODO: This logic should be in shared source
58 public static string GetAssemblyPath( Assembly assembly )
60 string path = assembly.CodeBase;
61 Uri uri = new Uri( path );
63 // If it wasn't loaded locally, use the Location
64 if ( !uri.IsFile )
65 return assembly.Location;
67 if ( uri.IsUnc )
68 return path.Substring( Uri.UriSchemeFile.Length+1 );
71 int start = Uri.UriSchemeFile.Length + Uri.SchemeDelimiter.Length;
73 if ( path[start] == '/' && path[start+2] == ':' )
74 ++start;
76 return path.Substring( start );
79 /// <summary>
80 /// Private constructor to prevent instantiation
81 /// </summary>
82 private TestFixtureBuilder() { }