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

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

Issue 3008573002: Migrate test block 113 to Dart 2.0. (Closed)
Patch Set: Merge fixups Created 3 years, 3 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 i<T extends Iterable<T>>() => null;
19 j<T extends Iterable<S>, S extends T>() => null;
20 Expect.throws(
21 () => (i as dynamic)(), (e) => '$e'.contains('Instantiate to bounds'));
22 Expect.throws(
23 () => (j as dynamic)(), (e) => '$e'.contains('Instantiate to bounds'));
24 }
25
26 void testChecksBound() {
27 f<T extends num>(T x) => x;
28 Expect.equals((f as dynamic)(42), 42);
29 Expect.throws(() => (f as dynamic)('42'));
30
31 msg(t1, t2, tf) => Expect.throws(() => (f as dynamic)<Object>(42),
32 (e) => '$e' == 'type `Object` does not extend `num` of `T');
33
34 g<T extends U, U extends num>(T x, U y) => x;
35 Expect.equals((g as dynamic)(42.0, 100), 42.0);
36 Expect.throws(() => (g as dynamic)('hi', 100));
37 Expect.throws(() => (g as dynamic)<double, int>(42.0, 100),
38 (e) => '$e' == 'type `double` does not extend `int` of `T`.');
39
40 Expect.throws(() => (g as dynamic)<num, Object>(42.0, 100),
41 (e) => '$e' == 'type `Object` does not extend `num` of `U`.');
42 }
43
44 typedef G<U> = T Function<T extends U>(T x);
45
46 void testSubtype() {
47 f<T extends num>(T x) => x + 2;
48
49 dynamic d = f;
50 Expect.equals(d(40.0), 42.0);
51 Expect.equals((f as G<int>)(40), 42);
52 Expect.equals((d as G<int>)(40), 42);
53 Expect.equals((f as G<double>)(40.0), 42.0);
54 Expect.equals((d as G<double>)(40.0), 42.0);
55
56 d as G<Null>;
57 Expect.throws(() => d as G);
58 Expect.throws(() => d as G<Object>);
59 Expect.throws(() => d as G<String>);
60 }
61
62 void testToString() {
63 // TODO(jmesserly): I don't think the cast on `y` should be required.
64 num f<T extends num, U extends T>(T x, U y) => min(x, y as num);
65 num g<T, U>(T x, U y) => max(x as num, y as num);
66 String h<T, U>(T x, U y) => h.runtimeType.toString();
67 Expect.equals(
68 f.runtimeType.toString(), '<T extends num, U extends T>(T, U) -> num');
69 Expect.equals(g.runtimeType.toString(), '<T, U>(T, U) -> num');
70 Expect.equals(h(42, 123.0), '<T, U>(T, U) -> String');
71 }
72
73 main() {
74 testInstantiateToBounds();
75 testToString();
76 testChecksBound();
77 testSubtype();
78 }
OLDNEW
« no previous file with comments | « tests/language_strong/generic_field_mixin_test.dart ('k') | tests/language_strong/generic_function_dcall_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698