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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: tests/language/conditional_method_invocation_test.dart
diff --git a/tests/language/conditional_method_invocation_test.dart b/tests/language/conditional_method_invocation_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..e194ab92b37298433f13521364775da0c34af16d
--- /dev/null
+++ b/tests/language/conditional_method_invocation_test.dart
@@ -0,0 +1,60 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// Verify semantics of the ?. operator when it is used to invoke a method.
+
+// SharedOptions=--enable-null-aware-operators
+
+import "package:expect/expect.dart";
+import "conditional_access_helper.dart" as h;
+
+bad() {
+ Expect.fail('Should not be executed');
+}
+
+noMethod(e) => e is NoSuchMethodError;
+
+class B {}
+
+class C extends B {
+ f(callback()) => callback();
+ int g(int callback()) => callback();
+}
+
+C nullC() => null;
+
+main() {
+ // Make sure the "none" test fails if method invocation using "?." is not
+ // implemented. This makes status files easier to maintain.
+ nullC()?.f(null);
+
+ // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o).
+ Expect.equals(null, nullC()?.f(bad())); /// 01: ok
+ Expect.equals(1, new C()?.f(() => 1)); /// 02: ok
+
+ // The static type of o?.m(...) is the same as the static type of
+ // o.m(...).
+ { int i = nullC()?.g(bad()); Expect.equals(null, i); } /// 03: ok
+ { int i = new C()?.g(() => 1); Expect.equals(1, i); } /// 04: ok
+ { String s = nullC()?.g(bad()); Expect.equals(null, s); } /// 05: static type warning
+ { String s = new C()?.g(() => null); Expect.equals(null, s); } /// 06: static type warning
+
+ // Let T be the static type of o and let y be a fresh variable of type T.
+ // Exactly the same static warnings that would be caused by y.m(...) are also
+ // generated in the case of o?.m(...).
+ { B b = new C(); Expect.equals(1, b?.f(() => 1)); } /// 07: static type warning
+ { int i = 1; Expect.equals(null, nullC()?.f(i)); } /// 08: static type warning
+
+ // Consequently, '?.' cannot be used to invoke static methods of classes.
+ Expect.throws(() => C?.staticMethod(), noMethod); /// 09: static type warning
+ Expect.throws(() => h.C?.staticMethod(), noMethod); /// 10: static type warning
+
+ // Nor can it be used to access toplevel functions in libraries imported via
+ // prefix.
+ Expect.throws(() => h?.topLevelFunction(), noMethod); /// 11: static type warning
+
+ // However, '?.' can be used to access the toString method on the class Type.
+ Expect.equals(C?.toString(), (C).toString()); /// 12: ok
+ Expect.equals(h.C?.toString(), (h.C).toString()); /// 13: ok
+}

Powered by Google App Engine
This is Rietveld 408576698