OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** This file is sourced by unittest.dart. It defines [Expectation]. */ | |
6 | |
7 /** | |
8 * Wraps an value and provides methods that can be used to verify that the value | |
9 * matches a given expectation. | |
10 */ | |
11 class Expectation { | |
12 final _value; | |
13 | |
14 Expectation(this._value); | |
15 | |
16 /** Asserts that the value is equivalent to [expected]. */ | |
17 void equals(expected) { | |
18 // Use the type-specialized versions when appropriate to give better | |
19 // error messages. | |
20 if (_value is String && expected is String) { | |
21 Expect.stringEquals(expected, _value); | |
22 } else if (_value is Map && expected is Map) { | |
23 Expect.mapEquals(expected, _value); | |
24 } else if (_value is Set && expected is Set) { | |
25 Expect.setEquals(expected, _value); | |
26 } else { | |
27 Expect.equals(expected, _value); | |
28 } | |
29 } | |
30 | |
31 /** | |
32 * Asserts that the difference between [expected] and the value is within | |
33 * [tolerance]. If no tolerance is given, it is assumed to be the value 4 | |
34 * significant digits smaller than the expected value. | |
35 */ | |
36 void approxEquals(num expected, | |
37 [num tolerance = null, String reason = null]) { | |
38 Expect.approxEquals(expected, _value, tolerance: tolerance, reason: reason); | |
39 } | |
40 | |
41 /** | |
42 * Asserts that two objects are same (using [:===:]). | |
43 */ | |
44 void same(expected) { | |
45 Expect.identical(_value, expected); | |
46 } | |
47 | |
48 /** Asserts that the value is [null]. */ | |
49 void isNull() { | |
50 Expect.equals(null, _value); | |
51 } | |
52 | |
53 /** Asserts that the value is not [null]. */ | |
54 void isNotNull() { | |
55 Expect.notEquals(null, _value); | |
56 } | |
57 | |
58 /** Asserts that the value is [true]. */ | |
59 void isTrue() { | |
60 Expect.equals(true, _value); | |
61 } | |
62 | |
63 /** Asserts that the value is [false]. */ | |
64 void isFalse() { | |
65 Expect.equals(false, _value); | |
66 } | |
67 | |
68 /** Asserts that the value has the same elements as [expected]. */ | |
69 void equalsCollection(Collection expected) { | |
70 Expect.listEquals(expected, _value); | |
71 } | |
72 | |
73 /** | |
74 * Checks that every element of [expected] is also in [actual], and that | |
75 * every element of [actual] is also in [expected]. | |
76 */ | |
77 void equalsSet(Iterable expected) { | |
78 Expect.setEquals(expected, _value); | |
79 } | |
80 } | |
OLD | NEW |