dart-sdk/tests/corelib/cast_list_test.dart
Riley Porter 654b725ee4 [tests] Fix cast_test and migrate to nnbd.
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>
2020-04-30 02:33:53 +00:00

47 lines
1.2 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() {
testDowncast();
testUpcast();
testRegression();
}
void testDowncast() {
var list = new List<C?>.from(elements);
var dList = List.castFrom<C?, D?>(list);
Expect.throws(() => dList.first); // C is not D?.
Expect.equals(d, dList[1]);
Expect.throws(() => dList[2]); // E is not D?.
Expect.equals(f, dList[3]);
Expect.equals(null, dList.last);
Expect.throws(() => dList.toList());
// Setting works.
dList[2] = d;
Expect.equals(d, dList[2]);
}
void testUpcast() {
var list = new List<C?>.from(elements);
var objectList = List.castFrom<C?, Object?>(list);
Expect.listEquals(elements, objectList);
Expect.throws(() => objectList[2] = new Object()); // Cannot set non-C.
Expect.listEquals(elements, objectList);
}
void testRegression() {
var numList = <num>[4, 3, 2, 1];
var intList = numList.cast<int>();
intList.sort(null);
Expect.listEquals([1, 2, 3, 4], numList);
Expect.listEquals([1, 2, 3, 4], intList);
}