Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: tests/language/null_bottom_test.dart

Issue 2577083002: Add test for Nullable-By-Default (Null is subtype of all types). (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 // Dart test for testing the ternary operator.
5
6 import "package:expect/expect.dart";
7
8 // Test that `Null` acts like the bottom type - less than any other type.
9
10 bool checkedMode = () { bool c = false; assert(c = true); return c; } ();
floitsch 2016/12/20 10:52:58 isCheckedMode
Lasse Reichstein Nielsen 2017/01/02 12:48:50 Done.
11
12 typedef R Fun<A, R>(A argument);
13
14 class C<T> {
15 const C();
16 T returns() => null;
17 void accepts(T x) {}
18 }
19
20 class NullBound<T extends num> {}
21
22 main() {
23 testClassTypes();
24 testFunctionTypes();
25 }
26
27 void testClassTypes() {
28 var cn = new C<Null>();
29
30 Expect.isTrue(cn is C<Null>);
31 Expect.isTrue(cn is C<Object>);
32 Expect.isTrue(cn is C<int>);
33
34 Expect.isNotNull(cn as C<Null>);
35 Expect.isNotNull(cn as C<Object>);
36 Expect.isNotNull(cn as C<int>);
37
38 var ccn = new C<C<Null>>();
39
40 Expect.isTrue(ccn is C<C<Null>>);
41 Expect.isTrue(ccn is C<C<Object>>);
42 Expect.isTrue(ccn is C<C<int>>);
43
44 Expect.isNotNull(ccn as C<C<Null>>);
45 Expect.isNotNull(ccn as C<C<Object>>);
46 Expect.isNotNull(ccn as C<C<int>>);
47
48 var ci = new C<int>();
49 Expect.isFalse(ci is C<Null>);
50
51 var co = new C<Object>();
52 Expect.isFalse(co is C<Null>);
53
54 if (!checkedMode) return;
55
56 List<int> = const <Null>[];
57
58 C<Null> x1 = cn;
59 C<Object> x2 = cn;
60
61 Expect.identical(x1 , cn);
62 Expect.identical(x2 , cn);
63
64 const C<Null> cocn = const C<Null>();
65 const C<Object> coco = cocn;
66 const C<int> coci = cocn;
67
68 Expect.identical(cocn, coco);
69 Expect.identical(cocn, coci);
70
71 Expect.throws(() {
72 Null x = "string" as dynamic;
73 use(x); // Avoid "x unused" warning.
74 });
75
76 Expect.throws(() {
77 Null x = new Object();
78 use(x); // Avoid "x unused" warning.
79 });
80
81 NullBound<int> nb = new NullBound<Null>(); // Should not fail.
82 use(nb); // Avoid "nb unused" warning.
83 }
84
85 void testFunctionTypes() {
86 T1 t1 = new T1();
87 T2 t2 = new T2();
88 T1 t = t2;
89
90 Fun<int, Null> f1 = t1.foo;
91 Fun<Null, int> f2 = t.bar;
92 f1 = t1.baz;
93 f2 = t.qux;
94 use(f1);
95 use(f2);
96
97 var l = new List<Fun<Null, Null>>();
98 Expect.isTrue(l is List<Fun<int, Null>>);
floitsch 2016/12/20 10:52:58 Why is that? a List of Null->Null should not be a
Lasse Reichstein Nielsen 2017/01/02 12:48:50 Done.
99 l = new List<Fun<int, int>>;
100 Expect.isTrue(l is List<Fun<Null, num>>);
101
102 Expect.isTrue(((int _) => null) is Fun<int, Null>);
103
104 Null fun(int x) => null;
105 Fun<Null, int> fun2 = fun; // Safe assignment.
106 if (fun2 is Fun<int, Null>) {
107 // If int->Null is *subtype* of Null->int (which it should be),
108 // then type promotion succeeds.
109 // If type promotion succeeds, the static type is int->Null, otherwise
110 // it's Null->int.
111 fun2(42); // Should not give a warning after type promotion.
112 fun2(null).abs(); /// 03: static type warning, runtime error
113 }
114 }
115
116 class T1 {
117 Null foo(int x) => null;
118 int bar(Null x) => null;
119 Null baz(int x) => null;
120 int qux(Null x) => null;
121 }
122
123 class T2 extends T1 {
124 Null foo(Null x) => null; /// 01: static type warning
125 Null bar(Null x) => null;
126 int baz(int x) => x; /// 02: static type warning
127 int qux(int x) => x;
128 }
129
130 // Avoid "variable not used" warnings.
131 use(x) {
132 return identical(x, x);
133 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698