[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Attributes / TestFixtureSourceAttribute.cs
1 // ***********************************************************************
2 // Copyright (c) 2008-2015 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;
31 using System.Collections.Generic;
32 using System.Reflection;
33 using NUnit.Compatibility;
34 using NUnit.Framework.Interfaces;
35 using NUnit.Framework.Internal;
36 using NUnit.Framework.Internal.Builders;
37
38 namespace NUnit.Framework
39 {
40     /// <summary>
41     /// TestCaseSourceAttribute indicates the source to be used to
42     /// provide test fixture instances for a test class.
43     /// </summary>
44     [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
45     public class TestFixtureSourceAttribute : NUnitAttribute, IFixtureBuilder
46     {
47         private readonly NUnitTestFixtureBuilder _builder = new NUnitTestFixtureBuilder();
48
49         /// <summary>
50         /// Error message string is public so the tests can use it
51         /// </summary>
52         public const string MUST_BE_STATIC = "The sourceName specified on a TestCaseSourceAttribute must refer to a static field, property or method.";
53
54         #region Constructors
55
56         /// <summary>
57         /// Construct with the name of the method, property or field that will provide data
58         /// </summary>
59         /// <param name="sourceName">The name of a static method, property or field that will provide data.</param>
60         public TestFixtureSourceAttribute(string sourceName)
61         {
62             this.SourceName = sourceName;
63         }
64
65         /// <summary>
66         /// Construct with a Type and name
67         /// </summary>
68         /// <param name="sourceType">The Type that will provide data</param>
69         /// <param name="sourceName">The name of a static method, property or field that will provide data.</param>
70         public TestFixtureSourceAttribute(Type sourceType, string sourceName)
71         {
72             this.SourceType = sourceType;
73             this.SourceName = sourceName;
74         }
75
76         /// <summary>
77         /// Construct with a Type
78         /// </summary>
79         /// <param name="sourceType">The type that will provide data</param>
80         public TestFixtureSourceAttribute(Type sourceType)
81         {
82             this.SourceType = sourceType;
83         }
84
85         #endregion
86
87         #region Properties
88
89         /// <summary>
90         /// The name of a the method, property or fiend to be used as a source
91         /// </summary>
92         public string SourceName { get; private set; }
93
94         /// <summary>
95         /// A Type to be used as a source
96         /// </summary>
97         public Type SourceType { get; private set; }
98
99         /// <summary>
100         /// Gets or sets the category associated with every fixture created from 
101         /// this attribute. May be a single category or a comma-separated list.
102         /// </summary>
103         public string Category { get; set; }
104
105         #endregion
106
107         #region IFixtureBuilder Members
108
109         /// <summary>
110         /// Construct one or more TestFixtures from a given Type,
111         /// using available parameter data.
112         /// </summary>
113         /// <param name="typeInfo">The TypeInfo for which fixures are to be constructed.</param>
114         /// <returns>One or more TestFixtures as TestSuite</returns>
115         public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo)
116         {
117             Type sourceType = SourceType ?? typeInfo.Type;
118
119             foreach (TestFixtureParameters parms in GetParametersFor(sourceType))
120                 yield return _builder.BuildFrom(typeInfo, parms);
121         }
122
123         #endregion
124
125         #region Helper Methods
126
127         /// <summary>
128         /// Returns a set of ITestFixtureData items for use as arguments
129         /// to a parameterized test fixture.
130         /// </summary>
131         /// <param name="sourceType">The type for which data is needed.</param>
132         /// <returns></returns>
133         public IEnumerable<ITestFixtureData> GetParametersFor(Type sourceType)
134         {
135             List<ITestFixtureData> data = new List<ITestFixtureData>();
136
137             try
138             {
139                 IEnumerable source = GetTestFixtureSource(sourceType);
140
141                 if (source != null)
142                 {
143                     foreach (object item in source)
144                     {
145                         var parms = item as ITestFixtureData;
146
147                         if (parms == null)
148                         {
149                             object[] args = item as object[];
150                             if (args == null)
151                             {
152                                 args = new object[] { item };
153                             }
154
155                             parms = new TestFixtureParameters(args);
156                         }
157
158                         if (this.Category != null)
159                             foreach (string cat in this.Category.Split(new char[] { ',' }))
160                                 parms.Properties.Add(PropertyNames.Category, cat);
161
162                         data.Add(parms);
163                     }
164                 }
165             }
166             catch (Exception ex)
167             {
168                 data.Clear();
169                 data.Add(new TestFixtureParameters(ex));
170             }
171
172             return data;
173         }
174
175         private IEnumerable GetTestFixtureSource(Type sourceType)
176         {
177             // Handle Type implementing IEnumerable separately
178             if (SourceName == null)
179                 return Reflect.Construct(sourceType) as IEnumerable;
180
181             MemberInfo[] members = sourceType.GetMember(SourceName,
182                     BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
183
184             if (members.Length == 1)
185             {
186                 MemberInfo member = members[0];
187
188                 var field = member as FieldInfo;
189                 if (field != null)
190                     return field.IsStatic
191                         ? (IEnumerable)field.GetValue(null)
192                         : SourceMustBeStaticError();
193
194                 var property = member as PropertyInfo;
195                 if (property != null)
196                     return property.GetGetMethod(true).IsStatic
197                         ? (IEnumerable)property.GetValue(null, null)
198                         : SourceMustBeStaticError();
199
200                 var m = member as MethodInfo;
201                 if (m != null)
202                     return m.IsStatic
203                         ? (IEnumerable)m.Invoke(null, null)
204                         : SourceMustBeStaticError();
205             }
206
207             return null;
208         }
209
210         private static IEnumerable SourceMustBeStaticError()
211         {
212             var parms = new TestFixtureParameters();
213             parms.RunState = RunState.NotRunnable;
214             parms.Properties.Set(PropertyNames.SkipReason, MUST_BE_STATIC);
215             return new TestFixtureParameters[] { parms };
216         }
217
218         #endregion
219     }
220 }