Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(836)

Side by Side Diff: test/typed_wrapper/iterable_test.dart

Issue 1840573002: Add type-asserting wrapper constructors. (Closed) Base URL: git@github.com:dart-lang/collection@master
Patch Set: Code review changes Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pubspec.yaml ('k') | test/typed_wrapper/list_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 import "package:collection/collection.dart";
6 import "package:test/test.dart";
7
8 import '../utils.dart';
9
10 void main() {
11 group("with valid types, forwards", () {
12 var wrapper;
13 var emptyWrapper;
14 var singleWrapper;
15 setUp(() {
16 wrapper = DelegatingIterable.typed/*<int>*/(
17 <Object>[1, 2, 3, 4, 5].map((i) => i));
18 emptyWrapper = DelegatingIterable.typed/*<int>*/(
19 <Object>[].map((i) => i));
20 singleWrapper = DelegatingIterable.typed/*<int>*/(
21 <Object>[1].map((i) => i));
22 });
23
24 test("any()", () {
25 expect(wrapper.any((i) => i > 3), isTrue);
26 expect(wrapper.any((i) => i > 5), isFalse);
27 });
28
29 test("contains()", () {
30 expect(wrapper.contains(2), isTrue);
31 expect(wrapper.contains(6), isFalse);
32 expect(wrapper.contains("foo"), isFalse);
33 });
34
35 test("elementAt()", () {
36 expect(wrapper.elementAt(1), equals(2));
37 expect(wrapper.elementAt(4), equals(5));
38 expect(() => wrapper.elementAt(5), throwsRangeError);
39 expect(() => wrapper.elementAt(-1), throwsRangeError);
40 });
41
42 test("every()", () {
43 expect(wrapper.every((i) => i < 6), isTrue);
44 expect(wrapper.every((i) => i > 3), isFalse);
45 });
46
47 test("expand()", () {
48 expect(wrapper.expand((i) => [i]), equals([1, 2, 3, 4, 5]));
49 expect(wrapper.expand((i) => [i, i]),
50 equals([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]));
51 });
52
53 test("first", () {
54 expect(wrapper.first, equals(1));
55 expect(() => emptyWrapper.first, throwsStateError);
56 });
57
58 test("firstWhere()", () {
59 expect(wrapper.firstWhere((i) => i > 3), equals(4));
60 expect(() => wrapper.firstWhere((i) => i > 5), throwsStateError);
61 expect(wrapper.firstWhere((i) => i > 5, orElse: () => -1), equals(-1));
62 });
63
64 test("fold()", () {
65 expect(wrapper.fold("", (previous, i) => previous + i.toString()),
66 equals("12345"));
67 expect(emptyWrapper.fold(null, (previous, i) => previous + i), isNull);
68 });
69
70 test("forEach()", () {
71 var results = [];
72 wrapper.forEach(results.add);
73 expect(results, equals([1, 2, 3, 4, 5]));
74
75 emptyWrapper.forEach(expectAsync((_) {}, count: 0));
76 });
77
78 test("isEmpty", () {
79 expect(wrapper.isEmpty, isFalse);
80 expect(emptyWrapper.isEmpty, isTrue);
81 });
82
83 test("isNotEmpty", () {
84 expect(wrapper.isNotEmpty, isTrue);
85 expect(emptyWrapper.isNotEmpty, isFalse);
86 });
87
88 test("iterator", () {
89 var iterator = wrapper.iterator;
90 expect(iterator.current, isNull);
91 expect(iterator.moveNext(), isTrue);
92 expect(iterator.current, equals(1));
93 expect(iterator.moveNext(), isTrue);
94 expect(iterator.current, equals(2));
95 expect(iterator.moveNext(), isTrue);
96 expect(iterator.current, equals(3));
97 expect(iterator.moveNext(), isTrue);
98 expect(iterator.current, equals(4));
99 expect(iterator.moveNext(), isTrue);
100 expect(iterator.current, equals(5));
101 expect(iterator.moveNext(), isFalse);
102 expect(iterator.current, isNull);
103 });
104
105 test("join()", () {
106 expect(wrapper.join(), "12345");
107 expect(wrapper.join("-"), "1-2-3-4-5");
108 });
109
110 test("last", () {
111 expect(wrapper.last, equals(5));
112 expect(() => emptyWrapper.last, throwsStateError);
113 });
114
115 test("lastWhere()", () {
116 expect(wrapper.lastWhere((i) => i > 3), equals(5));
117 expect(() => wrapper.lastWhere((i) => i > 5), throwsStateError);
118 expect(wrapper.lastWhere((i) => i > 5, orElse: () => -1), equals(-1));
119 });
120
121 test("length", () {
122 expect(wrapper.length, equals(5));
123 expect(emptyWrapper.length, equals(0));
124 });
125
126 test("map()", () {
127 expect(wrapper.map((i) => i + 1), equals([2, 3, 4, 5, 6]));
128 expect(wrapper.map((i) => i / 2), equals([0.5, 1.0, 1.5, 2.0, 2.5]));
129 });
130
131 test("reduce()", () {
132 expect(wrapper.reduce((value, i) => value + i), equals(15));
133 expect(() => emptyWrapper.reduce((value, i) => value + i),
134 throwsStateError);
135 });
136
137 test("single", () {
138 expect(() => wrapper.single, throwsStateError);
139 expect(singleWrapper.single, equals(1));
140 });
141
142 test("singleWhere()", () {
143 expect(() => wrapper.singleWhere((i) => i.isOdd), throwsStateError);
144 expect(singleWrapper.singleWhere((i) => i.isOdd), equals(1));
145 expect(() => singleWrapper.singleWhere((i) => i.isEven),
146 throwsStateError);
147 });
148
149 test("skip()", () {
150 expect(wrapper.skip(3), equals([4, 5]));
151 expect(wrapper.skip(10), isEmpty);
152 expect(() => wrapper.skip(-1), throwsRangeError);
153 });
154
155 test("skipWhile()", () {
156 expect(wrapper.skipWhile((i) => i < 3), equals([3, 4, 5]));
157 expect(wrapper.skipWhile((i) => i < 10), isEmpty);
158 });
159
160 test("take()", () {
161 expect(wrapper.take(3), equals([1, 2, 3]));
162 expect(wrapper.take(10), equals([1, 2, 3, 4, 5]));
163 expect(() => wrapper.take(-1), throwsRangeError);
164 });
165
166 test("takeWhile()", () {
167 expect(wrapper.takeWhile((i) => i < 3), equals([1, 2]));
168 expect(wrapper.takeWhile((i) => i < 10), equals([1, 2, 3, 4, 5]));
169 });
170
171 test("toList()", () {
172 expect(wrapper.toList(), equals([1, 2, 3, 4, 5]));
173 expect(wrapper.toList(growable: false), equals([1, 2, 3, 4, 5]));
174 expect(() => wrapper.toList(growable: false).add(6),
175 throwsUnsupportedError);
176 });
177
178 test("toSet()", () {
179 expect(wrapper.toSet(), unorderedEquals([1, 2, 3, 4, 5]));
180 expect(DelegatingIterable.typed/*<int>*/(<Object>[1, 1, 2, 2]).toSet(),
181 unorderedEquals([1, 2]));
182 });
183
184 test("where()", () {
185 expect(wrapper.where((i) => i.isOdd), equals([1, 3, 5]));
186 expect(wrapper.where((i) => i.isEven), equals([2, 4]));
187 });
188
189 test("toString()", () {
190 expect(wrapper.toString(), equals("(1, 2, 3, 4, 5)"));
191 expect(emptyWrapper.toString(), equals("()"));
192 });
193 });
194
195 group("with invalid types", () {
196 var wrapper;
197 var singleWrapper;
198 setUp(() {
199 wrapper = DelegatingIterable.typed/*<int>*/(
200 <Object>["foo", "bar", "baz"].map((element) => element));
201 singleWrapper = DelegatingIterable.typed/*<int>*/(
202 <Object>["foo"].map((element) => element));
203 });
204
205 group("throws a CastError for", () {
206 test("any()", () {
207 expect(() => wrapper.any(expectAsync((_) => false, count: 0)),
208 throwsCastError);
209 });
210
211 test("elementAt()", () {
212 expect(() => wrapper.elementAt(1), throwsCastError);
213 });
214
215 test("every()", () {
216 expect(() => wrapper.every(expectAsync((_) => false, count: 0)),
217 throwsCastError);
218 });
219
220 test("expand()", () {
221 var lazy = wrapper.expand(expectAsync((_) => [], count: 0));
222 expect(() => lazy.first, throwsCastError);
223 });
224
225 test("first", () {
226 expect(() => wrapper.first, throwsCastError);
227 });
228
229 test("firstWhere()", () {
230 expect(() => wrapper.firstWhere(expectAsync((_) => false, count: 0)),
231 throwsCastError);
232 });
233
234 test("fold()", () {
235 expect(() => wrapper.fold(null, expectAsync((_, __) => null, count: 0)),
236 throwsCastError);
237 });
238
239 test("forEach()", () {
240 expect(() => wrapper.forEach(expectAsync((_) {}, count: 0)),
241 throwsCastError);
242 });
243
244 test("iterator", () {
245 var iterator = wrapper.iterator;
246 expect(iterator.current, isNull);
247 expect(() => iterator.moveNext(), throwsCastError);
248 });
249
250 test("last", () {
251 expect(() => wrapper.last, throwsCastError);
252 });
253
254 test("lastWhere()", () {
255 expect(() => wrapper.lastWhere(expectAsync((_) => false, count: 0)),
256 throwsCastError);
257 });
258
259 test("map()", () {
260 var lazy = wrapper.map(expectAsync((_) => null, count: 0));
261 expect(() => lazy.first, throwsCastError);
262 });
263
264 test("reduce()", () {
265 expect(() => wrapper.reduce(expectAsync((_, __) => null, count: 0)),
266 throwsCastError);
267 });
268
269 test("single", () {
270 expect(() => singleWrapper.single, throwsCastError);
271 });
272
273 test("singleWhere()", () {
274 expect(() {
275 singleWrapper.singleWhere(expectAsync((_) => false, count: 0));
276 }, throwsCastError);
277 });
278
279 test("skip()", () {
280 var lazy = wrapper.skip(1);
281 expect(() => lazy.first, throwsCastError);
282 });
283
284 test("skipWhile()", () {
285 var lazy = wrapper.skipWhile(expectAsync((_) => false, count: 0));
286 expect(() => lazy.first, throwsCastError);
287 });
288
289 test("take()", () {
290 var lazy = wrapper.take(1);
291 expect(() => lazy.first, throwsCastError);
292 });
293
294 test("takeWhile()", () {
295 var lazy = wrapper.takeWhile(expectAsync((_) => false, count: 0));
296 expect(() => lazy.first, throwsCastError);
297 });
298
299 test("toList()", () {
300 var list = wrapper.toList();
301 expect(() => list.first, throwsCastError);
302 });
303
304 test("toSet()", () {
305 var asSet = wrapper.toSet();
306 expect(() => asSet.first, throwsCastError);
307 });
308
309 test("where()", () {
310 var lazy = wrapper.where(expectAsync((_) => false, count: 0));
311 expect(() => lazy.first, throwsCastError);
312 });
313 });
314
315 group("doesn't throw a CastError for", () {
316 test("contains()", () {
317 expect(wrapper.contains(1), isFalse);
318 expect(wrapper.contains("foo"), isTrue);
319 });
320
321 test("elementAt()", () {
322 expect(() => wrapper.elementAt(-1), throwsRangeError);
323 expect(() => wrapper.elementAt(10), throwsRangeError);
324 });
325
326 test("isEmpty", () {
327 expect(wrapper.isEmpty, isFalse);
328 });
329
330 test("isNotEmpty", () {
331 expect(wrapper.isNotEmpty, isTrue);
332 });
333
334 test("join()", () {
335 expect(wrapper.join(), "foobarbaz");
336 });
337
338 test("length", () {
339 expect(wrapper.length, equals(3));
340 });
341
342 test("single", () {
343 expect(() => wrapper.single, throwsStateError);
344 });
345
346 test("skip()", () {
347 expect(() => wrapper.skip(-1), throwsRangeError);
348 });
349
350 test("toString()", () {
351 expect(wrapper.toString(), equals("(foo, bar, baz)"));
352 });
353 });
354 }, skip: "Re-enable this when test can run DDC (test#414).");
355 }
OLDNEW
« no previous file with comments | « pubspec.yaml ('k') | test/typed_wrapper/list_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698