Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LoadingTestCollector.cs « NUnitCore « src « nunit « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63ac74ef5f007e92f21adb02504e9ad36efe178b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace NUnit.Runner {

  using System;
  using System.Reflection;

  using NUnit.Framework;

  /// <summary>
  /// An implementation of a TestCollector that loads
  /// all classes on the class path and tests whether
  /// it is assignable from ITest or provides a static Suite property.
  /// <see cref="ITestCollector"/>
  /// </summary>
  public class LoadingClassPathTestCollector: ClassPathTestCollector {
	
    TestCaseClassLoader fLoader;
	/// <summary>
	/// 
	/// </summary>
    public LoadingClassPathTestCollector() {
      fLoader= new TestCaseClassLoader();
    }
	/// <summary>
	/// 
	/// </summary>
	/// <param name="classFileName"></param>
	/// <returns></returns>
    protected override bool IsTestClass(string classFileName) {	
      try {
        if (classFileName.EndsWith(".dll") || classFileName.EndsWith(".exe")) {
          Type testClass= ClassFromFile(classFileName);
          return (testClass != null) && IsTestClass(testClass);
        }
      } catch (TypeLoadException) {
      } 
      return false;
    }
	
    Type ClassFromFile(string classFileName) {
      string className= ClassNameFromFile(classFileName);
      if (!fLoader.IsExcluded(className))
        return fLoader.LoadClass(className, false);
      return null;
    }
	
    bool IsTestClass(Type testClass) {
      if (HasSuiteMethod(testClass))
        return true;
      if (typeof(ITest).IsAssignableFrom(testClass) &&
        testClass.IsPublic &&
        HasPublicConstructor(testClass)) 
        return true;
      return false;
    }
	
    bool HasSuiteMethod(Type testClass) {
      return (testClass.GetProperty(BaseTestRunner.SUITE_PROPERTYNAME, new Type[0]) == null);
    }
	
    bool HasPublicConstructor(Type testClass) {
      Type[] args= { typeof(string) };
      ConstructorInfo c= null;
      try {
        c= testClass.GetConstructor(args);
      } catch(Exception) {
        return false;
      }
      return true;
    }
  }
}