| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.generic_function_typedef; | 5 library test.generic_function_typedef; |
| 6 | 6 |
| 7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
| 8 | 8 |
| 9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 10 | 10 |
| 11 import 'generics_helper.dart'; | 11 import 'generics_helper.dart'; |
| 12 | 12 |
| 13 typedef bool NonGenericPredicate(num n); | 13 typedef bool NonGenericPredicate(num n); |
| 14 typedef bool GenericPredicate<T>(T t); | 14 typedef bool GenericPredicate<T>(T t); |
| 15 typedef S GenericTransform<S>(S s); | 15 typedef S GenericTransform<S>(S s); |
| 16 | 16 |
| 17 class C<R> { | 17 class C<R> { |
| 18 GenericPredicate<num> predicateOfNum; | 18 GenericPredicate<num> predicateOfNum; |
| 19 GenericTransform<String> transformOfString; | 19 GenericTransform<String> transformOfString; |
| 20 GenericTransform<R> transformOfR; | 20 GenericTransform<R> transformOfR; |
| 21 } | 21 } |
| 22 | 22 |
| 23 reflectTypeDeclaration(t) => reflectType(t).originalDeclaration; | 23 reflectTypeDeclaration(t) => reflectType(t).originalDeclaration; |
| 24 | 24 |
| 25 main() { | 25 main() { |
| 26 TypeMirror dynamicMirror = currentMirrorSystem().dynamicType; | 26 TypeMirror dynamicMirror = currentMirrorSystem().dynamicType; |
| 27 | 27 |
| 28 TypedefMirror predicateOfNum = (reflectClass(C).declarations[#predicateOfNum]
as VariableMirror).type; | 28 TypedefMirror predicateOfNum = |
| 29 TypedefMirror transformOfString = (reflectClass(C).declarations[#transformOfSt
ring] as VariableMirror).type; | 29 (reflectClass(C).declarations[#predicateOfNum] as VariableMirror).type; |
| 30 TypedefMirror transformOfR = (reflectClass(C).declarations[#transformOfR] as V
ariableMirror).type; | 30 TypedefMirror transformOfString = |
| 31 TypedefMirror transformOfDouble = (reflect(new C<double>()).type.declarations[
#transformOfR] as VariableMirror).type; | 31 (reflectClass(C).declarations[#transformOfString] as VariableMirror).type; |
| 32 TypedefMirror transformOfR = |
| 33 (reflectClass(C).declarations[#transformOfR] as VariableMirror).type; |
| 34 TypedefMirror transformOfDouble = (reflect(new C<double>()) |
| 35 .type |
| 36 .declarations[#transformOfR] as VariableMirror) |
| 37 .type; |
| 32 | 38 |
| 33 TypeVariableMirror tFromGenericPredicate = reflectTypeDeclaration(GenericPredi
cate).typeVariables[0]; | 39 TypeVariableMirror tFromGenericPredicate = |
| 34 TypeVariableMirror sFromGenericTransform = reflectTypeDeclaration(GenericTrans
form).typeVariables[0]; | 40 reflectTypeDeclaration(GenericPredicate).typeVariables[0]; |
| 41 TypeVariableMirror sFromGenericTransform = |
| 42 reflectTypeDeclaration(GenericTransform).typeVariables[0]; |
| 35 TypeVariableMirror rFromC = reflectClass(C).typeVariables[0]; | 43 TypeVariableMirror rFromC = reflectClass(C).typeVariables[0]; |
| 36 | 44 |
| 37 // Typedefs. | 45 // Typedefs. |
| 38 typeParameters(reflectTypeDeclaration(NonGenericPredicate), []); | 46 typeParameters(reflectTypeDeclaration(NonGenericPredicate), []); |
| 39 typeParameters(reflectTypeDeclaration(GenericPredicate), [#T]); | 47 typeParameters(reflectTypeDeclaration(GenericPredicate), [#T]); |
| 40 typeParameters(reflectTypeDeclaration(GenericTransform), [#S]); | 48 typeParameters(reflectTypeDeclaration(GenericTransform), [#S]); |
| 41 typeParameters(predicateOfNum, [#T]); | 49 typeParameters(predicateOfNum, [#T]); |
| 42 typeParameters(transformOfString, [#S]); | 50 typeParameters(transformOfString, [#S]); |
| 43 typeParameters(transformOfR, [#S]); | 51 typeParameters(transformOfR, [#S]); |
| 44 typeParameters(transformOfDouble, [#S]); | 52 typeParameters(transformOfDouble, [#S]); |
| 45 | 53 |
| 46 typeArguments(reflectTypeDeclaration(NonGenericPredicate), []); | 54 typeArguments(reflectTypeDeclaration(NonGenericPredicate), []); |
| 47 typeArguments(reflectTypeDeclaration(GenericPredicate), []); | 55 typeArguments(reflectTypeDeclaration(GenericPredicate), []); |
| 48 typeArguments(reflectTypeDeclaration(GenericTransform), []); | 56 typeArguments(reflectTypeDeclaration(GenericTransform), []); |
| 49 typeArguments(predicateOfNum, [reflectClass(num)]); | 57 typeArguments(predicateOfNum, [reflectClass(num)]); |
| 50 typeArguments(transformOfString, [reflectClass(String)]); | 58 typeArguments(transformOfString, [reflectClass(String)]); |
| 51 typeArguments(transformOfR, [rFromC]); | 59 typeArguments(transformOfR, [rFromC]); |
| 52 typeArguments(transformOfDouble, [reflectClass(double)]); | 60 typeArguments(transformOfDouble, [reflectClass(double)]); |
| 53 | 61 |
| 54 Expect.isTrue(reflectTypeDeclaration(NonGenericPredicate).isOriginalDeclaratio
n); | 62 Expect.isTrue( |
| 63 reflectTypeDeclaration(NonGenericPredicate).isOriginalDeclaration); |
| 55 Expect.isTrue(reflectTypeDeclaration(GenericPredicate).isOriginalDeclaration); | 64 Expect.isTrue(reflectTypeDeclaration(GenericPredicate).isOriginalDeclaration); |
| 56 Expect.isTrue(reflectTypeDeclaration(GenericTransform).isOriginalDeclaration);
| 65 Expect.isTrue(reflectTypeDeclaration(GenericTransform).isOriginalDeclaration); |
| 57 Expect.isFalse(predicateOfNum.isOriginalDeclaration); | 66 Expect.isFalse(predicateOfNum.isOriginalDeclaration); |
| 58 Expect.isFalse(transformOfString.isOriginalDeclaration); | 67 Expect.isFalse(transformOfString.isOriginalDeclaration); |
| 59 Expect.isFalse(transformOfR.isOriginalDeclaration); | 68 Expect.isFalse(transformOfR.isOriginalDeclaration); |
| 60 Expect.isFalse(transformOfDouble.isOriginalDeclaration); | 69 Expect.isFalse(transformOfDouble.isOriginalDeclaration); |
| 61 | 70 |
| 62 // Function types. | 71 // Function types. |
| 63 typeParameters(reflectTypeDeclaration(NonGenericPredicate).referent, []); | 72 typeParameters(reflectTypeDeclaration(NonGenericPredicate).referent, []); |
| 64 typeParameters(reflectTypeDeclaration(GenericPredicate).referent, []); | 73 typeParameters(reflectTypeDeclaration(GenericPredicate).referent, []); |
| 65 typeParameters(reflectTypeDeclaration(GenericTransform).referent, []); | 74 typeParameters(reflectTypeDeclaration(GenericTransform).referent, []); |
| 66 typeParameters(predicateOfNum.referent, []); | 75 typeParameters(predicateOfNum.referent, []); |
| 67 typeParameters(transformOfString.referent, []); | 76 typeParameters(transformOfString.referent, []); |
| 68 typeParameters(transformOfR.referent, []); | 77 typeParameters(transformOfR.referent, []); |
| 69 typeParameters(transformOfDouble.referent, []); | 78 typeParameters(transformOfDouble.referent, []); |
| 70 | 79 |
| 71 typeArguments(reflectTypeDeclaration(NonGenericPredicate).referent, []); | 80 typeArguments(reflectTypeDeclaration(NonGenericPredicate).referent, []); |
| 72 typeArguments(reflectTypeDeclaration(GenericPredicate).referent, []); | 81 typeArguments(reflectTypeDeclaration(GenericPredicate).referent, []); |
| 73 typeArguments(reflectTypeDeclaration(GenericTransform).referent, []); | 82 typeArguments(reflectTypeDeclaration(GenericTransform).referent, []); |
| 74 typeArguments(predicateOfNum.referent, []); | 83 typeArguments(predicateOfNum.referent, []); |
| 75 typeArguments(transformOfString.referent, []); | 84 typeArguments(transformOfString.referent, []); |
| 76 typeArguments(transformOfR.referent, []); | 85 typeArguments(transformOfR.referent, []); |
| 77 typeArguments(transformOfDouble.referent, []); | 86 typeArguments(transformOfDouble.referent, []); |
| 78 | 87 |
| 79 // Function types are always non-generic. Only the typedef is generic. | 88 // Function types are always non-generic. Only the typedef is generic. |
| 80 Expect.isTrue(reflectTypeDeclaration(NonGenericPredicate).referent.isOriginalD
eclaration); | 89 Expect.isTrue(reflectTypeDeclaration(NonGenericPredicate) |
| 81 Expect.isTrue(reflectTypeDeclaration(GenericPredicate).referent.isOriginalDecl
aration); | 90 .referent |
| 82 Expect.isTrue(reflectTypeDeclaration(GenericTransform).referent.isOriginalDecl
aration); | 91 .isOriginalDeclaration); |
| 83 Expect.isTrue(predicateOfNum.referent.isOriginalDeclaration); | 92 Expect.isTrue( |
| 84 Expect.isTrue(transformOfString.referent.isOriginalDeclaration); | 93 reflectTypeDeclaration(GenericPredicate).referent.isOriginalDeclaration); |
| 94 Expect.isTrue( |
| 95 reflectTypeDeclaration(GenericTransform).referent.isOriginalDeclaration); |
| 96 Expect.isTrue(predicateOfNum.referent.isOriginalDeclaration); |
| 97 Expect.isTrue(transformOfString.referent.isOriginalDeclaration); |
| 85 Expect.isTrue(transformOfR.referent.isOriginalDeclaration); | 98 Expect.isTrue(transformOfR.referent.isOriginalDeclaration); |
| 86 Expect.isTrue(transformOfDouble.referent.isOriginalDeclaration); | 99 Expect.isTrue(transformOfDouble.referent.isOriginalDeclaration); |
| 87 | 100 |
| 88 Expect.equals(reflectClass(num), | 101 Expect.equals(reflectClass(num), |
| 89 reflectTypeDeclaration(NonGenericPredicate).referent.parameters[
0].type); | 102 reflectTypeDeclaration(NonGenericPredicate).referent.parameters[0].type); |
| 90 Expect.equals(tFromGenericPredicate, | 103 Expect.equals(tFromGenericPredicate, |
| 91 reflectTypeDeclaration(GenericPredicate).referent.parameters[0].
type); | 104 reflectTypeDeclaration(GenericPredicate).referent.parameters[0].type); |
| 92 Expect.equals(sFromGenericTransform, | 105 Expect.equals(sFromGenericTransform, |
| 93 reflectTypeDeclaration(GenericTransform).referent.parameters[0].
type); | 106 reflectTypeDeclaration(GenericTransform).referent.parameters[0].type); |
| 94 | 107 |
| 95 Expect.equals(reflectClass(num), | 108 Expect.equals(reflectClass(num), predicateOfNum.referent.parameters[0].type); |
| 96 predicateOfNum.referent.parameters[0].type); | 109 Expect.equals( |
| 97 Expect.equals(reflectClass(String), | 110 reflectClass(String), transformOfString.referent.parameters[0].type); |
| 98 transformOfString.referent.parameters[0].type); | 111 Expect.equals(rFromC, transformOfR.referent.parameters[0].type); |
| 99 Expect.equals(rFromC, | 112 Expect.equals( |
| 100 transformOfR.referent.parameters[0].type); | 113 reflectClass(double), transformOfDouble.referent.parameters[0].type); |
| 101 Expect.equals(reflectClass(double), | |
| 102 transformOfDouble.referent.parameters[0].type); | |
| 103 | 114 |
| 104 Expect.equals(reflectClass(bool), | 115 Expect.equals(reflectClass(bool), |
| 105 reflectTypeDeclaration(NonGenericPredicate).referent.returnType)
; | 116 reflectTypeDeclaration(NonGenericPredicate).referent.returnType); |
| 106 Expect.equals(reflectClass(bool), | 117 Expect.equals(reflectClass(bool), |
| 107 reflectTypeDeclaration(GenericPredicate).referent.returnType); | 118 reflectTypeDeclaration(GenericPredicate).referent.returnType); |
| 108 Expect.equals(sFromGenericTransform, | 119 Expect.equals(sFromGenericTransform, |
| 109 reflectTypeDeclaration(GenericTransform).referent.returnType); | 120 reflectTypeDeclaration(GenericTransform).referent.returnType); |
| 110 Expect.equals(reflectClass(bool), | 121 Expect.equals(reflectClass(bool), predicateOfNum.referent.returnType); |
| 111 predicateOfNum.referent.returnType); | 122 Expect.equals(reflectClass(String), transformOfString.referent.returnType); |
| 112 Expect.equals(reflectClass(String), | 123 Expect.equals(rFromC, transformOfR.referent.returnType); |
| 113 transformOfString.referent.returnType); | 124 Expect.equals(reflectClass(double), transformOfDouble.referent.returnType); |
| 114 Expect.equals(rFromC, | |
| 115 transformOfR.referent.returnType); | |
| 116 Expect.equals(reflectClass(double), | |
| 117 transformOfDouble.referent.returnType); | |
| 118 } | 125 } |
| OLD | NEW |