| 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) { | |
| 14 list.length = val; | |
| 15 } | |
| 16 | |
| 17 operator [](index) => list[index]; | |
| 18 operator []=(index, val) => list[index] = val; | |
| 19 | |
| 20 String toString() => "[" + join(", ") + "]"; | |
| 21 } | |
| 22 | |
| 23 main() { | |
| 24 test(expectation, iterable) { | |
| 25 Expect.listEquals(expectation, iterable.toList()); | |
| 26 } | |
| 27 | |
| 28 // Function not called on empty iterable. | |
| 29 test( | |
| 30 [], | |
| 31 [].expand((x) { | |
| 32 throw "not called"; | |
| 33 })); | |
| 34 | |
| 35 // Creating the iterable doesn't call the function. | |
| 36 [1].expand((x) { | |
| 37 throw "not called"; | |
| 38 }); | |
| 39 | |
| 40 test([1], [1].expand((x) => [x])); | |
| 41 test([1, 2, 3], [1, 2, 3].expand((x) => [x])); | |
| 42 | |
| 43 test([], [1].expand((x) => [])); | |
| 44 test([], [1, 2, 3].expand((x) => [])); | |
| 45 test([2], [1, 2, 3].expand((x) => x == 2 ? [2] : [])); | |
| 46 | |
| 47 test([1, 1, 2, 2, 3, 3], [1, 2, 3].expand((x) => [x, x])); | |
| 48 test([1, 1, 2], [1, 2, 3].expand((x) => [x, x, x].skip(x))); | |
| 49 | |
| 50 test([1], new MyList([1]).expand((x) => [x])); | |
| 51 test([1, 2, 3], new MyList([1, 2, 3]).expand((x) => [x])); | |
| 52 | |
| 53 test([], new MyList([1]).expand((x) => [])); | |
| 54 test([], new MyList([1, 2, 3]).expand((x) => [])); | |
| 55 test([2], new MyList([1, 2, 3]).expand((x) => x == 2 ? [2] : [])); | |
| 56 | |
| 57 test([1, 1, 2, 2, 3, 3], new MyList([1, 2, 3]).expand((x) => [x, x])); | |
| 58 test([1, 1, 2], new MyList([1, 2, 3]).expand((x) => [x, x, x].skip(x))); | |
| 59 | |
| 60 // if function throws, iteration is stopped. | |
| 61 Iterable iterable = [1, 2, 3].expand((x) { | |
| 62 if (x == 2) throw "FAIL"; | |
| 63 return [x, x]; | |
| 64 }); | |
| 65 Iterator it = iterable.iterator; | |
| 66 Expect.isTrue(it.moveNext()); | |
| 67 Expect.equals(1, it.current); | |
| 68 Expect.isTrue(it.moveNext()); | |
| 69 Expect.equals(1, it.current); | |
| 70 Expect.throws(it.moveNext, (e) => e == "FAIL"); | |
| 71 // After throwing, iteration is ended. | |
| 72 Expect.equals(null, it.current); | |
| 73 Expect.isFalse(it.moveNext()); | |
| 74 } | |
| OLD | NEW |