OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 import "dart:collection"; |
5 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
6 | 7 |
7 // Test foreach (aka. for-in) functionality. | 8 // Test foreach (aka. for-in) functionality. |
8 | 9 |
9 testIterator(List expect, Iterable input) { | 10 testIterator(List expect, Iterable input) { |
10 int i = 0; | 11 int i = 0; |
11 for (var value in input) { | 12 for (var value in input) { |
12 Expect.isTrue(i < expect.length); | 13 Expect.isTrue(i < expect.length); |
13 Expect.equals(expect[i], value); | 14 Expect.equals(expect[i], value); |
14 i += 1; | 15 i += 1; |
15 } | 16 } |
16 Expect.equals(expect.length, i); | 17 Expect.equals(expect.length, i); |
17 | 18 |
18 i = 0; | 19 i = 0; |
19 var value2; | 20 var value2; |
20 for (value2 in input) { | 21 for (value2 in input) { |
21 Expect.isTrue(i < expect.length); | 22 Expect.isTrue(i < expect.length); |
22 Expect.equals(expect[i], value2); | 23 Expect.equals(expect[i], value2); |
23 i += 1; | 24 i += 1; |
24 } | 25 } |
25 Expect.equals(expect.length, i); | 26 Expect.equals(expect.length, i); |
26 } | 27 } |
27 | 28 |
28 class MyIterable<T> extends Iterable<T> { | 29 class MyIterable<T> extends IterableBase<T> { |
29 final List<T> values; | 30 final List<T> values; |
30 MyIterable(List<T> values) : this.values = values; | 31 MyIterable(List<T> values) : this.values = values; |
31 Iterator get iterator { | 32 Iterator get iterator { |
32 return new MyListIterator(values); | 33 return new MyListIterator(values); |
33 } | 34 } |
34 } | 35 } |
35 | 36 |
36 class MyListIterator<T> implements Iterator<T> { | 37 class MyListIterator<T> implements Iterator<T> { |
37 final List<T> values; | 38 final List<T> values; |
38 int index; | 39 int index; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 75 |
75 // Using the same variable (showing that the expression is evaluated | 76 // Using the same variable (showing that the expression is evaluated |
76 // in the outer scope). | 77 // in the outer scope). |
77 int result = 0; | 78 int result = 0; |
78 var x = [1,2,3]; | 79 var x = [1,2,3]; |
79 for (var x in x) { | 80 for (var x in x) { |
80 result += x; | 81 result += x; |
81 } | 82 } |
82 Expect.equals(6, result); | 83 Expect.equals(6, result); |
83 } | 84 } |
OLD | NEW |