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('isNaN', () { | |
37 shouldPass(double.NAN, isNaN); | |
38 shouldFail(3.1, isNaN, "Expected: NaN Actual: <3.1>"); | |
39 }); | |
40 | |
41 test('isNotNaN', () { | |
42 shouldPass(3.1, isNotNaN); | |
43 shouldFail(double.NAN, isNotNaN, "Expected: not NaN Actual: <NaN>"); | |
44 }); | |
45 | |
46 test('same', () { | |
47 var a = new Map(); | |
48 var b = new Map(); | |
49 shouldPass(a, same(a)); | |
50 shouldFail(b, same(a), "Expected: same instance as {} Actual: {}"); | |
51 }); | |
52 | |
53 test('equals', () { | |
54 var a = new Map(); | |
55 var b = new Map(); | |
56 shouldPass(a, equals(a)); | |
57 shouldPass(a, equals(b)); | |
58 }); | |
59 | |
60 test('equals with a set', () { | |
61 var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
62 var set1 = numbers.toSet(); | |
63 numbers.shuffle(); | |
64 var set2 = numbers.toSet(); | |
65 | |
66 shouldPass(set2, equals(set1)); | |
67 shouldPass(numbers, equals(set1)); | |
68 shouldFail([1, 2, 3, 4, 5, 6, 7, 8, 9], equals(set1), matches( | |
69 r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" | |
70 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9\]" | |
71 r" Which: does not contain 10")); | |
72 shouldFail([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], equals(set1), matches( | |
73 r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" | |
74 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\]" | |
75 r" Which: larger than expected")); | |
76 }); | |
77 | |
78 test('anything', () { | |
79 var a = new Map(); | |
80 shouldPass(0, anything); | |
81 shouldPass(null, anything); | |
82 shouldPass(a, anything); | |
83 shouldFail(a, isNot(anything), "Expected: not anything Actual: {}"); | |
84 }); | |
85 | |
86 test('returnsNormally', () { | |
87 shouldPass(doesNotThrow, returnsNormally); | |
88 shouldFail(doesThrow, returnsNormally, | |
89 matches( | |
90 r"Expected: return normally" | |
91 r" Actual: <Closure(: \(\) => dynamic " | |
92 r"from Function 'doesThrow': static\.)?>" | |
93 r" Which: threw 'X'")); | |
94 }); | |
95 | |
96 test('hasLength', () { | |
97 var a = new Map(); | |
98 var b = new List(); | |
99 shouldPass(a, hasLength(0)); | |
100 shouldPass(b, hasLength(0)); | |
101 shouldPass('a', hasLength(1)); | |
102 shouldFail(0, hasLength(0), new PrefixMatcher( | |
103 "Expected: an object with length of <0> " | |
104 "Actual: <0> " | |
105 "Which: has no length property")); | |
106 | |
107 b.add(0); | |
108 shouldPass(b, hasLength(1)); | |
109 shouldFail(b, hasLength(2), | |
110 "Expected: an object with length of <2> " | |
111 "Actual: [0] " | |
112 "Which: has length of <1>"); | |
113 | |
114 b.add(0); | |
115 shouldFail(b, hasLength(1), | |
116 "Expected: an object with length of <1> " | |
117 "Actual: [0, 0] " | |
118 "Which: has length of <2>"); | |
119 shouldPass(b, hasLength(2)); | |
120 }); | |
121 | |
122 test('scalar type mismatch', () { | |
123 shouldFail('error', equals(5.1), | |
124 "Expected: <5.1> " | |
125 "Actual: 'error'"); | |
126 }); | |
127 | |
128 test('nested type mismatch', () { | |
129 shouldFail(['error'], equals([5.1]), | |
130 "Expected: [5.1] " | |
131 "Actual: ['error'] " | |
132 "Which: was 'error' instead of <5.1> at location [0]"); | |
133 }); | |
134 | |
135 test('doubly-nested type mismatch', () { | |
136 shouldFail([['error']], equals([[5.1]]), | |
137 "Expected: [[5.1]] " | |
138 "Actual: [['error']] " | |
139 "Which: was 'error' instead of <5.1> at location [0][0]"); | |
140 }); | |
141 | |
142 test('doubly nested inequality', () { | |
143 var actual1 = [['foo', 'bar'], ['foo'], 3, []]; | |
144 var expected1 = [['foo', 'bar'], ['foo'], 4, []]; | |
145 var reason1 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | |
146 "Actual: [['foo', 'bar'], ['foo'], 3, []] " | |
147 "Which: was <3> instead of <4> at location [2]"; | |
148 | |
149 var actual2 = [['foo', 'barry'], ['foo'], 4, []]; | |
150 var expected2 = [['foo', 'bar'], ['foo'], 4, []]; | |
151 var reason2 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | |
152 "Actual: [['foo', 'barry'], ['foo'], 4, []] " | |
153 "Which: was 'barry' instead of 'bar' at location [0][1]"; | |
154 | |
155 var actual3 = [['foo', 'bar'], ['foo'], 4, {'foo':'bar'}]; | |
156 var expected3 = [['foo', 'bar'], ['foo'], 4, {'foo':'barry'}]; | |
157 var reason3 = "Expected: [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}] " | |
158 "Actual: [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}] " | |
159 "Which: was 'bar' instead of 'barry' at location [3]['foo']"; | |
160 | |
161 shouldFail(actual1, equals(expected1), reason1); | |
162 shouldFail(actual2, equals(expected2), reason2); | |
163 shouldFail(actual3, equals(expected3), reason3); | |
164 }); | |
165 | |
166 test('isInstanceOf', () { | |
167 shouldFail(0, new isInstanceOf<String>('String'), | |
168 "Expected: an instance of String Actual: <0>"); | |
169 shouldPass('cow', new isInstanceOf<String>('String')); | |
170 }); | |
171 | |
172 group('Predicate Matchers', () { | |
173 test('isInstanceOf', () { | |
174 shouldFail(0, predicate((x) => x is String, "an instance of String"), | |
175 "Expected: an instance of String Actual: <0>"); | |
176 shouldPass('cow', predicate((x) => x is String, "an instance of String")); | |
177 }); | |
178 }); | |
179 } | |
OLD | NEW |