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

Side by Side Diff: tests/language/conditional_method_invocation_test.dart

Issue 1062723002: Implement the new '?.' operator in analyzer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Bump analyzer version. Created 5 years, 8 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Verify semantics of the ?. operator when it is used to invoke a method.
6
7 // SharedOptions=--enable-null-aware-operators
8
9 import "package:expect/expect.dart";
10 import "conditional_access_helper.dart" as h;
11
12 bad() {
13 Expect.fail('Should not be executed');
14 }
15
16 noMethod(e) => e is NoSuchMethodError;
17
18 class B {}
19
20 class C extends B {
21 f(callback()) => callback();
22 int g(int callback()) => callback();
23 }
24
25 C nullC() => null;
26
27 main() {
28 // Make sure the "none" test fails if method invocation using "?." is not
29 // implemented. This makes status files easier to maintain.
30 nullC()?.f(null);
31
32 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o).
33 Expect.equals(null, nullC()?.f(bad())); /// 01: ok
34 Expect.equals(1, new C()?.f(() => 1)); /// 02: ok
35
36 // The static type of o?.m(...) is the same as the static type of
37 // o.m(...).
38 { int i = nullC()?.g(bad()); Expect.equals(null, i); } /// 03: ok
39 { int i = new C()?.g(() => 1); Expect.equals(1, i); } /// 04: ok
40 { String s = nullC()?.g(bad()); Expect.equals(null, s); } /// 05: static type warning
41 { String s = new C()?.g(() => null); Expect.equals(null, s); } /// 06: static type warning
42
43 // Let T be the static type of o and let y be a fresh variable of type T.
44 // Exactly the same static warnings that would be caused by y.m(...) are also
45 // generated in the case of o?.m(...).
46 { B b = new C(); Expect.equals(1, b?.f(() => 1)); } /// 07: static type warnin g
47 { int i = 1; Expect.equals(null, nullC()?.f(i)); } /// 08: static type warning
48
49 // Consequently, '?.' cannot be used to invoke static methods of classes.
50 Expect.throws(() => C?.staticMethod(), noMethod); /// 09: static type warning
51 Expect.throws(() => h.C?.staticMethod(), noMethod); /// 10: static type warnin g
52
53 // Nor can it be used to access toplevel functions in libraries imported via
54 // prefix.
55 Expect.throws(() => h?.topLevelFunction(), noMethod); /// 11: static type warn ing
56
57 // 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(h.C?.toString(), (h.C).toString()); /// 13: ok
60 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698