| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 /** | 120 /** |
| 121 * Specialized equality test for strings. When the strings don't match, | 121 * Specialized equality test for strings. When the strings don't match, |
| 122 * this method shows where the mismatch starts and ends. | 122 * this method shows where the mismatch starts and ends. |
| 123 */ | 123 */ |
| 124 static void stringEquals(String expected, | 124 static void stringEquals(String expected, |
| 125 String actual, | 125 String actual, |
| 126 [String reason = null]) { | 126 [String reason = null]) { |
| 127 String msg = _getMessage(reason); | 127 String msg = _getMessage(reason); |
| 128 String defaultMessage = | 128 String defaultMessage = |
| 129 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; | 129 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; |
| 130 if (expected != actual) { | 130 |
| 131 // scan from the left until we find a mismatch | 131 if (expected == actual) return; |
| 132 int left = 0; | 132 if ((expected === null) || (actual === null)) { |
| 133 int eLen = expected.length; | 133 _fail('$defaultMessage'); |
| 134 int aLen = actual.length; | 134 } |
| 135 while (true) { | 135 // scan from the left until we find a mismatch |
| 136 if (left == eLen) { | 136 int left = 0; |
| 137 assert (left < aLen); | 137 int eLen = expected.length; |
| 138 String snippet = actual.substring(left, aLen); | 138 int aLen = actual.length; |
| 139 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); | 139 while (true) { |
| 140 return; | 140 if (left == eLen) { |
| 141 } | 141 assert (left < aLen); |
| 142 if (left == aLen) { | 142 String snippet = actual.substring(left, aLen); |
| 143 assert (left < eLen); | 143 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); |
| 144 String snippet = expected.substring(left, eLen); | 144 return; |
| 145 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); | |
| 146 return; | |
| 147 } | |
| 148 if (expected[left] != actual[left]) { | |
| 149 break; | |
| 150 } | |
| 151 left++; | |
| 152 } | 145 } |
| 146 if (left == aLen) { |
| 147 assert (left < eLen); |
| 148 String snippet = expected.substring(left, eLen); |
| 149 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); |
| 150 return; |
| 151 } |
| 152 if (expected[left] != actual[left]) { |
| 153 break; |
| 154 } |
| 155 left++; |
| 156 } |
| 153 | 157 |
| 154 // scan from the right until we find a mismatch | 158 // scan from the right until we find a mismatch |
| 155 int right = 0; | 159 int right = 0; |
| 156 while (true) { | 160 while (true) { |
| 157 if (right == eLen) { | 161 if (right == eLen) { |
| 158 assert (right < aLen); | 162 assert (right < aLen); |
| 159 String snippet = actual.substring(0, aLen - right); | 163 String snippet = actual.substring(0, aLen - right); |
| 160 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); | 164 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); |
| 161 return; | 165 return; |
| 162 } | |
| 163 if (right == aLen) { | |
| 164 assert (right < eLen); | |
| 165 String snippet = expected.substring(0, eLen - right); | |
| 166 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); | |
| 167 return; | |
| 168 } | |
| 169 if (expected[eLen - right - 1] != actual[aLen - right - 1]) { | |
| 170 break; | |
| 171 } | |
| 172 right++; | |
| 173 } | 166 } |
| 174 | 167 if (right == aLen) { |
| 175 String eSnippet = expected.substring(left, eLen - right); | 168 assert (right < eLen); |
| 176 String aSnippet = actual.substring(left, aLen - right); | 169 String snippet = expected.substring(0, eLen - right); |
| 177 String diff = '\nDiff:\n...[ $eSnippet} ]...\n...[ $aSnippet ]...'; | 170 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); |
| 178 _fail('$defaultMessage$diff'); | 171 return; |
| 172 } |
| 173 if (expected[eLen - right - 1] != actual[aLen - right - 1]) { |
| 174 break; |
| 175 } |
| 176 right++; |
| 179 } | 177 } |
| 178 String eSnippet = expected.substring(left, eLen - right); |
| 179 String aSnippet = actual.substring(left, aLen - right); |
| 180 String diff = '\nDiff:\n...[ $eSnippet} ]...\n...[ $aSnippet ]...'; |
| 181 _fail('$defaultMessage$diff'); |
| 180 } | 182 } |
| 181 | 183 |
| 182 /** | 184 /** |
| 183 * Checks that every element of [expected] is also in [actual], and that | 185 * Checks that every element of [expected] is also in [actual], and that |
| 184 * every element of [actual] is also in [expected]. | 186 * every element of [actual] is also in [expected]. |
| 185 */ | 187 */ |
| 186 static void setEquals(Iterable expected, | 188 static void setEquals(Iterable expected, |
| 187 Iterable actual, | 189 Iterable actual, |
| 188 [String reason = null]) { | 190 [String reason = null]) { |
| 189 final missingSet = new Set.from(expected); | 191 final missingSet = new Set.from(expected); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 } | 251 } |
| 250 } | 252 } |
| 251 | 253 |
| 252 typedef bool _CheckExceptionFn(exception); | 254 typedef bool _CheckExceptionFn(exception); |
| 253 | 255 |
| 254 class ExpectException implements Exception { | 256 class ExpectException implements Exception { |
| 255 ExpectException(this.message); | 257 ExpectException(this.message); |
| 256 String toString() => message; | 258 String toString() => message; |
| 257 String message; | 259 String message; |
| 258 } | 260 } |
| OLD | NEW |