| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 library unittestTests; | 5 library unittestTests; |
| 6 import 'package:unittest/unittest.dart'; | 6 import 'package:unittest/unittest.dart'; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 part 'test_utils.dart'; | 8 part 'test_utils.dart'; |
| 9 | 9 |
| 10 doesNotThrow() {} | 10 doesNotThrow() {} |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 class Widget { | 27 class Widget { |
| 28 int price; | 28 int price; |
| 29 } | 29 } |
| 30 | 30 |
| 31 class HasPrice extends CustomMatcher { | 31 class HasPrice extends CustomMatcher { |
| 32 const HasPrice(matcher) : | 32 const HasPrice(matcher) : |
| 33 super("Widget with a price that is", "price", matcher); | 33 super("Widget with a price that is", "price", matcher); |
| 34 featureValueOf(actual) => actual.price; | 34 featureValueOf(actual) => actual.price; |
| 35 } | 35 } |
| 36 | 36 |
| 37 class SimpleIterable extends Iterable { |
| 38 int count; |
| 39 SimpleIterable(this.count); |
| 40 |
| 41 bool contains(int val) => count < val ? false : true; |
| 42 |
| 43 bool any(bool f(element)) { |
| 44 for(var i = 0; i <= count; i++) { |
| 45 if(f(i)) return true; |
| 46 } |
| 47 return false; |
| 48 } |
| 49 |
| 50 String toString() => "<[$count]>"; |
| 51 |
| 52 Iterator get iterator { |
| 53 return new SimpleIterator(count); |
| 54 } |
| 55 } |
| 56 |
| 57 class SimpleIterator implements Iterator { |
| 58 int _count; |
| 59 int _current; |
| 60 |
| 61 SimpleIterator(this._count); |
| 62 |
| 63 bool moveNext() { |
| 64 if (_count > 0) { |
| 65 _current = _count; |
| 66 _count--; |
| 67 return true; |
| 68 } |
| 69 _current = null; |
| 70 return false; |
| 71 } |
| 72 |
| 73 get current => _current; |
| 74 } |
| 75 |
| 37 void main() { | 76 void main() { |
| 38 | 77 |
| 39 initUtils(); | 78 initUtils(); |
| 40 | 79 |
| 41 // Core matchers | 80 // Core matchers |
| 42 | 81 |
| 43 group('Core matchers', () { | 82 group('Core matchers', () { |
| 44 | 83 |
| 45 test('isTrue', () { | 84 test('isTrue', () { |
| 46 shouldPass(true, isTrue); | 85 shouldPass(true, isTrue); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 197 |
| 159 test('throwsUnsupportedError', () { | 198 test('throwsUnsupportedError', () { |
| 160 shouldPass(() { throw new UnsupportedError(''); }, | 199 shouldPass(() { throw new UnsupportedError(''); }, |
| 161 throwsUnsupportedError); | 200 throwsUnsupportedError); |
| 162 shouldFail(() { throw new Exception(); }, | 201 shouldFail(() { throw new Exception(); }, |
| 163 throwsUnsupportedError, | 202 throwsUnsupportedError, |
| 164 "Expected: throws an exception which matches UnsupportedError " | 203 "Expected: throws an exception which matches UnsupportedError " |
| 165 "but: exception <Exception> does not match " | 204 "but: exception <Exception> does not match " |
| 166 "UnsupportedError."); | 205 "UnsupportedError."); |
| 167 }); | 206 }); |
| 207 |
| 208 test('throwsStateError', () { |
| 209 shouldPass(() { throw new StateError(''); }, |
| 210 throwsStateError); |
| 211 shouldFail(() { throw new Exception(); }, |
| 212 throwsStateError, |
| 213 "Expected: throws an exception which matches StateError " |
| 214 "but: exception <Exception> does not match " |
| 215 "StateError."); |
| 216 }); |
| 168 | 217 |
| 169 test('returnsNormally', () { | 218 test('returnsNormally', () { |
| 170 shouldPass(doesNotThrow, returnsNormally); | 219 shouldPass(doesNotThrow, returnsNormally); |
| 171 shouldFail(doesThrow, returnsNormally, | 220 shouldFail(doesThrow, returnsNormally, |
| 172 "Expected: return normally but: threw 'X'."); | 221 "Expected: return normally but: threw 'X'."); |
| 173 }); | 222 }); |
| 174 | 223 |
| 175 test('hasLength', () { | 224 test('hasLength', () { |
| 176 var a = new Map(); | 225 var a = new Map(); |
| 177 var b = new List(); | 226 var b = new List(); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 }); | 430 }); |
| 382 | 431 |
| 383 test('matches', () { | 432 test('matches', () { |
| 384 shouldPass('c0d', matches('[a-z][0-9][a-z]')); | 433 shouldPass('c0d', matches('[a-z][0-9][a-z]')); |
| 385 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); | 434 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); |
| 386 shouldFail('cOd', matches('[a-z][0-9][a-z]'), | 435 shouldFail('cOd', matches('[a-z][0-9][a-z]'), |
| 387 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'."); | 436 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'."); |
| 388 }); | 437 }); |
| 389 }); | 438 }); |
| 390 | 439 |
| 440 group('Iterable Matchers', () { |
| 441 test('isEmpty', () { |
| 442 var d = new SimpleIterable(0); |
| 443 var e = new SimpleIterable(1); |
| 444 shouldPass(d, isEmpty); |
| 445 shouldFail(e, isEmpty, "Expected: empty but: was <[1]>."); |
| 446 }); |
| 447 |
| 448 test('contains', () { |
| 449 var d = new SimpleIterable(3); |
| 450 shouldPass(d, contains(2)); |
| 451 shouldFail(d, contains(5), "Expected: contains <5> but: was <[3]>."); |
| 452 }); |
| 453 }); |
| 454 |
| 391 group('Collection Matchers', () { | 455 group('Collection Matchers', () { |
| 392 | 456 |
| 393 test('isEmpty', () { | 457 test('isEmpty', () { |
| 394 shouldPass([], isEmpty); | 458 shouldPass([], isEmpty); |
| 395 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>."); | 459 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>."); |
| 396 }); | 460 }); |
| 397 | 461 |
| 398 test('contains', () { | 462 test('contains', () { |
| 399 var d = [1, 2]; | 463 var d = [1, 2]; |
| 400 shouldPass(d, contains(1)); | 464 shouldPass(d, contains(1)); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 var w = new Widget(); | 653 var w = new Widget(); |
| 590 w.price = 10; | 654 w.price = 10; |
| 591 shouldPass(w, new HasPrice(greaterThan(0))); | 655 shouldPass(w, new HasPrice(greaterThan(0))); |
| 592 shouldFail(w, new HasPrice(greaterThan(10)), | 656 shouldFail(w, new HasPrice(greaterThan(10)), |
| 593 'Expected: Widget with a price that is a value greater than <10> ' | 657 'Expected: Widget with a price that is a value greater than <10> ' |
| 594 'but: price was <10>.'); | 658 'but: price was <10>.'); |
| 595 }); | 659 }); |
| 596 }); | 660 }); |
| 597 } | 661 } |
| 598 | 662 |
| OLD | NEW |