| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 // VMOptions=--enable_type_checks | |
| 5 // | |
| 6 // Dart test for function type alias. | |
| 7 | |
| 8 typedef Fun(a, b); | |
| 9 | |
| 10 typedef int IntFun(a, b); | |
| 11 | |
| 12 typedef bool BoolFun(a, b); | |
| 13 | |
| 14 typedef int CompareObj(Object a, Object b); | |
| 15 | |
| 16 typedef int CompareInt(int a, int b); | |
| 17 | |
| 18 typedef int CompareString(String a, String b, [bool swap]); | |
| 19 | |
| 20 typedef void Test(); | |
| 21 | |
| 22 typedef ParameterizedFun1<T, U extends bool, V>(T t, U u); | |
| 23 | |
| 24 typedef List<T> ParameterizedFun2<T, U, V extends Map<T, int>>( | |
| 25 Map<T, int> t, U u); | |
| 26 | |
| 27 typedef void BoundsCheck<T extends num>(T arg); | |
| 28 | |
| 29 class FunctionTypeAliasTest { | |
| 30 FunctionTypeAliasTest() {} | |
| 31 static int test(CompareObj compare, Object a, Object b) { | |
| 32 return compare(a, b); | |
| 33 } | |
| 34 foo(Test arg) {} | |
| 35 static bar() { | |
| 36 FunctionTypeAliasTest a = new FunctionTypeAliasTest(); | |
| 37 a.foo(() { }); | |
| 38 return 0; | |
| 39 } | |
| 40 | |
| 41 static void testMain() { | |
| 42 int compareStrLen(String a, String b) { return a.length - b.length; } | |
| 43 Expect.isTrue(compareStrLen is Fun); | |
| 44 Expect.isTrue(compareStrLen is IntFun); | |
| 45 Expect.isTrue(compareStrLen is !BoolFun); | |
| 46 Expect.isTrue(compareStrLen is CompareObj); | |
| 47 Expect.isTrue(compareStrLen is !CompareInt); | |
| 48 Expect.isTrue(compareStrLen is !CompareString); | |
| 49 Expect.equals(3, test(compareStrLen, "abcdef", "xyz")); | |
| 50 | |
| 51 int compareStrLenSwap(String a, String b, [bool swap = false]) { | |
| 52 return swap ? (a.length - b.length) : (b.length - a.length); | |
| 53 } | |
| 54 Expect.isTrue(compareStrLenSwap is Fun); | |
| 55 Expect.isTrue(compareStrLenSwap is IntFun); | |
| 56 Expect.isTrue(compareStrLenSwap is !BoolFun); | |
| 57 Expect.isTrue(compareStrLenSwap is CompareObj); | |
| 58 Expect.isTrue(compareStrLenSwap is !CompareInt); | |
| 59 Expect.isTrue(compareStrLenSwap is CompareString); | |
| 60 | |
| 61 int compareStrLenReverse(String a, String b, [bool reverse = false]) { | |
| 62 return reverse ? (a.length - b.length) : (b.length - a.length); | |
| 63 } | |
| 64 Expect.isTrue(compareStrLenReverse is Fun); | |
| 65 Expect.isTrue(compareStrLenReverse is IntFun); | |
| 66 Expect.isTrue(compareStrLenReverse is !BoolFun); | |
| 67 Expect.isTrue(compareStrLenReverse is CompareObj); | |
| 68 Expect.isTrue(compareStrLenReverse is !CompareInt); | |
| 69 Expect.isTrue(compareStrLenReverse is !CompareString); | |
| 70 | |
| 71 int compareObj(Object a, Object b) { return a === b ? 0 : -1; } | |
| 72 Expect.isTrue(compareObj is Fun); | |
| 73 Expect.isTrue(compareObj is IntFun); | |
| 74 Expect.isTrue(compareObj is !BoolFun); | |
| 75 Expect.isTrue(compareObj is CompareObj); | |
| 76 Expect.isTrue(compareObj is CompareInt); | |
| 77 Expect.isTrue(compareObj is !CompareString); | |
| 78 Expect.equals(-1, test(compareObj, "abcdef", "xyz")); | |
| 79 | |
| 80 CompareInt minus = int _(int a, int b) { return a - b; }; | |
| 81 Expect.isTrue(minus is Fun); | |
| 82 Expect.isTrue(compareStrLen is IntFun); | |
| 83 Expect.isTrue(compareStrLen is !BoolFun); | |
| 84 Expect.isTrue(minus is CompareObj); | |
| 85 Expect.isTrue(minus is CompareInt); | |
| 86 Expect.isTrue(minus is !CompareString); | |
| 87 Expect.equals(99, test(minus, 100, 1)); | |
| 88 | |
| 89 int plus (int a, [int b = 1]) { return a + b; }; | |
| 90 Expect.isTrue(plus is !Fun); | |
| 91 Expect.isTrue(plus is !IntFun); | |
| 92 Expect.isTrue(plus is !BoolFun); | |
| 93 Expect.isTrue(plus is !CompareObj); | |
| 94 Expect.isTrue(plus is !CompareInt); | |
| 95 Expect.isTrue(plus is !CompareString); | |
| 96 | |
| 97 Expect.equals(0, bar()); | |
| 98 | |
| 99 Function boundsTrue = void _(int arg) { }; | |
| 100 Function boundsFalse = void _(String arg) { }; | |
| 101 Expect.isTrue(boundsTrue is BoundsCheck<num>); | |
| 102 Expect.isFalse(boundsFalse is BoundsCheck<num>); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 main() { | |
| 107 FunctionTypeAliasTest.testMain(); | |
| 108 } | |
| OLD | NEW |