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

Unified 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 side-by-side diff with in-line comments
Download patch
« sdk/lib/core/null.dart ('K') | « tests/language/language.status ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/language/null_test.dart
diff --git a/tests/language/null_test.dart b/tests/language/null_test.dart
index 6ce22eec85a3a2733469f9880f508e414f27fcee..b7d51093698cc966ea8f46db99c6f64c9a4f1199 100644
--- a/tests/language/null_test.dart
+++ b/tests/language/null_test.dart
@@ -1,54 +1,154 @@
-// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Second dart test program.
+import "dart:mirrors";
import "package:expect/expect.dart";
-class NullTest {
- static int foo(var obj) {
+class BadInherit
+ extends Null /// 01: compile-time error
+ implements Null /// 02: compile-time error
+ {}
+
+class BadEquals {
+ int get hashCode => null.hashCode;
+ bool operator==(Object other) => other == null;
+}
+
+class NotCalled {
Søren Gjesse 2013/08/13 14:53:52 Maybe name EqualsNotCalled
Lasse Reichstein Nielsen 2013/08/14 08:18:35 Done.
+ int get hashCode => throw "And don't warn!";
+ bool operator==(Object other) {
+ throw "SHOULD NOT GET HERE";
+ }
+}
+
+class Generic<T> {
+ bool test(o) => o is T;
+ T cast(o) => o as T;
+ Type get type => T;
+}
+
+class Generic2<T, S> {
+ bool test(o) => new Generic<T>().test(o);
+ T cast(o) => new Generic<T>().cast(o);
+ Type get type => new Generic<T>().type;
+}
+
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.
+confuse(x) {
+ try { throw [x]; } catch (e) { return e[0]; }
+ return 42;
+}
+
+main() {
+ new BadInherit(); // Make sure class is referenced.
+
+ int foo(var obj) {
Expect.equals(null, obj);
}
- static bool compareToNull(var value) {
+ bool compareToNull(var value) {
return null == value;
}
- static bool compareWithNull(var value) {
+ bool compareWithNull(var value) {
return value == null;
}
- static int testMain() {
- var val = 1;
- var obj = null;
+ var val = 1;
+ var obj = confuse(null);
+ var bad = new BadEquals();
- Expect.equals(null, obj);
- Expect.equals(null, null);
+ Expect.isTrue(identical(obj, null), "identical");
- foo(obj);
- foo(null);
+ Expect.isTrue(null == null);
+ Expect.isTrue(null == obj);
+ Expect.isTrue(obj == null);
+ Expect.isTrue(obj == obj);
+
+ // Using == null or null == will not call any equality method.
+ Expect.isFalse(new NotCalled() == null);
+ Expect.isFalse(null == new NotCalled());
+ Expect.isFalse(new NotCalled() == obj);
+ Expect.isFalse(obj == new NotCalled());
+
+ Expect.isFalse(null == false);
+ Expect.isFalse(null == 0);
+ Expect.isFalse(null == "");
+ Expect.isFalse(null == []);
+ Expect.isFalse(null == 0.0);
+ Expect.isFalse(null == -0.0);
+ Expect.isFalse(null == double.NAN);
+
+ const t1 = null == null;
+ const t2 = null == 0;
+ const t3 = false == null;
+ Expect.isTrue(t1);
+ Expect.isFalse(t2);
+ Expect.isFalse(t3);
- if (obj != null) {
- foo(null);
- } else {
- foo(obj);
- }
-
- Expect.isFalse(compareToNull(val));
- Expect.isTrue(compareToNull(obj));
- Expect.isFalse(compareWithNull(val));
- Expect.isTrue(compareWithNull(obj));
- Expect.isTrue(obj is Object);
- Expect.isFalse(obj is String);
- Expect.isTrue(obj is !String);
- Expect.isFalse(obj is !Object);
- Expect.isFalse(val is !Object);
-
- return 0;
+ // Shouldn't throw since bad.== is never called.
+ Expect.isFalse(bad == null);
+ Expect.isFalse(null == bad);
+
+ foo(obj);
+ foo(null);
+ if (obj != null) {
+ foo(null);
+ } else {
+ foo(obj);
}
-}
+ // Test "is" operator.
+ Expect.isTrue(null is Null);
+ Expect.isTrue(obj is Null);
+ Expect.isTrue(null is Object);
+ Expect.isTrue(obj is Object);
+ Expect.isTrue(null is dynamic);
+ Expect.isTrue(obj is dynamic);
+ Expect.isFalse(obj is String);
+ Expect.isFalse(null is! Null);
+ Expect.isFalse(obj is! Null);
+ Expect.isFalse(null is! Object);
+ Expect.isFalse(obj is! Object);
+ Expect.isFalse(null is! dynamic);
+ Expect.isFalse(obj is! dynamic);
+ Expect.isTrue(obj is !String);
+ Expect.isFalse(0 is Null); // It's only assignable.
-main() {
- NullTest.testMain();
+ // Test "is" operator with generic type variable.
+ // Currently fails in dart2js.
+ // FAILS // Expect.isTrue(new Generic<Null>().test(null));
+ // FAILS // Expect.isFalse(new Generic<Null>().test(42));
+ // FAILS // Expect.isTrue(new Generic2<Null, int>().test(null));
+ // FAILS // Expect.isFalse(new Generic2<Null, int>().test(42));
+
+ // Test cast, "as", operator.
+ Expect.equals(null, null as Null);
+ Expect.equals(null, null as Object);
+ Expect.equals(null, null as int);
+ Expect.throws(() => 42 as Null, (e) => e is CastError);
+ Expect.equals(null, new Generic<Null>().cast(null));
+ Expect.equals(null, new Generic<Object>().cast(null));
+ Expect.equals(null, new Generic<int>().cast(null));
+
+
+ Expect.equals("null", null.toString());
+ Expect.equals("null", "${null}");
+
+ Expect.equals(Null, null.runtimeType);
+ Expect.equals(Null, new Generic<Null>().type);
+ Expect.equals(Null, new Generic2<Null, int>().type);
+
+ Expect.isFalse(compareToNull(val));
+ Expect.isTrue(compareToNull(obj));
+ Expect.isFalse(compareWithNull(val));
+ Expect.isTrue(compareWithNull(obj));
+
+ ClassMirror cm = reflectClass(Null);
+ InstanceMirror im = reflect(null);
+ Expect.equals(cm, im.type);
+
+ Expect.isTrue(im.invoke(const Symbol("=="), [null]).reflectee);
+ Expect.isFalse(im.invoke(const Symbol("=="), [42]).reflectee);
}
« 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