OLD | NEW |
| (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 | |
5 import 'expect.dart'; | |
6 | |
7 class TypeTester<T> { | |
8 const TypeTester(); | |
9 bool isCorrectType(object) => object is T; | |
10 } | |
11 | |
12 class ClosureTypeTester<T> { | |
13 const ClosureTypeTester(); | |
14 bool isCorrectType(object) => (() => object is T)(); | |
15 } | |
16 | |
17 class Base<A, B> { | |
18 final A a; | |
19 final B b; | |
20 const Base(this.a, this.b); | |
21 const factory Base.fac(A a, B b) = Base<A, B>; | |
22 } | |
23 | |
24 class Sub1<C, D> extends Base<C, C> { | |
25 final D d; | |
26 const Sub1(C a, this.d) : super(a, a); | |
27 const factory Sub1.fac(C a, D d) = Sub1<C, D>; | |
28 } | |
29 | |
30 class Sub2<C, D> extends Base<D, C> { | |
31 const Sub2(C a, D b) : super(b, a); | |
32 const factory Sub2.fac(C a, D b) = Sub2<C, D>; | |
33 } | |
34 | |
35 class G<T> { } | |
36 class I { } | |
37 class A implements I { } | |
38 class B extends A { } | |
39 class C {} | |
40 | |
41 testConstantLiteralTypes() { | |
42 Expect.isTrue(const [1] is List); | |
43 Expect.isTrue(const [1] is List<int>); | |
44 Expect.isTrue(const [1] is List<String>); | |
45 Expect.isTrue(const <int>[1] is List); | |
46 Expect.isTrue(const <int>[1] is List<int>); | |
47 Expect.isTrue(!(const <int>[1] is List<String>)); | |
48 Expect.isTrue(const {"a": 1} is Map); | |
49 Expect.isTrue(const {"a": 1} is Map<String, int>); | |
50 Expect.isTrue(const {"a": 1} is Map<int, String>); | |
51 Expect.isTrue(const <String, int>{"a": 1} is Map); | |
52 Expect.isTrue(const <String, int>{"a": 1} is Map<String, int>); | |
53 Expect.isTrue(!(const <String, int>{"a": 1} is Map<int, String>)); | |
54 } | |
55 | |
56 testNonConstantLiteralTypes() { | |
57 Expect.isTrue([1] is List); | |
58 Expect.isTrue([1] is List<int>); | |
59 Expect.isTrue([1] is List<String>); | |
60 Expect.isTrue(<int>[1] is List); | |
61 Expect.isTrue(<int>[1] is List<int>); | |
62 Expect.isTrue(!(<int>[1] is List<String>)); | |
63 } | |
64 | |
65 testParametrizedClass() { | |
66 Expect.isTrue(new Base<int, int>(1, 1) is Base<int, int>); | |
67 Expect.isTrue(new Base<int, int>(1, 1) is Base); | |
68 Expect.isTrue(new Base<int, int>(1, 1) is Base<Object, Object>); | |
69 Expect.isTrue(!(new Base<int, int>(1, 1) is Base<int, String>)); | |
70 Expect.isTrue(!(new Base<int, int>(1, 1) is Base<String, int>)); | |
71 Expect.isTrue(new Sub1<int, String>(1, "1") is Base<int, int>); | |
72 Expect.isTrue(new Sub1<int, String>(1, "1") is Base); | |
73 Expect.isTrue(new Sub1<int, String>(1, "1") is Sub1<int, String>); | |
74 Expect.isTrue(new Sub1<int, String>(1, "1") is Sub1); | |
75 Expect.isTrue(!(new Sub1<int, String>(1, "1") is Base<String, int>)); | |
76 Expect.isTrue(!(new Sub1<int, String>(1, "1") is Base<int, String>)); | |
77 Expect.isTrue(!(new Sub1<int, String>(1, "1") is Sub1<String, String>)); | |
78 Expect.isTrue(new Sub2<int, String>(1, "1") is Base<String, int>); | |
79 Expect.isTrue(new Sub2<int, String>(1, "1") is Base); | |
80 Expect.isTrue(new Sub2<int, String>(1, "1") is Sub2<int, String>); | |
81 Expect.isTrue(new Sub2<int, String>(1, "1") is Sub2); | |
82 Expect.isTrue(!(new Sub2<int, String>(1, "1") is Base<int, int>)); | |
83 Expect.isTrue(!(new Sub2<int, String>(1, "1") is Base<int, String>)); | |
84 Expect.isTrue(!(new Sub2<int, String>(1, "1") is Sub2<String, String>)); | |
85 } | |
86 | |
87 testTypeTester() { | |
88 Expect.isTrue(new TypeTester<int>().isCorrectType(10)); | |
89 Expect.isTrue(!new TypeTester<int>().isCorrectType("abc")); | |
90 Expect.isTrue(new TypeTester<List<int>>().isCorrectType([1])); | |
91 Expect.isTrue(new TypeTester<List<int>>().isCorrectType(<int>[1])); | |
92 Expect.isTrue(!new TypeTester<List<int>>().isCorrectType(<String>["1"])); | |
93 Expect.isTrue(new TypeTester<Base<String, int>>() | |
94 .isCorrectType(new Sub2<int, String>(1, "1"))); | |
95 Expect.isTrue(new TypeTester<Sub2<int, String>>() | |
96 .isCorrectType(new Sub2<int, String>(1, "1"))); | |
97 } | |
98 | |
99 testClosureTypeTester() { | |
100 Expect.isTrue(new ClosureTypeTester<int>().isCorrectType(10)); | |
101 Expect.isTrue(!new ClosureTypeTester<int>().isCorrectType("abc")); | |
102 Expect.isTrue(new ClosureTypeTester<List<int>>().isCorrectType([1])); | |
103 Expect.isTrue(new ClosureTypeTester<List<int>>().isCorrectType(<int>[1])); | |
104 Expect.isTrue(!new ClosureTypeTester<List<int>>() | |
105 .isCorrectType(<String>["1"])); | |
106 Expect.isTrue(new ClosureTypeTester<Base<String, int>>() | |
107 .isCorrectType(new Sub2<int, String>(1, "1"))); | |
108 Expect.isTrue(new ClosureTypeTester<Sub2<int, String>>() | |
109 .isCorrectType(new Sub2<int, String>(1, "1"))); | |
110 } | |
111 | |
112 testConstTypeArguments() { | |
113 Expect.isTrue(const Sub1<int, String>(1, "1") is Sub1<int, String>); | |
114 Expect.isTrue(const Sub1<int, String>.fac(1, "1") is Sub1<int, String>); | |
115 Expect.isTrue(!(const Sub1<int, String>(1, "1") is Sub1<String, String>)); | |
116 Expect.isTrue(!(const Sub1<int, String>.fac(1, "1") is Sub1<String, String>)); | |
117 | |
118 Expect.isTrue(const ClosureTypeTester<List<Base<int, String>>>() | |
119 .isCorrectType( | |
120 const <Base<int, String>>[const Base<int, String>(1, "2")])); | |
121 Expect.isTrue(const ClosureTypeTester<List<Base<int, String>>>() | |
122 .isCorrectType( | |
123 const <Base<int, String>>[const Base<int, String>.fac(1, "2")])); | |
124 Expect.isTrue(!const ClosureTypeTester<List<Base<int, String>>>() | |
125 .isCorrectType( | |
126 const <Base<String, String>>[const Base<String, String>("1", "2")])); | |
127 Expect.isTrue(!const ClosureTypeTester<List<Base<int, String>>>() | |
128 .isCorrectType( | |
129 const <Base<String, String>>[const Base<String, String>.fac("1", "2")])); | |
130 | |
131 Expect.isTrue(const TypeTester<Sub2<int, String>>() | |
132 .isCorrectType(const Sub2<int, String>(1, "1"))); | |
133 Expect.isTrue(const TypeTester<Sub2<int, String>>() | |
134 .isCorrectType(const Sub2<int, String>.fac(1, "1"))); | |
135 Expect.isTrue(!const TypeTester<Sub2<int, String>>() | |
136 .isCorrectType(const Sub2<String, String>("a", "b"))); | |
137 Expect.isTrue(!const TypeTester<Sub2<int, String>>() | |
138 .isCorrectType(const Sub2<String, String>.fac("a", "b"))); | |
139 } | |
140 | |
141 testNoBound() { | |
142 new G<int>(); | |
143 new G<num>(); | |
144 new G<Function>(); | |
145 new G<Object>(); | |
146 new G(); | |
147 new G(); | |
148 new G<G>(); | |
149 } | |
150 | |
151 testSubtypeChecker() { | |
152 Expect.isTrue(new TypeTester<num>().isCorrectType(1)); | |
153 Expect.isTrue(new TypeTester<num>().isCorrectType(1.0)); | |
154 Expect.isTrue(new TypeTester<A>().isCorrectType(new B())); | |
155 Expect.isTrue(new TypeTester<Object>().isCorrectType(new C())); | |
156 Expect.isTrue(new TypeTester<I>().isCorrectType(new A())); | |
157 } | |
158 | |
159 testFunctionTypes() { | |
160 fun(int x, String y) => "${x}${y}"; | |
161 Expect.isTrue(fun is FunctionType); | |
162 Expect.isTrue(nan is FunctionType); | |
163 Expect.isTrue(nan is Function); | |
164 } | |
165 | |
166 num nan(double d, Pattern p) => double.NAN; | |
167 | |
168 typedef int FunctionType(num _, Pattern __); | |
169 | |
170 | |
171 testLiteralTypeArguments() { | |
172 Expect.isTrue(new Foo<String, int>().foo() is List<String>); | |
173 Expect.isTrue(new Foo<int, String>().bar() is Map<int, String>); | |
174 } | |
175 | |
176 class Foo<T1, T2> { | |
177 foo() => <T1>[]; | |
178 bar() => <T1, T2>{}; | |
179 } | |
180 | |
181 regressionTest1() { | |
182 Expect.isTrue(!StaticTypeTester.isInt('abc')); | |
183 } | |
184 | |
185 class StaticTypeTester<T> { | |
186 static isInt(x) => x is int; | |
187 } | |
188 | |
189 main() { | |
190 testConstantLiteralTypes(); | |
191 testNonConstantLiteralTypes(); | |
192 testParametrizedClass(); | |
193 testTypeTester(); | |
194 testClosureTypeTester(); | |
195 testConstTypeArguments(); | |
196 testNoBound(); | |
197 testSubtypeChecker(); | |
198 testFunctionTypes(); | |
199 testLiteralTypeArguments(); | |
200 regressionTest1(); | |
201 } | |
202 | |
OLD | NEW |