[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Attributes / ValueSourceAttribute.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
37 namespace NUnit.Framework
38 {
39     /// <summary>
40     /// ValueSourceAttribute indicates the source to be used to
41     /// provide data for one parameter of a test method.
42     /// </summary>
43     [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = false)]
44     public class ValueSourceAttribute : DataAttribute, IParameterDataSource
45     {
46         #region Constructors
47
48         /// <summary>
49         /// Construct with the name of the factory - for use with languages
50         /// that don't support params arrays.
51         /// </summary>
52         /// <param name="sourceName">The name of a static method, property or field that will provide data.</param>
53         public ValueSourceAttribute(string sourceName)
54         {
55             SourceName = sourceName;
56         }
57
58         /// <summary>
59         /// Construct with a Type and name - for use with languages
60         /// that don't support params arrays.
61         /// </summary>
62         /// <param name="sourceType">The Type that will provide data</param>
63         /// <param name="sourceName">The name of a static method, property or field that will provide data.</param>
64         public ValueSourceAttribute(Type sourceType, string sourceName)
65         {
66             SourceType = sourceType;
67             SourceName = sourceName;
68         }
69
70         #endregion
71
72         #region Properties
73
74         /// <summary>
75         /// The name of a the method, property or fiend to be used as a source
76         /// </summary>
77         public string SourceName { get; private set; }
78
79         /// <summary>
80         /// A Type to be used as a source
81         /// </summary>
82         public Type SourceType { get; private set; }
83
84         #endregion
85
86         #region IParameterDataSource Members
87
88         /// <summary>
89         /// Gets an enumeration of data items for use as arguments
90         /// for a test method parameter.
91         /// </summary>
92         /// <param name="parameter">The parameter for which data is needed</param>
93         /// <returns>
94         /// An enumeration containing individual data items
95         /// </returns>
96         public IEnumerable GetData(IParameterInfo parameter)
97         {
98             return GetDataSource(parameter);
99         }
100
101         #endregion
102
103         #region Helper Methods
104
105         private IEnumerable GetDataSource(IParameterInfo parameter)
106         {
107             Type sourceType = SourceType ?? parameter.Method.TypeInfo.Type;
108
109             // TODO: Test this
110             if (SourceName == null)
111                 return Reflect.Construct(sourceType) as IEnumerable;
112
113             MemberInfo[] members = sourceType.GetMember(SourceName,
114                 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
115
116             var dataSource = GetDataSourceValue(members);
117
118             if (dataSource == null)
119             {
120                 ThrowInvalidDataSourceException();
121             }
122
123             return dataSource;
124         }
125
126         private static IEnumerable GetDataSourceValue(MemberInfo[] members)
127         {
128             if (members.Length != 1) return null;
129
130             MemberInfo member = members[0];
131
132             var field = member as FieldInfo;
133             if (field != null)
134             {
135                 if (field.IsStatic)
136                     return (IEnumerable)field.GetValue(null);
137
138                 ThrowInvalidDataSourceException();
139             }
140
141             var property = member as PropertyInfo;
142             if (property != null)
143             {
144                 if (property.GetGetMethod(true).IsStatic)
145                     return (IEnumerable)property.GetValue(null, null);
146
147                 ThrowInvalidDataSourceException();
148             }
149
150             var m = member as MethodInfo;
151             if (m != null)
152             {
153                 if (m.IsStatic)
154                     return (IEnumerable)m.Invoke(null, null);
155
156                 ThrowInvalidDataSourceException();
157             }
158
159             return null;
160         }
161
162         private static void ThrowInvalidDataSourceException()
163         {
164             throw new InvalidDataSourceException("The sourceName specified on a ValueSourceAttribute must refer to a non null static field, property or method.");
165         }
166
167         #endregion
168     }
169 }