-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcast_set_test.dart
73 lines (59 loc) · 2.05 KB
/
cast_set_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 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() {
testOrder();
testDowncast();
testUpcast();
testNewSet();
}
void testOrder() {
var setEls = new Set<C?>.from(elements); // Linked HashSet.
Expect.listEquals(elements, setEls.toList()); // Preserves order.
}
void testDowncast() {
var setEls = new Set<C?>.from(elements);
var dSet = Set.castFrom<C?, D?>(setEls);
// Preserves order.
Expect.throws(() => dSet.first); // C is not D?.
Expect.equals(d, dSet.elementAt(1));
Expect.throws(() => dSet.elementAt(2)); // E is not D?.
Expect.equals(f, dSet.elementAt(3));
Expect.equals(null, dSet.elementAt(4));
Expect.throws(() => dSet.toList());
// Contains should not be typed.
var newC = new C();
Expect.isFalse(dSet.contains(newC));
Expect.isTrue(dSet.contains(c));
// Remove and length should not be typed.
Expect.equals(5, dSet.length);
dSet.remove(c); // Success, no type checks.
Expect.equals(4, dSet.length);
}
void testUpcast() {
var setEls = new Set<C?>.from(elements);
var objectSet = Set.castFrom<C?, Object?>(setEls);
Expect.listEquals(elements, objectSet.toList());
var newObject = new Object();
Expect.throws(() => objectSet.add(newObject));
Expect.isFalse(objectSet.contains(newObject));
var toSet = objectSet.toSet();
Expect.isTrue(toSet is LinkedHashSet<Object?>);
Expect.isFalse(toSet is LinkedHashSet<C?>);
}
void testNewSet() {
// Specified custom newSet as empty HashSet.
var setEls = new Set<C?>.from(elements);
var customNewSet;
var objectSet2 = Set.castFrom<C?, Object?>(
setEls,
newSet: <T>() => customNewSet = new HashSet<T>(),
);
var customToSet = objectSet2.toSet();
Expect.isTrue(customToSet is HashSet<Object?>);
Expect.isFalse(customToSet is HashSet<C?>);
Expect.identical(customToSet, customNewSet);
}