// Copyright (c) 2018, 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. library map_unmodifiable_cast_test; import "package:expect/expect.dart"; import 'dart:collection'; void main() { testNum(const {1: 37}, "const"); testNum(const {1: 37}.cast(), "const.cast"); testNum(new UnmodifiableMapView({1: 37}), "unmod"); testNum(new UnmodifiableMapView({1: 37}), "unmod.cast"); testNum(new UnmodifiableMapView({1: 37}).cast(), "unmodView(num).cast"); testNum(new UnmodifiableMapView({1: 37}).cast(), "unmodView(int).cast"); testNum( new UnmodifiableMapView({1: 37}) .cast(), "unmodView(num).cast"); testNum( new UnmodifiableMapView({1: 37}) .cast(), "unmodView(int).cast"); var m2 = new Map.unmodifiable({1: 37}); testNum(m2, "Map.unmod"); testNum(m2.cast(), "Map.unmod.cast"); Map nsm = new NsmMap().foo(a: 0); test(nsm, #a, 0, "nsm", noSuchMethodMap: true); test(nsm.cast(), #a, 0, "nsm.cast", noSuchMethodMap: true); } void testNum(Map map, String name) { test(map, 1, 37, name); } void test(map, firstKey, firstValue, String name, {bool noSuchMethodMap: false}) { if (!noSuchMethodMap) { Expect.isTrue(map.containsKey(firstKey), "$name.containsKey"); Expect.equals(1, map.length, "$name.length"); Expect.equals(firstKey, map.keys.first, "$name.keys.first"); Expect.equals(firstValue, map.values.first, "$name.values.first"); } Expect.throwsUnsupportedError(map.clear, "$name.clear"); Expect.throwsUnsupportedError(() { map.remove(firstKey); }, "$name.remove"); Expect.throwsUnsupportedError(() { map[firstKey] = firstValue; }, "$name[]="); Expect.throwsUnsupportedError(() { map.addAll({firstKey: firstValue}); }, "$name.addAll"); } class NsmMap { noSuchMethod(i) => i.namedArguments; foo({a, b, c, d}); }