2013-02-05 09:48:05 +00:00
|
|
|
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2013-04-05 19:43:16 +00:00
|
|
|
import "package:expect/expect.dart";
|
2013-04-15 14:02:43 +00:00
|
|
|
import 'dart:collection';
|
|
|
|
|
|
|
|
class MyList extends ListBase {
|
|
|
|
List list;
|
|
|
|
MyList(this.list);
|
|
|
|
|
|
|
|
get length => list.length;
|
2017-04-17 21:52:57 +00:00
|
|
|
set length(val) {
|
|
|
|
list.length = val;
|
|
|
|
}
|
2013-04-15 14:02:43 +00:00
|
|
|
|
|
|
|
operator [](index) => list[index];
|
|
|
|
operator []=(index, val) => list[index] = val;
|
|
|
|
|
|
|
|
String toString() => "[" + join(", ") + "]";
|
|
|
|
}
|
|
|
|
|
2013-02-05 09:48:05 +00:00
|
|
|
main() {
|
|
|
|
test(expectation, iterable) {
|
|
|
|
Expect.listEquals(expectation, iterable.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function not called on empty iterable.
|
2017-04-17 21:52:57 +00:00
|
|
|
test(
|
|
|
|
[],
|
|
|
|
[].expand((x) {
|
|
|
|
throw "not called";
|
|
|
|
}));
|
2013-02-05 09:48:05 +00:00
|
|
|
|
|
|
|
// Creating the iterable doesn't call the function.
|
2017-04-17 21:52:57 +00:00
|
|
|
[1].expand((x) {
|
|
|
|
throw "not called";
|
|
|
|
});
|
2013-02-05 09:48:05 +00:00
|
|
|
|
|
|
|
test([1], [1].expand((x) => [x]));
|
|
|
|
test([1, 2, 3], [1, 2, 3].expand((x) => [x]));
|
|
|
|
|
|
|
|
test([], [1].expand((x) => []));
|
|
|
|
test([], [1, 2, 3].expand((x) => []));
|
|
|
|
test([2], [1, 2, 3].expand((x) => x == 2 ? [2] : []));
|
|
|
|
|
|
|
|
test([1, 1, 2, 2, 3, 3], [1, 2, 3].expand((x) => [x, x]));
|
|
|
|
test([1, 1, 2], [1, 2, 3].expand((x) => [x, x, x].skip(x)));
|
|
|
|
|
2013-04-15 14:02:43 +00:00
|
|
|
test([1], new MyList([1]).expand((x) => [x]));
|
|
|
|
test([1, 2, 3], new MyList([1, 2, 3]).expand((x) => [x]));
|
|
|
|
|
|
|
|
test([], new MyList([1]).expand((x) => []));
|
|
|
|
test([], new MyList([1, 2, 3]).expand((x) => []));
|
|
|
|
test([2], new MyList([1, 2, 3]).expand((x) => x == 2 ? [2] : []));
|
|
|
|
|
|
|
|
test([1, 1, 2, 2, 3, 3], new MyList([1, 2, 3]).expand((x) => [x, x]));
|
|
|
|
test([1, 1, 2], new MyList([1, 2, 3]).expand((x) => [x, x, x].skip(x)));
|
|
|
|
|
2013-02-05 09:48:05 +00:00
|
|
|
// if function throws, iteration is stopped.
|
|
|
|
Iterable iterable = [1, 2, 3].expand((x) {
|
|
|
|
if (x == 2) throw "FAIL";
|
|
|
|
return [x, x];
|
|
|
|
});
|
|
|
|
Iterator it = iterable.iterator;
|
|
|
|
Expect.isTrue(it.moveNext());
|
|
|
|
Expect.equals(1, it.current);
|
|
|
|
Expect.isTrue(it.moveNext());
|
|
|
|
Expect.equals(1, it.current);
|
|
|
|
Expect.throws(it.moveNext, (e) => e == "FAIL");
|
|
|
|
// After throwing, iteration is ended.
|
|
|
|
Expect.equals(null, it.current);
|
|
|
|
Expect.isFalse(it.moveNext());
|
|
|
|
}
|