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

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

Issue 23012003: Add Null class to dart:core. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Made VM not fail on x is Null tests. Created 7 years, 4 months 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 | Annotate | Revision Log
« runtime/vm/object.cc ('K') | « tests/language/language.status ('k') | 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
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 // Second dart test program. 4 // Second dart test program.
5 5
6 import "dart:mirrors";
6 import "package:expect/expect.dart"; 7 import "package:expect/expect.dart";
7 8
8 class NullTest { 9 class BadInherit
9 static int foo(var obj) { 10 extends Null /// 01: compile-time error
11 implements Null /// 02: compile-time error
12 extends Object with Null /// 03: compile-time error
13 {}
14
15 class EqualsNotCalled {
16 int get hashCode => throw "And don't warn!";
17 bool operator==(Object other) {
18 throw "SHOULD NOT GET HERE";
19 }
20 }
21
22 class Generic<T> {
23 bool test(o) => o is T;
24 T cast(o) => o as T;
25 Type get type => T;
26 }
27
28 class Generic2<T, S> {
29 bool test(o) => new Generic<T>().test(o);
30 T cast(o) => new Generic<T>().cast(o);
31 Type get type => new Generic<T>().type;
32 }
33
34 // Magic incantation to avoid the compiler recognizing the constant values
35 // at compile time. If the result is computed at compile time, the dynamic code
36 // will not be tested.
37 confuse(x) {
38 try { throw [x]; } catch (e) { return e[0]; }
39 return 42;
40 }
41
42 main() {
43 new BadInherit(); // Make sure class is referenced.
44
45 int foo(var obj) {
10 Expect.equals(null, obj); 46 Expect.equals(null, obj);
11 } 47 }
12 48
13 static bool compareToNull(var value) { 49 bool compareToNull(var value) {
14 return null == value; 50 return null == value;
15 } 51 }
16 52
17 static bool compareWithNull(var value) { 53 bool compareWithNull(var value) {
18 return value == null; 54 return value == null;
19 } 55 }
20 56
21 static int testMain() { 57 var val = 1;
22 var val = 1; 58 var obj = confuse(null); // Null value that isn't known at compile-time.
23 var obj = null; 59 Expect.isTrue(identical(obj, null), "identical");
24 60
25 Expect.equals(null, obj); 61 Expect.isTrue(null == null);
26 Expect.equals(null, null); 62 Expect.isTrue(null == obj);
63 Expect.isTrue(obj == null);
64 Expect.isTrue(obj == obj);
27 65
66 // Using == null or null == will not call any equality method.
67 Expect.isFalse(new EqualsNotCalled() == null);
68 Expect.isFalse(null == new EqualsNotCalled());
69 Expect.isFalse(new EqualsNotCalled() == obj);
70 Expect.isFalse(obj == new EqualsNotCalled());
71
72 Expect.isFalse(null == false);
73 Expect.isFalse(null == 0);
74 Expect.isFalse(null == "");
75 Expect.isFalse(null == []);
76 Expect.isFalse(null == 0.0);
77 Expect.isFalse(null == -0.0);
78 Expect.isFalse(null == double.NAN);
79
80 Expect.isFalse(obj == false);
81 Expect.isFalse(obj == 0);
82 Expect.isFalse(obj == "");
83 Expect.isFalse(obj == []);
84 Expect.isFalse(obj == 0.0);
85 Expect.isFalse(obj == -0.0);
86 Expect.isFalse(obj == double.NAN);
87
88 // Explicit constant expressions.
89 const t1 = null == null;
90 const t2 = null == 0;
91 const t3 = false == null;
92 Expect.isTrue(t1);
93 Expect.isFalse(t2);
94 Expect.isFalse(t3);
95
96 foo(obj);
97 foo(null);
98 if (obj != null) {
99 foo(null);
100 } else {
28 foo(obj); 101 foo(obj);
29 foo(null); 102 }
30 103
31 if (obj != null) { 104 // Test "is" operator.
32 foo(null); 105 Expect.isTrue(null is Null);
33 } else { 106 Expect.isTrue(obj is Null);
34 foo(obj); 107 Expect.isTrue(null is Object);
35 } 108 Expect.isTrue(obj is Object);
109 Expect.isTrue(null is dynamic);
110 Expect.isTrue(obj is dynamic);
111 Expect.isFalse(null is String);
112 Expect.isFalse(obj is String);
113 Expect.isFalse(0 is Null); // It's only assignable.
114 Expect.isFalse(null is! Null);
115 Expect.isFalse(obj is! Null);
116 Expect.isFalse(null is! Object);
117 Expect.isFalse(obj is! Object);
118 Expect.isFalse(null is! dynamic);
119 Expect.isFalse(obj is! dynamic);
120 Expect.isTrue(null is! String);
121 Expect.isTrue(obj is! String);
122 Expect.isTrue(0 is! Null); // It's only assignable.
36 123
37 Expect.isFalse(compareToNull(val)); 124 // Test "is" operator with generic type variable.
38 Expect.isTrue(compareToNull(obj)); 125 Expect.isTrue(new Generic<Null>().test(null));
39 Expect.isFalse(compareWithNull(val)); 126 Expect.isFalse(new Generic<Null>().test(42));
40 Expect.isTrue(compareWithNull(obj)); 127 Expect.isTrue(new Generic2<Null, int>().test(null));
41 Expect.isTrue(obj is Object); 128 Expect.isFalse(new Generic2<Null, int>().test(42));
42 Expect.isFalse(obj is String);
43 Expect.isTrue(obj is !String);
44 Expect.isFalse(obj is !Object);
45 Expect.isFalse(val is !Object);
46 129
47 return 0; 130 // Test cast, "as", operator.
48 } 131 Expect.equals(null, null as Null);
132 Expect.equals(null, null as Object);
133 Expect.equals(null, null as int);
134 Expect.throws(() => 42 as Null, (e) => e is CastError);
135 Expect.equals(null, new Generic<Null>().cast(null));
136 Expect.equals(null, new Generic<Object>().cast(null));
137 Expect.equals(null, new Generic<int>().cast(null));
138
139 Expect.equals(null, obj as Null);
140 Expect.equals(null, obj as Object);
141 Expect.equals(null, obj as int);
142 Expect.equals(null, new Generic<Null>().cast(obj));
143 Expect.equals(null, new Generic<Object>().cast(obj));
144 Expect.equals(null, new Generic<int>().cast(obj));
145
146 Expect.equals("null", null.toString());
147 Expect.equals("null", "${null}");
148 Expect.equals("null", obj.toString());
149 Expect.equals("null", "${obj}");
150
151 Expect.equals(Null, null.runtimeType);
152 Expect.equals(Null, obj.runtimeType);
153 Expect.equals(Null, new Generic<Null>().type);
154 Expect.equals(Null, new Generic2<Null, int>().type);
155
156 Expect.isFalse(compareToNull(val));
157 Expect.isTrue(compareToNull(obj));
158 Expect.isFalse(compareWithNull(val));
159 Expect.isTrue(compareWithNull(obj));
160
161 ClassMirror cm = reflectClass(Null);
162
163 InstanceMirror im1 = reflect(null);
164 Expect.equals(cm, im1.type);
165 Expect.isTrue(im1.invoke(const Symbol("=="), [null]).reflectee);
166 Expect.isFalse(im1.invoke(const Symbol("=="), [42]).reflectee);
167
168 InstanceMirror im2 = reflect(obj);
169 Expect.equals(cm, im2.type);
170 Expect.isTrue(im2.invoke(const Symbol("=="), [null]).reflectee);
171 Expect.isFalse(im2.invoke(const Symbol("=="), [42]).reflectee);
172
173 // Method/value extraction. The runtimeType was checked above, and operator==
174 // cannot be extracted.
175 // Currently fails in VM.
176 // Expect.equals(null.toString, obj.toString);
177 // Expect.equals(null.noSuchMethod, obj.noSuchMethod);
178 Expect.equals(null.hashCode, obj.hashCode);
179
180 var toString = null.toString;
181 Expect.equals("null", toString());
182 Expect.equals("null", Function.apply(toString, []));
183
184 Expect.throws(() => null.notDeclared());
185 var noSuchMethod = null.noSuchMethod;
186 var mirror = new CaptureInvocationMirror().notDeclared();
187 Expect.throws(() => noSuchMethod(mirror));
188 Expect.throws(() => Function.apply(noSuchMethod, [mirror]));
49 } 189 }
50 190
51 191
52 main() { 192 class CaptureInvocationMirror {
53 NullTest.testMain(); 193 noSuchMethod(mirror) => mirror;
54 } 194 }
OLDNEW
« runtime/vm/object.cc ('K') | « tests/language/language.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698