| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 | 4 |
| 5 // Verify semantics of the ?. operator when it is used to invoke a method. | 5 // Verify semantics of the ?. operator when it is used to invoke a method. |
| 6 | 6 |
| 7 // SharedOptions=--enable-null-aware-operators | 7 // SharedOptions=--enable-null-aware-operators |
| 8 | 8 |
| 9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 import "conditional_access_helper.dart" as h; | 10 import "conditional_access_helper.dart" as h; |
| 11 | 11 |
| 12 bad() { | 12 bad() { |
| 13 Expect.fail('Should not be executed'); | 13 Expect.fail('Should not be executed'); |
| 14 } | 14 } |
| 15 | 15 |
| 16 noMethod(e) => e is NoSuchMethodError; | 16 noMethod(e) => e is NoSuchMethodError; |
| 17 | 17 |
| 18 class B {} | 18 class B {} |
| 19 | 19 |
| 20 class C extends B { | 20 class C extends B { |
| 21 f(callback()) => callback(); | 21 f(callback()) => callback(); |
| 22 int g(int callback()) => callback(); | 22 int g(int callback()) => callback(); |
| 23 static void staticMethod() {} |
| 23 } | 24 } |
| 24 | 25 |
| 25 C nullC() => null; | 26 C nullC() => null; |
| 26 | 27 |
| 27 main() { | 28 main() { |
| 28 // Make sure the "none" test fails if method invocation using "?." is not | 29 // Make sure the "none" test fails if method invocation using "?." is not |
| 29 // implemented. This makes status files easier to maintain. | 30 // implemented. This makes status files easier to maintain. |
| 30 nullC()?.f(null); | 31 nullC()?.f(null); |
| 31 | 32 |
| 32 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o). | 33 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o). |
| (...skipping 18 matching lines...) Expand all Loading... |
| 51 Expect.throws(() => h.C?.staticMethod(), noMethod); /// 10: static type warnin
g | 52 Expect.throws(() => h.C?.staticMethod(), noMethod); /// 10: static type warnin
g |
| 52 | 53 |
| 53 // Nor can it be used to access toplevel functions in libraries imported via | 54 // Nor can it be used to access toplevel functions in libraries imported via |
| 54 // prefix. | 55 // prefix. |
| 55 Expect.throws(() => h?.topLevelFunction(), noMethod); /// 11: static type warn
ing | 56 Expect.throws(() => h?.topLevelFunction(), noMethod); /// 11: static type warn
ing |
| 56 | 57 |
| 57 // However, '?.' can be used to access the toString method on the class Type. | 58 // However, '?.' can be used to access the toString method on the class Type. |
| 58 Expect.equals(C?.toString(), (C).toString()); /// 12: ok | 59 Expect.equals(C?.toString(), (C).toString()); /// 12: ok |
| 59 Expect.equals(h.C?.toString(), (h.C).toString()); /// 13: ok | 60 Expect.equals(h.C?.toString(), (h.C).toString()); /// 13: ok |
| 60 } | 61 } |
| OLD | NEW |