dart-sdk/tests/corelib/iterable_empty_test.dart
Robert Nystrom 284f662022 Migrate the corelib_2/ tests starting with "e" through "i".
Change-Id: I764d0457da85b2935e1d83309eab4c4b5b3ea000
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126204
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2019-12-02 23:19:10 +00:00

71 lines
2.9 KiB
Dart

// 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.
import "package:expect/expect.dart";
main() {
testEmpty(name, Iterable<int> it, [depth = 2]) {
Expect.isTrue(it.isEmpty, name);
Expect.isFalse(it.isNotEmpty, name);
Expect.equals(0, it.length, name);
Expect.isFalse(it.contains(null), name);
Expect.isFalse(it.any((x) => true), name);
Expect.isTrue(it.every((x) => false), name);
Expect.throwsStateError(() => it.first, name);
Expect.throwsStateError(() => it.last, name);
Expect.throwsStateError(() => it.single, name);
Expect.throwsRangeError(() => it.elementAt(0), name);
Expect.throwsStateError(() => it.reduce((a, b) => a), name);
Expect.throwsStateError(() => it.singleWhere((_) => true), name);
Expect.equals(42, it.fold(42, (a, b) => "not 42"), name);
Expect.equals(42, it.firstWhere((v) => true, orElse: () => 42), name);
Expect.equals(42, it.lastWhere((v) => true, orElse: () => 42), name);
Expect.equals("", it.join("separator"), name);
Expect.equals("()", it.toString(), name);
Expect.listEquals([], it.toList(), name);
Expect.listEquals([], it.toList(growable: false), name);
Expect.listEquals([], it.toList(growable: true), name);
Expect.equals(0, it.toSet().length, name);
// Doesn't throw:
it.forEach((v) => throw v);
for (var v in it) {
throw v;
}
// Check that returned iterables are also empty.
if (depth > 0) {
testEmpty("$name-map", it.map((x) => x), depth - 1);
testEmpty("$name-where", it.where((x) => true), depth - 1);
testEmpty("$name-expand", it.expand((x) => [x]), depth - 1);
testEmpty("$name-skip", it.skip(1), depth - 1);
testEmpty("$name-take", it.take(2), depth - 1);
testEmpty("$name-skipWhile", it.skipWhile((v) => false), depth - 1);
testEmpty("$name-takeWhile", it.takeWhile((v) => true), depth - 1);
}
}
testType(name, it, [depth = 2]) {
Expect.isTrue(it is Iterable<int>, name);
Expect.isFalse(it is Iterable<String>, name);
if (depth > 0) {
testType("$name-where", it.where((_) => true), depth - 1);
testType("$name-skip", it.skip(1), depth - 1);
testType("$name-take", it.take(1), depth - 1);
testType("$name-skipWhile", it.skipWhile((_) => false), depth - 1);
testType("$name-takeWhile", it.takeWhile((_) => true), depth - 1);
testType("$name-toList", it.toList(), depth - 1);
testType("$name-toList", it.toList(growable: false), depth - 1);
testType("$name-toList", it.toList(growable: true), depth - 1);
testType("$name-toSet", it.toSet(), depth - 1);
}
}
test(name, it) {
testEmpty(name, it);
testType(name, it);
}
test("const", const Iterable<int>.empty());
test("new", new Iterable<int>.empty());
}