| OLD | NEW |
| (Empty) | |
| 1 library unittest; |
| 2 |
| 3 import 'package:unittest/unittest.dart'; |
| 4 import 'package:di/mirrors.dart'; |
| 5 import 'dart:async'; |
| 6 |
| 7 export 'package:unittest/unittest.dart'; |
| 8 |
| 9 // Jasmine-like syntax for unittest. |
| 10 void describe(String spec, TestFunction body) => group(spec, body); |
| 11 void ddescribe(String spec, TestFunction body) => solo_group(spec, body); |
| 12 void xdescribe(String spec, TestFunction body) {} |
| 13 void it(String spec, TestFunction body) => test(spec, body); |
| 14 void xit(String spec, TestFunction body) {} |
| 15 void iit(String spec, TestFunction body) => solo_test(spec, body); |
| 16 |
| 17 Matcher toEqual(expected) => equals(expected); |
| 18 Matcher toContain(expected) => contains(expected); |
| 19 Matcher toBe(expected) => same(expected); |
| 20 Matcher instanceOf(Type t) => new IsInstanceOfTypeMatcher(t); |
| 21 |
| 22 Matcher toThrow(Type exceptionClass, String message) => |
| 23 new ThrowsMatcher(new ComplexExceptionMatcher( |
| 24 instanceOf(exceptionClass), toContain(message))); |
| 25 |
| 26 Matcher not(Matcher matcher) => new NegateMatcher(matcher); |
| 27 |
| 28 |
| 29 class NegateMatcher extends Matcher { |
| 30 final Matcher _matcher; |
| 31 |
| 32 const NegateMatcher(this._matcher); |
| 33 |
| 34 bool matches(obj, Map ms) => !_matcher.matches(obj, ms); |
| 35 |
| 36 Description describe(Description description) => |
| 37 _matcher.describe(description.add('NOT')); |
| 38 |
| 39 Description describeMismatch(item, Description mismatchDescription, |
| 40 Map matchState, bool verbose) { |
| 41 return _matcher.describeMismatch( |
| 42 item, mismatchDescription, matchState, verbose); |
| 43 } |
| 44 } |
| 45 |
| 46 |
| 47 class ThrowsMatcher extends Throws { |
| 48 final Matcher _matcher; |
| 49 |
| 50 const ThrowsMatcher([Matcher matcher]) : _matcher = matcher, super(matcher); |
| 51 |
| 52 Description describeMismatch(item, Description mismatchDescription, |
| 53 Map matchState, |
| 54 bool verbose) { |
| 55 if (item is! Function && item is! Future) { |
| 56 return mismatchDescription.add(' not a Function or Future'); |
| 57 } |
| 58 |
| 59 if (_matcher == null || matchState == null) { |
| 60 return mismatchDescription.add(' did not throw any exception'); |
| 61 } |
| 62 |
| 63 return _matcher.describeMismatch(item, mismatchDescription, |
| 64 matchState, verbose); |
| 65 } |
| 66 } |
| 67 |
| 68 class ComplexExceptionMatcher extends Matcher { |
| 69 Matcher classMatcher; |
| 70 Matcher messageMatcher; |
| 71 |
| 72 ComplexExceptionMatcher(this.classMatcher, this.messageMatcher); |
| 73 |
| 74 bool matches(obj, Map ms) { |
| 75 if (!classMatcher.matches(obj, ms)) { |
| 76 return false; |
| 77 } |
| 78 |
| 79 return messageMatcher.matches(obj.message, ms); |
| 80 } |
| 81 |
| 82 Description describe(Description description) => |
| 83 messageMatcher.describe(classMatcher.describe(description).add(' with mess
age ')); |
| 84 |
| 85 Description describeMismatch(item, Description mismatchDescription, |
| 86 Map matchState, bool verbose) { |
| 87 var e = matchState['exception']; |
| 88 |
| 89 mismatchDescription.add('threw ').addDescriptionOf(e); |
| 90 |
| 91 try { |
| 92 var message = e.message; // does not have to be defined |
| 93 mismatchDescription.add(' with message ').addDescriptionOf(message); |
| 94 } catch (e) {} |
| 95 } |
| 96 } |
| 97 |
| 98 class IsInstanceOfTypeMatcher extends Matcher { |
| 99 Type t; |
| 100 |
| 101 IsInstanceOfTypeMatcher(this.t); |
| 102 |
| 103 bool matches(obj, Map matchState) => |
| 104 reflect(obj).type.qualifiedName == reflectClass(t).qualifiedName; |
| 105 |
| 106 Description describe(Description description) => |
| 107 description.add('an instance of ${t.toString()}'); |
| 108 } |
| OLD | NEW |