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

Unified Diff: tests/language_strong/generic_methods_dynamic_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_dynamic_test.dart
diff --git a/tests/language_strong/generic_methods_dynamic_test.dart b/tests/language_strong/generic_methods_dynamic_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2da2b656e1a9e9c1cb01d865e9ed978fd282efdf
--- /dev/null
+++ b/tests/language_strong/generic_methods_dynamic_test.dart
@@ -0,0 +1,44 @@
+// 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 if the type of a parameter of a generic method is a type parameter,
+// the type of the passed argument is checked (01) at compile time
+// if the receiver is given via an interface-type variable, and (02) at runtime
+// if the receiver is dynamic.
+
+library generic_methods_dynamic_test;
+
+import "package:expect/expect.dart";
+
+class A {}
+
+class B {}
+
+class C {
+ T foo<T>(T t) => t;
+ List<T> bar<T>(Iterable<T> t) => <T>[t.first];
+}
+
+main() {
+ B b = new B();
+ C c = new C();
+ dynamic obj = c;
+
+ c.foo<A>(b); /// 01: compile-time error
+ obj.foo<A>(b); /// 02: runtime error
+
+ c.bar<A>(<B>[new B()]); /// 03: compile-time error
+ obj.bar<A>(<B>[new B()]); /// 04: runtime error
+
+ Expect.equals(c.foo<B>(b), b); /// 05: ok
+ Expect.equals(obj.foo<B>(b), b); /// 05: continued
+
+ dynamic x = c.bar<B>(<B>[new B()]); /// 05: continued
+ Expect.isTrue(x is List<B>); /// 05: continued
+ Expect.equals(x.length, 1); /// 05: continued
+
+ dynamic y = obj.bar<B>(<B>[new B()]); /// 05: continued
+ Expect.isTrue(y is List<B>); /// 05: continued
+ Expect.equals(y.length, 1); /// 05: continued
+}

Powered by Google App Engine
This is Rietveld 408576698