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

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: Fix "x is Null". Add more tests. Still fail "x is T" if T is Null. 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
« sdk/lib/core/null.dart ('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 {}
13
14 class BadEquals {
15 int get hashCode => null.hashCode;
16 bool operator==(Object other) => other == null;
17 }
18
19 class NotCalled {
Søren Gjesse 2013/08/13 14:53:52 Maybe name EqualsNotCalled
Lasse Reichstein Nielsen 2013/08/14 08:18:35 Done.
20 int get hashCode => throw "And don't warn!";
21 bool operator==(Object other) {
22 throw "SHOULD NOT GET HERE";
23 }
24 }
25
26 class Generic<T> {
27 bool test(o) => o is T;
28 T cast(o) => o as T;
29 Type get type => T;
30 }
31
32 class Generic2<T, S> {
33 bool test(o) => new Generic<T>().test(o);
34 T cast(o) => new Generic<T>().cast(o);
35 Type get type => new Generic<T>().type;
36 }
37
Søren Gjesse 2013/08/13 14:53:52 Please comment why this is here.
Lasse Reichstein Nielsen 2013/08/14 08:18:35 Done.
38 confuse(x) {
39 try { throw [x]; } catch (e) { return e[0]; }
40 return 42;
41 }
42
43 main() {
44 new BadInherit(); // Make sure class is referenced.
45
46 int foo(var obj) {
10 Expect.equals(null, obj); 47 Expect.equals(null, obj);
11 } 48 }
12 49
13 static bool compareToNull(var value) { 50 bool compareToNull(var value) {
14 return null == value; 51 return null == value;
15 } 52 }
16 53
17 static bool compareWithNull(var value) { 54 bool compareWithNull(var value) {
18 return value == null; 55 return value == null;
19 } 56 }
20 57
21 static int testMain() { 58 var val = 1;
22 var val = 1; 59 var obj = confuse(null);
23 var obj = null; 60 var bad = new BadEquals();
24 61
25 Expect.equals(null, obj); 62 Expect.isTrue(identical(obj, null), "identical");
26 Expect.equals(null, null);
27 63
64 Expect.isTrue(null == null);
65 Expect.isTrue(null == obj);
66 Expect.isTrue(obj == null);
67 Expect.isTrue(obj == obj);
68
69 // Using == null or null == will not call any equality method.
70 Expect.isFalse(new NotCalled() == null);
71 Expect.isFalse(null == new NotCalled());
72 Expect.isFalse(new NotCalled() == obj);
73 Expect.isFalse(obj == new NotCalled());
74
75 Expect.isFalse(null == false);
76 Expect.isFalse(null == 0);
77 Expect.isFalse(null == "");
78 Expect.isFalse(null == []);
79 Expect.isFalse(null == 0.0);
80 Expect.isFalse(null == -0.0);
81 Expect.isFalse(null == double.NAN);
82
83 const t1 = null == null;
84 const t2 = null == 0;
85 const t3 = false == null;
86 Expect.isTrue(t1);
87 Expect.isFalse(t2);
88 Expect.isFalse(t3);
89
90 // Shouldn't throw since bad.== is never called.
91 Expect.isFalse(bad == null);
92 Expect.isFalse(null == bad);
93
94 foo(obj);
95 foo(null);
96 if (obj != null) {
97 foo(null);
98 } else {
28 foo(obj); 99 foo(obj);
29 foo(null); 100 }
30 101
31 if (obj != null) { 102 // Test "is" operator.
32 foo(null); 103 Expect.isTrue(null is Null);
33 } else { 104 Expect.isTrue(obj is Null);
34 foo(obj); 105 Expect.isTrue(null is Object);
35 } 106 Expect.isTrue(obj is Object);
107 Expect.isTrue(null is dynamic);
108 Expect.isTrue(obj is dynamic);
109 Expect.isFalse(obj is String);
110 Expect.isFalse(null is! Null);
111 Expect.isFalse(obj is! Null);
112 Expect.isFalse(null is! Object);
113 Expect.isFalse(obj is! Object);
114 Expect.isFalse(null is! dynamic);
115 Expect.isFalse(obj is! dynamic);
116 Expect.isTrue(obj is !String);
117 Expect.isFalse(0 is Null); // It's only assignable.
36 118
37 Expect.isFalse(compareToNull(val)); 119 // Test "is" operator with generic type variable.
38 Expect.isTrue(compareToNull(obj)); 120 // Currently fails in dart2js.
39 Expect.isFalse(compareWithNull(val)); 121 // FAILS // Expect.isTrue(new Generic<Null>().test(null));
40 Expect.isTrue(compareWithNull(obj)); 122 // FAILS // Expect.isFalse(new Generic<Null>().test(42));
41 Expect.isTrue(obj is Object); 123 // FAILS // Expect.isTrue(new Generic2<Null, int>().test(null));
42 Expect.isFalse(obj is String); 124 // FAILS // Expect.isFalse(new Generic2<Null, int>().test(42));
43 Expect.isTrue(obj is !String);
44 Expect.isFalse(obj is !Object);
45 Expect.isFalse(val is !Object);
46 125
47 return 0; 126 // Test cast, "as", operator.
48 } 127 Expect.equals(null, null as Null);
49 } 128 Expect.equals(null, null as Object);
129 Expect.equals(null, null as int);
130 Expect.throws(() => 42 as Null, (e) => e is CastError);
131 Expect.equals(null, new Generic<Null>().cast(null));
132 Expect.equals(null, new Generic<Object>().cast(null));
133 Expect.equals(null, new Generic<int>().cast(null));
50 134
51 135
52 main() { 136 Expect.equals("null", null.toString());
53 NullTest.testMain(); 137 Expect.equals("null", "${null}");
138
139 Expect.equals(Null, null.runtimeType);
140 Expect.equals(Null, new Generic<Null>().type);
141 Expect.equals(Null, new Generic2<Null, int>().type);
142
143 Expect.isFalse(compareToNull(val));
144 Expect.isTrue(compareToNull(obj));
145 Expect.isFalse(compareWithNull(val));
146 Expect.isTrue(compareWithNull(obj));
147
148 ClassMirror cm = reflectClass(Null);
149 InstanceMirror im = reflect(null);
150 Expect.equals(cm, im.type);
151
152 Expect.isTrue(im.invoke(const Symbol("=="), [null]).reflectee);
153 Expect.isFalse(im.invoke(const Symbol("=="), [42]).reflectee);
54 } 154 }
OLDNEW
« sdk/lib/core/null.dart ('K') | « tests/language/language.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698