Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
|
Siggi Cherem (dart-lang)
2012/06/06 00:26:08
I'm very excited that we now have tests for the un
gram
2012/06/06 16:23:56
I can add some tests for unit test framework itsel
| |
| 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'); | |
|
Siggi Cherem (dart-lang)
2012/06/06 00:26:08
rename to unittest_test?
gram
2012/06/06 16:23:56
We aren't consistent here. I took the camel case f
| |
| 6 | |
| 7 #import('../../lib/unittest/unittest.dart'); | |
| 8 | |
| 9 | |
| 10 doesNotThrow() {} | |
| 11 doesThrow() { throw 'X'; } | |
| 12 | |
| 13 int errorCount; | |
| 14 String errorString; | |
| 15 var testHandler; | |
| 16 | |
| 17 class MyFailureHandler implements FailureHandler { | |
| 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, Matcher matcher, String reason) { | |
| 28 fail(format(actual, matcher, reason)); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 void shouldFail(var value, Matcher matcher, String expected) { | |
| 33 errorCount = 0; | |
| 34 errorString = ''; | |
| 35 configureExpectHandler(testHandler); | |
| 36 expect(value, matcher); | |
| 37 configureExpectHandler(null); | |
| 38 expect(errorCount, equals(1)); | |
| 39 if (expected is String) | |
| 40 expect(errorString, equalsIgnoringWhitespace(expected)); | |
| 41 else | |
| 42 expect(errorString, expected); | |
| 43 } | |
| 44 | |
| 45 void shouldPass(var value, Matcher matcher) { | |
| 46 errorCount = 0; | |
| 47 errorString = ''; | |
| 48 configureExpectHandler(testHandler); | |
| 49 expect(value, matcher); | |
| 50 configureExpectHandler(null); | |
| 51 expect(errorCount, equals(0)); | |
| 52 } | |
| 53 | |
| 54 class PrefixMatcher extends BaseMatcher { | |
| 55 final String _prefix; | |
| 56 const PrefixMatcher(this._prefix); | |
| 57 bool matches(item) { | |
| 58 return item is String && | |
| 59 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | |
| 60 } | |
| 61 | |
| 62 Description describe(Description description) => | |
| 63 description.add('a string starting with '). | |
| 64 addDescriptionOf(collapseWhitespace(_prefix)). | |
| 65 add(' ignoring whitespace'); | |
| 66 } | |
| 67 | |
| 68 void main() { | |
| 69 | |
| 70 testHandler = new MyFailureHandler(); | |
| 71 | |
| 72 var a = new Map(); | |
| 73 var b = new Map(); | |
| 74 var c = new List(); | |
| 75 | |
| 76 // Core matchers | |
| 77 | |
| 78 group('Core matchers', () { | |
| 79 test('isTrue', () { | |
| 80 shouldPass(true, isTrue); | |
| 81 shouldFail(false, isTrue, "Expected: true but: was <false>"); | |
| 82 }); | |
| 83 | |
| 84 test('isFalse', () { | |
| 85 shouldPass(false, isFalse); | |
| 86 shouldFail(true, isFalse, "Expected: false but: was <true>"); | |
| 87 }); | |
| 88 | |
| 89 test('isNull', () { | |
| 90 shouldPass(null, isNull); | |
| 91 shouldFail(false, isNull, "Expected: null but: was <false>"); | |
| 92 }); | |
| 93 | |
| 94 test('isNotNull', () { | |
| 95 shouldPass(false, isNotNull); | |
| 96 shouldFail(null, isNotNull, "Expected: not null but: was <null>"); | |
| 97 }); | |
| 98 | |
| 99 test('same', () { | |
| 100 shouldPass(a, same(a)); | |
| 101 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>"); | |
| 102 }); | |
| 103 | |
| 104 test('equals', () { | |
| 105 shouldPass(a, equals(a)); | |
| 106 shouldFail(a, equals(b), "Expected: <{}> but: was <{}>"); | |
| 107 }); | |
| 108 | |
| 109 test('anything', () { | |
| 110 shouldPass(a, anything); | |
| 111 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); | |
| 112 }); | |
| 113 | |
| 114 test('throws', () { | |
| 115 shouldFail(doesNotThrow, throws, | |
| 116 "Expected: throws an exception but: no exception"); | |
| 117 shouldPass(doesThrow, throws); | |
| 118 }); | |
| 119 | |
| 120 test('throwsA', () { | |
| 121 shouldPass(doesThrow, throwsA(equals('X'))); | |
| 122 shouldFail(doesThrow, throwsA(equals('Y')), | |
| 123 "Expected: throws an exception which matches 'Y' " | |
| 124 "but: exception does not match 'Y'"); | |
| 125 }); | |
| 126 | |
| 127 test('returnsNormally', () { | |
| 128 shouldPass(doesNotThrow, returnsNormally); | |
| 129 shouldFail(doesThrow, returnsNormally, | |
| 130 "Expected: return normally but: threw exception"); | |
| 131 }); | |
| 132 | |
| 133 test('isInstanceOf', () { | |
| 134 shouldFail(0, new isInstanceOf<String>('String'), | |
|
Siggi Cherem (dart-lang)
2012/06/06 00:26:08
does this fail in frog?
gram
2012/06/06 16:23:56
Will test. I confess I tested only in Dartium.
| |
| 135 "Expected: an instance of String but: was <0>"); | |
| 136 shouldPass('cow', new isInstanceOf<String>('String')); | |
| 137 }); | |
| 138 | |
| 139 test('hasLength', () { | |
| 140 shouldPass(c, hasLength(0)); | |
| 141 shouldPass(a, hasLength(0)); | |
| 142 shouldPass('a', hasLength(1)); | |
| 143 shouldFail(0, hasLength(0), new PrefixMatcher( | |
| 144 "Expected: an object with length of <0> " | |
| 145 "but: was <0> has no length property")); | |
| 146 | |
| 147 c.add(0); | |
| 148 shouldPass(c, hasLength(1)); | |
| 149 shouldFail(c, hasLength(2), | |
| 150 "Expected: an object with length of <2> " | |
| 151 "but: was <[0]> with length of <1>"); | |
| 152 | |
| 153 c.add(0); | |
| 154 shouldFail(c, hasLength(1), | |
| 155 "Expected: an object with length of <1> " | |
| 156 "but: was <[0, 0]> with length of <2>"); | |
| 157 shouldPass(c, hasLength(2)); | |
| 158 }); | |
| 159 }); | |
| 160 | |
| 161 group('Numeric Matchers', () { | |
| 162 | |
| 163 test('greaterThan', () { | |
| 164 shouldPass(10, greaterThan(9)); | |
| 165 shouldFail(9, greaterThan(10), | |
| 166 "Expected: a value greater than <10> but: was <9>"); | |
| 167 }); | |
| 168 | |
| 169 test('greaterThanOrEqualTo', () { | |
| 170 shouldPass(10, greaterThanOrEqualTo(10)); | |
| 171 shouldFail(9, greaterThanOrEqualTo(10), | |
| 172 "Expected: a value greater than or equal to <10> but: was <9>"); | |
| 173 }); | |
| 174 | |
| 175 test('lessThan', () { | |
| 176 shouldFail(10, lessThan(9), "Expected: a value less than <9> " | |
| 177 "but: was <10>"); | |
| 178 shouldPass(9, lessThan(10)); | |
| 179 }); | |
| 180 | |
| 181 test('lessThanOrEqualTo', () { | |
| 182 shouldPass(10, lessThanOrEqualTo(10)); | |
| 183 shouldFail(11, lessThanOrEqualTo(10), | |
| 184 "Expected: a value less than or equal to <10> but: was <11>"); | |
| 185 }); | |
| 186 | |
| 187 test('isZero', () { | |
| 188 shouldPass(0, isZero); | |
| 189 shouldFail(1, isZero, "Expected: a value equal to <0> but: was <1>"); | |
| 190 }); | |
| 191 | |
| 192 test('isNonZero', () { | |
| 193 shouldFail(0, isNonZero, "Expected: a value not equal to <0> " | |
| 194 "but: was <0>"); | |
| 195 shouldPass(1, isNonZero); | |
| 196 }); | |
| 197 | |
| 198 test('isPositive', () { | |
| 199 shouldFail(-1, isPositive, "Expected: a positive value " | |
| 200 "but: was <-1>"); | |
| 201 shouldFail(0, isPositive, "Expected: a positive value " | |
| 202 "but: was <0>"); | |
| 203 shouldPass(1, isPositive); | |
| 204 }); | |
| 205 | |
| 206 test('isNegative', () { | |
| 207 shouldPass(-1, isNegative); | |
| 208 shouldFail(0, isNegative, | |
| 209 "Expected: a negative value but: was <0>"); | |
| 210 }); | |
| 211 | |
| 212 test('isNonPositive', () { | |
| 213 shouldPass(-1, isNonPositive); | |
| 214 shouldPass(0, isNonPositive); | |
| 215 shouldFail(1, isNonPositive, | |
| 216 "Expected: a non-positive value but: was <1>"); | |
| 217 }); | |
| 218 | |
| 219 test('isNonNegative', () { | |
| 220 shouldPass(1, isNonNegative); | |
| 221 shouldPass(0, isNonNegative); | |
| 222 shouldFail(-1, isNonNegative, | |
| 223 "Expected: a non-negative value but: was <-1>"); | |
| 224 }); | |
| 225 | |
| 226 test('closeTo', () { | |
| 227 shouldPass(0, closeTo(0, 1)); | |
| 228 shouldPass(-1, closeTo(0, 1)); | |
| 229 shouldPass(1, closeTo(0, 1)); | |
| 230 shouldFail(1.001, closeTo(0, 1), | |
| 231 "Expected: a numeric value within <1> of <0> " | |
| 232 "but: <1.001> differed by <1.001>"); | |
| 233 shouldFail(-1.001, closeTo(0, 1), | |
| 234 "Expected: a numeric value within <1> of <0> " | |
| 235 "but: <-1.001> differed by <1.001>"); | |
| 236 }); | |
| 237 | |
| 238 test('inInclusiveRange', () { | |
| 239 shouldFail(-1, inInclusiveRange(0,2), | |
| 240 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
| 241 "but: was <-1>"); | |
| 242 shouldPass(0, inInclusiveRange(0,2)); | |
| 243 shouldPass(1, inInclusiveRange(0,2)); | |
| 244 shouldPass(2, inInclusiveRange(0,2)); | |
| 245 shouldFail(3, inInclusiveRange(0,2), | |
| 246 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
| 247 "but: was <3>"); | |
| 248 }); | |
| 249 | |
| 250 test('inExclusiveRange', () { | |
| 251 shouldFail(0, inExclusiveRange(0,2), | |
| 252 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
| 253 "but: was <0>"); | |
| 254 shouldPass(1, inExclusiveRange(0,2)); | |
| 255 shouldFail(2, inExclusiveRange(0,2), | |
| 256 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
| 257 "but: was <2>"); | |
| 258 }); | |
| 259 | |
| 260 test('inOpenClosedRange', () { | |
| 261 shouldFail(0, inOpenClosedRange(0,2), | |
| 262 "Expected: be in range from 0 (exclusive) to 2 (inclusive) " | |
| 263 "but: was <0>"); | |
| 264 shouldPass(1, inOpenClosedRange(0,2)); | |
| 265 shouldPass(2, inOpenClosedRange(0,2)); | |
| 266 }); | |
| 267 | |
| 268 test('inClosedOpenRange', () { | |
| 269 shouldPass(0, inClosedOpenRange(0,2)); | |
| 270 shouldPass(1, inClosedOpenRange(0,2)); | |
| 271 shouldFail(2, inClosedOpenRange(0,2), | |
| 272 "Expected: be in range from 0 (inclusive) to 2 (exclusive) " | |
| 273 "but: was <2>"); | |
| 274 }); | |
| 275 }); | |
| 276 | |
| 277 | |
| 278 group('String Matchers', () { | |
| 279 | |
| 280 test('isEmpty', () { | |
| 281 shouldPass('', isEmpty); | |
| 282 shouldFail(null, isEmpty, | |
| 283 "Expected: empty but: was <null>"); | |
| 284 shouldFail(0, isEmpty, | |
| 285 "Expected: empty but: was <0>"); | |
| 286 shouldFail('a', isEmpty, "Expected: empty but: was 'a'"); | |
| 287 }); | |
| 288 | |
| 289 test('equalsIgnoringCase', () { | |
| 290 shouldPass('hello', equalsIgnoringCase('HELLO')); | |
| 291 shouldFail('hi', equalsIgnoringCase('HELLO'), | |
| 292 "Expected: 'HELLO' ignoring case but: was 'hi'"); | |
| 293 }); | |
| 294 | |
| 295 test('equalsIgnoringWhitespace', () { | |
| 296 shouldPass(' hello world ', equalsIgnoringWhitespace('hello world')); | |
| 297 shouldFail(' helloworld ', equalsIgnoringWhitespace('hello world'), | |
| 298 "Expected: 'hello world' ignoring whitespace but: was 'helloworld'"); | |
| 299 }); | |
| 300 | |
| 301 test('startsWith', () { | |
| 302 shouldPass('hello', startsWith('')); | |
| 303 shouldPass('hello', startsWith('hell')); | |
| 304 shouldPass('hello', startsWith('hello')); | |
| 305 shouldFail('hello', startsWith('hello '), | |
| 306 "Expected: a string starting with 'hello ' but: was 'hello'"); | |
| 307 }); | |
| 308 | |
| 309 test('endsWith', () { | |
| 310 shouldPass('hello', endsWith('')); | |
| 311 shouldPass('hello', endsWith('lo')); | |
| 312 shouldPass('hello', endsWith('hello')); | |
| 313 shouldFail('hello', endsWith(' hello'), | |
| 314 "Expected: a string ending with ' hello' but: was 'hello'"); | |
| 315 }); | |
| 316 | |
| 317 test('contains', () { | |
| 318 shouldPass('hello', contains('')); | |
| 319 shouldPass('hello', contains('h')); | |
| 320 shouldPass('hello', contains('o')); | |
| 321 shouldPass('hello', contains('hell')); | |
| 322 shouldPass('hello', contains('hello')); | |
| 323 shouldFail('hello', contains(' '), | |
| 324 "Expected: contains ' ' but: was 'hello'"); | |
| 325 }); | |
| 326 | |
| 327 test('stringContainsInOrder', () { | |
| 328 shouldPass('goodbye cruel world', stringContainsInOrder([''])); | |
| 329 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye'])); | |
| 330 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel'])); | |
| 331 shouldPass('goodbye cruel world', stringContainsInOrder(['world'])); | |
| 332 shouldPass('goodbye cruel world', | |
| 333 stringContainsInOrder(['good', 'bye', 'world'])); | |
| 334 shouldPass('goodbye cruel world', | |
| 335 stringContainsInOrder(['goodbye', 'cruel'])); | |
| 336 shouldPass('goodbye cruel world', | |
| 337 stringContainsInOrder(['cruel', 'world'])); | |
| 338 shouldPass('goodbye cruel world', | |
| 339 stringContainsInOrder(['goodbye', 'cruel', 'world'])); | |
| 340 shouldFail('goodbye cruel world', | |
| 341 stringContainsInOrder(['goo', 'cruel', 'bye']), | |
| 342 "Expected: a string containing 'goo', 'cruel', 'bye' in order " | |
| 343 "but: was 'goodbye cruel world'"); | |
| 344 }); | |
| 345 | |
| 346 test('matches', () { | |
| 347 shouldPass('c0d', matches('[a-z][0-9][a-z]')); | |
| 348 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); | |
| 349 shouldFail('cOd', matches('[a-z][0-9][a-z]'), | |
| 350 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'"); | |
| 351 }); | |
| 352 }); | |
| 353 | |
| 354 group('Collection Matchers', () { | |
| 355 | |
| 356 test('isEmpty', () { | |
| 357 shouldPass([], isEmpty); | |
| 358 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>"); | |
| 359 }); | |
| 360 | |
| 361 var d = [1, 2]; | |
| 362 | |
| 363 test('contains', () { | |
| 364 shouldPass(d, contains(1)); | |
| 365 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>"); | |
| 366 }); | |
| 367 | |
| 368 var e = [1, 1, 1]; | |
| 369 | |
| 370 test('everyElement', () { | |
| 371 shouldFail(d, everyElement(1), | |
| 372 "Expected: every element <1> but: was <[1, 2]>"); | |
| 373 shouldPass(e, everyElement(1)); | |
| 374 }); | |
| 375 | |
| 376 test('someElement', () { | |
| 377 shouldPass(d, someElement(2)); | |
| 378 shouldFail(e, someElement(2), | |
| 379 "Expected: some element <2> but: was <[1, 1, 1]>"); | |
| 380 }); | |
| 381 | |
| 382 test('orderedEquals', () { | |
| 383 shouldPass(d, orderedEquals([1, 2])); | |
| 384 shouldFail(d, orderedEquals([2, 1]), | |
| 385 "Expected: equals <[2, 1]> ordered " | |
| 386 "but: mismatch at position 0"); | |
| 387 }); | |
| 388 | |
| 389 test('unorderedEquals', () { | |
| 390 shouldPass(d, unorderedEquals([2, 1])); | |
| 391 shouldFail(d, unorderedEquals([1]), | |
| 392 "Expected: equals <[1]> unordered " | |
| 393 "but: has too many elements (2 > 1)"); | |
| 394 shouldFail(d, unorderedEquals([3, 2, 1]), | |
| 395 "Expected: equals <[3, 2, 1]> unordered " | |
| 396 "but: has too few elements (2 < 3)"); | |
| 397 shouldFail(d, unorderedEquals([3, 1]), | |
| 398 "Expected: equals <[3, 1]> unordered " | |
| 399 "but: has no match for element 3 at position 0"); | |
| 400 }); | |
| 401 }); | |
| 402 | |
| 403 group('Map Matchers', () { | |
| 404 | |
| 405 test('isEmpty', () { | |
| 406 shouldPass({}, isEmpty); | |
| 407 shouldPass(a, isEmpty); | |
| 408 a['foo'] = 'bar'; | |
| 409 shouldFail(a, isEmpty, "Expected: empty but: was <{foo: bar}>"); | |
| 410 }); | |
| 411 | |
| 412 test('contains', () { | |
| 413 shouldPass(a, contains('foo')); | |
| 414 shouldFail(b, contains('foo'), | |
| 415 "Expected: contains 'foo' but: was <{}>"); | |
| 416 shouldFail(10, contains('foo'), | |
| 417 "Expected: contains 'foo' but: was <10>"); | |
| 418 }); | |
| 419 | |
| 420 test('containsValue', () { | |
| 421 shouldPass(a, containsValue('bar')); | |
| 422 shouldFail(a, containsValue('ba'), | |
| 423 "Expected: contains value 'ba' but: was <{foo: bar}>"); | |
| 424 }); | |
| 425 | |
| 426 test('containsPair', () { | |
| 427 shouldPass(a, containsPair('foo', 'bar')); | |
| 428 shouldFail(a, containsPair('foo', 'ba'), | |
| 429 "Expected: contains pair 'foo' => 'ba' " | |
| 430 "but: contains key 'foo' but with value "); | |
| 431 shouldFail(a, containsPair('fo', 'bar'), | |
| 432 "Expected: contains pair 'fo' => 'bar' " | |
| 433 "but: <{foo: bar}>' doesn't contain key ''fo'"); | |
| 434 }); | |
| 435 | |
| 436 test('hasLength', () { | |
| 437 shouldPass(a, hasLength(1)); | |
| 438 shouldFail(b, hasLength(1), | |
| 439 "Expected: an object with length of <1> " | |
| 440 "but: was <{}> with length of <0>"); | |
| 441 }); | |
| 442 }); | |
| 443 | |
| 444 group('Operator Matchers', () { | |
| 445 | |
| 446 test('anyOf', () { | |
| 447 shouldFail(0, anyOf([equals(1), equals(2)]), | |
| 448 "Expected: (<1> or <2>) but: was <0>"); | |
| 449 shouldPass(1, anyOf([equals(1), equals(2)])); | |
| 450 }); | |
| 451 | |
| 452 test('allOf', () { | |
| 453 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | |
| 454 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | |
| 455 "Expected: (a value less than <10> and a value greater than <0>) " | |
| 456 "but: a value greater than <0> was <-1>"); | |
| 457 }); | |
| 458 }); | |
| 459 } | |
| 460 | |
| 461 | |
| OLD | NEW |