[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / ParameterWrapper.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.Collections.Generic;
31 using System.Reflection;
32 using NUnit.Compatibility;
33 using NUnit.Framework.Interfaces;
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 ParameterWrapper class wraps a ParameterInfo so that it may
45     /// be used in a platform-independent manner.
46     /// </summary>
47     public class ParameterWrapper : IParameterInfo
48     {
49         /// <summary>
50         /// Construct a ParameterWrapper for a given method and parameter
51         /// </summary>
52         /// <param name="method"></param>
53         /// <param name="parameterInfo"></param>
54         public ParameterWrapper(IMethodInfo method, ParameterInfo parameterInfo)
55         {
56             Method = method;
57             ParameterInfo = parameterInfo;
58         }
59
60         #region Properties
61
62 #if !NETCF
63         /// <summary>
64         /// Gets a value indicating whether the parameter is optional
65         /// </summary>
66         public bool IsOptional
67         {
68             get { return ParameterInfo.IsOptional; }
69         }
70 #endif
71
72         /// <summary>
73         /// Gets an IMethodInfo representing the method for which this is a parameter.
74         /// </summary>
75         public IMethodInfo Method { get; private set; }
76
77         /// <summary>
78         /// Gets the underlying ParameterInfo
79         /// </summary>
80         public ParameterInfo ParameterInfo { get; private set; }
81
82         /// <summary>
83         /// Gets the Type of the parameter
84         /// </summary>
85         public Type ParameterType
86         {
87             get { return ParameterInfo.ParameterType; }
88         }
89
90         #endregion
91
92         #region Methods
93
94         /// <summary>
95         /// Returns an array of custom attributes of the specified type applied to this method
96         /// </summary>
97         public T[] GetCustomAttributes<T>(bool inherit) where T : class
98         {
99 #if PORTABLE
100             var allAttributes = ParameterInfo.GetCustomAttributes();
101             List<string> attributeDic = new List<string>();
102             foreach (Attribute atb in allAttributes)
103             {
104                 attributeDic.Add(atb.GetType().FullName);
105             }
106             var assembly = ParameterInfo.ParameterType.GetTypeInfo().Assembly;
107             List<T> objects = new List<T>();
108
109             string path = System.IO.Path.GetDirectoryName(assembly.Location);
110             if (!Directory.Exists(path))
111             {
112                 TLogger.WriteError(TLogger.ExceptionTag, "" + path + " - not a directory");
113                 return objects.ToArray();
114             }
115             foreach (var assemblyPath in Directory.GetFiles(path, "*.Tests.dll"))
116             {
117                 IEnumerable<Type> types;
118                 try
119                 {
120                     Assembly please = AssemblyHelper.Load(assemblyPath);
121                     if (please == null) continue;
122                     types = please.GetTypes().Where(p => !p.GetTypeInfo().IsAbstract && p.GetTypeInfo().IsClass && p.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T)));
123                 }
124                 catch (Exception)
125                 {
126                     //TLogger.Write(TLogger.ExceptionTag, ex.ToString());
127                     continue;
128                 }
129                 for (int i = 0; i < types.Count(); i++)
130                 {
131                     try
132                     {
133                         if (attributeDic.Contains(types.ElementAt(i).FullName))
134                         {
135                             objects.Add((T)Activator.CreateInstance(types.ElementAt(i)));
136                         }
137                     }
138                     catch (Exception)
139                     {
140                         //TLogger.Write(TLogger.ExceptionTag, ex.ToString());
141                     }
142                 }
143             }
144
145             return objects.ToArray();
146 #else
147             return (T[])ParameterInfo.GetCustomAttributes(typeof(T), inherit);
148 #endif
149         }
150
151         /// <summary>
152         /// Gets a value indicating whether one or more attributes of the specified type are defined on the parameter.
153         /// </summary>
154         public bool IsDefined<T>(bool inherit)
155         {
156 #if PORTABLE
157             return ParameterInfo.GetCustomAttributes(inherit).Any(a => typeof(T).IsAssignableFrom(a.GetType()));
158 #else
159             return ParameterInfo.IsDefined(typeof(T), inherit);
160 #endif
161         }
162
163         #endregion
164
165         #region extra
166         private string GetAssemblyName(string assemblyFullPath)
167         {
168
169             string[] delimiter1 = { "\\" };
170             string[] delimiter2 = { "/" };
171             string[] delimiterDot = { "." };
172             string[] strAry;
173             string returnValue = "";
174             try
175             {
176                 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
177
178                 if (strAry.Length < 2)
179                     strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
180
181                 foreach (string str in strAry)
182                 {
183                     if (str.Contains("Tests.dll"))
184                     {
185                         string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
186                         returnValue = strSplit[0];
187                         //                      LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
188                         break;
189                     }
190                 }
191             }
192             catch (Exception e)
193             {
194                 LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, e.ToString());
195             }
196
197             return returnValue;
198         }
199         #endregion
200     }
201 }