PageRenderTime 44ms CodeModel.GetById 35ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Constraints/RangeConstraint.cs

#
C# | 92 lines | 48 code | 12 blank | 32 comment | 6 complexity | 065cbe59d9c01bf5a8a807b5d76f16ae MD5 | raw file
 1// ****************************************************************
 2// Copyright 2008, Charlie Poole
 3// This is free software licensed under the NUnit license. You may
 4// obtain a copy of the license at http://nunit.org
 5// ****************************************************************
 6
 7using System;
 8using System.Collections;
 9#if NET_2_0
10using System.Collections.Generic;
11#endif
12
13namespace NUnit.Framework.Constraints
14{
15    /// <summary>
16    /// RangeConstraint tests whethe two values are within a 
17    /// specified range.
18    /// </summary>
19    public class RangeConstraint : Constraint
20    {
21        private IComparable from;
22        private IComparable to;
23
24        private ComparisonAdapter comparer = ComparisonAdapter.Default;
25
26        /// <summary>
27        /// Initializes a new instance of the <see cref="T:RangeConstraint"/> class.
28        /// </summary>
29        /// <param name="from">From.</param>
30        /// <param name="to">To.</param>
31        public RangeConstraint(IComparable from, IComparable to) : base( from, to )
32        {
33            this.from = from;
34            this.to = to;
35        }
36
37        /// <summary>
38        /// Test whether the constraint is satisfied by a given value
39        /// </summary>
40        /// <param name="actual">The value to be tested</param>
41        /// <returns>True for success, false for failure</returns>
42        public override bool Matches(object actual)
43        {
44            this.actual = actual;
45
46            if ( from == null || to == null || actual == null)
47                throw new ArgumentException( "Cannot compare using a null reference", "expected" );
48
49            return comparer.Compare(from, actual) <= 0 &&
50                   comparer.Compare(to, actual) >= 0;
51        }
52
53        /// <summary>
54        /// Write the constraint description to a MessageWriter
55        /// </summary>
56        /// <param name="writer">The writer on which the description is displayed</param>
57        public override void WriteDescriptionTo(MessageWriter writer)
58        {
59
60            writer.Write("in range ({0},{1})", from, to);
61        }
62
63        /// <summary>
64        /// Modifies the constraint to use an IComparer and returns self.
65        /// </summary>
66        public RangeConstraint Using(IComparer comparer)
67        {
68            this.comparer = ComparisonAdapter.For(comparer);
69            return this;
70        }
71
72#if NET_2_0
73        /// <summary>
74        /// Modifies the constraint to use an IComparer&lt;T&gt; and returns self.
75        /// </summary>
76        public RangeConstraint Using<T>(IComparer<T> comparer)
77        {
78            this.comparer = ComparisonAdapter.For(comparer);
79            return this;
80        }
81
82        /// <summary>
83        /// Modifies the constraint to use a Comparison&lt;T&gt; and returns self.
84        /// </summary>
85        public RangeConstraint Using<T>(Comparison<T> comparer)
86        {
87            this.comparer = ComparisonAdapter.For(comparer);
88            return this;
89        }
90#endif
91    }
92}