1 | <!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
---|
2 | <html>
|
---|
3 | <!-- Standard Head Part -->
|
---|
4 | <head>
|
---|
5 | <title>NUnit - EqualConstraint</title>
|
---|
6 | <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
---|
7 | <meta http-equiv="Content-Language" content="en-US">
|
---|
8 | <link rel="stylesheet" type="text/css" href="nunit.css">
|
---|
9 | <link rel="shortcut icon" href="favicon.ico">
|
---|
10 | </head>
|
---|
11 | <!-- End Standard Head Part -->
|
---|
12 |
|
---|
13 | <body>
|
---|
14 |
|
---|
15 | <!-- Standard Header for NUnit.org -->
|
---|
16 | <div id="header">
|
---|
17 | <a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
|
---|
18 | <div id="nav">
|
---|
19 | <a href="http://www.nunit.org">NUnit</a>
|
---|
20 | <a class="active" href="index.html">Documentation</a>
|
---|
21 | </div>
|
---|
22 | </div>
|
---|
23 | <!-- End of Header -->
|
---|
24 |
|
---|
25 | <div id="content">
|
---|
26 |
|
---|
27 | <h2>Equal Constraint (NUnit 2.4 / 2.5)</h2>
|
---|
28 |
|
---|
29 | <p>An EqualConstraint is used to test whether an actual value
|
---|
30 | is equal to the expected value supplied in its constructor,
|
---|
31 | optionally within a specified tolerance.
|
---|
32 |
|
---|
33 | <h4>Constructor</h4>
|
---|
34 | <div class="code"><pre>
|
---|
35 | EqualConstraint(object expected )
|
---|
36 | </pre></div>
|
---|
37 |
|
---|
38 | <h4>Syntax</h4>
|
---|
39 | <div class="code"><pre>
|
---|
40 | Is.EqualTo( object expected )
|
---|
41 | </pre></div>
|
---|
42 |
|
---|
43 | <h4>Modifiers</h4>
|
---|
44 | <div class="code"><pre>
|
---|
45 | ...IgnoreCase
|
---|
46 | ...AsCollection
|
---|
47 | ...NoClip
|
---|
48 | ...Within(object tolerance)
|
---|
49 | .Ulps
|
---|
50 | .Percent
|
---|
51 | .Days
|
---|
52 | .Hours
|
---|
53 | .Minutes
|
---|
54 | .Seconds
|
---|
55 | .Milliseconds
|
---|
56 | .Ticks
|
---|
57 | ...Using(IEqualityComparer comparer)
|
---|
58 | ...Using(IEqualityComparer<T> comparer)
|
---|
59 | ...Using(IComparer comparer)
|
---|
60 | ...Using<T>(IComparer<T> comparer)
|
---|
61 | ...Using<T>(Comparison<T> comparer)
|
---|
62 | </pre></div>
|
---|
63 |
|
---|
64 | <h4>Comparing Numerics</h4>
|
---|
65 | <p>Numerics are compared based on their values. Different types
|
---|
66 | may be compared successfully if their values are equal.
|
---|
67 |
|
---|
68 | <p>Using the <b>Within</b> modifier, numerics may be tested
|
---|
69 | for equality within a fixed or percent tolerance.
|
---|
70 |
|
---|
71 | <div class="code"><pre>
|
---|
72 | Assert.That(2 + 2, Is.EqualTo(4.0));
|
---|
73 | Assert.That(2 + 2 == 4);
|
---|
74 | Assert.That(2 + 2, Is.Not.EqualTo(5));
|
---|
75 | Assert.That(2 + 2 != 5);
|
---|
76 | Assert.That( 5.0, Is.EqualTo( 5 );
|
---|
77 | Assert.That( 5.5, Is.EqualTo( 5 ).Within(0.075);
|
---|
78 | Assert.That( 5.5, Is.EqualTo( 5 ).Within(1.5).Percent;
|
---|
79 | </pre></div>
|
---|
80 |
|
---|
81 | <h4>Comparing Floating Point Values</h4>
|
---|
82 | <p>Values of type float and double are normally compared using a tolerance
|
---|
83 | specified by the <b>Within</b> modifier. The special values PositiveInfinity,
|
---|
84 | NegativeInfinity and NaN compare
|
---|
85 | as equal to themselves.
|
---|
86 |
|
---|
87 | <p>With version 2.5, floating-point values may be compared using a tolerance
|
---|
88 | in "Units in the Last Place" or ULPs. For certain types of numerical work,
|
---|
89 | this is safer than a fixed tolerance because it automatically compensates
|
---|
90 | for the added inaccuracy of larger numbers.
|
---|
91 |
|
---|
92 | <div class="code" style="width: 42em"><pre>
|
---|
93 | Assert.That( 2.1 + 1.2, Is.EqualTo( 3.3 ).Within( .0005 );
|
---|
94 | Assert.That( double.PositiveInfinity, Is.EqualTo( double.PositiveInfinity ) );
|
---|
95 | Assert.That( double.NegativeInfinity, Is.EqualTo( double.NegativeInfinity ) );
|
---|
96 | Assert.That( double.NaN, Is.EqualTo( double.NaN ) );
|
---|
97 | Assert.That( 20000000000000004.0, Is.EqualTo(20000000000000000.0).Within(1).Ulps);
|
---|
98 | </pre></div>
|
---|
99 |
|
---|
100 | <h4>Comparing Strings</h4>
|
---|
101 |
|
---|
102 | <p>String comparisons normally respect case. The <b>IgnoreCase</b> modifier
|
---|
103 | causes the comparison to be case-insensitive. It may also be used when
|
---|
104 | comparing arrays or collections of strings.
|
---|
105 |
|
---|
106 | <div class="code"><pre>
|
---|
107 | Assert.That( "Hello!", Is.Not.EqualTo( "HELLO!" ) );
|
---|
108 | Assert.That( "Hello!", Is.EqualTo( "HELLO!" ).IgnoreCase );
|
---|
109 |
|
---|
110 | string[] expected = new string[] { "Hello", World" };
|
---|
111 | string[] actual = new string[] { "HELLO", "world" };
|
---|
112 | Assert.That( actual, Is.EqualTo( expected ).IgnoreCase;
|
---|
113 | </pre></div>
|
---|
114 |
|
---|
115 | <h4>Comparing DateTimes and TimeSpans</h4>
|
---|
116 |
|
---|
117 | <p><b>DateTimes</b> and <b>TimeSpans</b> may be compared either with or without
|
---|
118 | a tolerance. A tolerance is specified using <b>Within</b> with either a
|
---|
119 | <b>TimeSpan</b> as an argument or with a numeric value followed by a one of
|
---|
120 | the time conversion modifiers: <b>Days</b>, <b>Hours</b>, <b>Minutes</b>,
|
---|
121 | <b>Seconds</b>, <b>Milliseconds</b> or <b>Ticks</b>.
|
---|
122 |
|
---|
123 | <div class="code"><pre>
|
---|
124 | DateTime now = DateTime.Now;
|
---|
125 | DateTime later = now + TimeSpan.FromHours(1.0);
|
---|
126 |
|
---|
127 | Assert.That( now, Is.EqualTo(now) );
|
---|
128 | Assert.That( later. Is.EqualTo(now).Within(TimeSpan.FromHours(3.0);
|
---|
129 | Assert.That( later, Is.EqualTo(now).Within(3).Hours;
|
---|
130 | </pre></div>
|
---|
131 |
|
---|
132 | <h4>Comparing Arrays and Collections</h4>
|
---|
133 |
|
---|
134 | <p>Since version 2.2, NUnit has been able to compare two single-dimensioned arrays.
|
---|
135 | Beginning with version 2.4, multi-dimensioned arrays, nested arrays (arrays of arrays)
|
---|
136 | and collections may be compared. With version 2.5, any IEnumerable is supported.
|
---|
137 | Two arrays, collections or IEnumerables are considered equal if they have the
|
---|
138 | the same dimensions and if each of the corresponding elements is equal.</p>
|
---|
139 |
|
---|
140 | <p>If you want to treat two arrays of different shapes as simple collections
|
---|
141 | for purposes of comparison, use the <b>AsCollection</b> modifier, which causes
|
---|
142 | the comparison to be made element by element, without regard for the rank or
|
---|
143 | dimensions of the array. Note that jagged arrays (arrays of arrays) do not
|
---|
144 | have a single underlying collection. The modifier would be applied
|
---|
145 | to each array separately, which has no effect in most cases.
|
---|
146 |
|
---|
147 | <div class="code"><pre>
|
---|
148 | int[] i3 = new int[] { 1, 2, 3 };
|
---|
149 | double[] d3 = new double[] { 1.0, 2.0, 3.0 };
|
---|
150 | int[] iunequal = new int[] { 1, 3, 2 };
|
---|
151 | Assert.That(i3, Is.EqualTo(d3));
|
---|
152 | Assert.That(i3, Is.Not.EqualTo(iunequal));
|
---|
153 |
|
---|
154 | int array2x2 = new int[,] { { 1, 2 } { 3, 4 } };
|
---|
155 | int array4 = new int[] { 1, 2, 3, 4 };
|
---|
156 | Assert.That( array2x2, Is.Not.EqualTo( array4 ) );
|
---|
157 | Assert.That( array2x2, Is.EqualTo( array4 ).AsCollection );
|
---|
158 | </pre></div>
|
---|
159 |
|
---|
160 | <h4>Comparing Dictionaries</h4>
|
---|
161 |
|
---|
162 | <p>Dictionaries implement <b>ICollection</b>, and NUnit has treated
|
---|
163 | them as collections since version 2.4. However, this did not
|
---|
164 | give useful results, since the dictionary entries had to be
|
---|
165 | in the same order for the comparison to succeed and the
|
---|
166 | underlying implementation had to be the same.
|
---|
167 |
|
---|
168 | <p>Beginning with NUnit 2.5.6, NUnit has specific code for
|
---|
169 | comparing dictionaries. Two dictionaries are considered equal if
|
---|
170 |
|
---|
171 | <ol>
|
---|
172 | <li>The list of keys is the same - without regard to ordering.
|
---|
173 | <li>The values associated with each key are equal.
|
---|
174 | </ol>
|
---|
175 |
|
---|
176 | <p>You can use this capability to compare any two objects implementing
|
---|
177 | <b>IDictionary</b>. Generic and non-generic dictionaries (Hashtables)
|
---|
178 | may be successfully compared.
|
---|
179 |
|
---|
180 | <h4>User-Specified Comparers</h4>
|
---|
181 |
|
---|
182 | <p>If the default NUnit or .NET behavior for testing equality doesn't
|
---|
183 | meet your needs, you can supply a comparer of your own through the
|
---|
184 | <b>Using</b> modifier. When used with <b>EqualConstraint</b>, you
|
---|
185 | may supply an <b>IEqualityComparer</b>, <b>IEqualityComparer<T></b>,
|
---|
186 | <b>IComparer</b>, <b>IComparer<T></b>; or <b>Comparison<T></b>
|
---|
187 | as the argument to <b>Using</b>.
|
---|
188 |
|
---|
189 | <div class="code"><pre>
|
---|
190 | Assert.That( myObj1, Is.EqualTo( myObj2 ).Using( myComparer ) );
|
---|
191 | </pre></div>
|
---|
192 |
|
---|
193 | <h4>Notes</h4>
|
---|
194 | <ol>
|
---|
195 | <li><p>When checking the equality of user-defined classes, NUnit makes use
|
---|
196 | of the <b>Equals</b> override on the expected object. If you neglect to
|
---|
197 | override <b>Equals</b>, you can expect failures non-identical objects.
|
---|
198 | In particular, overriding <b>operator==</b> without overriding <b>Equals</b>
|
---|
199 | has no effect.
|
---|
200 | <li><p>The <b>Within</b> modifier was originally designed for use with floating point
|
---|
201 | values only. Beginning with NUnit 2.4, comparisons of <b>DateTime</b> values
|
---|
202 | may use a <b>TimeSpan</b> as a tolerance. Beginning with NUnit 2.4.2,
|
---|
203 | non-float numeric comparisons may also specify a tolerance.
|
---|
204 | <li><p>Beginning with NUnit 2.4.4, float and double comparisons for which no
|
---|
205 | tolerance is specified use a default, use the value of
|
---|
206 | <b>GlobalSettings.DefaultFloatingPointTolerance</b>. If this is not
|
---|
207 | set, a tolerance of 0.0d is used.
|
---|
208 | <li><p>Prior to NUnit 2.2.3, comparison of two NaN values would always fail,
|
---|
209 | as specified by IEEE floating point standards. The new behavior, was
|
---|
210 | introduced after some discussion becuase it seems more useful in tests.
|
---|
211 | To avoid confusion, consider using <b>Is.NaN</b> where appropriate.
|
---|
212 | <li><p>When an equality test between two strings fails, the relevant portion of
|
---|
213 | of both strings is displayed in the error message, clipping the strings to
|
---|
214 | fit the length of the line as needed. Beginning with 2.4.4, this behavior
|
---|
215 | may be modified by use of the <b>NoClip</b> modifier on the constraint. In
|
---|
216 | addition, the maximum line length may be modified for all tests by setting
|
---|
217 | the value of <b>TextMessageWriter.MaximumLineLength</b> in the appropriate
|
---|
218 | level of setup.
|
---|
219 | <li><p>When used with arrays, collections or dictionaries, EqualConstraint
|
---|
220 | operates recursively. Any modifiers are saved and used as they apply to
|
---|
221 | individual items.
|
---|
222 | <li><p>A user-specified comparer will not be called by <b>EqualConstraint</b>
|
---|
223 | if either or both arguments are null. If both are null, the Constraint
|
---|
224 | succeeds. If only one is null, it fails.
|
---|
225 | <li><p>NUnit has special semantics for comparing <b>Streams</b> and
|
---|
226 | <b>DirectoryInfos</b>. For a <b>Stream</b>, the contents are compared.
|
---|
227 | For a <b>DirectoryInfo</b>, the first-level directory contents are compared.
|
---|
228 | </ol>
|
---|
229 |
|
---|
230 | </div>
|
---|
231 |
|
---|
232 | <!-- Submenu -->
|
---|
233 | <div id="subnav">
|
---|
234 | <ul>
|
---|
235 | <li><a href="index.html">NUnit 2.5.10</a></li>
|
---|
236 | <ul>
|
---|
237 | <li><a href="getStarted.html">Getting Started</a></li>
|
---|
238 | <li><a href="assertions.html">Assertions</a></li>
|
---|
239 | <li><a href="constraintModel.html">Constraints</a></li>
|
---|
240 | <ul>
|
---|
241 | <li id="current"><a href="equalConstraint.html">Equal Constraint</a></li>
|
---|
242 | <li><a href="sameasConstraint.html">SameAs Constraint</a></li>
|
---|
243 | <li><a href="conditionConstraints.html">Condition Constraints</a></li>
|
---|
244 | <li><a href="comparisonConstraints.html">Comparison Constrants</a></li>
|
---|
245 | <li><a href="pathConstraints.html">Path Constraints</a></li>
|
---|
246 | <li><a href="typeConstraints.html">Type Constraints</a></li>
|
---|
247 | <li><a href="stringConstraints.html">String Constraints</a></li>
|
---|
248 | <li><a href="collectionConstraints.html">Collection Constraints</a></li>
|
---|
249 | <li><a href="propertyConstraint.html">Property Constraint</a></li>
|
---|
250 | <li><a href="throwsConstraint.html">Throws Constraint</a></li>
|
---|
251 | <li><a href="compoundConstraints.html">Compound Constraints</a></li>
|
---|
252 | <li><a href="delayedConstraint.html">Delayed Constraint</a></li>
|
---|
253 | <li><a href="listMapper.html">List Mapper</a></li>
|
---|
254 | <li><a href="reusableConstraint.html">Reusable Constraint</a></li>
|
---|
255 | </ul>
|
---|
256 | <li><a href="attributes.html">Attributes</a></li>
|
---|
257 | <li><a href="runningTests.html">Running Tests</a></li>
|
---|
258 | <li><a href="extensibility.html">Extensibility</a></li>
|
---|
259 | <li><a href="releaseNotes.html">Release Notes</a></li>
|
---|
260 | <li><a href="samples.html">Samples</a></li>
|
---|
261 | <li><a href="license.html">License</a></li>
|
---|
262 | </ul>
|
---|
263 | </ul>
|
---|
264 | </div>
|
---|
265 | <!-- End of Submenu -->
|
---|
266 |
|
---|
267 |
|
---|
268 | <!-- Standard Footer for NUnit.org -->
|
---|
269 | <div id="footer">
|
---|
270 | Copyright © 2010 Charlie Poole. All Rights Reserved.
|
---|
271 | </div>
|
---|
272 | <!-- End of Footer -->
|
---|
273 |
|
---|
274 | </body>
|
---|
275 | </html>
|
---|