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

Side by Side Diff: tests/lib/mirrors/relation_subclass_test.dart

Issue 126823004: Add TypeMirror.isSubtypeOf, TypeMirror.isAssignableTo, ClassMirror.isSubclassOf (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: mark source mirrors as failing Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 library test.relation_subclass;
6
7 import "dart:mirrors";
8
9 import "package:expect/expect.dart";
10
11 class Superclass {}
12 class Subclass1 extends Superclass {}
13 class Subclass2 extends Superclass {}
14
15 typedef bool NumberPredicate(num x);
16 typedef bool IntegerPredicate(int x);
17 typedef bool DoublePredicate(double x);
18
19 typedef num NumberGenerator();
20 typedef int IntegerGenerator();
21 typedef double DoubleGenerator();
22
23 test(MirrorSystem mirrors) {
24 LibraryMirror coreLibrary = mirrors.findLibrary(#dart.core);
25 LibraryMirror thisLibrary = mirrors.findLibrary(#test.relation_subclass);
26
27 ClassMirror Super = thisLibrary.declarations[#Superclass];
28 ClassMirror Sub1 = thisLibrary.declarations[#Subclass1];
29 ClassMirror Sub2 = thisLibrary.declarations[#Subclass2];
30 ClassMirror Obj = coreLibrary.declarations[#Object];
31
32 Expect.isTrue(Obj.isSubclassOf(Obj));
33 Expect.isTrue(Super.isSubclassOf(Super));
34 Expect.isTrue(Sub1.isSubclassOf(Sub1));
35 Expect.isTrue(Sub2.isSubclassOf(Sub2));
36
37 Expect.isTrue(Sub1.isSubclassOf(Super));
38 Expect.isFalse(Super.isSubclassOf(Sub1));
39
40 Expect.isTrue(Sub2.isSubclassOf(Super));
41 Expect.isFalse(Super.isSubclassOf(Sub2));
42
43 Expect.isFalse(Sub2.isSubclassOf(Sub1));
44 Expect.isFalse(Sub1.isSubclassOf(Sub2));
45
46 Expect.isTrue(Sub1.isSubclassOf(Obj));
47 Expect.isFalse(Obj.isSubclassOf(Sub1));
48
49 Expect.isTrue(Sub2.isSubclassOf(Obj));
50 Expect.isFalse(Obj.isSubclassOf(Sub2));
51
52 Expect.isTrue(Super.isSubclassOf(Obj));
53 Expect.isFalse(Obj.isSubclassOf(Super));
54
55 var Func = coreLibrary.declarations[#Function];
56 Expect.isTrue(Func.isSubclassOf(Obj));
57 Expect.isFalse(Obj.isSubclassOf(Func));
58
59 // Function typedef.
60 var NumPred = thisLibrary.declarations[#NumberPredicate];
61 var IntPred = thisLibrary.declarations[#IntegerPredicate];
62 var DubPred = thisLibrary.declarations[#DoublePredicate];
63 var NumGen = thisLibrary.declarations[#NumberGenerator];
64 var IntGen = thisLibrary.declarations[#IntegerGenerator];
65 var DubGen = thisLibrary.declarations[#DoubleGenerator];
66
67 isArgumentOrTypeError(e) => e is ArgumentError || e is TypeError;
68 Expect.throws(() => Func.isSubclassOf(NumPred), isArgumentOrTypeError);
69 Expect.throws(() => Func.isSubclassOf(IntPred), isArgumentOrTypeError);
70 Expect.throws(() => Func.isSubclassOf(DubPred), isArgumentOrTypeError);
71 Expect.throws(() => Func.isSubclassOf(NumGen), isArgumentOrTypeError);
72 Expect.throws(() => Func.isSubclassOf(IntGen), isArgumentOrTypeError);
73 Expect.throws(() => Func.isSubclassOf(DubGen), isArgumentOrTypeError);
74
75 isNoSuchMethodError(e) => e is NoSuchMethodError;
76 Expect.throws(() => NumPred.isSubclassOf(Func), isNoSuchMethodError);
77 Expect.throws(() => IntPred.isSubclassOf(Func), isNoSuchMethodError);
78 Expect.throws(() => DubPred.isSubclassOf(Func), isNoSuchMethodError);
79 Expect.throws(() => NumGen.isSubclassOf(Func), isNoSuchMethodError);
80 Expect.throws(() => IntGen.isSubclassOf(Func), isNoSuchMethodError);
81 Expect.throws(() => DubGen.isSubclassOf(Func), isNoSuchMethodError);
82
83 // Function type.
84 TypeMirror NumPredRef = (NumPred as TypedefMirror).referent;
85 TypeMirror IntPredRef = (IntPred as TypedefMirror).referent;
86 TypeMirror DubPredRef = (DubPred as TypedefMirror).referent;
87 TypeMirror NumGenRef = (NumGen as TypedefMirror).referent;
88 TypeMirror IntGenRef = (IntGen as TypedefMirror).referent;
89 TypeMirror DubGenRef = (DubGen as TypedefMirror).referent;
90
91 Expect.isFalse(Func.isSubclassOf(NumPredRef));
92 Expect.isFalse(Func.isSubclassOf(IntPredRef));
93 Expect.isFalse(Func.isSubclassOf(DubPredRef));
94 Expect.isFalse(Func.isSubclassOf(NumGenRef));
95 Expect.isFalse(Func.isSubclassOf(IntGenRef));
96 Expect.isFalse(Func.isSubclassOf(DubGenRef));
97
98 // The spec doesn't require these to be either value, only that they implement
99 // Function.
100 // NumPredRef.isSubclassOf(Func);
101 // IntPredRef.isSubclassOf(Func);
102 // DubPredRef.isSubclassOf(Func);
103 // NumGenRef.isSubclassOf(Func);
104 // IntGenRef.isSubclassOf(Func);
105 // DubGenRef.isSubclassOf(Func);
106 }
107
108 main() {
109 test(currentMirrorSystem());
110 }
OLDNEW
« no previous file with comments | « tests/lib/mirrors/relation_assignable_test.dart ('k') | tests/lib/mirrors/relation_subtype_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698