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