| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import 'dart:collection'; |
| 7 |
| 8 class MyList extends ListBase { |
| 9 List list; |
| 10 MyList(this.list); |
| 11 |
| 12 get length => list.length; |
| 13 set length(val) { list.length = val; } |
| 14 |
| 15 operator [](index) => list[index]; |
| 16 operator []=(index, val) => list[index] = val; |
| 17 |
| 18 String toString() => "[" + join(", ") + "]"; |
| 19 } |
| 20 |
| 6 | 21 |
| 7 main() { | 22 main() { |
| 8 test(expectation, iterable) { | 23 test(expectation, iterable) { |
| 9 Expect.listEquals(expectation, iterable.toList()); | 24 Expect.listEquals(expectation, iterable.toList()); |
| 10 } | 25 } |
| 11 | 26 |
| 12 // Function not called on empty iterable. | 27 // Function not called on empty iterable. |
| 13 test([], [].expand((x) { throw "not called"; })); | 28 test([], [].expand((x) { throw "not called"; })); |
| 14 | 29 |
| 15 // Creating the iterable doesn't call the function. | 30 // Creating the iterable doesn't call the function. |
| 16 [1].expand((x) { throw "not called"; }); | 31 [1].expand((x) { throw "not called"; }); |
| 17 | 32 |
| 18 test([1], [1].expand((x) => [x])); | 33 test([1], [1].expand((x) => [x])); |
| 19 test([1, 2, 3], [1, 2, 3].expand((x) => [x])); | 34 test([1, 2, 3], [1, 2, 3].expand((x) => [x])); |
| 20 | 35 |
| 21 test([], [1].expand((x) => [])); | 36 test([], [1].expand((x) => [])); |
| 22 test([], [1, 2, 3].expand((x) => [])); | 37 test([], [1, 2, 3].expand((x) => [])); |
| 23 test([2], [1, 2, 3].expand((x) => x == 2 ? [2] : [])); | 38 test([2], [1, 2, 3].expand((x) => x == 2 ? [2] : [])); |
| 24 | 39 |
| 25 test([1, 1, 2, 2, 3, 3], [1, 2, 3].expand((x) => [x, x])); | 40 test([1, 1, 2, 2, 3, 3], [1, 2, 3].expand((x) => [x, x])); |
| 26 test([1, 1, 2], [1, 2, 3].expand((x) => [x, x, x].skip(x))); | 41 test([1, 1, 2], [1, 2, 3].expand((x) => [x, x, x].skip(x))); |
| 27 | 42 |
| 43 test([1], new MyList([1]).expand((x) => [x])); |
| 44 test([1, 2, 3], new MyList([1, 2, 3]).expand((x) => [x])); |
| 45 |
| 46 test([], new MyList([1]).expand((x) => [])); |
| 47 test([], new MyList([1, 2, 3]).expand((x) => [])); |
| 48 test([2], new MyList([1, 2, 3]).expand((x) => x == 2 ? [2] : [])); |
| 49 |
| 50 test([1, 1, 2, 2, 3, 3], new MyList([1, 2, 3]).expand((x) => [x, x])); |
| 51 test([1, 1, 2], new MyList([1, 2, 3]).expand((x) => [x, x, x].skip(x))); |
| 52 |
| 28 // if function throws, iteration is stopped. | 53 // if function throws, iteration is stopped. |
| 29 Iterable iterable = [1, 2, 3].expand((x) { | 54 Iterable iterable = [1, 2, 3].expand((x) { |
| 30 if (x == 2) throw "FAIL"; | 55 if (x == 2) throw "FAIL"; |
| 31 return [x, x]; | 56 return [x, x]; |
| 32 }); | 57 }); |
| 33 Iterator it = iterable.iterator; | 58 Iterator it = iterable.iterator; |
| 34 Expect.isTrue(it.moveNext()); | 59 Expect.isTrue(it.moveNext()); |
| 35 Expect.equals(1, it.current); | 60 Expect.equals(1, it.current); |
| 36 Expect.isTrue(it.moveNext()); | 61 Expect.isTrue(it.moveNext()); |
| 37 Expect.equals(1, it.current); | 62 Expect.equals(1, it.current); |
| 38 Expect.throws(it.moveNext, (e) => e == "FAIL"); | 63 Expect.throws(it.moveNext, (e) => e == "FAIL"); |
| 39 // After throwing, iteration is ended. | 64 // After throwing, iteration is ended. |
| 40 Expect.equals(null, it.current); | 65 Expect.equals(null, it.current); |
| 41 Expect.isFalse(it.moveNext()); | 66 Expect.isFalse(it.moveNext()); |
| 42 } | 67 } |
| OLD | NEW |