[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Attributes / ValuesAttribute.cs
1 // ***********************************************************************
2 // Copyright (c) 2008 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.Reflection;
32 using NUnit.Compatibility;
33 using NUnit.Framework.Interfaces;
34 using NUnit.Framework.Internal;
35
36 namespace NUnit.Framework
37 {
38     /// <summary>
39     /// ValuesAttribute is used to provide literal arguments for
40     /// an individual parameter of a test.
41     /// </summary>
42     [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
43     public class ValuesAttribute : DataAttribute, IParameterDataSource
44     {
45         /// <summary>
46         /// The collection of data to be returned. Must
47         /// be set by any derived attribute classes.
48         /// We use an object[] so that the individual
49         /// elements may have their type changed in GetData
50         /// if necessary
51         /// </summary>
52         // TODO: This causes a lot of boxing so we should eliminate it
53         protected object[] data;
54
55         /// <summary>
56         /// Constructs for use with an Enum parameter. Will pass every enum
57         /// value in to the test.
58         /// </summary>
59         public ValuesAttribute()
60         {
61             data = new object[]{};
62         }
63
64         /// <summary>
65         /// Construct with one argument
66         /// </summary>
67         /// <param name="arg1"></param>
68         public ValuesAttribute(object arg1)
69         {
70             data = new object[] { arg1 };
71         }
72
73         /// <summary>
74         /// Construct with two arguments
75         /// </summary>
76         /// <param name="arg1"></param>
77         /// <param name="arg2"></param>
78         public ValuesAttribute(object arg1, object arg2)
79         {
80             data = new object[] { arg1, arg2 };
81         }
82
83         /// <summary>
84         /// Construct with three arguments
85         /// </summary>
86         /// <param name="arg1"></param>
87         /// <param name="arg2"></param>
88         /// <param name="arg3"></param>
89         public ValuesAttribute(object arg1, object arg2, object arg3)
90         {
91             data = new object[] { arg1, arg2, arg3 };
92         }
93
94         /// <summary>
95         /// Construct with an array of arguments
96         /// </summary>
97         /// <param name="args"></param>
98         public ValuesAttribute(params object[] args)
99         {
100             data = args;
101         }
102
103         /// <summary>
104         /// Get the collection of _values to be used as arguments
105         /// </summary>
106         public IEnumerable GetData(IParameterInfo parameter)
107         {
108             Type targetType = parameter.ParameterType;
109
110             if (targetType.GetTypeInfo().IsEnum && data.Length == 0)
111             {
112                 return TypeHelper.GetEnumValues(targetType);
113             }
114             if (targetType == typeof(bool) && data.Length == 0)
115             {
116                 return new object[] {true, false};
117             }
118             return GetData(targetType);
119         }
120
121         private IEnumerable GetData(Type targetType)
122         {
123             for (int i = 0; i < data.Length; i++)
124             {
125                 object arg = data[i];
126
127                 if (arg == null)
128                     continue;
129
130                 if (arg.GetType().FullName == "NUnit.Framework.SpecialValue" &&
131                     arg.ToString() == "Null")
132                 {
133                     data[i] = null;
134                     continue;
135                 }
136
137                 if (targetType.GetTypeInfo().IsAssignableFrom(arg.GetType().GetTypeInfo()))
138                     continue;
139
140 #if !PORTABLE
141                 if (arg is DBNull)
142                 {
143                     data[i] = null;
144                     continue;
145                 }
146 #endif
147
148                 bool convert = false;
149
150                 if (targetType == typeof(short) || targetType == typeof(byte) || targetType == typeof(sbyte))
151                     convert = arg is int;
152                 else
153                     if (targetType == typeof(decimal))
154                         convert = arg is double || arg is string || arg is int;
155                     else
156                         if (targetType == typeof(DateTime) || targetType == typeof(TimeSpan))
157                             convert = arg is string;
158
159                 if (convert)
160                     data[i] = Convert.ChangeType(arg, targetType, System.Globalization.CultureInfo.InvariantCulture);
161             }
162
163             return data;
164         }
165     }
166 }