dart-sdk/tests/corelib/cast_map_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

58 lines
1.8 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();
}
void testDowncast() {
var map = new Map.fromIterables(elements, elements);
var dMap = Map.castFrom<C?, C?, D?, D?>(map);
Expect.isTrue(dMap is Map<D?, D?>);
Expect.equals(null, dMap[new C()]);
Expect.throws(() => dMap[c]); // C is not D?.
Expect.isTrue(dMap.containsKey(c)); // containsKey should not be typed.
Expect.equals(d, dMap[d]);
Expect.throws(() => dMap[e]); // E is not D?.
Expect.isTrue(dMap.containsKey(null));
Expect.equals(null, dMap[null]);
Expect.equals(5, dMap.length);
Expect.throws(() => dMap.remove(c)); // Removes key but fails to return value.
Expect.equals(4, dMap.length);
Expect.equals(null, dMap[c]);
// Test keys and values.
Expect.isTrue(dMap.keys is Iterable<D?>);
Expect.isTrue(dMap.values is Iterable<D?>);
Expect.throws(() => dMap.keys.toList());
Expect.throws(() => dMap.values.toList());
}
void testUpcast() {
var map = new Map.fromIterables(elements, elements);
var objectMap = Map.castFrom<C?, C?, Object?, Object?>(map);
Expect.equals(5, objectMap.length);
Expect.equals(c, objectMap[c]);
Expect.isTrue(objectMap.containsKey(c));
Expect.equals(c, objectMap.remove(c));
Expect.equals(4, objectMap.length);
// Test keys and values.
Expect.isTrue(objectMap.keys is Iterable<Object?>);
Expect.isTrue(objectMap.values is Iterable<Object?>);
var expected = new List<Object?>.from(elements);
expected.remove(c);
Expect.listEquals(expected, objectMap.keys.toList());
Expect.listEquals(expected, objectMap.values.toList());
}