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 matcher.core_matchers_test; | |
6 | |
7 import 'package:matcher/matcher.dart'; | |
8 import 'package:unittest/unittest.dart' show test, group; | |
9 | |
10 import 'test_utils.dart'; | |
11 | |
12 void main() { | |
13 initUtils(); | |
14 | |
15 test('isTrue', () { | |
16 shouldPass(true, isTrue); | |
17 shouldFail(false, isTrue, "Expected: true Actual: <false>"); | |
18 }); | |
19 | |
20 test('isFalse', () { | |
21 shouldPass(false, isFalse); | |
22 shouldFail(10, isFalse, "Expected: false Actual: <10>"); | |
23 shouldFail(true, isFalse, "Expected: false Actual: <true>"); | |
24 }); | |
25 | |
26 test('isNull', () { | |
27 shouldPass(null, isNull); | |
28 shouldFail(false, isNull, "Expected: null Actual: <false>"); | |
29 }); | |
30 | |
31 test('isNotNull', () { | |
32 shouldPass(false, isNotNull); | |
33 shouldFail(null, isNotNull, "Expected: not null Actual: <null>"); | |
34 }); | |
35 | |
36 test('same', () { | |
37 var a = new Map(); | |
38 var b = new Map(); | |
39 shouldPass(a, same(a)); | |
40 shouldFail(b, same(a), "Expected: same instance as {} Actual: {}"); | |
41 }); | |
42 | |
43 test('equals', () { | |
44 var a = new Map(); | |
45 var b = new Map(); | |
46 shouldPass(a, equals(a)); | |
47 shouldPass(a, equals(b)); | |
48 }); | |
49 | |
50 test('anything', () { | |
51 var a = new Map(); | |
52 shouldPass(0, anything); | |
53 shouldPass(null, anything); | |
54 shouldPass(a, anything); | |
55 shouldFail(a, isNot(anything), "Expected: not anything Actual: {}"); | |
56 }); | |
57 | |
58 test('throws', () { | |
59 shouldFail(doesNotThrow, throws, | |
60 matches( | |
61 r"Expected: throws" | |
62 r" Actual: <Closure(: \(\) => dynamic " | |
63 r"from Function 'doesNotThrow': static\.)?>" | |
64 r" Which: did not throw")); | |
65 shouldPass(doesThrow, throws); | |
66 shouldFail(true, throws, | |
67 "Expected: throws" | |
68 " Actual: <true>" | |
69 " Which: is not a Function or Future"); | |
70 }); | |
71 | |
72 test('throwsA', () { | |
73 shouldPass(doesThrow, throwsA(equals('X'))); | |
74 shouldFail(doesThrow, throwsA(equals('Y')), | |
75 matches( | |
76 r"Expected: throws 'Y'" | |
77 r" Actual: <Closure(: \(\) => dynamic " | |
78 r"from Function 'doesThrow': static\.)?>" | |
79 r" Which: threw 'X'")); | |
80 }); | |
81 | |
82 test('throwsA', () { | |
83 shouldPass(doesThrow, throwsA(equals('X'))); | |
84 shouldFail(doesThrow, throwsA(equals('Y')), | |
85 matches("Expected: throws 'Y'.*" | |
86 "Actual: <Closure.*" | |
87 "Which: threw 'X'")); | |
88 }); | |
89 | |
90 test('returnsNormally', () { | |
91 shouldPass(doesNotThrow, returnsNormally); | |
92 shouldFail(doesThrow, returnsNormally, | |
93 matches( | |
94 r"Expected: return normally" | |
95 r" Actual: <Closure(: \(\) => dynamic " | |
96 r"from Function 'doesThrow': static\.)?>" | |
97 r" Which: threw 'X'")); | |
98 }); | |
99 | |
100 | |
101 test('hasLength', () { | |
102 var a = new Map(); | |
103 var b = new List(); | |
104 shouldPass(a, hasLength(0)); | |
105 shouldPass(b, hasLength(0)); | |
106 shouldPass('a', hasLength(1)); | |
107 shouldFail(0, hasLength(0), new PrefixMatcher( | |
108 "Expected: an object with length of <0> " | |
109 "Actual: <0> " | |
110 "Which: has no length property")); | |
111 | |
112 b.add(0); | |
113 shouldPass(b, hasLength(1)); | |
114 shouldFail(b, hasLength(2), | |
115 "Expected: an object with length of <2> " | |
116 "Actual: [0] " | |
117 "Which: has length of <1>"); | |
118 | |
119 b.add(0); | |
120 shouldFail(b, hasLength(1), | |
121 "Expected: an object with length of <1> " | |
122 "Actual: [0, 0] " | |
123 "Which: has length of <2>"); | |
124 shouldPass(b, hasLength(2)); | |
125 }); | |
126 | |
127 test('scalar type mismatch', () { | |
128 shouldFail('error', equals(5.1), | |
129 "Expected: <5.1> " | |
130 "Actual: 'error'"); | |
131 }); | |
132 | |
133 test('nested type mismatch', () { | |
134 shouldFail(['error'], equals([5.1]), | |
135 "Expected: [5.1] " | |
136 "Actual: ['error'] " | |
137 "Which: was 'error' instead of <5.1> at location [0]"); | |
138 }); | |
139 | |
140 test('doubly-nested type mismatch', () { | |
141 shouldFail([['error']], equals([[5.1]]), | |
142 "Expected: [[5.1]] " | |
143 "Actual: [['error']] " | |
144 "Which: was 'error' instead of <5.1> at location [0][0]"); | |
145 }); | |
146 | |
147 test('doubly nested inequality', () { | |
148 var actual1 = [['foo', 'bar'], ['foo'], 3, []]; | |
149 var expected1 = [['foo', 'bar'], ['foo'], 4, []]; | |
150 var reason1 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | |
151 "Actual: [['foo', 'bar'], ['foo'], 3, []] " | |
152 "Which: was <3> instead of <4> at location [2]"; | |
153 | |
154 var actual2 = [['foo', 'barry'], ['foo'], 4, []]; | |
155 var expected2 = [['foo', 'bar'], ['foo'], 4, []]; | |
156 var reason2 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | |
157 "Actual: [['foo', 'barry'], ['foo'], 4, []] " | |
158 "Which: was 'barry' instead of 'bar' at location [0][1]"; | |
159 | |
160 var actual3 = [['foo', 'bar'], ['foo'], 4, {'foo':'bar'}]; | |
161 var expected3 = [['foo', 'bar'], ['foo'], 4, {'foo':'barry'}]; | |
162 var reason3 = "Expected: [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}] " | |
163 "Actual: [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}] " | |
164 "Which: was 'bar' instead of 'barry' at location [3]['foo']"; | |
165 | |
166 shouldFail(actual1, equals(expected1), reason1); | |
167 shouldFail(actual2, equals(expected2), reason2); | |
168 shouldFail(actual3, equals(expected3), reason3); | |
169 }); | |
170 | |
171 test('isInstanceOf', () { | |
172 shouldFail(0, new isInstanceOf<String>('String'), | |
173 "Expected: an instance of String Actual: <0>"); | |
174 shouldPass('cow', new isInstanceOf<String>('String')); | |
175 }); | |
176 | |
177 group('Predicate Matchers', () { | |
178 test('isInstanceOf', () { | |
179 shouldFail(0, predicate((x) => x is String, "an instance of String"), | |
180 "Expected: an instance of String Actual: <0>"); | |
181 shouldPass('cow', predicate((x) => x is String, "an instance of String")); | |
182 }); | |
183 }); | |
184 | |
185 group('exception/error matchers', () { | |
186 test('throwsCyclicInitializationError', () { | |
187 expect(() => _Bicycle.foo, throwsCyclicInitializationError); | |
188 }); | |
189 | |
190 test('throwsConcurrentModificationError', () { | |
191 expect(() { | |
192 var a = { 'foo': 'bar' }; | |
193 for (var k in a.keys) { | |
194 a.remove(k); | |
195 } | |
196 }, throwsConcurrentModificationError); | |
197 }); | |
198 | |
199 test('throwsNullThrownError', () { | |
200 expect(() => throw null, throwsNullThrownError); | |
201 }); | |
202 }); | |
203 } | |
204 | |
205 class _Bicycle { | |
206 static final foo = bar(); | |
207 | |
208 static bar() { | |
209 return foo + 1; | |
210 } | |
211 } | |
OLD | NEW |