|
| 1 | +// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import "package:expect/expect.dart"; |
| 8 | + |
| 9 | +void main() { |
| 10 | + test(); |
| 11 | +} |
| 12 | + |
| 13 | +void test() { |
| 14 | + checkSet<T>(Object o, List elements) { |
| 15 | + Expect.type<Set<T>>(o); |
| 16 | + Set<T> set = o as Set<T>; |
| 17 | + Expect.listEquals(elements, set.toList()); |
| 18 | + Expect.throws<Error>(set.clear); |
| 19 | + } |
| 20 | + |
| 21 | + // Various context types for literals. |
| 22 | + Object setContext<T>(Set<T> object) => object; |
| 23 | + Object iterableContext<T>(Iterable<T> object) => object; |
| 24 | + Object foSetContext<T>(FutureOr<Set<T>> object) => object; |
| 25 | + Object foIterableContext<T>(FutureOr<Set<T>> object) => object; |
| 26 | + |
| 27 | + // Empty literal, no type arguments. |
| 28 | + // No context. |
| 29 | + Expect.type<Map<dynamic, dynamic>>(const {}); |
| 30 | + // Set context with no inferred type argument. |
| 31 | + checkSet<dynamic>(setContext(const {}), []); |
| 32 | + checkSet<dynamic>(iterableContext(const {}), []); |
| 33 | + checkSet<dynamic>(foSetContext(const {}), []); |
| 34 | + checkSet<dynamic>(foIterableContext(const {}), []); |
| 35 | + // Specific set context. |
| 36 | + checkSet<int>(setContext<int>(const {}), []); |
| 37 | + checkSet<int>(iterableContext<int>(const {}), []); |
| 38 | + checkSet<int>(foSetContext<int>(const {}), []); |
| 39 | + checkSet<int>(foIterableContext<int>(const {}), []); |
| 40 | + |
| 41 | + // Non-empty set literal, no type argument. |
| 42 | + // No context. |
| 43 | + checkSet<int>(const {1}, [1]); |
| 44 | + checkSet<int>(const {3, 1, 2, 4}, [3, 1, 2, 4]); |
| 45 | + // Set context with no inferred type argument. |
| 46 | + checkSet<int>(setContext(const {1}), [1]); |
| 47 | + checkSet<int>(iterableContext(const {1}), [1]); |
| 48 | + checkSet<int>(foSetContext(const {1}), [1]); |
| 49 | + checkSet<int>(foIterableContext(const {1}), [1]); |
| 50 | + checkSet<int>(setContext(const {3, 1, 2, 4}), [3, 1, 2, 4]); |
| 51 | + checkSet<int>(iterableContext(const {3, 1, 2, 4}), [3, 1, 2, 4]); |
| 52 | + checkSet<int>(foSetContext(const {1}), [1]); |
| 53 | + checkSet<int>(foIterableContext(const {1}), [1]); |
| 54 | + // Specific set context. |
| 55 | + checkSet<num>(setContext<num>(const {1}), [1]); |
| 56 | + checkSet<num>(iterableContext<num>(const {1}), [1]); |
| 57 | + checkSet<num>(foSetContext<num>(const {1}), [1]); |
| 58 | + checkSet<num>(foIterableContext<num>(const {1}), [1]); |
| 59 | + checkSet<num>(setContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]); |
| 60 | + checkSet<num>(iterableContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]); |
| 61 | + checkSet<num>(foSetContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]); |
| 62 | + checkSet<num>(foIterableContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]); |
| 63 | + |
| 64 | + // Non-empty set literal with type argument. |
| 65 | + checkSet<num>(const <num>{1}, [1]); |
| 66 | + checkSet<num>(const <num>{3, 1, 2, 4}, [3, 1, 2, 4]); |
| 67 | + |
| 68 | + // Integers, String and symbols work, even if they override ==. |
| 69 | + checkSet<String>(const {"foo", "bar"}, ["foo", "bar"]); |
| 70 | + checkSet<Symbol>(const {#foo, #bar}, [#foo, #bar]); |
| 71 | + checkSet<Symbol>(const {#_foo, #_bar}, [#_foo, #_bar]); |
| 72 | + const object = Object(); |
| 73 | + checkSet<Object>(const {#foo, 1, "string", object, true}, |
| 74 | + [#foo, 1, "string", object, true]); |
| 75 | + |
| 76 | + // Nested constant literals. |
| 77 | + const Object o = {{2}}; |
| 78 | + Expect.type<Set<Set<int>>>(o); |
| 79 | + Set<Set<int>> set = o as Set<Set<int>>; |
| 80 | + Expect.equals(1, set.length); |
| 81 | + Expect.equals(1, set.first.length); |
| 82 | + Expect.equals(2, set.first.first); |
| 83 | + |
| 84 | + const Object o2 = {{2}, <int>{}}; |
| 85 | + Expect.type<Set<Set<int>>>(o); |
| 86 | + set = o2 as Set<Set<int>>; |
| 87 | + Expect.equals(2, set.length); |
| 88 | + Expect.equals(1, set.first.length); |
| 89 | + Expect.equals(2, set.first.first); |
| 90 | + |
| 91 | + const Set<Set<int>> o3 = {{}}; |
| 92 | + Expect.equals(1, o3.length); |
| 93 | + Expect.equals(0, o3.first.length); |
| 94 | + |
| 95 | + const o4 = {{}}; |
| 96 | + Expect.type<Set<Map<dynamic, dynamic>>>(o4); |
| 97 | + Expect.equals(1, o4.length); |
| 98 | + Expect.equals(0, o4.first.length); |
| 99 | + |
| 100 | + const o5 = {{1}, {}}; // Set<Object> |
| 101 | + Expect.type<Set<Object>>(o5); |
| 102 | + Expect.notType<Set<Set<Object>>>(o5); |
| 103 | + |
| 104 | + // User defined constant class. |
| 105 | + const o6 = { |
| 106 | + Something(1, "a"), |
| 107 | + Something(2, "a"), |
| 108 | + Something(1, "b"), |
| 109 | + Something(2, "b"), |
| 110 | + }; |
| 111 | + Expect.equals("1:a,2:a,1:b,2:b", o6.toList().join(",")); |
| 112 | + |
| 113 | + // Canonicalization of constant sets takes ordering into account, |
| 114 | + // that is, o7 and o8 cannot be the same object. |
| 115 | + const o7 = {1, 2, 3}; |
| 116 | + const o8 = {3, 2, 1}; |
| 117 | + Expect.notIdentical(o7, o8); |
| 118 | + // But o7 and o9 must be identical. |
| 119 | + const o9 = {1, 2, 3}; |
| 120 | + Expect.identical(o7, o9); |
| 121 | +} |
| 122 | + |
| 123 | +class Something { |
| 124 | + final int id; |
| 125 | + final String name; |
| 126 | + const Something(this.id, this.name); |
| 127 | + String toString() => "$id:$name"; |
| 128 | +} |
0 commit comments