OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 class InstanceofTest { | |
6 | |
7 InstanceofTest() {} | |
8 | |
9 static void testBasicTypes() { | |
10 Expect.equals(true, 0 is int); | |
11 Expect.equals(false, (0 is bool)); | |
12 Expect.equals(false, (0 is String)); | |
13 Expect.equals(true, 1 is int); | |
14 Expect.equals(false, (1 is bool)); | |
15 Expect.equals(false, (1 is String)); | |
16 | |
17 Expect.equals(false, (true is int)); | |
18 Expect.equals(true, true is bool); | |
19 Expect.equals(false, (true is String)); | |
20 Expect.equals(false, (false is int)); | |
21 Expect.equals(true, false is bool); | |
22 Expect.equals(false, (false is String)); | |
23 | |
24 Expect.equals(false, ("a" is int)); | |
25 Expect.equals(false, ("a" is bool)); | |
26 Expect.equals(true, "a" is String); | |
27 | |
28 Expect.equals(false, ("" is int)); | |
29 Expect.equals(false, ("" is bool)); | |
30 Expect.equals(true, "" is String); | |
31 } | |
32 | |
33 static void testInterfaces() { | |
34 // Simple Cases with interfaces. | |
35 var a = new A(); | |
36 Expect.equals(true, a is I); | |
37 Expect.equals(true, a is A); | |
38 Expect.equals(false, (a is String)); | |
39 Expect.equals(false, (a is int)); | |
40 Expect.equals(false, (a is bool)); | |
41 Expect.equals(false, (a is B)); | |
42 Expect.equals(false, (a is J)); | |
43 | |
44 // Interfaces with parent | |
45 var c = new C(); | |
46 Expect.equals(true, c is I); | |
47 Expect.equals(true, c is J); | |
48 Expect.equals(true, c is K); | |
49 | |
50 var d = new D(); | |
51 Expect.equals(true, d is I); | |
52 Expect.equals(true, d is J); | |
53 Expect.equals(true, d is K); | |
54 | |
55 Expect.equals(true, [] is List); | |
56 Expect.equals(true, [1,2,3] is List); | |
57 Expect.equals(false, (d is List)); | |
58 Expect.equals(false, (null is List)); | |
59 Expect.equals(false, (null is D)); | |
60 } | |
61 | |
62 static void testnum() { | |
63 Expect.equals(true, 0 is num); | |
64 Expect.equals(true, 123 is num); | |
65 Expect.equals(true, 123.34 is num); | |
66 Expect.equals(false, ("123" is num)); | |
67 Expect.equals(false, (null is num)); | |
68 Expect.equals(false, (true is num)); | |
69 Expect.equals(false, (false is num)); | |
70 var a = new A(); | |
71 Expect.equals(false, (a is num)); | |
72 } | |
73 | |
74 | |
75 static void testTypeOfInstanceOf() { | |
76 var a = new A(); | |
77 // Interfaces with parent | |
78 var c = new C(); | |
79 var d = new D(); | |
80 | |
81 Expect.equals(true, (null is int) is bool); | |
82 Expect.equals(true, (null is bool) is bool); | |
83 Expect.equals(true, (null is String) is bool); | |
84 Expect.equals(true, (null is A) is bool); | |
85 Expect.equals(true, (null is B) is bool); | |
86 Expect.equals(true, (null is I) is bool); | |
87 Expect.equals(true, (null is J) is bool); | |
88 | |
89 Expect.equals(true, (0 is int) is bool); | |
90 Expect.equals(true, (0 is bool) is bool); | |
91 Expect.equals(true, (0 is String) is bool); | |
92 Expect.equals(true, (0 is A) is bool); | |
93 Expect.equals(true, (0 is B) is bool); | |
94 Expect.equals(true, (0 is I) is bool); | |
95 Expect.equals(true, (0 is J) is bool); | |
96 | |
97 Expect.equals(true, (1 is int) is bool); | |
98 Expect.equals(true, (1 is bool) is bool); | |
99 Expect.equals(true, (1 is String) is bool); | |
100 Expect.equals(true, (1 is A) is bool); | |
101 Expect.equals(true, (1 is B) is bool); | |
102 Expect.equals(true, (1 is I) is bool); | |
103 Expect.equals(true, (1 is J) is bool); | |
104 | |
105 Expect.equals(true, (true is int) is bool); | |
106 Expect.equals(true, (true is bool) is bool); | |
107 Expect.equals(true, (true is String) is bool); | |
108 Expect.equals(true, (true is A) is bool); | |
109 Expect.equals(true, (true is B) is bool); | |
110 Expect.equals(true, (true is I) is bool); | |
111 Expect.equals(true, (true is J) is bool); | |
112 | |
113 Expect.equals(true, (false is int) is bool); | |
114 Expect.equals(true, (false is bool) is bool); | |
115 Expect.equals(true, (false is String) is bool); | |
116 Expect.equals(true, (false is A) is bool); | |
117 Expect.equals(true, (false is B) is bool); | |
118 Expect.equals(true, (false is I) is bool); | |
119 Expect.equals(true, (false is J) is bool); | |
120 | |
121 Expect.equals(true, ("a" is int) is bool); | |
122 Expect.equals(true, ("a" is bool) is bool); | |
123 Expect.equals(true, ("a" is String) is bool); | |
124 Expect.equals(true, ("a" is A) is bool); | |
125 Expect.equals(true, ("a" is B) is bool); | |
126 Expect.equals(true, ("a" is I) is bool); | |
127 Expect.equals(true, ("a" is J) is bool); | |
128 | |
129 Expect.equals(true, ("" is int) is bool); | |
130 Expect.equals(true, ("" is bool) is bool); | |
131 Expect.equals(true, ("" is String) is bool); | |
132 Expect.equals(true, ("" is A) is bool); | |
133 Expect.equals(true, ("" is B) is bool); | |
134 Expect.equals(true, ("" is I) is bool); | |
135 Expect.equals(true, ("" is J) is bool); | |
136 | |
137 Expect.equals(true, (a is int) is bool); | |
138 Expect.equals(true, (a is bool) is bool); | |
139 Expect.equals(true, (a is String) is bool); | |
140 Expect.equals(true, (a is A) is bool); | |
141 Expect.equals(true, (a is B) is bool); | |
142 Expect.equals(true, (a is I) is bool); | |
143 Expect.equals(true, (a is J) is bool); | |
144 | |
145 Expect.equals(true, (c is int) is bool); | |
146 Expect.equals(true, (c is bool) is bool); | |
147 Expect.equals(true, (c is String) is bool); | |
148 Expect.equals(true, (c is A) is bool); | |
149 Expect.equals(true, (c is B) is bool); | |
150 Expect.equals(true, (c is I) is bool); | |
151 Expect.equals(true, (c is J) is bool); | |
152 | |
153 Expect.equals(true, (d is int) is bool); | |
154 Expect.equals(true, (d is bool) is bool); | |
155 Expect.equals(true, (d is String) is bool); | |
156 Expect.equals(true, (d is A) is bool); | |
157 Expect.equals(true, (d is B) is bool); | |
158 Expect.equals(true, (d is I) is bool); | |
159 Expect.equals(true, (d is J) is bool); | |
160 } | |
161 | |
162 static void testMain() { | |
163 testBasicTypes(); | |
164 // TODO(sra): enable after fixing b/4604295 | |
165 // testnum(); | |
166 testInterfaces(); | |
167 testTypeOfInstanceOf(); | |
168 } | |
169 } | |
170 | |
171 interface I {} | |
172 class A implements I {A() {}} | |
173 class B {B() {}} | |
174 | |
175 interface J {} | |
176 | |
177 interface K extends J {} | |
178 class C implements I, K {C() {}} | |
179 | |
180 class D extends C {D() : super() {}} | |
181 | |
182 main() { | |
183 // Repeat type checks so that inlined tests can be tested as well. | |
184 for (int i = 0; i < 5; i++) { | |
185 InstanceofTest.testMain(); | |
186 } | |
187 } | |
188 | |
OLD | NEW |