[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / RangeConstraint.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.Collections.Generic;
32
33 namespace NUnit.Framework.Constraints
34 {
35     /// <summary>
36     /// RangeConstraint tests whether two _values are within a 
37     /// specified range.
38     /// </summary>
39     public class RangeConstraint : Constraint
40     {
41         private readonly IComparable from;
42         private readonly IComparable to;
43
44         private ComparisonAdapter comparer = ComparisonAdapter.Default;
45
46         /// <summary>
47         /// Initializes a new instance of the <see cref="RangeConstraint"/> class.
48         /// </summary>
49         /// <remarks>from must be less than or equal to true</remarks> 
50         /// <param name="from">Inclusive beginning of the range. Must be less than or equal to to.</param>
51         /// <param name="to">Inclusive end of the range. Must be greater than or equal to from.</param>
52         public RangeConstraint(IComparable from, IComparable to) : base( from, to )
53         {
54             // Issue #21 - https://github.com/nunit/nunit-framework/issues/21
55             // from must be less than or equal to to
56             if (comparer.Compare(from, to) > 0)
57                 throw new ArgumentException( "from must be less than to" );
58
59             this.from = from;
60             this.to = to;
61         }
62
63         /// <summary>
64         /// Gets text describing a constraint
65         /// </summary>
66         public override string Description
67         {
68             get { return string.Format("in range ({0},{1})", from, to); }
69         }
70
71         /// <summary>
72         /// Test whether the constraint is satisfied by a given value
73         /// </summary>
74         /// <param name="actual">The value to be tested</param>
75         /// <returns>True for success, false for failure</returns>
76         public override ConstraintResult ApplyTo<TActual>(TActual actual)
77         {
78             if ( from == null || to == null || actual == null)
79                 throw new ArgumentException( "Cannot compare using a null reference", "actual" );
80
81             bool isInsideRange = comparer.Compare(from, actual) <= 0 && comparer.Compare(to, actual) >= 0;
82             return new ConstraintResult(this, actual, isInsideRange);
83         }
84
85         /// <summary>
86         /// Modifies the constraint to use an <see cref="IComparer"/> and returns self.
87         /// </summary>
88         public RangeConstraint Using(IComparer comparer)
89         {
90             this.comparer = ComparisonAdapter.For(comparer);
91             return this;
92         }
93
94         /// <summary>
95         /// Modifies the constraint to use an <see cref="IComparer{T}"/> and returns self.
96         /// </summary>
97         public RangeConstraint Using<T>(IComparer<T> comparer)
98         {
99             this.comparer = ComparisonAdapter.For(comparer);
100             return this;
101         }
102
103         /// <summary>
104         /// Modifies the constraint to use a <see cref="Comparison{T}"/> and returns self.
105         /// </summary>
106         public RangeConstraint Using<T>(Comparison<T> comparer)
107         {
108             this.comparer = ComparisonAdapter.For(comparer);
109             return this;
110         }
111     }
112 }