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

Unified Diff: tests/language_strong/generic_typedef_test.dart

Issue 2832913003: fix #27971, implement generic function RTTI (Closed)
Patch Set: Created 3 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_strong/generic_typedef_test.dart
diff --git a/tests/language_strong/generic_typedef_test.dart b/tests/language_strong/generic_typedef_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..fb7fd0cbdf3a344fd85f5fa4fcfec445737db2e0
--- /dev/null
+++ b/tests/language_strong/generic_typedef_test.dart
@@ -0,0 +1,64 @@
+// 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.
+
+import 'package:expect/expect.dart';
+
+
+// Test runtime behavior of generic function typedefs:
+//
+// - use "is" and "as" on them.
+// - get Type values from runtimeType.
+// - pass type parameters from another generic type to them.
+
+typedef A<T> = T Function(T x, T y);
+typedef B = T Function<T>(T x, T y);
+
+typedef C<K> = Map<K, V> Function<V>(K k, V v);
+typedef D = Map<String, V> Function<V>(String k, V v);
+
+expectCastIf(fn(), bool expectSuccess) =>
+ expectSuccess ? fn() : Expect.throws(fn);
+
+class G<Y, Z> {
+ test() {
+ dynamic d = (Y x, Y y) => y;
+ Expect.isTrue(d is A<Y>);
+ expectCastIf(() => d as A<Z>, Y == Z);
+
+ Expect.isFalse(d is B);
+ Expect.throws(() => d as B);
+
+ d = (Y y, Z z) => <Y, Z>{};
+ Expect.isFalse(d is C<Y>);
+
+ d = <S>(Y y, S s) => <Y, S>{};
+ Expect.isTrue(d is C<Y>);
+ expectCastIf(() => d as C<Z>, Y == Z);
+ expectCastIf(() => d as D, Y == String);
+ }
+}
+
+main() {
+ dynamic d = (int x, int y) => x + y;
+ Expect.isTrue(d is A<int>);
+ Expect.equals((d as A<int>)(1, 2), 3);
+
+ Expect.isFalse(d is B);
+ Expect.throws(() => d as B);
+
+ d = <S>(S x, S y) => x is String ? x : y;
+ Expect.isFalse(d is A);
+ Expect.throws(() => d as A);
+
+ Expect.isTrue(d is B);
+ // TODO(jmesserly): Analyzer incorrectly rejects this form:
+ // Expect.equals((d as B)<int>(1, 2), 2);
+ B b = d;
+ Expect.equals(b<int>(1, 2), 2);
+ Expect.equals(b<String>('a', 'b'), 'a');
+
+
+ new G<int, String>().test();
+ new G<String, String>().test();
+}

Powered by Google App Engine
This is Rietveld 408576698