Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, 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 #library('matcherTest'); | |
| 6 #import('../../../lib/unittest/unittest.dart'); | |
| 7 #source('test_utils.dart'); | |
| 8 | |
| 9 doesNotThrow() {} | |
| 10 doesThrow() { throw 'X'; } | |
| 11 | |
| 12 class PrefixMatcher extends BaseMatcher { | |
| 13 final String _prefix; | |
| 14 const PrefixMatcher(this._prefix); | |
| 15 bool matches(item) { | |
| 16 return item is String && | |
| 17 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | |
| 18 } | |
| 19 | |
| 20 Description describe(Description description) => | |
| 21 description.add('a string starting with '). | |
| 22 addDescriptionOf(collapseWhitespace(_prefix)). | |
| 23 add(' ignoring whitespace'); | |
| 24 } | |
| 25 | |
| 26 void main() { | |
| 27 | |
| 28 initUtils(); | |
| 29 | |
| 30 var a = new Map(); | |
| 31 var b = new Map(); | |
| 32 var c = new List(); | |
| 33 | |
| 34 // Core matchers | |
| 35 | |
| 36 group('Core matchers', () { | |
| 37 test('isTrue', () { | |
| 38 shouldPass(true, isTrue); | |
| 39 shouldFail(false, isTrue, "Expected: true but: was <false>"); | |
| 40 }); | |
| 41 | |
| 42 test('isFalse', () { | |
| 43 shouldPass(false, isFalse); | |
| 44 shouldFail(true, isFalse, "Expected: false but: was <true>"); | |
| 45 }); | |
| 46 | |
| 47 test('isNull', () { | |
| 48 shouldPass(null, isNull); | |
| 49 shouldFail(false, isNull, "Expected: null but: was <false>"); | |
| 50 }); | |
| 51 | |
| 52 test('isNotNull', () { | |
| 53 shouldPass(false, isNotNull); | |
| 54 shouldFail(null, isNotNull, "Expected: not null but: was <null>"); | |
| 55 }); | |
| 56 | |
| 57 test('same', () { | |
| 58 shouldPass(a, same(a)); | |
| 59 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>"); | |
| 60 }); | |
| 61 | |
| 62 test('equals', () { | |
| 63 shouldPass(a, equals(a)); | |
| 64 shouldFail(a, equals(b), "Expected: <{}> but: was <{}>"); | |
|
eub
2012/06/15 21:02:49
Today I learned something about Map's equality ope
gram
2012/06/15 21:25:55
Done.
| |
| 65 }); | |
| 66 | |
| 67 test('anything', () { | |
| 68 shouldPass(a, anything); | |
|
eub
2012/06/15 21:02:49
Might exercise some other cases like (null, anythi
gram
2012/06/15 21:25:55
Done.
| |
| 69 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); | |
| 70 }); | |
| 71 | |
| 72 test('throws', () { | |
| 73 shouldFail(doesNotThrow, throws, | |
| 74 "Expected: throws an exception but: no exception"); | |
| 75 shouldPass(doesThrow, throws); | |
| 76 }); | |
| 77 | |
| 78 test('returnsNormally', () { | |
| 79 shouldPass(doesNotThrow, returnsNormally); | |
| 80 shouldFail(doesThrow, returnsNormally, | |
| 81 "Expected: return normally but: threw exception"); | |
| 82 }); | |
| 83 | |
| 84 test('hasLength', () { | |
| 85 shouldPass(c, hasLength(0)); | |
| 86 shouldPass(a, hasLength(0)); | |
| 87 shouldPass('a', hasLength(1)); | |
| 88 shouldFail(0, hasLength(0), new PrefixMatcher( | |
| 89 "Expected: an object with length of <0> " | |
| 90 "but: was <0> has no length property")); | |
| 91 | |
| 92 c.add(0); | |
|
eub
2012/06/15 21:02:49
I have a religious objection to mutating shared te
gram
2012/06/15 21:25:55
Point taken; I replicated these so each test has i
| |
| 93 shouldPass(c, hasLength(1)); | |
| 94 shouldFail(c, hasLength(2), | |
| 95 "Expected: an object with length of <2> " | |
| 96 "but: was <[0]> with length of <1>"); | |
| 97 | |
| 98 c.add(0); | |
| 99 shouldFail(c, hasLength(1), | |
| 100 "Expected: an object with length of <1> " | |
| 101 "but: was <[0, 0]> with length of <2>"); | |
| 102 shouldPass(c, hasLength(2)); | |
| 103 }); | |
| 104 }); | |
| 105 | |
| 106 group('Numeric Matchers', () { | |
| 107 | |
| 108 test('greaterThan', () { | |
| 109 shouldPass(10, greaterThan(9)); | |
| 110 shouldFail(9, greaterThan(10), | |
| 111 "Expected: a value greater than <10> but: was <9>"); | |
| 112 }); | |
| 113 | |
| 114 test('greaterThanOrEqualTo', () { | |
| 115 shouldPass(10, greaterThanOrEqualTo(10)); | |
| 116 shouldFail(9, greaterThanOrEqualTo(10), | |
| 117 "Expected: a value greater than or equal to <10> but: was <9>"); | |
| 118 }); | |
| 119 | |
| 120 test('lessThan', () { | |
| 121 shouldFail(10, lessThan(9), "Expected: a value less than <9> " | |
| 122 "but: was <10>"); | |
| 123 shouldPass(9, lessThan(10)); | |
| 124 }); | |
| 125 | |
| 126 test('lessThanOrEqualTo', () { | |
| 127 shouldPass(10, lessThanOrEqualTo(10)); | |
| 128 shouldFail(11, lessThanOrEqualTo(10), | |
| 129 "Expected: a value less than or equal to <10> but: was <11>"); | |
| 130 }); | |
| 131 | |
| 132 test('isZero', () { | |
| 133 shouldPass(0, isZero); | |
| 134 shouldFail(1, isZero, "Expected: a value equal to <0> but: was <1>"); | |
| 135 }); | |
| 136 | |
| 137 test('isNonZero', () { | |
| 138 shouldFail(0, isNonZero, "Expected: a value not equal to <0> " | |
| 139 "but: was <0>"); | |
| 140 shouldPass(1, isNonZero); | |
| 141 }); | |
| 142 | |
| 143 test('isPositive', () { | |
| 144 shouldFail(-1, isPositive, "Expected: a positive value " | |
| 145 "but: was <-1>"); | |
| 146 shouldFail(0, isPositive, "Expected: a positive value " | |
| 147 "but: was <0>"); | |
| 148 shouldPass(1, isPositive); | |
| 149 }); | |
| 150 | |
| 151 test('isNegative', () { | |
| 152 shouldPass(-1, isNegative); | |
| 153 shouldFail(0, isNegative, | |
| 154 "Expected: a negative value but: was <0>"); | |
| 155 }); | |
| 156 | |
| 157 test('isNonPositive', () { | |
| 158 shouldPass(-1, isNonPositive); | |
| 159 shouldPass(0, isNonPositive); | |
| 160 shouldFail(1, isNonPositive, | |
| 161 "Expected: a non-positive value but: was <1>"); | |
| 162 }); | |
| 163 | |
| 164 test('isNonNegative', () { | |
| 165 shouldPass(1, isNonNegative); | |
| 166 shouldPass(0, isNonNegative); | |
| 167 shouldFail(-1, isNonNegative, | |
| 168 "Expected: a non-negative value but: was <-1>"); | |
| 169 }); | |
| 170 | |
| 171 test('closeTo', () { | |
| 172 shouldPass(0, closeTo(0, 1)); | |
| 173 shouldPass(-1, closeTo(0, 1)); | |
| 174 shouldPass(1, closeTo(0, 1)); | |
| 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 shouldFail(-1.001, closeTo(0, 1), | |
| 179 "Expected: a numeric value within <1> of <0> " | |
| 180 "but: <-1.001> differed by <1.001>"); | |
| 181 }); | |
| 182 | |
| 183 test('inInclusiveRange', () { | |
| 184 shouldFail(-1, inInclusiveRange(0,2), | |
| 185 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
| 186 "but: was <-1>"); | |
| 187 shouldPass(0, inInclusiveRange(0,2)); | |
| 188 shouldPass(1, inInclusiveRange(0,2)); | |
| 189 shouldPass(2, inInclusiveRange(0,2)); | |
| 190 shouldFail(3, inInclusiveRange(0,2), | |
| 191 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
| 192 "but: was <3>"); | |
| 193 }); | |
| 194 | |
| 195 test('inExclusiveRange', () { | |
| 196 shouldFail(0, inExclusiveRange(0,2), | |
| 197 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
| 198 "but: was <0>"); | |
| 199 shouldPass(1, inExclusiveRange(0,2)); | |
| 200 shouldFail(2, inExclusiveRange(0,2), | |
| 201 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
| 202 "but: was <2>"); | |
| 203 }); | |
| 204 | |
| 205 test('inOpenClosedRange', () { | |
| 206 shouldFail(0, inOpenClosedRange(0,2), | |
| 207 "Expected: be in range from 0 (exclusive) to 2 (inclusive) " | |
| 208 "but: was <0>"); | |
| 209 shouldPass(1, inOpenClosedRange(0,2)); | |
| 210 shouldPass(2, inOpenClosedRange(0,2)); | |
| 211 }); | |
| 212 | |
| 213 test('inClosedOpenRange', () { | |
| 214 shouldPass(0, inClosedOpenRange(0,2)); | |
| 215 shouldPass(1, inClosedOpenRange(0,2)); | |
| 216 shouldFail(2, inClosedOpenRange(0,2), | |
| 217 "Expected: be in range from 0 (inclusive) to 2 (exclusive) " | |
| 218 "but: was <2>"); | |
| 219 }); | |
| 220 }); | |
| 221 | |
| 222 | |
| 223 group('String Matchers', () { | |
| 224 | |
| 225 test('isEmpty', () { | |
| 226 shouldPass('', isEmpty); | |
| 227 shouldFail(null, isEmpty, | |
| 228 "Expected: empty but: was <null>"); | |
| 229 shouldFail(0, isEmpty, | |
| 230 "Expected: empty but: was <0>"); | |
| 231 shouldFail('a', isEmpty, "Expected: empty but: was 'a'"); | |
| 232 }); | |
| 233 | |
| 234 test('equalsIgnoringCase', () { | |
| 235 shouldPass('hello', equalsIgnoringCase('HELLO')); | |
| 236 shouldFail('hi', equalsIgnoringCase('HELLO'), | |
| 237 "Expected: 'HELLO' ignoring case but: was 'hi'"); | |
| 238 }); | |
| 239 | |
| 240 test('equalsIgnoringWhitespace', () { | |
| 241 shouldPass(' hello world ', equalsIgnoringWhitespace('hello world')); | |
| 242 shouldFail(' helloworld ', equalsIgnoringWhitespace('hello world'), | |
| 243 "Expected: 'hello world' ignoring whitespace but: was 'helloworld'"); | |
| 244 }); | |
| 245 | |
| 246 test('startsWith', () { | |
| 247 shouldPass('hello', startsWith('')); | |
| 248 shouldPass('hello', startsWith('hell')); | |
| 249 shouldPass('hello', startsWith('hello')); | |
| 250 shouldFail('hello', startsWith('hello '), | |
| 251 "Expected: a string starting with 'hello ' but: was 'hello'"); | |
| 252 }); | |
| 253 | |
| 254 test('endsWith', () { | |
| 255 shouldPass('hello', endsWith('')); | |
| 256 shouldPass('hello', endsWith('lo')); | |
| 257 shouldPass('hello', endsWith('hello')); | |
| 258 shouldFail('hello', endsWith(' hello'), | |
| 259 "Expected: a string ending with ' hello' but: was 'hello'"); | |
| 260 }); | |
| 261 | |
| 262 test('contains', () { | |
| 263 shouldPass('hello', contains('')); | |
| 264 shouldPass('hello', contains('h')); | |
| 265 shouldPass('hello', contains('o')); | |
| 266 shouldPass('hello', contains('hell')); | |
| 267 shouldPass('hello', contains('hello')); | |
| 268 shouldFail('hello', contains(' '), | |
| 269 "Expected: contains ' ' but: was 'hello'"); | |
| 270 }); | |
| 271 | |
| 272 test('stringContainsInOrder', () { | |
| 273 shouldPass('goodbye cruel world', stringContainsInOrder([''])); | |
| 274 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye'])); | |
| 275 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel'])); | |
| 276 shouldPass('goodbye cruel world', stringContainsInOrder(['world'])); | |
| 277 shouldPass('goodbye cruel world', | |
| 278 stringContainsInOrder(['good', 'bye', 'world'])); | |
| 279 shouldPass('goodbye cruel world', | |
| 280 stringContainsInOrder(['goodbye', 'cruel'])); | |
| 281 shouldPass('goodbye cruel world', | |
| 282 stringContainsInOrder(['cruel', 'world'])); | |
| 283 shouldPass('goodbye cruel world', | |
| 284 stringContainsInOrder(['goodbye', 'cruel', 'world'])); | |
| 285 shouldFail('goodbye cruel world', | |
| 286 stringContainsInOrder(['goo', 'cruel', 'bye']), | |
| 287 "Expected: a string containing 'goo', 'cruel', 'bye' in order " | |
| 288 "but: was 'goodbye cruel world'"); | |
| 289 }); | |
| 290 | |
| 291 test('matches', () { | |
| 292 shouldPass('c0d', matches('[a-z][0-9][a-z]')); | |
| 293 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); | |
| 294 shouldFail('cOd', matches('[a-z][0-9][a-z]'), | |
| 295 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'"); | |
| 296 }); | |
| 297 }); | |
| 298 | |
| 299 group('Collection Matchers', () { | |
| 300 | |
| 301 test('isEmpty', () { | |
| 302 shouldPass([], isEmpty); | |
| 303 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>"); | |
| 304 }); | |
| 305 | |
| 306 var d = [1, 2]; | |
| 307 | |
| 308 test('contains', () { | |
| 309 shouldPass(d, contains(1)); | |
| 310 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>"); | |
| 311 }); | |
| 312 | |
| 313 var e = [1, 1, 1]; | |
| 314 | |
| 315 test('everyElement', () { | |
| 316 shouldFail(d, everyElement(1), | |
| 317 "Expected: every element <1> but: was <[1, 2]>"); | |
| 318 shouldPass(e, everyElement(1)); | |
| 319 }); | |
| 320 | |
| 321 test('someElement', () { | |
| 322 shouldPass(d, someElement(2)); | |
| 323 shouldFail(e, someElement(2), | |
| 324 "Expected: some element <2> but: was <[1, 1, 1]>"); | |
| 325 }); | |
| 326 | |
| 327 test('orderedEquals', () { | |
| 328 shouldPass(d, orderedEquals([1, 2])); | |
| 329 shouldFail(d, orderedEquals([2, 1]), | |
| 330 "Expected: equals <[2, 1]> ordered " | |
| 331 "but: mismatch at position 0"); | |
| 332 }); | |
| 333 | |
| 334 test('unorderedEquals', () { | |
| 335 shouldPass(d, unorderedEquals([2, 1])); | |
| 336 shouldFail(d, unorderedEquals([1]), | |
| 337 "Expected: equals <[1]> unordered " | |
| 338 "but: has too many elements (2 > 1)"); | |
| 339 shouldFail(d, unorderedEquals([3, 2, 1]), | |
| 340 "Expected: equals <[3, 2, 1]> unordered " | |
| 341 "but: has too few elements (2 < 3)"); | |
| 342 shouldFail(d, unorderedEquals([3, 1]), | |
| 343 "Expected: equals <[3, 1]> unordered " | |
| 344 "but: has no match for element 3 at position 0"); | |
| 345 }); | |
| 346 }); | |
| 347 | |
| 348 group('Map Matchers', () { | |
| 349 | |
| 350 test('isEmpty', () { | |
| 351 shouldPass({}, isEmpty); | |
| 352 shouldPass(a, isEmpty); | |
| 353 a['foo'] = 'bar'; | |
| 354 shouldFail(a, isEmpty, "Expected: empty but: was <{foo: bar}>"); | |
| 355 }); | |
| 356 | |
| 357 test('contains', () { | |
| 358 shouldPass(a, contains('foo')); | |
| 359 shouldFail(b, contains('foo'), | |
| 360 "Expected: contains 'foo' but: was <{}>"); | |
| 361 shouldFail(10, contains('foo'), | |
| 362 "Expected: contains 'foo' but: was <10>"); | |
| 363 }); | |
| 364 | |
| 365 test('containsValue', () { | |
| 366 shouldPass(a, containsValue('bar')); | |
| 367 shouldFail(a, containsValue('ba'), | |
| 368 "Expected: contains value 'ba' but: was <{foo: bar}>"); | |
| 369 }); | |
| 370 | |
| 371 test('containsPair', () { | |
| 372 shouldPass(a, containsPair('foo', 'bar')); | |
| 373 shouldFail(a, containsPair('foo', 'ba'), | |
| 374 "Expected: contains pair 'foo' => 'ba' " | |
| 375 "but: contains key 'foo' but with value was 'bar'"); | |
| 376 shouldFail(a, containsPair('fo', 'bar'), | |
| 377 "Expected: contains pair 'fo' => 'bar' " | |
| 378 "but: <{foo: bar}>' doesn't contain key ''fo'"); | |
| 379 }); | |
| 380 | |
| 381 test('hasLength', () { | |
| 382 shouldPass(a, hasLength(1)); | |
| 383 shouldFail(b, hasLength(1), | |
| 384 "Expected: an object with length of <1> " | |
| 385 "but: was <{}> with length of <0>"); | |
| 386 }); | |
| 387 }); | |
| 388 | |
| 389 group('Operator Matchers', () { | |
| 390 | |
| 391 test('anyOf', () { | |
| 392 shouldFail(0, anyOf([equals(1), equals(2)]), | |
| 393 "Expected: (<1> or <2>) but: was <0>"); | |
| 394 shouldPass(1, anyOf([equals(1), equals(2)])); | |
| 395 }); | |
| 396 | |
| 397 test('allOf', () { | |
| 398 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | |
| 399 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | |
| 400 "Expected: (a value less than <10> and a value greater than <0>) " | |
| 401 "but: a value greater than <0> was <-1>"); | |
| 402 }); | |
| 403 }); | |
| 404 } | |
| 405 | |
| OLD | NEW |