~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/NUnitFramework/core/TestCaseBuilder.cs

  • Committer: jnewkirk
  • Date: 2002-07-10 20:00:13 UTC
  • Revision ID: vcs-imports@canonical.com-20020710200013-j8us5mbi0usp7p02
initialĀ load

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright (C) 2002. James W. Newkirk, Michael C. Two, Alexei A. Vorontsov. All Rights Reserved.
 
3
//
 
4
namespace Nunit.Core
 
5
{
 
6
        using System;
 
7
        using System.Reflection;
 
8
        using Nunit.Framework;
 
9
 
 
10
        /// <summary>
 
11
        /// Summary description for TestCaseBuilder.
 
12
        /// </summary>
 
13
        public class TestCaseBuilder
 
14
        {
 
15
                public static TestCase Make(object fixture, MethodInfo method)
 
16
                {
 
17
                        TestCase testCase = null;
 
18
 
 
19
                        if(HasTestAttribute(method) || HasObsoleteTestName(method))
 
20
                        {
 
21
                                if(IsTestMethodSignatureCorrect(method))
 
22
                                {
 
23
                                        if(!IsExpectedException(method))
 
24
                                                testCase = new NormalTestCase(fixture, method);
 
25
                                        else
 
26
                                        {
 
27
                                                Type expectedException = GetExpectedExceptions(method);
 
28
                                                testCase = new ExpectedExceptionTestCase(fixture, method, expectedException);
 
29
                                        }
 
30
                                        if(HasIgnoreAttribute(method))
 
31
                                        {
 
32
                                                testCase.ShouldRun = false;
 
33
                                                testCase.IgnoreReason = GetIgnoreReason(method);
 
34
                                        }
 
35
 
 
36
                                }
 
37
                                else
 
38
                                {
 
39
                                        string reason = String.Format("Method: {0}'s signature is not correct", method.Name);
 
40
                                        testCase = new NotRunnableTestCase(method, reason);
 
41
                                }
 
42
                        }
 
43
 
 
44
                        return testCase;
 
45
                }
 
46
 
 
47
                public static TestCase Make(object fixture, string methodName)
 
48
                {
 
49
                        MethodInfo [] methods = fixture.GetType().GetMethods(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance);
 
50
                        foreach(MethodInfo method in methods)
 
51
                        {
 
52
                                if(method.Name.Equals(methodName))
 
53
                                        return Make(fixture, method);
 
54
                        }
 
55
 
 
56
                        return null;
 
57
                }
 
58
 
 
59
                private static bool IsExpectedException(MethodInfo method)
 
60
                {
 
61
                        Type exceptionAttr = typeof(Nunit.Framework.ExpectedExceptionAttribute);
 
62
                        object[] attributes = method.GetCustomAttributes(exceptionAttr, false);
 
63
                        return attributes.Length == 1;
 
64
                }
 
65
 
 
66
                private static Type GetExpectedExceptions(MethodInfo method)
 
67
                {
 
68
                        Type exceptionAttr = typeof(Nunit.Framework.ExpectedExceptionAttribute);
 
69
                        object[] attributes = method.GetCustomAttributes(exceptionAttr, false);
 
70
 
 
71
                        Type returnType = null;
 
72
 
 
73
                        if(attributes.Length == 1)
 
74
                        {
 
75
                                ExpectedExceptionAttribute expectedAttr = (ExpectedExceptionAttribute)attributes[0];
 
76
                                returnType = expectedAttr.ExceptionType;
 
77
                        }
 
78
 
 
79
                        return returnType;
 
80
                }
 
81
 
 
82
                public static int CountTestCases(object fixture) 
 
83
                {
 
84
                        int testCases = 0;
 
85
 
 
86
                        MethodInfo [] methods = fixture.GetType().GetMethods();
 
87
                        foreach(MethodInfo method in methods)
 
88
                        {
 
89
                                if(IsTestMethod(method))
 
90
                                        testCases++;
 
91
                        }
 
92
 
 
93
                        return testCases;
 
94
                }
 
95
 
 
96
 
 
97
                public static bool IsTestMethod(MethodInfo methodToCheck) 
 
98
                {
 
99
                        return
 
100
                                (HasTestAttribute(methodToCheck) || HasObsoleteTestName(methodToCheck))
 
101
                                && IsTestMethodSignatureCorrect(methodToCheck);
 
102
                }
 
103
 
 
104
                private static bool IsTestMethodSignatureCorrect(MethodInfo methodToCheck)
 
105
                {
 
106
                        return 
 
107
                                !methodToCheck.IsAbstract
 
108
                                && methodToCheck.IsPublic
 
109
                                && methodToCheck.GetParameters().Length == 0
 
110
                                && methodToCheck.ReturnType.Equals(typeof(void));
 
111
                }
 
112
 
 
113
                private static bool HasTestAttribute(MethodInfo methodToCheck)
 
114
                {
 
115
                        return methodToCheck.IsDefined(typeof(Nunit.Framework.TestAttribute),false);
 
116
                }
 
117
                
 
118
                private static bool HasObsoleteTestName(MethodInfo methodToCheck)
 
119
                {
 
120
                        return methodToCheck.Name.ToLower().StartsWith("test");
 
121
                }
 
122
 
 
123
                private static bool HasIgnoreAttribute(MethodInfo methodToCheck)
 
124
                {
 
125
                        Type ignoreMethodAttribute = typeof(Nunit.Framework.IgnoreAttribute);
 
126
                        object[] attributes = methodToCheck.GetCustomAttributes(ignoreMethodAttribute, false);
 
127
                        return attributes.Length == 1;
 
128
                }
 
129
 
 
130
                private static string GetIgnoreReason(MethodInfo methodToCheck)
 
131
                {
 
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;
 
137
 
 
138
                        return result;
 
139
                }
 
140
        }
 
141
}