[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / Builders / DefaultTestCaseBuilder.cs
1 // ***********************************************************************
2 // Copyright (c) 2008-2014 Charlie Poole
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Collections.Generic;
31 using NUnit.Framework.Interfaces;
32 using NUnit.Framework.Internal.Commands;
33
34 namespace NUnit.Framework.Internal.Builders
35 {
36     /// <summary>
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
47     /// </summary>
48     public class DefaultTestCaseBuilder : ITestCaseBuilder
49     {
50         private NUnitTestCaseBuilder _nunitTestCaseBuilder = new NUnitTestCaseBuilder();
51
52         #region ITestCaseBuilder Methods
53         /// <summary>
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.
57         /// 
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.
64         /// </summary>
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)
68         {
69             return method.IsDefined<ITestBuilder>(false)
70                 || method.IsDefined<ISimpleTestBuilder>(false);
71         }
72
73         /// <summary>
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.
78         /// </summary>
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)
82         {
83             return BuildFrom(method, null);
84         }
85
86         #endregion
87
88         #region ITestCaseBuilder2 Members
89
90         /// <summary>
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.
94         /// 
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.
101         /// </summary>
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)
106         {
107             return CanBuildFrom(method);
108         }
109
110         /// <summary>
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.
115         /// </summary>
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)
120         {
121             var tests = new List<TestMethod>();
122
123             List<ITestBuilder> builders = new List<ITestBuilder>(
124                 (ITestBuilder[])method.GetCustomAttributes<ITestBuilder>(false));
125
126             // See if we need a CombinatorialAttribute added
127             bool needCombinatorial = true;
128             foreach (var attr in builders)
129             {
130                 if (attr is CombiningStrategyAttribute)
131                     needCombinatorial = false;
132             }
133
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
139             // a generic method.
140             if (needCombinatorial)
141                 builders.Add(new CombinatorialAttribute());
142
143             foreach (var attr in builders)
144             {
145                 foreach (var test in attr.BuildFrom(method, parentSuite))
146                     tests.Add(test);
147             }
148
149             return tests.Count > 0
150                 ? BuildParameterizedMethodSuite(method, tests)
151                 : BuildSingleTestMethod(method, parentSuite);
152         }
153
154         #endregion
155
156         #region Helper Methods
157
158         /// <summary>
159         /// Builds a ParameterizedMethodSuite containing individual test cases.
160         /// </summary>
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)
165         {
166             ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method);
167             methodSuite.ApplyAttributesToTest(method.MethodInfo);
168
169             foreach (TestMethod test in tests)
170                 methodSuite.Add(test);
171
172             return methodSuite;
173         }
174
175         /// <summary>
176         /// Build a simple, non-parameterized TestMethod for this method.
177         /// </summary>
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)
182         {
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);
187         }
188
189         #endregion
190     }
191 }