| OLD | NEW |
| 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 "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 | 7 |
| 8 class NullTest { | 8 class BadInherit |
| 9 static int foo(var obj) { | 9 extends Null /// 01: compile-time error |
| 10 implements Null /// 02: compile-time error |
| 11 {} |
| 12 |
| 13 class BadEquals { |
| 14 int get hashCode => null.hashCode; |
| 15 bool operator==(Object other) => other == null; |
| 16 } |
| 17 |
| 18 class NotCalled { |
| 19 bool operator==(Object other) { |
| 20 throw "SHOULD NOT GET HERE"; |
| 21 } |
| 22 } |
| 23 |
| 24 confuse(x) { |
| 25 try { throw [x]; } catch (e) { return e[0]; } |
| 26 return 42; |
| 27 } |
| 28 |
| 29 main() { |
| 30 new BadInherit(); // Make sure class is referenced. |
| 31 |
| 32 int foo(var obj) { |
| 10 Expect.equals(null, obj); | 33 Expect.equals(null, obj); |
| 11 } | 34 } |
| 12 | 35 |
| 13 static bool compareToNull(var value) { | 36 bool compareToNull(var value) { |
| 14 return null == value; | 37 return null == value; |
| 15 } | 38 } |
| 16 | 39 |
| 17 static bool compareWithNull(var value) { | 40 bool compareWithNull(var value) { |
| 18 return value == null; | 41 return value == null; |
| 19 } | 42 } |
| 20 | 43 |
| 21 static int testMain() { | 44 var val = 1; |
| 22 var val = 1; | 45 var obj = confuse(null); |
| 23 var obj = null; | 46 var bad = new BadEquals(); |
| 24 | 47 |
| 25 Expect.equals(null, obj); | 48 Expect.isTrue(identical(obj, null)); |
| 26 Expect.equals(null, null); | |
| 27 | 49 |
| 50 Expect.isTrue(null == null); |
| 51 Expect.isTrue(null == obj); |
| 52 Expect.isTrue(obj == null); |
| 53 Expect.isTrue(obj == obj); |
| 54 |
| 55 // Using == null or null == will not call any equality method. |
| 56 Expect.isFalse(new NotCalled() == null); |
| 57 Expect.isFalse(null == new NotCalled()); |
| 58 Expect.isFalse(new NotCalled() == obj); |
| 59 Expect.isFalse(obj == new NotCalled()); |
| 60 |
| 61 Expect.isFalse(null == false); |
| 62 Expect.isFalse(null == 0); |
| 63 Expect.isFalse(null == ""); |
| 64 Expect.isFalse(null == []); |
| 65 Expect.isFalse(null == 0.0); |
| 66 Expect.isFalse(null == -0.0); |
| 67 Expect.isFalse(null == double.NAN); |
| 68 |
| 69 const t1 = null == null; |
| 70 const t2 = null == 0; |
| 71 const t3 = false == null; |
| 72 Expect.isTrue(t1); |
| 73 Expect.isFalse(t2); |
| 74 Expect.isFalse(t3); |
| 75 |
| 76 Expect.isFalse(bad == null); |
| 77 Expect.isFalse(null == bad); |
| 78 |
| 79 foo(obj); |
| 80 foo(null); |
| 81 |
| 82 if (obj != null) { |
| 83 foo(null); |
| 84 } else { |
| 28 foo(obj); | 85 foo(obj); |
| 29 foo(null); | 86 } |
| 30 | 87 |
| 31 if (obj != null) { | 88 Expect.isTrue(null is Null); |
| 32 foo(null); | 89 Expect.isTrue(obj is Null); |
| 33 } else { | 90 Expect.isTrue(null is Object); |
| 34 foo(obj); | 91 Expect.isTrue(obj is Object); |
| 35 } | 92 Expect.isTrue(null is dynamic); |
| 93 Expect.isTrue(obj is dynamic); |
| 94 Expect.isFalse(obj is String); |
| 95 Expect.isFalse(null is! Null); |
| 96 Expect.isFalse(obj is! Null); |
| 97 Expect.isFalse(null is! Object); |
| 98 Expect.isFalse(obj is! Object); |
| 99 Expect.isFalse(null is! dynamic); |
| 100 Expect.isFalse(obj is! dynamic); |
| 101 Expect.isTrue(obj is !String); |
| 102 Expect.isFalse(0 is Null); // It's just assignable. |
| 36 | 103 |
| 37 Expect.isFalse(compareToNull(val)); | 104 Expect.equals("null", null.toString()); |
| 38 Expect.isTrue(compareToNull(obj)); | 105 Expect.equals("null", "${null}"); |
| 39 Expect.isFalse(compareWithNull(val)); | 106 Expect.equals(Null, null.runtimeType); |
| 40 Expect.isTrue(compareWithNull(obj)); | |
| 41 Expect.isTrue(obj is Object); | |
| 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 | 107 |
| 47 return 0; | 108 Expect.isFalse(compareToNull(val)); |
| 48 } | 109 Expect.isTrue(compareToNull(obj)); |
| 110 Expect.isFalse(compareWithNull(val)); |
| 111 Expect.isTrue(compareWithNull(obj)); |
| 49 } | 112 } |
| 50 | |
| 51 | |
| 52 main() { | |
| 53 NullTest.testMain(); | |
| 54 } | |
| OLD | NEW |