// 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. import 'package:expect/expect.dart'; // Typed as dynamic to also test spreading a value of type dynamic. const dynamic list = [1, 2, 3, 4]; const dynamic map = {1: 1, 2: 2, 3: 3, 4: 4}; const dynamic set = {1, 2, 3, 4}; void main() { testList(); testMap(); testSet(); testKeyOrder(); } void testList() { // Only spread. Expect.identical(list, const [...list]); Expect.identical(list, const [...set]); // Spread at beginning. Expect.identical(list, const [...[1, 2], 3, 4]); // Spread in middle. Expect.identical(list, const [1, ...[2, 3], 4]); // Spread at end. Expect.identical(list, const [1, 2, ...[3, 4]]); // Empty spreads. Expect.identical(list, const [...[], 1, 2, ...[], 3, 4, ...[]]); // Multiple spreads. Expect.identical(list, const [...[1], 2, ...[3, 4]]); // Nested spreads. Expect.identical(list, const [...[...[1, 2], ...[3, 4]]]); // Null-aware. Expect.identical(list, const [1, ...?[2, 3], ...?(null), ...?[4]]); // Does not deep flatten. Expect.identical( const [1, 2, [3], 4], const [1, ...[2, [3], 4]]); // Establishes const context. Expect.identical(const [Symbol("sym")], const [...[Symbol("sym")]]); } void testMap() { // Only spread. Expect.identical(map, const {...map}); // Spread at beginning. Expect.identical(map, const {...{1: 1, 2: 2}, 3: 3, 4: 4}); // Spread in middle. Expect.identical(map, const {1: 1, ...{2: 2, 3: 3}, 4: 4}); // Spread at end. Expect.identical(map, const {1: 1, 2: 2, ...{3: 3, 4: 4}}); // Empty spreads. Expect.identical(map, const { ...{}, 1: 1, 2: 2, ...{}, 3: 3, 4: 4, ...{} }); // Multiple spreads. Expect.identical(map, const {...{1: 1}, 2: 2, ...{3: 3, 4: 4}}); // Nested spreads. Expect.identical(map, const { ...{ ...{1: 1, 2: 2}, ...{3: 3, 4: 4} } }); // Null-aware. Expect.identical(map, const { 1: 1, ...?{2: 2, 3: 3}, ...?(null), ...?{4: 4} }); // Does not deep flatten. Expect.identical(const { 1: 1, 2: 2, 3: {3: 3}, 4: 4 }, const { 1: 1, ...{ 2: 2, 3: {3: 3}, 4: 4 } }); // Establishes const context. Expect.identical(const { Symbol("sym"): Symbol("bol") }, const { ...{Symbol("sym"): Symbol("bol")} }); } void testSet() { // Only spread. Expect.identical(set, const {...set}); Expect.identical(set, const {...list}); // Spread at beginning. Expect.identical(set, const {...[1, 2], 3, 4}); // Spread in middle. Expect.identical(set, const {1, ...[2, 3], 4}); // Spread at end. Expect.identical(set, const {1, 2, ...[3, 4]}); // Empty spreads. Expect.identical(set, const {...[], 1, 2, ...[], 3, 4, ...[]}); // Multiple spreads. Expect.identical(set, const {...[1], 2, ...[3, 4]}); // Nested spreads. Expect.identical(set, const {...{...[1, 2], ...[3, 4]}}); // Null-aware. Expect.identical(set, const {1, ...?[2, 3], ...?(null), ...?[4]}); // Does not deep flatten. Expect.identical(const {1, 2, {3}, 4}, const {1, ...{2, {3}, 4}}); // Establishes const context. Expect.identical(const {Symbol("sym")}, const {...{Symbol("sym")}}); } void testKeyOrder() { // Canonicalization isn't affected by which elements are spread. Expect.identical(map, const {1: 1, ...{2: 2, 3: 3}, 4: 4}); Expect.identical(map, const {1: 1, ...{2: 2}, 3: 3, ...{4: 4}}); Expect.identical(set, const {1, ...{2, 3}, 4}); Expect.identical(set, const {1, ...{2}, 3, ...{4}}); // Ordering does affect canonicalization. Expect.notIdentical(const {1: 1, 2: 2, 3: 3}, const {1: 1, ...{3: 3, 2: 2}}); Expect.notIdentical(const {1, 2, 3}, const {1, ...{3, 2}}); }