[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Attributes / RepeatAttribute.cs
1 // ***********************************************************************
2 // Copyright (c) 2007-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
24 // TODO: Rework this
25 // RepeatAttribute should either
26 //  1) Apply at load time to create the exact number of tests, or
27 //  2) Apply at run time, generating tests or results dynamically
28 //
29 // #1 is feasible but doesn't provide much benefit
30 // #2 requires infrastructure for dynamic test cases first
31 #define PORTABLE
32 #define TIZEN
33 #define NUNIT_FRAMEWORK
34 #define NUNITLITE
35 #define NET_4_5
36 #define PARALLEL
37 using System;
38 using NUnit.Framework.Interfaces;
39 using NUnit.Framework.Internal;
40 using NUnit.Framework.Internal.Commands;
41
42 namespace NUnit.Framework
43 {
44     /// <summary>
45     /// RepeatAttribute may be applied to test case in order
46     /// to run it multiple times.
47     /// </summary>
48     [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
49     public class RepeatAttribute : PropertyAttribute, IWrapSetUpTearDown
50     {
51         private int _count;
52
53         /// <summary>
54         /// Construct a RepeatAttribute
55         /// </summary>
56         /// <param name="count">The number of times to run the test</param>
57         public RepeatAttribute(int count) : base(count)
58         {
59             _count = count;
60         }
61
62         #region IWrapSetUpTearDown Members
63
64         /// <summary>
65         /// Wrap a command and return the result.
66         /// </summary>
67         /// <param name="command">The command to be wrapped</param>
68         /// <returns>The wrapped command</returns>
69         public TestCommand Wrap(TestCommand command)
70         {
71             return new RepeatedTestCommand(command, _count);
72         }
73
74         #endregion
75
76         #region Nested RepeatedTestCommand Class
77
78         /// <summary>
79         /// The test command for the RepeatAttribute
80         /// </summary>
81         public class RepeatedTestCommand : DelegatingTestCommand
82         {
83             private int repeatCount;
84
85             /// <summary>
86             /// Initializes a new instance of the <see cref="RepeatedTestCommand"/> class.
87             /// </summary>
88             /// <param name="innerCommand">The inner command.</param>
89             /// <param name="repeatCount">The number of repetitions</param>
90             public RepeatedTestCommand(TestCommand innerCommand, int repeatCount)
91                 : base(innerCommand)
92             {
93                 this.repeatCount = repeatCount;
94             }
95
96             /// <summary>
97             /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
98             /// </summary>
99             /// <param name="context">The context in which the test should run.</param>
100             /// <returns>A TestResult</returns>
101             public override TestResult Execute(TestExecutionContext context)
102             {
103                 int count = repeatCount;
104
105                 while (count-- > 0)
106                 {
107                     context.CurrentResult = innerCommand.Execute(context);
108
109                     // TODO: We may want to change this so that all iterations are run
110                     if (context.CurrentResult.ResultState != ResultState.Success)
111                         break;
112                 }
113
114                 return context.CurrentResult;
115             }
116         }
117
118         #endregion
119     }
120 }