OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
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. | |
7 library minitest; | |
8 | |
9 import 'dom.dart'; | |
10 | |
11 final console = (window as dynamic).console; | |
12 | |
13 void group(String name, void body()) { | |
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) { | |
21 console.warn('SKIP $name: $skip'); | |
22 return; | |
23 } | |
24 console.log(name); | |
25 try { | |
26 body(); | |
27 } catch(e) { | |
28 console.error(e); | |
29 } | |
30 } | |
31 | |
32 void expect(Object actual, matcher) { | |
33 if (matcher is! Matcher) matcher = equals(matcher); | |
34 if (!matcher(actual)) { | |
35 throw 'Expect failed to match $actual with $matcher'; | |
36 } | |
37 } | |
38 | |
39 Matcher equals(Object expected) { | |
40 return (actual) { | |
41 if (expected is List && actual is List) { | |
42 int len = expected.length; | |
43 if (len != actual.length) return false; | |
44 for (int i = 0; i < len; i++) { | |
45 if (!equals(expected[i])(actual[i])) return false; | |
46 } | |
47 return true; | |
48 } else { | |
49 return expected == actual; | |
50 } | |
51 }; | |
52 } | |
53 | |
54 Matcher same(Object expected) => (actual) => identical(expected, actual); | |
55 Matcher isNot(matcher) { | |
56 if (matcher is! Matcher) matcher = equals(matcher); | |
57 return (actual) => !matcher(actual); | |
58 } | |
59 | |
60 bool isNull(actual) => actual == null; | |
61 final Matcher isNotNull = isNot(isNull); | |
62 bool isRangeError(actual) => actual is RangeError; | |
63 bool isNoSuchMethodError(actual) => actual is NoSuchMethodError; | |
64 | |
65 Matcher throwsA(matcher) { | |
66 if (matcher is! Matcher) matcher = equals(matcher); | |
67 return (actual) { | |
68 try { | |
69 actual(); | |
70 return false; | |
71 } catch(e) { | |
72 return matcher(e); | |
73 } | |
74 }; | |
75 } | |
76 | |
77 final Matcher throws = throwsA((a) => true); | |
78 | |
79 typedef Matcher(actual); | |
OLD | NEW |