Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
|
Bob Nystrom
2012/06/01 18:22:23
Doesn't look like you got to the comments on this
gram
2012/06/01 22:22:00
Done.
| |
| 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 #library('unittestTest'); | |
| 6 | |
| 7 #import('../../lib/unittest/unittest.dart'); | |
| 8 | |
| 9 | |
| 10 foo() {} | |
| 11 bar() { throw 'X'; } | |
| 12 | |
| 13 int errorCount; | |
| 14 String errorString; | |
| 15 var myhandler; | |
| 16 | |
| 17 class MyFailureHandler implements IFailureHandler { | |
| 18 ErrorFormatter format; | |
| 19 | |
| 20 MyFailureHandler() { | |
| 21 format = configureExpectFormatter(); | |
| 22 } | |
| 23 void fail(String reason) { | |
| 24 ++errorCount; | |
| 25 errorString = reason; | |
| 26 } | |
| 27 void failMatch(actual, IMatcher matcher, String reason) { | |
| 28 fail(format(actual, matcher, reason)); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 void shouldFail(var value, IMatcher matcher, String reason) { | |
| 33 errorCount = 0; | |
| 34 errorString = ''; | |
| 35 configureExpectHandler(myhandler); | |
| 36 expect(value, matcher); | |
| 37 configureExpectHandler(null); | |
| 38 expect(errorCount, equals(1)); | |
| 39 expect(errorString, equalsIgnoringWhitespace(reason)); | |
| 40 } | |
| 41 | |
| 42 void shouldPass(var value, IMatcher matcher) { | |
| 43 errorCount = 0; | |
| 44 errorString = ''; | |
| 45 configureExpectHandler(myhandler); | |
| 46 expect(value, matcher); | |
| 47 configureExpectHandler(null); | |
| 48 expect(errorCount, equals(0)); | |
| 49 } | |
| 50 | |
| 51 void main() { | |
| 52 | |
| 53 myhandler = new MyFailureHandler(); | |
| 54 | |
| 55 // Core matchers | |
| 56 | |
| 57 // isTrue | |
| 58 shouldPass(true, isTrue); | |
| 59 shouldFail(false, isTrue, "Expected: true but: was <false>"); | |
| 60 | |
| 61 // isFalse | |
| 62 shouldPass(false, isFalse); | |
| 63 shouldFail(true, isFalse, "Expected: false but: was <true>"); | |
| 64 | |
| 65 // isNull | |
| 66 shouldPass(null, isNull); | |
| 67 shouldFail(false, isNull, "Expected: null but: was <false>"); | |
| 68 | |
| 69 // isNotNull | |
| 70 shouldPass(false, isNotNull); | |
| 71 shouldFail(null, isNotNull, "Expected: not null but: was <null>"); | |
| 72 | |
| 73 var a = new Map(); | |
| 74 var b = new Map(); | |
| 75 | |
| 76 // same | |
| 77 shouldPass(a, same(a)); | |
| 78 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>"); | |
| 79 | |
| 80 // equals - only for scalars | |
| 81 shouldPass(a, equals(a)); | |
| 82 shouldFail(a, equals(b), "Expected: <{}> but: was <{}>"); | |
| 83 | |
| 84 // anything | |
| 85 shouldPass(a, anything); | |
| 86 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); | |
| 87 | |
| 88 // throwsException | |
| 89 shouldFail(foo, throws, | |
| 90 "Expected: throws an exception but: no exception"); | |
| 91 shouldPass(bar, throws); | |
| 92 | |
| 93 // throwsExceptionWhich | |
| 94 shouldPass(bar, throwsA(equals('X'))); | |
| 95 shouldFail(bar, throwsA(equals('Y')), | |
| 96 "Expected: throws an exception which matches 'Y' " | |
| 97 "but: exception does not match 'Y'"); | |
| 98 | |
| 99 // returnsNormally | |
| 100 shouldPass(foo, returnsNormally); | |
| 101 shouldFail(bar, returnsNormally, | |
| 102 "Expected: return normally but: threw exception"); | |
| 103 | |
| 104 // isInstanceOf | |
| 105 shouldFail(0, new isInstanceOf<String>('String'), | |
| 106 "Expected: an instance of String but: was <0>"); | |
| 107 shouldPass('cow', new isInstanceOf<String>('String')); | |
| 108 | |
| 109 // hasLength | |
| 110 var c = new List(); | |
| 111 shouldPass(c, hasLength(0)); | |
| 112 shouldPass(a, hasLength(0)); | |
| 113 shouldPass('a', hasLength(1)); | |
| 114 shouldFail(0, hasLength(0), | |
| 115 "Expected: an object with length of <0> " | |
| 116 "but: was <0> has no length property"); | |
| 117 | |
| 118 c.add(0); | |
| 119 shouldPass(c, hasLength(1)); | |
| 120 shouldFail(c, hasLength(2), | |
| 121 "Expected: an object with length of <2> " | |
| 122 "but: was <[0]> with length of <1>"); | |
| 123 | |
| 124 c.add(0); | |
| 125 shouldFail(c, hasLength(1), | |
| 126 "Expected: an object with length of <1> " | |
| 127 "but: was <[0, 0]> with length of <2>"); | |
| 128 shouldPass(c, hasLength(2)); | |
| 129 | |
| 130 // Numeric Matchers | |
| 131 | |
| 132 //greaterThan | |
| 133 shouldPass(10, greaterThan(9)); | |
| 134 shouldFail(9, greaterThan(10), | |
| 135 "Expected: a value greater than <10> but: was <9>"); | |
| 136 | |
| 137 // greaterThanOrEqualTo | |
| 138 shouldPass(10, greaterThanOrEqualTo(10)); | |
| 139 shouldFail(9, greaterThanOrEqualTo(10), | |
| 140 "Expected: a value greater than or equal to <10> but: was <9>"); | |
| 141 | |
| 142 // lessThan | |
| 143 shouldFail(10, lessThan(9), "Expected: a value less than <9> but: was <10>"); | |
| 144 shouldPass(9, lessThan(10)); | |
| 145 | |
| 146 // lessThanOrEqualTo | |
| 147 shouldPass(10, lessThanOrEqualTo(10)); | |
| 148 shouldFail(11, lessThanOrEqualTo(10), | |
| 149 "Expected: a value less than or equal to <10> but: was <11>"); | |
| 150 | |
| 151 // isZero | |
| 152 shouldPass(0, isZero); | |
| 153 shouldFail(1, isZero, "Expected: a value equal to <0> but: was <1>"); | |
| 154 | |
| 155 // isNonZero | |
| 156 shouldFail(0, isNonZero, "Expected: a value not equal to <0> but: was <0>"); | |
| 157 shouldPass(1, isNonZero); | |
| 158 | |
| 159 // isPositive | |
| 160 shouldFail(-1, isPositive, "Expected: a value positive <0> but: was <-1>"); | |
| 161 shouldPass(0, isPositive); | |
| 162 shouldPass(1, isPositive); | |
| 163 | |
| 164 // isNegative | |
| 165 shouldPass(-1, isNegative); | |
| 166 shouldFail(0, isNegative, "Expected: a value negative <0> but: was <0>"); | |
| 167 | |
| 168 // closeTo | |
| 169 shouldPass(0, closeTo(0, 1)); | |
| 170 shouldPass(-1, closeTo(0, 1)); | |
| 171 shouldPass(1, closeTo(0, 1)); | |
| 172 shouldFail(1.001, closeTo(0, 1), | |
| 173 "Expected: a numeric value within <1> of <0> " | |
| 174 "but: <1.001> differed by <1.001>"); | |
| 175 shouldFail(-1.001, closeTo(0, 1), | |
| 176 "Expected: a numeric value within <1> of <0> " | |
| 177 "but: <-1.001> differed by <1.001>"); | |
| 178 | |
| 179 // inInclusiveRange | |
| 180 shouldFail(-1, inInclusiveRange(0,2), | |
| 181 "Expected: be in range from 0 (inclusive) to 2 (inclusive) but: was <-1>"); | |
| 182 shouldPass(0, inInclusiveRange(0,2)); | |
| 183 shouldPass(1, inInclusiveRange(0,2)); | |
| 184 shouldPass(2, inInclusiveRange(0,2)); | |
| 185 shouldFail(3, inInclusiveRange(0,2), | |
| 186 "Expected: be in range from 0 (inclusive) to 2 (inclusive) but: was <3>"); | |
| 187 | |
| 188 // inExclusiveRange | |
| 189 shouldFail(0, inExclusiveRange(0,2), | |
| 190 "Expected: be in range from 0 (exclusive) to 2 (exclusive) but: was <0>"); | |
| 191 shouldPass(1, inExclusiveRange(0,2)); | |
| 192 shouldFail(2, inExclusiveRange(0,2), | |
| 193 "Expected: be in range from 0 (exclusive) to 2 (exclusive) but: was <2>"); | |
| 194 | |
| 195 // inOpenClosedRange | |
| 196 shouldFail(0, inOpenClosedRange(0,2), | |
| 197 "Expected: be in range from 0 (exclusive) to 2 (inclusive) but: was <0>"); | |
| 198 shouldPass(1, inOpenClosedRange(0,2)); | |
| 199 shouldPass(2, inOpenClosedRange(0,2)); | |
| 200 | |
| 201 // inClosedOpenRange | |
| 202 shouldPass(0, inClosedOpenRange(0,2)); | |
| 203 shouldPass(1, inClosedOpenRange(0,2)); | |
| 204 shouldFail(2, inClosedOpenRange(0,2), | |
| 205 "Expected: be in range from 0 (inclusive) to 2 (exclusive) but: was <2>"); | |
| 206 | |
| 207 // String Matchers | |
| 208 | |
| 209 // isEmpty | |
| 210 shouldPass('', isEmpty); | |
| 211 shouldFail(null, isEmpty, | |
| 212 "Expected: empty but: was <null>"); | |
| 213 shouldFail(0, isEmpty, | |
| 214 "Expected: empty but: was <0>"); | |
| 215 shouldFail('a', isEmpty, "Expected: empty but: was 'a'"); | |
| 216 | |
| 217 // equalsIgnoringCase | |
| 218 shouldPass('hello', equalsIgnoringCase('HELLO')); | |
| 219 shouldFail('hi', equalsIgnoringCase('HELLO'), | |
| 220 "Expected: 'HELLO' ignoring case but: was 'hi'"); | |
| 221 | |
| 222 // equalsIgnoringWhitespace | |
| 223 shouldPass(' hello world ', equalsIgnoringWhitespace('hello world')); | |
| 224 shouldFail(' helloworld ', equalsIgnoringWhitespace('hello world'), | |
| 225 "Expected: 'hello world' ignoring whitespace but: was 'helloworld'"); | |
| 226 | |
| 227 // startsWith | |
| 228 shouldPass('hello', startsWith('')); | |
| 229 shouldPass('hello', startsWith('hell')); | |
| 230 shouldPass('hello', startsWith('hello')); | |
| 231 shouldFail('hello', startsWith('hello '), | |
| 232 "Expected: a string starting with 'hello ' but: was 'hello'"); | |
| 233 | |
| 234 // endsWith | |
| 235 shouldPass('hello', endsWith('')); | |
| 236 shouldPass('hello', endsWith('lo')); | |
| 237 shouldPass('hello', endsWith('hello')); | |
| 238 shouldFail('hello', endsWith(' hello'), | |
| 239 "Expected: a string ending with ' hello' but: was 'hello'"); | |
| 240 | |
| 241 // contains | |
| 242 shouldPass('hello', contains('')); | |
| 243 shouldPass('hello', contains('h')); | |
| 244 shouldPass('hello', contains('o')); | |
| 245 shouldPass('hello', contains('hell')); | |
| 246 shouldPass('hello', contains('hello')); | |
| 247 shouldFail('hello', contains(' '), | |
| 248 "Expected: contains ' ' but: was 'hello'"); | |
| 249 | |
| 250 // stringContainsInOrder | |
| 251 shouldPass('goodbye cruel world', stringContainsInOrder([''])); | |
| 252 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye'])); | |
| 253 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel'])); | |
| 254 shouldPass('goodbye cruel world', stringContainsInOrder(['world'])); | |
| 255 shouldPass('goodbye cruel world', | |
| 256 stringContainsInOrder(['good', 'bye', 'world'])); | |
| 257 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye', 'cruel'])) ; | |
| 258 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel', 'world'])); | |
| 259 shouldPass('goodbye cruel world', | |
| 260 stringContainsInOrder(['goodbye', 'cruel', 'world'])); | |
| 261 shouldFail('goodbye cruel world', | |
| 262 stringContainsInOrder(['goo', 'cruel', 'bye']), | |
| 263 "Expected: a string containing 'goo', 'cruel', 'bye' in order " | |
| 264 "but: was 'goodbye cruel world'"); | |
| 265 | |
| 266 // matches | |
| 267 shouldPass('c0d', matches('[a-z][0-9][a-z]')); | |
| 268 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); | |
| 269 shouldFail('cOd', matches('[a-z][0-9][a-z]'), | |
| 270 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'"); | |
| 271 | |
| 272 // Collection Matchers | |
| 273 | |
| 274 // emptyCollection | |
| 275 shouldPass([], isEmpty); | |
| 276 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>"); | |
| 277 | |
| 278 var d = [ 1, 2 ]; | |
| 279 | |
| 280 // contains | |
| 281 shouldPass(d, contains(1)); | |
| 282 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>"); | |
| 283 | |
| 284 var e = [1, 1, 1]; | |
| 285 | |
| 286 // everyElement | |
| 287 shouldFail(d, everyElement(1), | |
| 288 "Expected: every element <1> but: was <[1, 2]>"); | |
| 289 shouldPass(e, everyElement(1)); | |
| 290 | |
| 291 // someElement | |
| 292 shouldPass(d, someElement(2)); | |
| 293 shouldFail(e, someElement(2), | |
| 294 "Expected: some element <2> but: was <[1, 1, 1]>"); | |
| 295 | |
| 296 // orderedEquals | |
| 297 shouldPass(d, orderedEquals([1, 2])); | |
| 298 shouldFail(d, orderedEquals([2, 1]), | |
| 299 "Expected: equals <[2, 1]> ordered but: mismatch at position 0"); | |
| 300 | |
| 301 // unorderedEquals | |
| 302 shouldPass(d, unorderedEquals([2, 1])); | |
| 303 shouldFail(d, unorderedEquals([1]), | |
| 304 "Expected: equals <[1]> unordered but: has too many elements (2 > 1)"); | |
| 305 shouldFail(d, unorderedEquals([3, 2, 1]), | |
| 306 "Expected: equals <[3, 2, 1]> unordered but: has too few elements (2 < 3)"); | |
| 307 shouldFail(d, unorderedEquals([3, 1]), | |
| 308 "Expected: equals <[3, 1]> unordered " | |
| 309 "but: has no match for element 3 at position 0"); | |
| 310 | |
| 311 // Map Matchers | |
| 312 | |
| 313 // emptyMap | |
| 314 | |
| 315 shouldPass({}, isEmpty); | |
| 316 shouldPass(a, isEmpty); | |
| 317 a['foo'] = 'bar'; | |
| 318 shouldFail(a, isEmpty, "Expected: empty but: was <{foo: bar}>"); | |
| 319 | |
| 320 // mapContainsKey | |
| 321 shouldPass(a, contains('foo')); | |
| 322 shouldFail(b, contains('foo'), | |
| 323 "Expected: contains 'foo' but: was <{}>"); | |
| 324 shouldFail(10, contains('foo'), | |
| 325 "Expected: contains 'foo' but: was <10>"); | |
| 326 | |
| 327 // mapContainsValue | |
| 328 shouldPass(a, containsValue('bar')); | |
| 329 shouldFail(a, containsValue('ba'), | |
| 330 "Expected: contains value 'ba' but: was <{foo: bar}>"); | |
| 331 | |
| 332 // mapContains | |
| 333 shouldPass(a, containsPair('foo', 'bar')); | |
| 334 shouldFail(a, containsPair('foo', 'ba'), | |
| 335 "Expected: contains pair 'foo' => 'ba' " | |
| 336 "but: contains key 'foo' but with value "); | |
| 337 shouldFail(a, containsPair('fo', 'bar'), | |
| 338 "Expected: contains pair 'fo' => 'bar' " | |
| 339 "but: <{foo: bar}>' doesn't contain key ''fo'"); | |
| 340 | |
| 341 // mapLength | |
| 342 shouldPass(a, hasLength(1)); | |
| 343 shouldFail(b, hasLength(1), | |
| 344 "Expected: an object with length of <1> but: was <{}> with length of <0>") ; | |
| 345 | |
| 346 // Operator Matchers | |
| 347 | |
| 348 // anyOf | |
| 349 shouldFail(0, anyOf([equals(1), equals(2)]), | |
| 350 "Expected: (<1> or <2>) but: was <0>"); | |
| 351 shouldPass(1, anyOf([equals(1), equals(2)])); | |
| 352 | |
| 353 // allOf | |
| 354 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | |
| 355 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | |
| 356 "Expected: (a value less than <10> and a value greater than <0>) " | |
| 357 "but: a value greater than <0> was <-1>"); | |
| 358 } | |
| 359 | |
| 360 | |
| OLD | NEW |