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

Side by Side Diff: tests/language/conditional_property_access_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 does not appear on the LHS of an
6 // assignment.
7
8 // SharedOptions=--enable-null-aware-operators
9
10 import "package:expect/expect.dart";
11 import "conditional_access_helper.dart" as h;
12
13 noMethod(e) => e is NoSuchMethodError;
14
15 class B {}
16
17 class C extends B {
18 int v;
19 C(this.v);
20 static var staticField;
21 }
22
23 C nullC() => null;
24
25 main() {
26 // Make sure the "none" test fails if property access using "?." is not
27 // implemented. This makes status files easier to maintain.
28 nullC()?.v;
29
30 // e1?.id is equivalent to ((x) => x == null ? null : x.id)(e1).
31 Expect.equals(null, nullC()?.v); /// 01: ok
32 Expect.equals(1, new C(1)?.v); /// 02: ok
33
34 // The static type of e1?.d is the static type of e1.id.
35 { int i = new C(1)?.v; Expect.equals(1, i); } /// 03: ok
36 { String s = new C(null)?.v; Expect.equals(null, s); } /// 04: static type war ning
37
38 // Let T be the static type of e1 and let y be a fresh variable of type T.
39 // Exactly the same static warnings that would be caused by y.id are also
40 // generated in the case of e1?.id.
41 Expect.equals(null, nullC()?.bad); /// 05: static type warning
42 { B b = new C(1); Expect.equals(1, b?.v); } /// 06: static type warning
43
44 // Consequently, '?.' cannot be used to access static properties of classes.
45 Expect.throws(() => C?.staticField, noMethod); /// 07: static type warning
46 Expect.throws(() => h.C?.staticField, noMethod); /// 08: static type warning
47
48 // Nor can it be used to access toplevel properties in libraries imported via
49 // prefix.
50 Expect.throws(() => h?.topLevelVar, noMethod); /// 09: static type warning
51
52 // However, '?.' can be used to access the hashCode getter on the class Type.
53 Expect.equals(C?.hashCode, (C).hashCode); /// 10: ok
54 Expect.equals(h.C?.hashCode, (h.C).hashCode); /// 11: ok
55 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698