mirror of
https://github.com/dart-lang/sdk
synced 2024-09-06 00:59:09 +00:00
![Riley Porter](/assets/img/avatar_default.png)
Also splits up cast_test to test collections separately and updates corelib_2 version with the same changes, including fixes in relevant pending CL: https://dart-review.googlesource.com/c/sdk/+/126320 Fixes #39517 Bug: http://dartbug.com/39517 Change-Id: I8df1fde1d48ed23561ce9d0d8b87d7fc2ba52eb1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/144469 Commit-Queue: Riley Porter <rileyporter@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com> Reviewed-by: Bob Nystrom <rnystrom@google.com>
69 lines
2.3 KiB
Dart
69 lines
2.3 KiB
Dart
// Copyright (c) 2020, 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 "dart:collection";
|
|
import "package:expect/expect.dart";
|
|
import 'cast_helper.dart';
|
|
|
|
void main() {
|
|
testDowncastDirectAccess();
|
|
testDowncastNoDirectAccess();
|
|
testDowncastUntouchableElements();
|
|
testUpcast();
|
|
}
|
|
|
|
void testDowncastDirectAccess() {
|
|
var iterable = new Iterable<C?>.generate(elements.length, (n) => elements[n]);
|
|
|
|
// An iterable that (likely) can do direct access.
|
|
var dIterable = Iterable.castFrom<C?, D?>(iterable);
|
|
|
|
Expect.throws(() => dIterable.first);
|
|
Expect.equals(d, dIterable.elementAt(1));
|
|
Expect.throws(() => dIterable.elementAt(2)); // E is not D?
|
|
Expect.equals(f, dIterable.skip(3).first); // Skip does not access element.
|
|
Expect.equals(null, dIterable.skip(3).elementAt(1));
|
|
|
|
Expect.throws(() => dIterable.toList());
|
|
}
|
|
|
|
void testDowncastNoDirectAccess() {
|
|
var iterable = new Iterable<C?>.generate(elements.length, (n) => elements[n]);
|
|
|
|
// An iterable that cannot do direct access.
|
|
var dIterable = Iterable.castFrom<C?, D?>(iterable.where((_) => true));
|
|
|
|
Expect.throws(() => dIterable.first);
|
|
Expect.equals(d, dIterable.elementAt(1));
|
|
// E is not D?.
|
|
Expect.throws(() => dIterable.elementAt(2));
|
|
Expect.equals(f, dIterable.skip(3).first); // Skip does not access element.
|
|
Expect.equals(null, dIterable.skip(3).elementAt(1));
|
|
|
|
Expect.throws(() => dIterable.toList());
|
|
}
|
|
|
|
void testDowncastUntouchableElements() {
|
|
// Iterable that definitely won't survive accessing element 3.
|
|
var iterable = new Iterable<C?>.generate(
|
|
elements.length, (n) => n == 3 ? throw "untouchable" : elements[n]);
|
|
var dIterable = Iterable.castFrom<C?, D?>(iterable);
|
|
|
|
Expect.throws(() => dIterable.first);
|
|
Expect.equals(d, dIterable.elementAt(1));
|
|
Expect.throws(() => dIterable.elementAt(3));
|
|
// Skip does not access element.
|
|
Expect.equals(null, dIterable.skip(4).first);
|
|
Expect.equals(null, dIterable.skip(3).elementAt(1));
|
|
|
|
Expect.throws(() => dIterable.toList());
|
|
}
|
|
|
|
void testUpcast() {
|
|
var iterable = new Iterable<C?>.generate(elements.length, (n) => elements[n]);
|
|
|
|
var objectIterable = Iterable.castFrom<C?, Object?>(iterable);
|
|
Expect.listEquals(elements, objectIterable.toList());
|
|
}
|