[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / Tests / Test.cs
1 // ***********************************************************************
2 // Copyright (c) 2012-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
34 namespace NUnit.Framework.Internal
35 {
36     /// <summary>
37     /// The Test abstract class represents a test within the framework.
38     /// </summary>
39     public abstract class Test : ITest, IComparable
40     {
41         #region Fields
42
43         /// <summary>
44         /// Static value to seed ids. It's started at 1000 so any
45         /// uninitialized ids will stand out.
46         /// </summary>
47         private static int _nextID = 1000;
48
49         /// <summary>
50         /// The SetUp methods.
51         /// </summary>
52         protected MethodInfo[] setUpMethods;
53
54         /// <summary>
55         /// The teardown methods
56         /// </summary>
57         protected MethodInfo[] tearDownMethods;
58
59         /// <summary>
60         /// Used to cache the declaring type for this MethodInfo
61         /// </summary>
62         protected ITypeInfo DeclaringTypeInfo;
63
64         /// <summary>
65         /// Method property backing field
66         /// </summary>
67         private IMethodInfo _method;
68
69         #endregion
70
71         #region Construction
72
73         /// <summary>
74         /// Constructs a test given its name
75         /// </summary>
76         /// <param name="name">The name of the test</param>
77         protected Test( string name )
78         {
79             Guard.ArgumentNotNullOrEmpty(name, "name");
80
81             Initialize(name);
82         }
83
84         /// <summary>
85         /// Constructs a test given the path through the
86         /// test hierarchy to its parent and a name.
87         /// </summary>
88         /// <param name="pathName">The parent tests full name</param>
89         /// <param name="name">The name of the test</param>
90         protected Test( string pathName, string name ) 
91         {
92             Guard.ArgumentNotNullOrEmpty(pathName, "pathName");
93
94             Initialize(name);
95
96             FullName = pathName + "." + name;
97         }
98
99         /// <summary>
100         ///  TODO: Documentation needed for constructor
101         /// </summary>
102         /// <param name="typeInfo"></param>
103         protected Test(ITypeInfo typeInfo)
104         {
105             Initialize(typeInfo.GetDisplayName());
106
107             string nspace = typeInfo.Namespace;
108             if (nspace != null && nspace != "")
109                 FullName = nspace + "." + Name;
110             TypeInfo = typeInfo;
111         }
112
113         /// <summary>
114         /// Construct a test from a MethodInfo
115         /// </summary>
116         /// <param name="method"></param>
117         protected Test(IMethodInfo method)
118         {
119             Initialize(method.Name);
120
121             Method = method;
122             TypeInfo = method.TypeInfo;
123             FullName = method.TypeInfo.FullName + "." + Name;
124         }
125
126         private void Initialize(string name)
127         {
128             FullName = Name = name;
129             Id = GetNextId();
130             Properties = new PropertyBag();
131             RunState = RunState.Runnable;
132         }
133
134         private static string GetNextId()
135         {            
136             return IdPrefix + unchecked(_nextID++);
137         }
138
139         #endregion
140
141         #region ITest Members
142
143         /// <summary>
144         /// Gets or sets the id of the test
145         /// </summary>
146         /// <value></value>
147         public string Id { get; set; }
148
149         /// <summary>
150         /// Gets or sets the name of the test
151         /// </summary>
152         public string Name { get; set; }
153
154         /// <summary>
155         /// Gets or sets the fully qualified name of the test
156         /// </summary>
157         /// <value></value>
158         public string FullName { get; set; }
159
160         /// <summary>
161         /// Gets the name of the class where this test was declared.
162         /// Returns null if the test is not associated with a class.
163         /// </summary>
164         public string ClassName
165         {
166             get
167             {
168                 ITypeInfo typeInfo = TypeInfo;
169
170                 if (Method != null)
171                 {
172                     if (DeclaringTypeInfo == null)
173                         DeclaringTypeInfo = new TypeWrapper(Method.MethodInfo.DeclaringType);
174
175                     typeInfo = DeclaringTypeInfo;
176                 }
177
178                 if (typeInfo == null)
179                     return null;
180
181                 return typeInfo.IsGenericType
182                     ? typeInfo.GetGenericTypeDefinition().FullName
183                     : typeInfo.FullName;
184             }
185         }
186
187         /// <summary>
188         /// Gets the name of the method implementing this test.
189         /// Returns null if the test is not implemented as a method.
190         /// </summary>
191         public virtual string MethodName
192         {
193             get { return null; }
194         }
195
196         /// <summary>
197         /// Gets the TypeInfo of the fixture used in running this test
198         /// or null if no fixture type is associated with it.
199         /// </summary>
200         public ITypeInfo TypeInfo { get; private set; }
201
202         /// <summary>
203         /// Gets a MethodInfo for the method implementing this test.
204         /// Returns null if the test is not implemented as a method.
205         /// </summary>
206         public IMethodInfo Method
207         {
208             get { return _method; }
209             set
210             {
211                 DeclaringTypeInfo = null;
212                 _method = value;
213             }
214         } // public setter needed by NUnitTestCaseBuilder
215
216         /// <summary>
217         /// Whether or not the test should be run
218         /// </summary>
219         public RunState RunState { get; set; }
220
221         /// <summary>
222         /// Gets the name used for the top-level element in the
223         /// XML representation of this test
224         /// </summary>
225         public abstract string XmlElementName { get; }
226
227         /// <summary>
228         /// Gets a string representing the type of test. Used as an attribute
229         /// value in the XML representation of a test and has no other
230         /// function in the framework.
231         /// </summary>
232         public virtual string TestType
233         {
234             get { return this.GetType().Name; }
235         }
236
237         /// <summary>
238         /// Gets a count of test cases represented by
239         /// or contained under this test.
240         /// </summary>
241         public virtual int TestCaseCount 
242         { 
243             get { return 1; } 
244         }
245
246         /// <summary>
247         /// Gets the properties for this test
248         /// </summary>
249         public IPropertyBag Properties { get; private set; }
250
251         /// <summary>
252         /// Returns true if this is a TestSuite
253         /// </summary>
254         public bool IsSuite
255         {
256             get { return this is TestSuite; }
257         }
258
259         /// <summary>
260         /// Gets a bool indicating whether the current test
261         /// has any descendant tests.
262         /// </summary>
263         public abstract bool HasChildren { get; }
264
265         /// <summary>
266         /// Gets the parent as a Test object.
267         /// Used by the core to set the parent.
268         /// </summary>
269         public ITest Parent { get; set; }
270
271         /// <summary>
272         /// Gets this test's child tests
273         /// </summary>
274         /// <value>A list of child tests</value>
275         public abstract System.Collections.Generic.IList<ITest> Tests { get; }
276
277         /// <summary>
278         /// Gets or sets a fixture object for running this test.
279         /// </summary>
280         public virtual object Fixture { get; set; }
281
282         #endregion
283
284         #region Other Public Properties
285
286         /// <summary>
287         /// Static prefix used for ids in this AppDomain.
288         /// Set by FrameworkController.
289         /// </summary>
290         public static string IdPrefix { get; set; }
291
292         /// <summary>
293         /// Gets or Sets the Int value representing the seed for the RandomGenerator
294         /// </summary>
295         /// <value></value>
296         public int Seed { get; set; }
297
298         #endregion
299
300         #region Internal Properties
301
302         internal bool RequiresThread { get; set; }
303
304         internal bool IsAsynchronous { get; set; }
305
306         #endregion
307
308         #region Other Public Methods
309
310         /// <summary>
311         /// Creates a TestResult for this test.
312         /// </summary>
313         /// <returns>A TestResult suitable for this type of test.</returns>
314         public abstract TestResult MakeTestResult();
315
316 #if PORTABLE
317         /// <summary>
318         /// Modify a newly constructed test by applying any of NUnit's common
319         /// attributes, based on a supplied ICustomAttributeProvider, which is
320         /// usually the reflection element from which the test was constructed,
321         /// but may not be in some instances. The attributes retrieved are 
322         /// saved for use in subsequent operations.
323         /// </summary>
324         /// <param name="provider">An object deriving from MemberInfo</param>
325         public void ApplyAttributesToTest(MemberInfo provider)
326         {
327             //foreach (IApplyToTest iApply in provider.GetAttributes<IApplyToTest>(true))
328             //    iApply.ApplyToTest(this);
329         }
330
331         /// <summary>
332         /// Modify a newly constructed test by applying any of NUnit's common
333         /// attributes, based on a supplied ICustomAttributeProvider, which is
334         /// usually the reflection element from which the test was constructed,
335         /// but may not be in some instances. The attributes retrieved are 
336         /// saved for use in subsequent operations.
337         /// </summary>
338         /// <param name="provider">An object deriving from MemberInfo</param>
339         public void ApplyAttributesToTest(Assembly provider)
340         {
341             //foreach (IApplyToTest iApply in provider.GetAttributes<IApplyToTest>())
342             //    iApply.ApplyToTest(this);
343         }
344 #else
345         /// <summary>
346         /// Modify a newly constructed test by applying any of NUnit's common
347         /// attributes, based on a supplied ICustomAttributeProvider, which is
348         /// usually the reflection element from which the test was constructed,
349         /// but may not be in some instances. The attributes retrieved are 
350         /// saved for use in subsequent operations.
351         /// </summary>
352         /// <param name="provider">An object implementing ICustomAttributeProvider</param>
353         public void ApplyAttributesToTest(ICustomAttributeProvider provider)
354         {
355             foreach (IApplyToTest iApply in provider.GetCustomAttributes(typeof(IApplyToTest), true))
356                 iApply.ApplyToTest(this);
357         }
358 #endif
359
360         #endregion
361
362         #region Protected Methods
363
364         /// <summary>
365         /// Add standard attributes and members to a test node.
366         /// </summary>
367         /// <param name="thisNode"></param>
368         /// <param name="recursive"></param>
369         protected void PopulateTestNode(TNode thisNode, bool recursive)
370         {
371             thisNode.AddAttribute("id", this.Id.ToString());
372             thisNode.AddAttribute("name", this.Name);
373             thisNode.AddAttribute("fullname", this.FullName);
374             if (this.MethodName != null)
375                 thisNode.AddAttribute("methodname", this.MethodName);
376             if (this.ClassName != null)
377                 thisNode.AddAttribute("classname", this.ClassName);
378             thisNode.AddAttribute("runstate", this.RunState.ToString());
379
380             if (Properties.Keys.Count > 0)
381                 Properties.AddToXml(thisNode, recursive);
382         }
383
384         #endregion
385
386         #region IXmlNodeBuilder Members
387
388         /// <summary>
389         /// Returns the Xml representation of the test
390         /// </summary>
391         /// <param name="recursive">If true, include child tests recursively</param>
392         /// <returns></returns>
393         public TNode ToXml(bool recursive)
394         {
395             return AddToXml(new TNode("dummy"), recursive);
396         }
397
398         /// <summary>
399         /// Returns an XmlNode representing the current result after
400         /// adding it as a child of the supplied parent node.
401         /// </summary>
402         /// <param name="parentNode">The parent node.</param>
403         /// <param name="recursive">If true, descendant results are included</param>
404         /// <returns></returns>
405         public abstract TNode AddToXml(TNode parentNode, bool recursive);
406
407         #endregion
408
409         #region IComparable Members
410
411         /// <summary>
412         /// Compares this test to another test for sorting purposes
413         /// </summary>
414         /// <param name="obj">The other test</param>
415         /// <returns>Value of -1, 0 or +1 depending on whether the current test is less than, equal to or greater than the other test</returns>
416         public int CompareTo(object obj)
417         {
418             Test other = obj as Test;
419
420             if (other == null)
421                 return -1;
422
423             return this.FullName.CompareTo(other.FullName);
424         }
425
426         #endregion
427     }
428 }