// Copyright (c) 2019, 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. // Test how spread interacts with inference. import 'package:expect/expect.dart'; void main() { testBottomUpInference(); testTopDownInference(); } void testBottomUpInference() { // Lists. Expect.type>([...[]]); Expect.type>([...[]]); Expect.type>([...[1]]); Expect.type>([1, ...[2]]); Expect.type>([1, ...[0.2]]); Expect.type>([...[1, 2]]); Expect.type>([...[1, 0.2]]); Expect.type>([...[1], ...[2]]); Expect.type>([...[1], ...[0.2]]); // Maps. Expect.type>({...{}}); Expect.type>({...{}}); Expect.type>({...{1: 1}}); Expect.type>({1: 1, ...{2: 2}}); Expect.type>({1: 1, ...{0.2: 0.2}}); Expect.type>({...{1: 1, 2: 2}}); Expect.type>({...{1: 1, 0.2: 0.2}}); Expect.type>({...{1: 1}, ...{2: 2}}); Expect.type>({...{1: 1}, ...{0.2: 0.2}}); // Sets. Expect.type>({...[]}); Expect.type>({...[]}); Expect.type>({...[1]}); Expect.type>({1, ...[2]}); Expect.type>({1, ...[0.2]}); Expect.type>({...[1, 2]}); Expect.type>({...[1, 0.2]}); Expect.type>({...[1], ...[2]}); Expect.type>({...[1], ...[0.2]}); Expect.type>({...{1}, ...[0.2]}); Expect.type>({...{1}, ...{0.2}}); // If the iterable's type is dynamic, the element type is inferred as dynamic. Expect.type>([...([] as dynamic)]); Expect.type>({1, ...([] as dynamic)}); // If the iterable's type is dynamic, the key and value types are inferred as // dynamic. Expect.type>({1: 1, ...({} as dynamic)}); } void testTopDownInference() { // Lists. Iterable expectIntIterable() { Expect.equals(int, T); return []; } Iterable expectDynamicIterable() { Expect.equals(dynamic, T); return []; } // The context element type is pushed into the spread expression if it is // Iterable. Expect.listEquals([], [...expectIntIterable()]); // Bottom up-inference from elements is not pushed back down into spread. Expect.listEquals([1], [1, ...expectDynamicIterable()]); // Maps. Map expectIntStringMap() { Expect.equals(int, K); Expect.equals(String, V); return {}; } Map expectDynamicDynamicMap() { Expect.equals(dynamic, K); Expect.equals(dynamic, V); return {}; } // The context element type is pushed into the spread expression if it is // Map. Expect.mapEquals({}, {...expectIntStringMap()}); // Bottom up-inference from elements is not pushed back down into spread. Expect.mapEquals({1: "s"}, {1: "s", ...expectDynamicDynamicMap()}); // Sets. Set expectIntSet() { Expect.equals(int, T); return Set(); } Set expectDynamicSet() { Expect.equals(dynamic, T); return Set(); } // The context element type is pushed into the spread expression if it is // Iterable. Expect.setEquals({}, {...expectIntSet()}); // Bottom up-inference from elements is not pushed back down into spread. Expect.setEquals({1}, {1, ...expectDynamicSet()}); }