[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / TypeWrapper.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.Linq;
31 using System.Reflection;
32 using NUnit.Compatibility;
33 using NUnit.Framework.Interfaces;
34 using System.Collections.Generic;
35 using System.IO;
36 using NUnit.Framework.TUnit;
37
38 namespace NUnit.Framework.Internal
39 {
40     /// <summary>
41     /// The TypeWrapper class wraps a Type so it may be used in
42     /// a platform-independent manner.
43     /// </summary>
44     public class TypeWrapper : ITypeInfo
45     {
46         /// <summary>
47         /// Construct a TypeWrapper for a specified Type.
48         /// </summary>
49         public TypeWrapper(Type type)
50         {
51             Guard.ArgumentNotNull(type, "Type");
52
53             Type = type;
54         }
55
56         /// <summary>
57         /// Gets the underlying Type on which this TypeWrapper is based.
58         /// </summary>
59         public Type Type { get; private set; }
60
61         /// <summary>
62         /// Gets the base type of this type as an ITypeInfo
63         /// </summary>
64         public ITypeInfo BaseType
65         {
66             get
67             {
68                 var baseType = Type.GetTypeInfo().BaseType;
69
70                 return baseType != null
71                     ? new TypeWrapper(baseType)
72                     : null;
73             }
74         }
75
76         /// <summary>
77         /// Gets the Name of the Type
78         /// </summary>
79         public string Name
80         {
81             get { return Type.Name; }
82         }
83
84         /// <summary>
85         /// Gets the FullName of the Type
86         /// </summary>
87         public string FullName
88         {
89             get { return Type.FullName; }
90         }
91
92         /// <summary>
93         /// Gets the assembly in which the type is declared
94         /// </summary>
95         public Assembly Assembly
96         {
97             get { return Type.GetTypeInfo().Assembly; }
98         }
99
100         /// <summary>
101         /// Gets the namespace of the Type
102         /// </summary>
103         public string Namespace
104         {
105             get { return Type.Namespace; }
106         }
107
108         /// <summary>
109         /// Gets a value indicating whether the type is abstract.
110         /// </summary>
111         public bool IsAbstract
112         {
113             get { return Type.GetTypeInfo().IsAbstract; }
114         }
115
116         /// <summary>
117         /// Gets a value indicating whether the Type is a generic Type
118         /// </summary>
119         public bool IsGenericType
120         {
121             get { return Type.GetTypeInfo().IsGenericType; }
122         }
123
124         /// <summary>
125         /// Returns true if the Type wrapped is T
126         /// </summary>
127         public bool IsType(Type type)
128         {
129             return Type == type;
130         }
131
132         /// <summary>
133         /// Gets a value indicating whether the Type has generic parameters that have not been replaced by specific Types.
134         /// </summary>
135         public bool ContainsGenericParameters
136         {
137             get { return Type.GetTypeInfo().ContainsGenericParameters; }
138         }
139
140         /// <summary>
141         /// Gets a value indicating whether the Type is a generic Type definition
142         /// </summary>
143         public bool IsGenericTypeDefinition
144         {
145             get { return Type.GetTypeInfo().IsGenericTypeDefinition; }
146         }
147
148         /// <summary>
149         /// Gets a value indicating whether the type is sealed.
150         /// </summary>
151         public bool IsSealed
152         {
153             get { return Type.GetTypeInfo().IsSealed; }
154         }
155
156         /// <summary>
157         /// Gets a value indicating whether this type represents a static class.
158         /// </summary>
159         public bool IsStaticClass
160         {
161             get { return Type.GetTypeInfo().IsSealed && Type.GetTypeInfo().IsAbstract; }
162         }
163
164         /// <summary>
165         /// Get the display name for this type
166         /// </summary>
167         public string GetDisplayName()
168         {
169             return TypeHelper.GetDisplayName(Type);
170         }
171
172         /// <summary>
173         /// Get the display name for an object of this type, constructed with the specified args.
174         /// </summary>
175         public string GetDisplayName(object[] args)
176         {
177             return TypeHelper.GetDisplayName(Type, args);
178         }
179
180         /// <summary>
181         /// Returns a new ITypeInfo representing an instance of this generic Type using the supplied Type arguments
182         /// </summary>
183         public ITypeInfo MakeGenericType(Type[] typeArgs)
184         {
185             return new TypeWrapper(Type.MakeGenericType(typeArgs));
186         }
187
188         /// <summary>
189         /// Returns a Type representing a generic type definition from which this Type can be constructed.
190         /// </summary>
191         public Type GetGenericTypeDefinition()
192         {
193             return Type.GetGenericTypeDefinition();
194         }
195
196         /// <summary>
197         /// Returns an array of custom attributes of the specified type applied to this type
198         /// </summary>
199         public T[] GetCustomAttributes<T>(bool inherit) where T : class
200         {
201 #if PORTABLE
202             var allAttribute = Type.GetTypeInfo().GetCustomAttributes();
203             List<string> attributeDic = new List<string>();
204             foreach (Attribute atb in allAttribute)
205             {
206                 attributeDic.Add(atb.GetType().FullName);
207             }
208
209             List<T> objects = new List<T>();
210
211             string path = System.IO.Path.GetDirectoryName(Assembly.Location);
212             if (!Directory.Exists(path))
213             {
214                 TLogger.WriteError(TLogger.ExceptionTag, "" + path + " - not a directory");
215                 return objects.ToArray();
216             }
217             foreach (var assemblyPath in Directory.GetFiles(path, "*.Tests.dll"))
218             {
219                 IEnumerable<Type> types;
220                 try
221                 {
222                     Assembly please = AssemblyHelper.Load(assemblyPath);
223                     if (please == null) continue;
224                     types = please.GetTypes().Where(p => !p.GetTypeInfo().IsAbstract && p.GetTypeInfo().IsClass && p.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T)));
225                 }
226                 catch (Exception)
227                 {
228                     continue;
229                 }
230                 for (int i = 0; i < types.Count(); i++)
231                 {
232                     try
233                     {
234                         if (attributeDic.Contains(types.ElementAt(i).FullName))
235                         {
236                             objects.Add((T)Activator.CreateInstance(types.ElementAt(i)));
237                         }
238                     }
239                     catch (Exception)
240                     {
241                     }
242                 }
243             }
244
245             return objects.ToArray();
246 #else
247             return (T[])Type.GetCustomAttributes(typeof(T), inherit);
248 #endif
249         }
250
251         /// <summary>
252         /// Returns a value indicating whether the type has an attribute of the specified type.
253         /// </summary>
254         /// <typeparam name="T"></typeparam>
255         /// <param name="inherit"></param>
256         /// <returns></returns>
257         public bool IsDefined<T>(bool inherit)
258         {
259 #if PORTABLE
260             return Type.GetTypeInfo().GetCustomAttributes(inherit).Any(a => typeof(T).IsAssignableFrom(a.GetType()));
261 #else
262             return Type.GetTypeInfo().IsDefined(typeof(T), inherit);
263 #endif
264         }
265
266         /// <summary>
267         /// Returns a flag indicating whether this type has a method with an attribute of the specified type.
268         /// </summary>
269         /// <param name="attributeType"></param>
270         /// <returns></returns>
271         public bool HasMethodWithAttribute(Type attributeType)
272         {
273             return Reflect.HasMethodWithAttribute(Type, attributeType);
274         }
275
276         /// <summary>
277         /// Returns an array of IMethodInfos for methods of this Type
278         /// that match the specified flags.
279         /// </summary>
280         public IMethodInfo[] GetMethods(BindingFlags flags)
281         {
282             var methods = Type.GetMethods(flags);
283             var result = new MethodWrapper[methods.Length];
284
285             for (int i = 0; i < methods.Length; i++)
286                 result[i] = new MethodWrapper(Type, methods[i]);
287
288             return result;
289         }
290
291         /// <summary>
292         /// Gets the public constructor taking the specified argument Types
293         /// </summary>
294         public ConstructorInfo GetConstructor(Type[] argTypes)
295         {
296             return Type.GetConstructors()
297                 .Where(c => c.GetParameters().ParametersMatch(argTypes))
298                 .FirstOrDefault();
299         }
300
301         /// <summary>
302         /// Returns a value indicating whether this Type has a public constructor taking the specified argument Types.
303         /// </summary>
304         public bool HasConstructor(Type[] argTypes)
305         {
306             return GetConstructor(argTypes) != null;
307         }
308
309         /// <summary>
310         /// Construct an object of this Type, using the specified arguments.
311         /// </summary>
312         public object Construct(object[] args)
313         {
314             return Reflect.Construct(Type, args);
315         }
316
317         /// <summary>
318         /// Override ToString() so that error messages in NUnit's own tests make sense
319         /// </summary>
320         public override string ToString()
321         {
322             return Type.ToString();
323         }
324
325         #region extra
326         private string GetAssemblyName(string assemblyFullPath)
327         {
328
329             string[] delimiter1 = { "\\" };
330             string[] delimiter2 = { "/" };
331             string[] delimiterDot = { "." };
332             string[] strAry;
333             string returnValue = "";
334             try
335             {
336                 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
337
338                 if (strAry.Length < 2)
339                     strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
340
341                 foreach (string str in strAry)
342                 {
343                     if (str.Contains("Tests.dll"))
344                     {
345                         string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
346                         returnValue = strSplit[0];
347                         //                      LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
348                         break;
349                     }
350                 }
351             }
352             catch (Exception e)
353             {
354                 LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, e.ToString());
355             }
356
357             return returnValue;
358         }
359         #endregion
360     }
361 }