[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Compatibility / ReflectionExtensions.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 System.Linq;
33
34 namespace NUnit.Compatibility
35 {
36     /// <summary>
37     /// Type extensions that apply to all target frameworks
38     /// </summary>
39     public static class AdditionalTypeExtensions
40     {
41         /// <summary>
42         /// Determines if the given <see cref="Type"/> array is castable/matches the <see cref="ParameterInfo"/> array.
43         /// </summary>
44         /// <param name="pinfos"></param>
45         /// <param name="ptypes"></param>
46         /// <returns></returns>
47         public static bool ParametersMatch(this ParameterInfo[] pinfos, Type[] ptypes)
48         {
49             if (pinfos.Length != ptypes.Length)
50                 return false;
51
52             for (int i = 0; i < pinfos.Length; i++)
53             {
54                 if (!pinfos[i].ParameterType.IsCastableFrom(ptypes[i]))
55                     return false;
56             }
57             return true;
58         }
59
60         // §6.1.2 (Implicit numeric conversions) of the specification
61         static Dictionary<Type, List<Type>> convertibleValueTypes = new Dictionary<Type, List<Type>>() {
62             { typeof(decimal), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char) } },
63             { typeof(double), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char), typeof(float) } },
64             { typeof(float), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char), typeof(float) } },
65             { typeof(ulong), new List<Type> { typeof(byte), typeof(ushort), typeof(uint), typeof(char) } },
66             { typeof(long), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(char) } },
67             { typeof(uint), new List<Type> { typeof(byte), typeof(ushort), typeof(char) } },
68             { typeof(int), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(char) } },
69             { typeof(ushort), new List<Type> { typeof(byte), typeof(char) } },
70             { typeof(short), new List<Type> { typeof(byte) } }
71         };
72
73         /// <summary>
74         /// Determines if one type can be implicitly converted from another
75         /// </summary>
76         /// <param name="to"></param>
77         /// <param name="from"></param>
78         /// <returns></returns>
79         public static bool IsCastableFrom(this Type to, Type from)
80         {
81             if (to.IsAssignableFrom(from))
82                 return true;
83             
84             // Look for the marker that indicates from was null
85             if (from == typeof(NUnitNullType) && (to.GetTypeInfo().IsClass || to.FullName.StartsWith("System.Nullable")))
86                 return true;
87
88             if (convertibleValueTypes.ContainsKey(to) && convertibleValueTypes[to].Contains(from))
89                 return true;
90
91             return from.GetMethods(BindingFlags.Public | BindingFlags.Static)
92                        .Any(m => m.ReturnType == to && m.Name == "op_Implicit");
93         }
94     }
95
96     /// <summary>
97     /// This class is used as a flag when we get a parameter list for a method/constructor, but
98     /// we do not know one of the types because null was passed in.
99     /// </summary>
100     public class NUnitNullType
101     {
102     }
103 }