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

Unified Diff: tests/language_strong/generic_methods_local_variable_declaration_test.dart

Issue 2737933002: Add tests for generic methods in strong mode (Closed)
Patch Set: 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_local_variable_declaration_test.dart
diff --git a/tests/language_strong/generic_methods_local_variable_declaration_test.dart b/tests/language_strong/generic_methods_local_variable_declaration_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..00805b73c16603dbd03b8d62d12275dd0e401a43
--- /dev/null
+++ b/tests/language_strong/generic_methods_local_variable_declaration_test.dart
@@ -0,0 +1,49 @@
+// 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.
+
+library generic_methods_local_variable_declaration_test;
+
+import "package:expect/expect.dart";
+
+abstract class Generator<T> {
+ T generate();
+}
+
+class A implements Generator<A> {
+ @override
karlklose 2017/03/09 09:17:00 Consider leaving out '@override'.
floitsch 2017/03/09 11:27:17 Also note that not writing any type here would inf
Dmitry Stefantsov 2017/03/10 13:21:54 Thanks!
Dmitry Stefantsov 2017/03/10 13:21:54 Done.
+ A generate() {
+ return new A();
+ }
+
+ @override
+ String toString() => "instance of A";
+}
+
+class B implements Generator<B> {
+ @override
+ B generate() {
+ return new B();
+ }
+
+ @override
+ String toString() => "instance of B";
+}
+
+String fun<T extends Generator<T>>(T t) {
+ T another = t.generate();
+ String anotherName = "$another";
+
+ Expect.isTrue(another is T);
+ Expect.isTrue(anotherName is! T); // fails if T is substituted with dynamic
floitsch 2017/03/09 11:27:17 Start comments with uppercase and finish them with
eernst 2017/03/09 14:55:02 This is again a check that seems to be justified s
Dmitry Stefantsov 2017/03/10 13:21:54 That's right. It's Dart 1 specific, so I'm removin
Dmitry Stefantsov 2017/03/10 13:21:54 Done.
+
+ return anotherName;
+}
+
+main() {
+ A a = new A();
+ B b = new B();
+
+ Expect.isTrue(fun<A>(a) == "instance of A");
+ Expect.isTrue(fun<B>(b) == "instance of B");
+}

Powered by Google App Engine
This is Rietveld 408576698