1 // ***********************************************************************
2 // Copyright (c) 2008 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 
24 using System;
25 using System.Collections;
26 #if CLR_2_0 || CLR_4_0
27 using System.Collections.Generic;
28 #endif
29 using System.Reflection;
30 using NUnit.Framework.Api;
31 using NUnit.Framework.Extensibility;
32 using NUnit.Framework.Internal;
33 
34 namespace NUnit.Framework.Builders
35 {
36     /// <summary>
37     /// DataAttributeTestCaseProvider provides data for methods
38     /// annotated with any DataAttribute. For correct operation,
39     /// any new or custom Attributes must implement one of the
40     /// following interfaces:
41     ///    ITestCaseData
42     ///    ITestCaseSource
43     /// </summary>
44     public class DataAttributeTestCaseProvider : ITestCaseProvider
45     {
46         #region ITestCaseProvider Members
47 
48         /// <summary>
49         /// Determine whether any test cases are available for a parameterized method.
50         /// </summary>
51         /// <param name="method">A MethodInfo representing a parameterized test</param>
52         /// <returns>True if any cases are available, otherwise false.</returns>
HasTestCasesFor(MethodInfo method)53         public bool HasTestCasesFor(MethodInfo method)
54         {
55             return method.IsDefined(typeof(DataAttribute), false);
56         }
57 
58         /// <summary>
59         /// Return an IEnumerable providing test cases for use in
60         /// running a parameterized test.
61         /// </summary>
62         /// <param name="method"></param>
63         /// <returns></returns>
64 #if CLR_2_0 || CLR_4_0
GetTestCasesFor(MethodInfo method)65         public IEnumerable<ITestCaseData> GetTestCasesFor(MethodInfo method)
66         {
67             List<ITestCaseData> testCases = new List<ITestCaseData>();
68 #else
69        public IEnumerable GetTestCasesFor(MethodInfo method)
70        {
71            ArrayList testCases = new ArrayList();
72 #endif
73 
74             foreach (DataAttribute attr in method.GetCustomAttributes(typeof(DataAttribute), false))
75             {
76                 ITestCaseSource source = attr as ITestCaseSource;
77                 if (source != null)
78                 {
79                     // TODO: Create a class to handle exceptions for NUnitLite
80                     foreach (ITestCaseData testCase in ((ITestCaseSource)attr).GetTestCasesFor(method))
81                         testCases.Add(testCase);
82                     continue;
83                 }
84             }
85 
86             return testCases;
87         }
88         #endregion
89     }
90 }
91