[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / Constraint.cs
1 // ***********************************************************************
2 // Copyright (c) 2007 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 NUnit.Framework.Internal;
30 using NUnit.Compatibility;
31 using System.Collections;
32 using System;
33 using System.Reflection;
34
35 namespace NUnit.Framework.Constraints
36 {
37     /// <summary>
38     /// Delegate used to delay evaluation of the actual value
39     /// to be used in evaluating a constraint
40     /// </summary>
41     public delegate TActual ActualValueDelegate<TActual>();
42
43     /// <summary>
44     /// The Constraint class is the base of all built-in constraints
45     /// within NUnit. It provides the operator overloads used to combine 
46     /// constraints.
47     /// </summary>
48     public abstract class Constraint : IConstraint
49     {
50         Lazy<string> _displayName;
51
52         #region Constructor
53
54         /// <summary>
55         /// Construct a constraint with optional arguments
56         /// </summary>
57         /// <param name="args">Arguments to be saved</param>
58         protected Constraint(params object[] args)
59         {
60             Arguments = args;
61
62             _displayName = new Lazy<string>(() =>
63             {
64                 var type = this.GetType();
65                 var displayName = type.Name;
66                 if (type.GetTypeInfo().IsGenericType)
67                     displayName = displayName.Substring(0, displayName.Length - 2);
68                 if (displayName.EndsWith("Constraint", StringComparison.Ordinal))
69                     displayName = displayName.Substring(0, displayName.Length - 10);
70                 return displayName;
71             });
72         }
73
74         #endregion
75
76         #region Properties
77
78         /// <summary>
79         /// The display name of this Constraint for use by ToString().
80         /// The default value is the name of the constraint with
81         /// trailing "Constraint" removed. Derived classes may set
82         /// this to another name in their constructors.
83         /// </summary>
84         public virtual string DisplayName { get { return _displayName.Value; } }
85
86         /// <summary>
87         /// The Description of what this constraint tests, for
88         /// use in messages and in the ConstraintResult.
89         /// </summary>
90         public virtual string Description { get; protected set; }
91
92         /// <summary>
93         /// Arguments provided to this Constraint, for use in
94         /// formatting the description.
95         /// </summary>
96         public object[] Arguments { get; private set; }
97
98         /// <summary>
99         /// The ConstraintBuilder holding this constraint
100         /// </summary>
101         public ConstraintBuilder Builder { get; set; }
102
103         #endregion
104
105         #region Abstract and Virtual Methods
106
107         /// <summary>
108         /// Applies the constraint to an actual value, returning a ConstraintResult.
109         /// </summary>
110         /// <param name="actual">The value to be tested</param>
111         /// <returns>A ConstraintResult</returns>
112         public abstract ConstraintResult ApplyTo<TActual>(TActual actual);
113
114         /// <summary>
115         /// Applies the constraint to an ActualValueDelegate that returns 
116         /// the value to be tested. The default implementation simply evaluates 
117         /// the delegate but derived classes may override it to provide for 
118         /// delayed processing.
119         /// </summary>
120         /// <param name="del">An ActualValueDelegate</param>
121         /// <returns>A ConstraintResult</returns>
122         public virtual ConstraintResult ApplyTo<TActual>(ActualValueDelegate<TActual> del)
123         {
124 #if NET_4_0 || NET_4_5 || PORTABLE
125             if (AsyncInvocationRegion.IsAsyncOperation(del))
126                 using (var region = AsyncInvocationRegion.Create(del))
127                     return ApplyTo(region.WaitForPendingOperationsToComplete(del()));
128 #endif
129             return ApplyTo(GetTestObject(del));
130         }
131
132 #pragma warning disable 3006
133         /// <summary>
134         /// Test whether the constraint is satisfied by a given reference.
135         /// The default implementation simply dereferences the value but
136         /// derived classes may override it to provide for delayed processing.
137         /// </summary>
138         /// <param name="actual">A reference to the value to be tested</param>
139         /// <returns>A ConstraintResult</returns>
140         public virtual ConstraintResult ApplyTo<TActual>(ref TActual actual)
141         {
142             return ApplyTo(actual);
143         }
144 #pragma warning restore 3006
145
146         /// <summary>
147         /// Retrieves the value to be tested from an ActualValueDelegate.
148         /// The default implementation simply evaluates the delegate but derived
149         /// classes may override it to provide for delayed processing.
150         /// </summary>
151         /// <param name="del">An ActualValueDelegate</param>
152         /// <returns>Delegate evaluation result</returns>
153         protected virtual object GetTestObject<TActual>(ActualValueDelegate<TActual> del)
154         {
155             return del();
156         }
157
158         #endregion
159
160         #region ToString Override
161
162         /// <summary>
163         /// Default override of ToString returns the constraint DisplayName
164         /// followed by any arguments within angle brackets.
165         /// </summary>
166         /// <returns></returns>
167         public override string ToString()
168         {
169             string rep = GetStringRepresentation();
170
171             return this.Builder == null ? rep : string.Format("<unresolved {0}>", rep);
172         }
173
174         /// <summary>
175         /// Returns the string representation of this constraint
176         /// </summary>
177         protected virtual string GetStringRepresentation()
178         {
179             System.Text.StringBuilder sb = new System.Text.StringBuilder();
180
181             sb.Append("<");
182             sb.Append(DisplayName.ToLower());
183
184             foreach (object arg in Arguments)
185             {
186                 sb.Append(" ");
187                 sb.Append(_displayable(arg));
188             }
189
190             sb.Append(">");
191
192             return sb.ToString();
193         }
194
195         private static string _displayable(object o)
196         {
197             if (o == null) return "null";
198
199             string fmt = o is string ? "\"{0}\"" : "{0}";
200             return string.Format(System.Globalization.CultureInfo.InvariantCulture, fmt, o);
201         }
202
203         #endregion
204
205         #region Operator Overloads
206
207         /// <summary>
208         /// This operator creates a constraint that is satisfied only if both 
209         /// argument constraints are satisfied.
210         /// </summary>
211         public static Constraint operator &(Constraint left, Constraint right)
212         {
213             IResolveConstraint l = (IResolveConstraint)left;
214             IResolveConstraint r = (IResolveConstraint)right;
215             return new AndConstraint(l.Resolve(), r.Resolve());
216         }
217
218         /// <summary>
219         /// This operator creates a constraint that is satisfied if either 
220         /// of the argument constraints is satisfied.
221         /// </summary>
222         public static Constraint operator |(Constraint left, Constraint right)
223         {
224             IResolveConstraint l = (IResolveConstraint)left;
225             IResolveConstraint r = (IResolveConstraint)right;
226             return new OrConstraint(l.Resolve(), r.Resolve());
227         }
228
229         /// <summary>
230         /// This operator creates a constraint that is satisfied if the 
231         /// argument constraint is not satisfied.
232         /// </summary>
233         public static Constraint operator !(Constraint constraint)
234         {
235             IResolveConstraint r = (IResolveConstraint)constraint;
236             return new NotConstraint(r.Resolve());
237         }
238
239         #endregion
240
241         #region Binary Operators
242
243         /// <summary>
244         /// Returns a ConstraintExpression by appending And
245         /// to the current constraint.
246         /// </summary>
247         public ConstraintExpression And
248         {
249             get
250             {
251                 ConstraintBuilder builder = this.Builder;
252                 if (builder == null)
253                 {
254                     builder = new ConstraintBuilder();
255                     builder.Append(this);
256                 }
257
258                 builder.Append(new AndOperator());
259
260                 return new ConstraintExpression(builder);
261             }
262         }
263
264         /// <summary>
265         /// Returns a ConstraintExpression by appending And
266         /// to the current constraint.
267         /// </summary>
268         public ConstraintExpression With
269         {
270             get { return this.And; }
271         }
272
273         /// <summary>
274         /// Returns a ConstraintExpression by appending Or
275         /// to the current constraint.
276         /// </summary>
277         public ConstraintExpression Or
278         {
279             get
280             {
281                 ConstraintBuilder builder = this.Builder;
282                 if (builder == null)
283                 {
284                     builder = new ConstraintBuilder();
285                     builder.Append(this);
286                 }
287
288                 builder.Append(new OrOperator());
289
290                 return new ConstraintExpression(builder);
291             }
292         }
293
294         #endregion
295
296         #region After Modifier
297
298 #if !PORTABLE
299         /// <summary>
300         /// Returns a DelayedConstraint with the specified delay time.
301         /// </summary>
302         /// <param name="delayInMilliseconds">The delay in milliseconds.</param>
303         /// <returns></returns>
304         public DelayedConstraint After(int delayInMilliseconds)
305         {
306             return new DelayedConstraint(
307                 Builder == null ? this : Builder.Resolve(),
308                 delayInMilliseconds);
309         }
310
311         /// <summary>
312         /// Returns a DelayedConstraint with the specified delay time
313         /// and polling interval.
314         /// </summary>
315         /// <param name="delayInMilliseconds">The delay in milliseconds.</param>
316         /// <param name="pollingInterval">The interval at which to test the constraint.</param>
317         /// <returns></returns>
318         public DelayedConstraint After(int delayInMilliseconds, int pollingInterval)
319         {
320             return new DelayedConstraint(
321                 Builder == null ? this : Builder.Resolve(),
322                 delayInMilliseconds,
323                 pollingInterval);
324         }
325 #endif
326
327         #endregion
328
329         #region IResolveConstraint Members
330
331         /// <summary>
332         /// Resolves any pending operators and returns the resolved constraint.
333         /// </summary>
334         IConstraint IResolveConstraint.Resolve()
335         {
336             return Builder == null ? this : Builder.Resolve();
337         }
338
339         #endregion
340     }
341 }