[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / MethodWrapper.cs
1 // ***********************************************************************
2 // Copyright (c) 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.Reflection;
31 using NUnit.Compatibility;
32 using NUnit.Framework.Interfaces;
33 using System.Collections.Generic;
34 using System.IO;
35 using NUnit.Framework.TUnit;
36
37 #if PORTABLE
38 using System.Linq;
39 #endif
40
41 namespace NUnit.Framework.Internal
42 {
43     /// <summary>
44     /// The MethodWrapper class wraps a MethodInfo so that it may
45     /// be used in a platform-independent manner.
46     /// </summary>
47     public class MethodWrapper : IMethodInfo
48     {
49         /// <summary>
50         /// Construct a MethodWrapper for a Type and a MethodInfo.
51         /// </summary>
52         public MethodWrapper(Type type, MethodInfo method)
53         {
54             TypeInfo = new TypeWrapper(type);
55             MethodInfo = method;
56         }
57
58         /// <summary>
59         /// Construct a MethodInfo for a given Type and method name.
60         /// </summary>
61         public MethodWrapper(Type type, string methodName)
62         {
63             TypeInfo = new TypeWrapper(type);
64             MethodInfo = type.GetMethod(methodName);
65         }
66
67         #region IMethod Implementation
68
69         /// <summary>
70         /// Gets the Type from which this method was reflected.
71         /// </summary>
72         public ITypeInfo TypeInfo { get; private set; }
73
74         /// <summary>
75         /// Gets the MethodInfo for this method.
76         /// </summary>
77         public MethodInfo MethodInfo { get; private set; }
78
79         /// <summary>
80         /// Gets the name of the method.
81         /// </summary>
82         public string Name
83         {
84             get { return MethodInfo.Name; }
85         }
86
87         /// <summary>
88         /// Gets a value indicating whether the method is abstract.
89         /// </summary>
90         public bool IsAbstract
91         {
92             get { return MethodInfo.IsAbstract; }
93         }
94
95         /// <summary>
96         /// Gets a value indicating whether the method is public.
97         /// </summary>
98         public bool IsPublic
99         {
100             get { return MethodInfo.IsPublic; }
101         }
102
103         /// <summary>
104         /// Gets a value indicating whether the method contains unassigned generic type parameters.
105         /// </summary>
106         public bool ContainsGenericParameters
107         {
108             get { return MethodInfo.ContainsGenericParameters; }
109         }
110
111         /// <summary>
112         /// Gets a value indicating whether the method is a generic method.
113         /// </summary>
114         public bool IsGenericMethod
115         {
116             get { return MethodInfo.IsGenericMethod; }
117         }
118
119         /// <summary>
120         /// Gets a value indicating whether the MethodInfo represents the definition of a generic method.
121         /// </summary>
122         public bool IsGenericMethodDefinition
123         {
124             get { return MethodInfo.IsGenericMethodDefinition; }
125         }
126
127         /// <summary>
128         /// Gets the return Type of the method.
129         /// </summary>
130         public ITypeInfo ReturnType
131         {
132             get { return new TypeWrapper(MethodInfo.ReturnType); }
133         }
134
135         /// <summary>
136         /// Gets the parameters of the method.
137         /// </summary>
138         /// <returns></returns>
139         public IParameterInfo[] GetParameters()
140         {
141             var parameters = MethodInfo.GetParameters();
142             var result = new IParameterInfo[parameters.Length];
143
144             for (int i = 0; i < parameters.Length; i++)
145                 result[i] = new ParameterWrapper(this, parameters[i]);
146
147             return result;
148         }
149
150         /// <summary>
151         /// Returns the Type arguments of a generic method or the Type parameters of a generic method definition.
152         /// </summary>
153         public Type[] GetGenericArguments()
154         {
155             return MethodInfo.GetGenericArguments();
156         }
157
158         /// <summary>
159         /// Replaces the type parameters of the method with the array of types provided and returns a new IMethodInfo.
160         /// </summary>
161         /// <param name="typeArguments">The type arguments to be used</param>
162         /// <returns>A new IMethodInfo with the type arguments replaced</returns>
163         public IMethodInfo MakeGenericMethod(params Type[] typeArguments)
164         {
165             return new MethodWrapper(TypeInfo.Type, MethodInfo.MakeGenericMethod(typeArguments));
166         }
167
168         /// <summary>
169         /// Returns an array of custom attributes of the specified type applied to this method
170         /// </summary>
171         public T[] GetCustomAttributes<T>(bool inherit) where T : class
172         {
173 #if PORTABLE
174             //return MethodInfo.GetAttributes<T>(inherit).ToArray();
175             var allAttributes = MethodInfo.GetCustomAttributes();
176             List<string> attributeDic = new List<string>();
177             foreach (Attribute atb in allAttributes)
178             {
179                 attributeDic.Add(atb.GetType().FullName);
180             }
181             var assembly = MethodInfo.DeclaringType.GetTypeInfo().Assembly;
182             List<T> objects = new List<T>();
183
184             string path = System.IO.Path.GetDirectoryName(assembly.Location);
185
186             if (!Directory.Exists(path))
187             {
188                 TLogger.WriteError(TLogger.ExceptionTag, "" + path + " - not a directory");
189                 return objects.ToArray();
190             }
191             foreach (var assemblyPath in Directory.GetFiles(path, "*.Tests.dll"))
192             {
193
194                 IEnumerable<Type> types;
195                 try
196                 {
197                     Assembly please = AssemblyHelper.Load(assemblyPath);
198                     if (please == null) continue;
199                     types = please.GetTypes().Where(p => !p.GetTypeInfo().IsAbstract && p.GetTypeInfo().IsClass && p.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T)));
200                 }
201                 catch (Exception)
202                 {
203                     continue;
204                 }
205                 for (int i = 0; i < types.Count(); i++)
206                 {
207                     try
208                     {
209                         if (attributeDic.Contains(types.ElementAt(i).FullName))
210                         {
211                             objects.Add((T)Activator.CreateInstance(types.ElementAt(i)));
212                         }
213                     }
214                     catch (Exception)
215                     {
216                         //TLogger.Write(TLogger.ExceptionTag, ex.ToString());
217                     }
218                 }
219             }
220
221             return objects.ToArray();
222 #else
223             return (T[])MethodInfo.GetCustomAttributes(typeof(T), inherit);
224 #endif
225         }
226
227         /// <summary>
228         /// Gets a value indicating whether one or more attributes of the spcified type are defined on the method.
229         /// </summary>
230         public bool IsDefined<T>(bool inherit)
231         {
232 #if PORTABLE
233             return MethodInfo.GetCustomAttributes(inherit).Any(a => typeof(T).IsAssignableFrom(a.GetType()));
234 #else
235             return MethodInfo.IsDefined(typeof(T), inherit);
236 #endif
237         }
238
239         /// <summary>
240         /// Invokes the method, converting any TargetInvocationException to an NUnitException.
241         /// </summary>
242         /// <param name="fixture">The object on which to invoke the method</param>
243         /// <param name="args">The argument list for the method</param>
244         /// <returns>The return value from the invoked method</returns>
245         public object Invoke(object fixture, params object[] args)
246         {
247             return Reflect.InvokeMethod(MethodInfo, fixture, args);
248         }
249
250         /// <summary>
251         /// Override ToString() so that error messages in NUnit's own tests make sense
252         /// </summary>
253         public override string ToString()
254         {
255             return MethodInfo.Name;
256         }
257
258         #endregion
259
260         #region extra
261         private string GetAssemblyName(string assemblyFullPath)
262         {
263
264             string[] delimiter1 = { "\\" };
265             string[] delimiter2 = { "/" };
266             string[] delimiterDot = { "." };
267             string[] strAry;
268             string returnValue = "";
269             try
270             {
271                 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
272
273                 if (strAry.Length < 2)
274                     strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
275
276                 foreach (string str in strAry)
277                 {
278                     if (str.Contains(".Tests.dll"))
279                     {
280                         string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
281                         returnValue = strSplit[0];
282                         //LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
283                         break;
284                     }
285                 }
286             }
287             catch (Exception)
288             {
289             }
290
291             return returnValue;
292         }
293         #endregion
294     }
295 }