OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 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 |
| 21 |
| 22 main() { |
| 23 test(expectation, iterable) { |
| 24 Expect.listEquals(expectation, iterable.toList()); |
| 25 } |
| 26 |
| 27 // Function not called on empty iterable. |
| 28 test([], [].expand((x) { throw "not called"; })); |
| 29 |
| 30 // Creating the iterable doesn't call the function. |
| 31 [1].expand((x) { throw "not called"; }); |
| 32 |
| 33 test([1], [1].expand((x) => [x])); |
| 34 test([1, 2, 3], [1, 2, 3].expand((x) => [x])); |
| 35 |
| 36 test([], [1].expand((x) => [])); |
| 37 test([], [1, 2, 3].expand((x) => [])); |
| 38 test([2], [1, 2, 3].expand((x) => x == 2 ? [2] : [])); |
| 39 |
| 40 test([1, 1, 2, 2, 3, 3], [1, 2, 3].expand((x) => [x, x])); |
| 41 test([1, 1, 2], [1, 2, 3].expand((x) => [x, x, x].skip(x))); |
| 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 |
| 53 // if function throws, iteration is stopped. |
| 54 Iterable iterable = [1, 2, 3].expand((x) { |
| 55 if (x == 2) throw "FAIL"; |
| 56 return [x, x]; |
| 57 }); |
| 58 Iterator it = iterable.iterator; |
| 59 Expect.isTrue(it.moveNext()); |
| 60 Expect.equals(1, it.current); |
| 61 Expect.isTrue(it.moveNext()); |
| 62 Expect.equals(1, it.current); |
| 63 Expect.throws(it.moveNext, (e) => e == "FAIL"); |
| 64 // After throwing, iteration is ended. |
| 65 Expect.equals(null, it.current); |
| 66 Expect.isFalse(it.moveNext()); |
| 67 } |
OLD | NEW |