| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // TODO(jmesserly): replace this with the real package:test. | 5 // TODO(jmesserly): replace this with the real package:test. |
| 6 // Not possible yet because it uses on async/await which we don't support. | 6 // Not possible yet due to various bugs we still have. |
| 7 library minitest; | 7 library minitest; |
| 8 | 8 |
| 9 import 'dom.dart'; | 9 import 'dart:async'; |
| 10 import 'package:dom/dom.dart'; |
| 10 | 11 |
| 11 final console = (window as dynamic).console; | 12 void group(String name, void body()) => (window as dynamic).suite(name, body); |
| 12 | 13 |
| 13 void group(String name, void body()) { | 14 void test(String name, body(), {String skip}) { |
| 14 console.group(name); | |
| 15 body(); | |
| 16 console.groupEnd(name); | |
| 17 } | |
| 18 | |
| 19 void test(String name, void body(), {String skip}) { | |
| 20 if (skip != null) { | 15 if (skip != null) { |
| 21 console.warn('SKIP $name: $skip'); | 16 print('SKIP $name: $skip'); |
| 22 return; | 17 return; |
| 23 } | 18 } |
| 24 console.log(name); | 19 (window as dynamic).test(name, (done) { |
| 25 try { | 20 _finishTest(f) { |
| 26 body(); | 21 if (f is Future) { |
| 27 } catch(e) { | 22 f.then(_finishTest); |
| 28 console.error(e); | 23 } else { |
| 29 } | 24 done(); |
| 25 } |
| 26 } |
| 27 _finishTest(body()); |
| 28 }); |
| 30 } | 29 } |
| 31 | 30 |
| 32 void expect(Object actual, matcher) { | 31 void expect(Object actual, matcher) { |
| 33 if (matcher is! Matcher) matcher = equals(matcher); | 32 if (matcher is! Matcher) matcher = equals(matcher); |
| 34 if (!matcher(actual)) { | 33 if (!matcher(actual)) { |
| 35 throw 'Expect failed to match $actual with $matcher'; | 34 throw 'Expect failed to match $actual with $matcher'; |
| 36 } | 35 } |
| 37 } | 36 } |
| 38 | 37 |
| 38 void fail(String message) { |
| 39 throw 'TestFailure: ' + message; |
| 40 } |
| 41 |
| 39 Matcher equals(Object expected) { | 42 Matcher equals(Object expected) { |
| 40 return (actual) { | 43 return (actual) { |
| 41 if (expected is List && actual is List) { | 44 if (expected is List && actual is List) { |
| 42 int len = expected.length; | 45 int len = expected.length; |
| 43 if (len != actual.length) return false; | 46 if (len != actual.length) return false; |
| 44 for (int i = 0; i < len; i++) { | 47 for (int i = 0; i < len; i++) { |
| 45 if (!equals(expected[i])(actual[i])) return false; | 48 if (!equals(expected[i])(actual[i])) return false; |
| 46 } | 49 } |
| 47 return true; | 50 return true; |
| 48 } else { | 51 } else { |
| 49 return expected == actual; | 52 return expected == actual; |
| 50 } | 53 } |
| 51 }; | 54 }; |
| 52 } | 55 } |
| 53 | 56 |
| 54 Matcher same(Object expected) => (actual) => identical(expected, actual); | 57 Matcher same(Object expected) => (actual) => identical(expected, actual); |
| 55 Matcher isNot(matcher) { | 58 Matcher isNot(matcher) { |
| 56 if (matcher is! Matcher) matcher = equals(matcher); | 59 if (matcher is! Matcher) matcher = equals(matcher); |
| 57 return (actual) => !matcher(actual); | 60 return (actual) => !matcher(actual); |
| 58 } | 61 } |
| 59 | 62 |
| 63 bool isTrue(actual) => actual == true; |
| 60 bool isNull(actual) => actual == null; | 64 bool isNull(actual) => actual == null; |
| 61 final Matcher isNotNull = isNot(isNull); | 65 final Matcher isNotNull = isNot(isNull); |
| 62 bool isRangeError(actual) => actual is RangeError; | 66 bool isRangeError(actual) => actual is RangeError; |
| 63 bool isNoSuchMethodError(actual) => actual is NoSuchMethodError; | 67 bool isNoSuchMethodError(actual) => actual is NoSuchMethodError; |
| 68 Matcher lessThan(expected) => (actual) => actual < expected; |
| 69 Matcher greaterThan(expected) => (actual) => actual > expected; |
| 64 | 70 |
| 65 Matcher throwsA(matcher) { | 71 Matcher throwsA(matcher) { |
| 66 if (matcher is! Matcher) matcher = equals(matcher); | 72 if (matcher is! Matcher) matcher = equals(matcher); |
| 67 return (actual) { | 73 return (actual) { |
| 68 try { | 74 try { |
| 69 actual(); | 75 actual(); |
| 70 return false; | 76 return false; |
| 71 } catch(e) { | 77 } catch(e) { |
| 72 return matcher(e); | 78 return matcher(e); |
| 73 } | 79 } |
| 74 }; | 80 }; |
| 75 } | 81 } |
| 76 | 82 |
| 77 final Matcher throws = throwsA((a) => true); | 83 final Matcher throws = throwsA((a) => true); |
| 78 | 84 |
| 79 typedef Matcher(actual); | 85 typedef Matcher(actual); |
| OLD | NEW |