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

Side by Side Diff: tests/language_strong/generic_function_bounds_test.dart

Issue 2867493002: fix #27256, track type bounds for generic functions (Closed)
Patch Set: rebase Created 3 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:math';
6 import 'package:expect/expect.dart';
7
8 void testInstantiateToBounds() {
9 f<T extends num, U extends T>() => [T, U];
10 g<T extends List<U>, U extends int>() => [T, U];
11 h<T extends num, U extends T>(T x, U y) => h.runtimeType.toString();
12
13 Expect.listEquals((f as dynamic)(), [num, num]);
14 Expect.equals((g as dynamic)().join('|'), 'List<int>|int');
15 Expect.equals((h as dynamic)(null, null),
16 '<T extends num, U extends T>(T, U) -> String');
17 }
18
19 void testChecksBound() {
20 f<T extends num>(T x) => x;
21 Expect.equals((f as dynamic)(42), 42);
22 Expect.throws(() => (f as dynamic)('42'));
23
24 g<T extends U, U extends num>(T x, U y) => x;
25 Expect.equals((g as dynamic)(42.0, 100), 42.0);
26 Expect.throws(() => (g as dynamic)('hi', 100));
27 }
28
29 typedef G<U> = T Function<T extends U>(T x);
30
31 void testSubtype() {
32 f<T extends num>(T x) => x + 2;
33
34 dynamic d = f;
35 Expect.equals(d(40.0), 42.0);
36 Expect.equals((f as G<int>)(40), 42);
37 Expect.equals((d as G<int>)(40), 42);
38 Expect.equals((f as G<double>)(40.0), 42.0);
39 Expect.equals((d as G<double>)(40.0), 42.0);
40
41 d as G<Null>;
42 Expect.throws(() => d as G);
43 Expect.throws(() => d as G<Object>);
44 Expect.throws(() => d as G<String>);
45 }
46
47 void testToString() {
48 // TODO(jmesserly): I don't think the cast on `y` should be required.
49 num f<T extends num, U extends T>(T x, U y) => min(x, y as num);
50 num g<T, U>(T x, U y) => max(x as num, y as num);
51 String h<T, U>(T x, U y) => h.runtimeType.toString();
52 Expect.equals(f.runtimeType.toString(), '<T extends num, U extends T>(T, U) -> num');
53 Expect.equals(g.runtimeType.toString(), '<T, U>(T, U) -> num');
54 Expect.equals(h(42, 123.0), '<T, U>(T, U) -> String');
55 }
56
57 main() {
58 testInstantiateToBounds();
59 testToString();
60 testChecksBound();
61 testSubtype();
62 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698