1 // ***********************************************************************
2 // Copyright (c) 2008-2014 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections.Generic;
31 using NUnit.Framework.Interfaces;
32 using NUnit.Framework.Internal.Commands;
34 namespace NUnit.Framework.Internal.Builders
37 /// Class to build ether a parameterized or a normal NUnitTestMethod.
38 /// There are four cases that the builder must deal with:
39 /// 1. The method needs no params and none are provided
40 /// 2. The method needs params and they are provided
41 /// 3. The method needs no params but they are provided in error
42 /// 4. The method needs params but they are not provided
43 /// This could have been done using two different builders, but it
44 /// turned out to be simpler to have just one. The BuildFrom method
45 /// takes a different branch depending on whether any parameters are
46 /// provided, but all four cases are dealt with in lower-level methods
48 public class DefaultTestCaseBuilder : ITestCaseBuilder
50 private NUnitTestCaseBuilder _nunitTestCaseBuilder = new NUnitTestCaseBuilder();
52 #region ITestCaseBuilder Methods
54 /// Determines if the method can be used to build an NUnit test
55 /// test method of some kind. The method must normally be marked
56 /// with an identifying attribute for this to be true.
58 /// Note that this method does not check that the signature
59 /// of the method for validity. If we did that here, any
60 /// test methods with invalid signatures would be passed
61 /// over in silence in the test run. Since we want such
62 /// methods to be reported, the check for validity is made
63 /// in BuildFrom rather than here.
65 /// <param name="method">An IMethodInfo for the method being used as a test method</param>
66 /// <returns>True if the builder can create a test case from this method</returns>
67 public bool CanBuildFrom(IMethodInfo method)
69 return method.IsDefined<ITestBuilder>(false)
70 || method.IsDefined<ISimpleTestBuilder>(false);
74 /// Build a Test from the provided MethodInfo. Depending on
75 /// whether the method takes arguments and on the availability
76 /// of test case data, this method may return a single test
77 /// or a group of tests contained in a ParameterizedMethodSuite.
79 /// <param name="method">The method for which a test is to be built</param>
80 /// <returns>A Test representing one or more method invocations</returns>
81 public Test BuildFrom(IMethodInfo method)
83 return BuildFrom(method, null);
88 #region ITestCaseBuilder2 Members
91 /// Determines if the method can be used to build an NUnit test
92 /// test method of some kind. The method must normally be marked
93 /// with an identifying attribute for this to be true.
95 /// Note that this method does not check that the signature
96 /// of the method for validity. If we did that here, any
97 /// test methods with invalid signatures would be passed
98 /// over in silence in the test run. Since we want such
99 /// methods to be reported, the check for validity is made
100 /// in BuildFrom rather than here.
102 /// <param name="method">An IMethodInfo for the method being used as a test method</param>
103 /// <param name="parentSuite">The test suite being built, to which the new test would be added</param>
104 /// <returns>True if the builder can create a test case from this method</returns>
105 public bool CanBuildFrom(IMethodInfo method, Test parentSuite)
107 return CanBuildFrom(method);
111 /// Build a Test from the provided MethodInfo. Depending on
112 /// whether the method takes arguments and on the availability
113 /// of test case data, this method may return a single test
114 /// or a group of tests contained in a ParameterizedMethodSuite.
116 /// <param name="method">The method for which a test is to be built</param>
117 /// <param name="parentSuite">The test fixture being populated, or null</param>
118 /// <returns>A Test representing one or more method invocations</returns>
119 public Test BuildFrom(IMethodInfo method, Test parentSuite)
121 var tests = new List<TestMethod>();
123 List<ITestBuilder> builders = new List<ITestBuilder>(
124 (ITestBuilder[])method.GetCustomAttributes<ITestBuilder>(false));
126 // See if we need a CombinatorialAttribute added
127 bool needCombinatorial = true;
128 foreach (var attr in builders)
130 if (attr is CombiningStrategyAttribute)
131 needCombinatorial = false;
134 // We could check to see if here are any data attributes specified
135 // on the parameters but that's what CombinatorialAttribute does
136 // and it simply won't return any cases if it finds nothing.
137 // TODO: We need to add some other ITestBuilder than a combinatorial attribute
138 // because we want the attribute to generate an error if it's present on
140 if (needCombinatorial)
141 builders.Add(new CombinatorialAttribute());
143 foreach (var attr in builders)
145 foreach (var test in attr.BuildFrom(method, parentSuite))
149 return tests.Count > 0
150 ? BuildParameterizedMethodSuite(method, tests)
151 : BuildSingleTestMethod(method, parentSuite);
156 #region Helper Methods
159 /// Builds a ParameterizedMethodSuite containing individual test cases.
161 /// <param name="method">The method for which a test is to be built.</param>
162 /// <param name="tests">The list of test cases to include.</param>
163 /// <returns>A ParameterizedMethodSuite populated with test cases</returns>
164 private Test BuildParameterizedMethodSuite(IMethodInfo method, IEnumerable<TestMethod> tests)
166 ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method);
167 methodSuite.ApplyAttributesToTest(method.MethodInfo);
169 foreach (TestMethod test in tests)
170 methodSuite.Add(test);
176 /// Build a simple, non-parameterized TestMethod for this method.
178 /// <param name="method">The MethodInfo for which a test is to be built</param>
179 /// <param name="suite">The test suite for which the method is being built</param>
180 /// <returns>A TestMethod.</returns>
181 private Test BuildSingleTestMethod(IMethodInfo method, Test suite)
183 var builders = (ISimpleTestBuilder[])method.GetCustomAttributes<ISimpleTestBuilder>(false);
184 return builders.Length > 0
185 ? builders[0].BuildFrom(method, suite)
186 : _nunitTestCaseBuilder.BuildTestMethod(method, suite, null);