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

Unified Diff: tests/language_strong/generic_methods_overriding_test.dart

Issue 2737933002: Add tests for generic methods in strong mode (Closed)
Patch Set: Fixes according to the received comments Created 3 years, 9 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_strong/generic_methods_overriding_test.dart
diff --git a/tests/language_strong/generic_methods_overriding_test.dart b/tests/language_strong/generic_methods_overriding_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2de07fc2576d3628043e40e6acdb545afb1850eb
--- /dev/null
+++ b/tests/language_strong/generic_methods_overriding_test.dart
@@ -0,0 +1,62 @@
+// Copyright (c) 2017, 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.
+
+// Test that generic methods can be overloaded (a) with widened type bounds, and
+// (b) using the bound as the type of the parameter in the overloaded method.
+
+library generic_methods_overriding_test;
+
+import "package:expect/expect.dart";
+
+class X {}
+
+class Y extends X {}
+
+class Z extends Y {}
+
+class C {
+ String fun<T extends Y>(T t) => "C";
+}
+
+class D extends C {
+ String fun<T extends X>(T t) => "D"; /// 01: compile-time error
+ String fun<T extends Y>(T t) => "D"; /// 02: ok
+}
+
+class E extends C {
+ String fun<T>(Y y) => "E"; /// 03: compile-time error
+ String fun<T extends Y>(Y y) => "E"; /// 04: ok
+}
+
+class F extends C {
+ String foobar(Z z) {
+ return "FZ";
+ }
+
+ String fun<T extends Y>(T t) {
+ if (t is Z) {
+ return this.foobar(t as Z); /// 05: ok
+ return this.foobar(t); /// 06: ok
+ }
+ return "FY";
+ }
+}
+
+main() {
+ Y y = new Y();
+ Z z = new Z();
+
+ C c = new C();
+ D d = new D();
+ E e = new E();
+ F f = new F();
+
+ Expect.equals(c.fun<Y>(y), "C");
+ Expect.equals(d.fun<Y>(y), "D"); /// 02: continued
+ Expect.equals(e.fun<Y>(y), "E"); /// 04: continued
+ Expect.equals(f.fun<Y>(y), "FY"); /// 05: continued
+ Expect.equals(f.fun<Z>(z), "FZ"); /// 05: continued
+ Expect.equals(f.fun<Y>(y), "FY"); /// 06: continued
+ Expect.equals(f.fun<Z>(z), "FZ"); /// 06: continued
+}

Powered by Google App Engine
This is Rietveld 408576698