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 matcher.core_matchers_test; | |
6 | |
7 import 'package:matcher/matcher.dart'; | 5 import 'package:matcher/matcher.dart'; |
8 import 'package:test/test.dart' show test, group; | 6 import 'package:test/test.dart' show test, group; |
9 | 7 |
10 import 'test_utils.dart'; | 8 import 'test_utils.dart'; |
11 | 9 |
| 10 class BadCustomMatcher extends CustomMatcher { |
| 11 BadCustomMatcher() : super("feature", "description", {1: "a"}); |
| 12 featureValueOf(actual) => throw new Exception("bang"); |
| 13 } |
| 14 |
12 void main() { | 15 void main() { |
13 test('isTrue', () { | 16 test('isTrue', () { |
14 shouldPass(true, isTrue); | 17 shouldPass(true, isTrue); |
15 shouldFail(false, isTrue, "Expected: true Actual: <false>"); | 18 shouldFail(false, isTrue, "Expected: true Actual: <false>"); |
16 }); | 19 }); |
17 | 20 |
18 test('isFalse', () { | 21 test('isFalse', () { |
19 shouldPass(false, isFalse); | 22 shouldPass(false, isFalse); |
20 shouldFail(10, isFalse, "Expected: false Actual: <10>"); | 23 shouldFail(10, isFalse, "Expected: false Actual: <10>"); |
21 shouldFail(true, isFalse, "Expected: false Actual: <true>"); | 24 shouldFail(true, isFalse, "Expected: false Actual: <true>"); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 }); | 59 }); |
57 | 60 |
58 test('equals with a set', () { | 61 test('equals with a set', () { |
59 var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | 62 var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
60 var set1 = numbers.toSet(); | 63 var set1 = numbers.toSet(); |
61 numbers.shuffle(); | 64 numbers.shuffle(); |
62 var set2 = numbers.toSet(); | 65 var set2 = numbers.toSet(); |
63 | 66 |
64 shouldPass(set2, equals(set1)); | 67 shouldPass(set2, equals(set1)); |
65 shouldPass(numbers, equals(set1)); | 68 shouldPass(numbers, equals(set1)); |
66 shouldFail([ | 69 shouldFail( |
67 1, | 70 [1, 2, 3, 4, 5, 6, 7, 8, 9], |
68 2, | 71 equals(set1), |
69 3, | 72 matches(r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" |
70 4, | 73 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9\]" |
71 5, | 74 r" Which: does not contain 10")); |
72 6, | 75 shouldFail( |
73 7, | 76 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], |
74 8, | 77 equals(set1), |
75 9 | 78 matches(r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" |
76 ], equals(set1), matches(r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" | 79 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\]" |
77 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9\]" | 80 r" Which: larger than expected")); |
78 r" Which: does not contain 10")); | |
79 shouldFail([ | |
80 1, | |
81 2, | |
82 3, | |
83 4, | |
84 5, | |
85 6, | |
86 7, | |
87 8, | |
88 9, | |
89 10, | |
90 11 | |
91 ], equals(set1), matches(r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" | |
92 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\]" | |
93 r" Which: larger than expected")); | |
94 }); | 81 }); |
95 | 82 |
96 test('anything', () { | 83 test('anything', () { |
97 var a = new Map(); | 84 var a = new Map(); |
98 shouldPass(0, anything); | 85 shouldPass(0, anything); |
99 shouldPass(null, anything); | 86 shouldPass(null, anything); |
100 shouldPass(a, anything); | 87 shouldPass(a, anything); |
101 shouldFail(a, isNot(anything), "Expected: not anything Actual: {}"); | 88 shouldFail(a, isNot(anything), "Expected: not anything Actual: {}"); |
102 }); | 89 }); |
103 | 90 |
104 test('returnsNormally', () { | 91 test('returnsNormally', () { |
105 shouldPass(doesNotThrow, returnsNormally); | 92 shouldPass(doesNotThrow, returnsNormally); |
106 shouldFail(doesThrow, returnsNormally, matches(r"Expected: return normally" | 93 shouldFail( |
107 r" Actual: <Closure(: \(\) => dynamic " | 94 doesThrow, |
108 r"from Function 'doesThrow': static\.)?>" | 95 returnsNormally, |
109 r" Which: threw 'X'")); | 96 matches(r"Expected: return normally" |
| 97 r" Actual: <Closure.*>" |
| 98 r" Which: threw 'X'")); |
110 }); | 99 }); |
111 | 100 |
112 test('hasLength', () { | 101 test('hasLength', () { |
113 var a = new Map(); | 102 var a = new Map(); |
114 var b = new List(); | 103 var b = new List(); |
115 shouldPass(a, hasLength(0)); | 104 shouldPass(a, hasLength(0)); |
116 shouldPass(b, hasLength(0)); | 105 shouldPass(b, hasLength(0)); |
117 shouldPass('a', hasLength(1)); | 106 shouldPass('a', hasLength(1)); |
118 shouldFail(0, hasLength(0), | 107 shouldFail( |
| 108 0, |
| 109 hasLength(0), |
119 "Expected: an object with length of <0> " | 110 "Expected: an object with length of <0> " |
120 "Actual: <0> " | 111 "Actual: <0> " |
121 "Which: has no length property"); | 112 "Which: has no length property"); |
122 | 113 |
123 b.add(0); | 114 b.add(0); |
124 shouldPass(b, hasLength(1)); | 115 shouldPass(b, hasLength(1)); |
125 shouldFail(b, hasLength(2), "Expected: an object with length of <2> " | 116 shouldFail( |
| 117 b, |
| 118 hasLength(2), |
| 119 "Expected: an object with length of <2> " |
126 "Actual: [0] " | 120 "Actual: [0] " |
127 "Which: has length of <1>"); | 121 "Which: has length of <1>"); |
128 | 122 |
129 b.add(0); | 123 b.add(0); |
130 shouldFail(b, hasLength(1), "Expected: an object with length of <1> " | 124 shouldFail( |
| 125 b, |
| 126 hasLength(1), |
| 127 "Expected: an object with length of <1> " |
131 "Actual: [0, 0] " | 128 "Actual: [0, 0] " |
132 "Which: has length of <2>"); | 129 "Which: has length of <2>"); |
133 shouldPass(b, hasLength(2)); | 130 shouldPass(b, hasLength(2)); |
134 }); | 131 }); |
135 | 132 |
136 test('scalar type mismatch', () { | 133 test('scalar type mismatch', () { |
137 shouldFail('error', equals(5.1), "Expected: <5.1> " | 134 shouldFail( |
| 135 'error', |
| 136 equals(5.1), |
| 137 "Expected: <5.1> " |
138 "Actual: 'error'"); | 138 "Actual: 'error'"); |
139 }); | 139 }); |
140 | 140 |
141 test('nested type mismatch', () { | 141 test('nested type mismatch', () { |
142 shouldFail(['error'], equals([5.1]), "Expected: [5.1] " | 142 shouldFail( |
| 143 ['error'], |
| 144 equals([5.1]), |
| 145 "Expected: [5.1] " |
143 "Actual: ['error'] " | 146 "Actual: ['error'] " |
144 "Which: was 'error' instead of <5.1> at location [0]"); | 147 "Which: was 'error' instead of <5.1> at location [0]"); |
145 }); | 148 }); |
146 | 149 |
147 test('doubly-nested type mismatch', () { | 150 test('doubly-nested type mismatch', () { |
148 shouldFail([['error']], equals([[5.1]]), "Expected: [[5.1]] " | 151 shouldFail( |
| 152 [ |
| 153 ['error'] |
| 154 ], |
| 155 equals([ |
| 156 [5.1] |
| 157 ]), |
| 158 "Expected: [[5.1]] " |
149 "Actual: [['error']] " | 159 "Actual: [['error']] " |
150 "Which: was 'error' instead of <5.1> at location [0][0]"); | 160 "Which: was 'error' instead of <5.1> at location [0][0]"); |
151 }); | 161 }); |
152 | 162 |
153 test('doubly nested inequality', () { | 163 test('doubly nested inequality', () { |
154 var actual1 = [['foo', 'bar'], ['foo'], 3, []]; | 164 var actual1 = [ |
155 var expected1 = [['foo', 'bar'], ['foo'], 4, []]; | 165 ['foo', 'bar'], |
| 166 ['foo'], |
| 167 3, |
| 168 [] |
| 169 ]; |
| 170 var expected1 = [ |
| 171 ['foo', 'bar'], |
| 172 ['foo'], |
| 173 4, |
| 174 [] |
| 175 ]; |
156 var reason1 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | 176 var reason1 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " |
157 "Actual: [['foo', 'bar'], ['foo'], 3, []] " | 177 "Actual: [['foo', 'bar'], ['foo'], 3, []] " |
158 "Which: was <3> instead of <4> at location [2]"; | 178 "Which: was <3> instead of <4> at location [2]"; |
159 | 179 |
160 var actual2 = [['foo', 'barry'], ['foo'], 4, []]; | 180 var actual2 = [ |
161 var expected2 = [['foo', 'bar'], ['foo'], 4, []]; | 181 ['foo', 'barry'], |
| 182 ['foo'], |
| 183 4, |
| 184 [] |
| 185 ]; |
| 186 var expected2 = [ |
| 187 ['foo', 'bar'], |
| 188 ['foo'], |
| 189 4, |
| 190 [] |
| 191 ]; |
162 var reason2 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | 192 var reason2 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " |
163 "Actual: [['foo', 'barry'], ['foo'], 4, []] " | 193 "Actual: [['foo', 'barry'], ['foo'], 4, []] " |
164 "Which: was 'barry' instead of 'bar' at location [0][1]"; | 194 "Which: was 'barry' instead of 'bar' at location [0][1]"; |
165 | 195 |
166 var actual3 = [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}]; | 196 var actual3 = [ |
167 var expected3 = [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}]; | 197 ['foo', 'bar'], |
| 198 ['foo'], |
| 199 4, |
| 200 {'foo': 'bar'} |
| 201 ]; |
| 202 var expected3 = [ |
| 203 ['foo', 'bar'], |
| 204 ['foo'], |
| 205 4, |
| 206 {'foo': 'barry'} |
| 207 ]; |
168 var reason3 = "Expected: [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}] " | 208 var reason3 = "Expected: [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}] " |
169 "Actual: [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}] " | 209 "Actual: [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}] " |
170 "Which: was 'bar' instead of 'barry' at location [3]['foo']"; | 210 "Which: was 'bar' instead of 'barry' at location [3]['foo']"; |
171 | 211 |
172 shouldFail(actual1, equals(expected1), reason1); | 212 shouldFail(actual1, equals(expected1), reason1); |
173 shouldFail(actual2, equals(expected2), reason2); | 213 shouldFail(actual2, equals(expected2), reason2); |
174 shouldFail(actual3, equals(expected3), reason3); | 214 shouldFail(actual3, equals(expected3), reason3); |
175 }); | 215 }); |
176 | 216 |
177 test('isInstanceOf', () { | 217 test('isInstanceOf', () { |
178 shouldFail(0, new isInstanceOf<String>(), | 218 shouldFail(0, new isInstanceOf<String>(), |
179 "Expected: an instance of String Actual: <0>"); | 219 "Expected: an instance of String Actual: <0>"); |
180 shouldPass('cow', new isInstanceOf<String>()); | 220 shouldPass('cow', new isInstanceOf<String>()); |
181 }); | 221 }); |
182 | 222 |
183 group('Predicate Matchers', () { | 223 group('Predicate Matchers', () { |
184 test('isInstanceOf', () { | 224 test('isInstanceOf', () { |
185 shouldFail(0, predicate((x) => x is String, "an instance of String"), | 225 shouldFail(0, predicate((x) => x is String, "an instance of String"), |
186 "Expected: an instance of String Actual: <0>"); | 226 "Expected: an instance of String Actual: <0>"); |
187 shouldPass('cow', predicate((x) => x is String, "an instance of String")); | 227 shouldPass('cow', predicate((x) => x is String, "an instance of String")); |
188 }); | 228 }); |
189 }); | 229 }); |
190 | 230 |
191 test("Feature Matcher", () { | 231 test("Feature Matcher", () { |
192 var w = new Widget(); | 232 var w = new Widget(); |
193 w.price = 10; | 233 w.price = 10; |
194 shouldPass(w, new HasPrice(10)); | 234 shouldPass(w, new HasPrice(10)); |
195 shouldPass(w, new HasPrice(greaterThan(0))); | 235 shouldPass(w, new HasPrice(greaterThan(0))); |
196 shouldFail(w, new HasPrice(greaterThan(10)), | 236 shouldFail( |
| 237 w, |
| 238 new HasPrice(greaterThan(10)), |
197 "Expected: Widget with a price that is a value greater than <10> " | 239 "Expected: Widget with a price that is a value greater than <10> " |
198 "Actual: <Instance of 'Widget'> " | 240 "Actual: <Instance of 'Widget'> " |
199 "Which: has price with value <10> which is not " | 241 "Which: has price with value <10> which is not " |
200 "a value greater than <10>"); | 242 "a value greater than <10>"); |
201 }); | 243 }); |
| 244 |
| 245 test("Custom Matcher Exception", () { |
| 246 shouldFail( |
| 247 "a", |
| 248 new BadCustomMatcher(), |
| 249 allOf([ |
| 250 contains("Expected: feature {1: 'a'} "), |
| 251 contains("Actual: 'a' "), |
| 252 contains("Which: threw 'Exception: bang' "), |
| 253 contains("test/core_matchers_test.dart "), |
| 254 contains("package:test ") |
| 255 ])); |
| 256 }); |
202 } | 257 } |
OLD | NEW |