OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 class Expect { | 5 class Expect { |
6 | 6 |
7 /** | 7 /** |
8 * Checks whether the expected and actual values are equal (using [:==:]). | 8 * Checks whether the expected and actual values are equal (using [:==:]). |
9 */ | 9 */ |
10 static void equals(var expected, var actual, [String reason = null]) { | 10 static void equals(var expected, var actual, [String reason = null]) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); | 49 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); |
50 } | 50 } |
51 | 51 |
52 /** | 52 /** |
53 * Checks whether the expected and actual values are identical | 53 * Checks whether the expected and actual values are identical |
54 * (using [:===:]). | 54 * (using [:===:]). |
55 */ | 55 */ |
56 static void identical(var expected, var actual, [String reason = null]) { | 56 static void identical(var expected, var actual, [String reason = null]) { |
57 if (expected === actual) return; | 57 if (expected === actual) return; |
58 String msg = _getMessage(reason); | 58 String msg = _getMessage(reason); |
59 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " + | 59 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " |
60 "fails."); | 60 "fails."); |
61 } | 61 } |
62 | 62 |
63 // Unconditional failure. | 63 // Unconditional failure. |
64 static void fail(String msg) { | 64 static void fail(String msg) { |
65 _fail("Expect.fail('$msg')"); | 65 _fail("Expect.fail('$msg')"); |
66 } | 66 } |
67 | 67 |
68 /** | 68 /** |
69 * Failure if the difference between expected and actual is greater than the | 69 * Failure if the difference between expected and actual is greater than the |
70 * given tolerance. If no tolerance is given, tolerance is assumed to be the | 70 * given tolerance. If no tolerance is given, tolerance is assumed to be the |
71 * value 4 significant digits smaller than the value given for expected. | 71 * value 4 significant digits smaller than the value given for expected. |
72 */ | 72 */ |
73 static void approxEquals(num expected, | 73 static void approxEquals(num expected, |
74 num actual, | 74 num actual, |
75 [num tolerance = null, | 75 [num tolerance = null, |
76 String reason = null]) { | 76 String reason = null]) { |
77 if (tolerance === null) { | 77 if (tolerance === null) { |
78 tolerance = (expected / Math.pow(10.0, 4.0)).abs(); | 78 tolerance = (expected / Math.pow(10.0, 4.0)).abs(); |
79 } | 79 } |
80 // Note: use !( <= ) rather than > so we fail on NaNs | 80 // Note: use !( <= ) rather than > so we fail on NaNs |
81 if ((expected - actual).abs() <= tolerance) return; | 81 if ((expected - actual).abs() <= tolerance) return; |
82 | 82 |
83 String msg = _getMessage(reason); | 83 String msg = _getMessage(reason); |
84 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' + | 84 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' |
85 'tolerance:<$tolerance>$msg) fails'); | 85 'tolerance:<$tolerance>$msg) fails'); |
86 } | 86 } |
87 | 87 |
88 static void notEquals(unexpected, actual, [String reason = null]) { | 88 static void notEquals(unexpected, actual, [String reason = null]) { |
89 if (unexpected != actual) return; | 89 if (unexpected != actual) return; |
90 String msg = _getMessage(reason); | 90 String msg = _getMessage(reason); |
91 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " + | 91 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " |
92 "fails."); | 92 "fails."); |
93 } | 93 } |
94 | 94 |
95 /** | 95 /** |
96 * Checks that all elements in [expected] and [actual] are equal [:==:]. | 96 * Checks that all elements in [expected] and [actual] are equal [:==:]. |
97 * This is different than the typical check for identity equality [:===:] | 97 * This is different than the typical check for identity equality [:===:] |
98 * used by the standard list implementation. It should also produce nicer | 98 * used by the standard list implementation. It should also produce nicer |
99 * error messages than just calling [:Expect.equals(expected, actual):]. | 99 * error messages than just calling [:Expect.equals(expected, actual):]. |
100 */ | 100 */ |
101 static void listEquals(List expected, List actual, [String reason = null]) { | 101 static void listEquals(List expected, List actual, [String reason = null]) { |
102 String msg = _getMessage(reason); | 102 String msg = _getMessage(reason); |
103 int n = Math.min(expected.length, actual.length); | 103 int n = Math.min(expected.length, actual.length); |
104 for (int i = 0; i < n; i++) { | 104 for (int i = 0; i < n; i++) { |
105 if (expected[i] != actual[i]) { | 105 if (expected[i] != actual[i]) { |
106 _fail('Expect.listEquals(at index $i, ' + | 106 _fail('Expect.listEquals(at index $i, ' |
107 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); | 107 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); |
108 } | 108 } |
109 } | 109 } |
110 // We check on length at the end in order to provide better error | 110 // We check on length at the end in order to provide better error |
111 // messages when an unexpected item is inserted in a list. | 111 // messages when an unexpected item is inserted in a list. |
112 if (expected.length != actual.length) { | 112 if (expected.length != actual.length) { |
113 _fail('Expect.listEquals(list length, ' + | 113 _fail('Expect.listEquals(list length, ' |
114 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' + | 114 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' |
115 'fails'); | 115 'fails'); |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 /** | 119 /** |
120 * Checks that all [expected] and [actual] have the same set of keys (using | 120 * Checks that all [expected] and [actual] have the same set of keys (using |
121 * the semantics of [Map.containsKey] to determine what "same" means. For | 121 * the semantics of [Map.containsKey] to determine what "same" means. For |
122 * each key, checks that the values in both maps are equal using [:==:]. | 122 * each key, checks that the values in both maps are equal using [:==:]. |
123 */ | 123 */ |
124 static void mapEquals(Map expected, Map actual, [String reason = null]) { | 124 static void mapEquals(Map expected, Map actual, [String reason = null]) { |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 } | 279 } |
280 } | 280 } |
281 | 281 |
282 typedef bool _CheckExceptionFn(exception); | 282 typedef bool _CheckExceptionFn(exception); |
283 | 283 |
284 class ExpectException implements Exception { | 284 class ExpectException implements Exception { |
285 ExpectException(this.message); | 285 ExpectException(this.message); |
286 String toString() => message; | 286 String toString() => message; |
287 String message; | 287 String message; |
288 } | 288 } |
OLD | NEW |