2
// Copyright (C) 2002. James W. Newkirk, Michael C. Two, Alexei A. Vorontsov. All Rights Reserved.
7
using System.Reflection;
11
/// Summary description for TestCaseBuilder.
13
public class TestCaseBuilder
15
public static TestCase Make(object fixture, MethodInfo method)
17
TestCase testCase = null;
19
if(HasTestAttribute(method) || HasObsoleteTestName(method))
21
if(IsTestMethodSignatureCorrect(method))
23
if(!IsExpectedException(method))
24
testCase = new NormalTestCase(fixture, method);
27
Type expectedException = GetExpectedExceptions(method);
28
testCase = new ExpectedExceptionTestCase(fixture, method, expectedException);
30
if(HasIgnoreAttribute(method))
32
testCase.ShouldRun = false;
33
testCase.IgnoreReason = GetIgnoreReason(method);
39
string reason = String.Format("Method: {0}'s signature is not correct", method.Name);
40
testCase = new NotRunnableTestCase(method, reason);
47
public static TestCase Make(object fixture, string methodName)
49
MethodInfo [] methods = fixture.GetType().GetMethods(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance);
50
foreach(MethodInfo method in methods)
52
if(method.Name.Equals(methodName))
53
return Make(fixture, method);
59
private static bool IsExpectedException(MethodInfo method)
61
Type exceptionAttr = typeof(Nunit.Framework.ExpectedExceptionAttribute);
62
object[] attributes = method.GetCustomAttributes(exceptionAttr, false);
63
return attributes.Length == 1;
66
private static Type GetExpectedExceptions(MethodInfo method)
68
Type exceptionAttr = typeof(Nunit.Framework.ExpectedExceptionAttribute);
69
object[] attributes = method.GetCustomAttributes(exceptionAttr, false);
71
Type returnType = null;
73
if(attributes.Length == 1)
75
ExpectedExceptionAttribute expectedAttr = (ExpectedExceptionAttribute)attributes[0];
76
returnType = expectedAttr.ExceptionType;
82
public static int CountTestCases(object fixture)
86
MethodInfo [] methods = fixture.GetType().GetMethods();
87
foreach(MethodInfo method in methods)
89
if(IsTestMethod(method))
97
public static bool IsTestMethod(MethodInfo methodToCheck)
100
(HasTestAttribute(methodToCheck) || HasObsoleteTestName(methodToCheck))
101
&& IsTestMethodSignatureCorrect(methodToCheck);
104
private static bool IsTestMethodSignatureCorrect(MethodInfo methodToCheck)
107
!methodToCheck.IsAbstract
108
&& methodToCheck.IsPublic
109
&& methodToCheck.GetParameters().Length == 0
110
&& methodToCheck.ReturnType.Equals(typeof(void));
113
private static bool HasTestAttribute(MethodInfo methodToCheck)
115
return methodToCheck.IsDefined(typeof(Nunit.Framework.TestAttribute),false);
118
private static bool HasObsoleteTestName(MethodInfo methodToCheck)
120
return methodToCheck.Name.ToLower().StartsWith("test");
123
private static bool HasIgnoreAttribute(MethodInfo methodToCheck)
125
Type ignoreMethodAttribute = typeof(Nunit.Framework.IgnoreAttribute);
126
object[] attributes = methodToCheck.GetCustomAttributes(ignoreMethodAttribute, false);
127
return attributes.Length == 1;
130
private static string GetIgnoreReason(MethodInfo methodToCheck)
132
Type ignoreMethodAttribute = typeof(Nunit.Framework.IgnoreAttribute);
133
IgnoreAttribute[] attributes = (IgnoreAttribute[])methodToCheck.GetCustomAttributes(ignoreMethodAttribute, false);
134
string result = "no reason";
135
if(attributes.Length > 0)
136
result = attributes[0].Reason;