[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / ComparisonConstraint.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 System;
30 using System.Collections;
31 using System.Collections.Generic;
32
33 namespace NUnit.Framework.Constraints
34 {
35     /// <summary>
36     /// Abstract base class for constraints that compare _values to
37     /// determine if one is greater than, equal to or less than
38     /// the other.
39     /// </summary>
40     public abstract class ComparisonConstraint : Constraint
41     {
42         /// <summary>
43         /// The value against which a comparison is to be made
44         /// </summary>
45         protected object expected;
46         /// <summary>
47         /// If true, less than returns success
48         /// </summary>
49         protected bool lessComparisonResult = false;
50         /// <summary>
51         /// if true, equal returns success
52         /// </summary>
53         protected bool equalComparisonResult = false;
54         /// <summary>
55         /// if true, greater than returns success
56         /// </summary>
57         protected bool greaterComparisonResult = false;
58
59         /// <summary>
60         /// ComparisonAdapter to be used in making the comparison
61         /// </summary>
62         private ComparisonAdapter comparer = ComparisonAdapter.Default;
63
64         /// <summary>
65         /// Initializes a new instance of the <see cref="ComparisonConstraint"/> class.
66         /// </summary>
67         /// <param name="value">The value against which to make a comparison.</param>
68         /// <param name="lessComparisonResult">if set to <c>true</c> less succeeds.</param>
69         /// <param name="equalComparisonResult">if set to <c>true</c> equal succeeds.</param>
70         /// <param name="greaterComparisonResult">if set to <c>true</c> greater succeeds.</param>
71         /// <param name="predicate">String used in describing the constraint.</param>
72         protected ComparisonConstraint(object value, bool lessComparisonResult, bool equalComparisonResult, bool greaterComparisonResult, string predicate)
73             : base(value)
74         {
75             this.expected = value;
76             this.lessComparisonResult = lessComparisonResult;
77             this.equalComparisonResult = equalComparisonResult;
78             this.greaterComparisonResult = greaterComparisonResult;
79             this.Description = predicate + " " + MsgUtils.FormatValue(expected);
80         }
81
82         /// <summary>
83         /// Test whether the constraint is satisfied by a given value
84         /// </summary>
85         /// <param name="actual">The value to be tested</param>
86         /// <returns>True for success, false for failure</returns>
87         public override ConstraintResult ApplyTo<TActual>(TActual actual)
88         {
89             if (expected == null)
90                 throw new ArgumentException("Cannot compare using a null reference", "expected");
91
92             if (actual == null)
93                 throw new ArgumentException("Cannot compare to null reference", "actual");
94
95             int icomp = comparer.Compare(expected, actual);
96
97             bool hasSucceeded = icomp < 0 && greaterComparisonResult || icomp == 0 && equalComparisonResult || icomp > 0 && lessComparisonResult;
98             return new ConstraintResult(this, actual, hasSucceeded);
99         }
100
101         /// <summary>
102         /// Modifies the constraint to use an <see cref="IComparer"/> and returns self
103         /// </summary>
104         /// <param name="comparer">The comparer used for comparison tests</param>
105         /// <returns>A constraint modified to use the given comparer</returns>
106         public ComparisonConstraint Using(IComparer comparer)
107         {
108             this.comparer = ComparisonAdapter.For(comparer);
109             return this;
110         }
111
112         /// <summary>
113         /// Modifies the constraint to use an <see cref="IComparer{T}"/> and returns self
114         /// </summary>
115         /// <param name="comparer">The comparer used for comparison tests</param>
116         /// <returns>A constraint modified to use the given comparer</returns>
117         public ComparisonConstraint Using<T>(IComparer<T> comparer)
118         {
119             this.comparer = ComparisonAdapter.For(comparer);
120             return this;
121         }
122
123         /// <summary>
124         /// Modifies the constraint to use a <see cref="Comparison{T}"/> and returns self
125         /// </summary>
126         /// <param name="comparer">The comparer used for comparison tests</param>
127         /// <returns>A constraint modified to use the given comparer</returns>
128         public ComparisonConstraint Using<T>(Comparison<T> comparer)
129         {
130             this.comparer = ComparisonAdapter.For(comparer);
131             return this;
132         }
133     }
134 }