OLD | NEW |
| (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(() => (i as dynamic)(), | |
21 (e) => '$e'.contains('Instantiate to bounds')); | |
22 Expect.throws(() => (j as dynamic)(), | |
23 (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 g<T extends U, U extends num>(T x, U y) => x; | |
32 Expect.equals((g as dynamic)(42.0, 100), 42.0); | |
33 Expect.throws(() => (g as dynamic)('hi', 100)); | |
34 } | |
35 | |
36 typedef G<U> = T Function<T extends U>(T x); | |
37 | |
38 void testSubtype() { | |
39 f<T extends num>(T x) => x + 2; | |
40 | |
41 dynamic d = f; | |
42 Expect.equals(d(40.0), 42.0); | |
43 Expect.equals((f as G<int>)(40), 42); | |
44 Expect.equals((d as G<int>)(40), 42); | |
45 Expect.equals((f as G<double>)(40.0), 42.0); | |
46 Expect.equals((d as G<double>)(40.0), 42.0); | |
47 | |
48 d as G<Null>; | |
49 Expect.throws(() => d as G); | |
50 Expect.throws(() => d as G<Object>); | |
51 Expect.throws(() => d as G<String>); | |
52 } | |
53 | |
54 void testToString() { | |
55 // TODO(jmesserly): I don't think the cast on `y` should be required. | |
56 num f<T extends num, U extends T>(T x, U y) => min(x, y as num); | |
57 num g<T, U>(T x, U y) => max(x as num, y as num); | |
58 String h<T, U>(T x, U y) => h.runtimeType.toString(); | |
59 Expect.equals(f.runtimeType.toString(), | |
60 '<T extends num, U extends T>(T, U) -> num'); | |
61 Expect.equals(g.runtimeType.toString(), '<T, U>(T, U) -> num'); | |
62 Expect.equals(h(42, 123.0), '<T, U>(T, U) -> String'); | |
63 } | |
64 | |
65 main() { | |
66 testInstantiateToBounds(); | |
67 testToString(); | |
68 testChecksBound(); | |
69 testSubtype(); | |
70 } | |
OLD | NEW |