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

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: Created 6 years, 11 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 import "dart:mirrors";
6
7 import "package:expect/expect.dart";
8
9 class Superclass {}
10 class Subclass1 extends Superclass {}
11 class Subclass2 extends Superclass {}
12
13 typedef bool NumberPredicate(num x);
14 typedef bool IntegerPredicate(int x);
15 typedef bool DoublePredicate(double x);
16
17 typedef num NumberGenerator();
18 typedef int IntegerGenerator();
19 typedef double DoubleGenerator();
20
21 main() {
22 ClassMirror Super = reflectType(Superclass);
23 ClassMirror Sub1 = reflectType(Subclass1);
24 ClassMirror Sub2 = reflectType(Subclass2);
25 ClassMirror Obj = reflectType(Object);
26
27 Expect.isTrue(Obj.isSubclassOf(Obj));
28 Expect.isTrue(Super.isSubclassOf(Super));
29 Expect.isTrue(Sub1.isSubclassOf(Sub1));
30 Expect.isTrue(Sub2.isSubclassOf(Sub2));
31
32 Expect.isTrue(Sub1.isSubclassOf(Super));
33 Expect.isFalse(Super.isSubclassOf(Sub1));
34
35 Expect.isTrue(Sub2.isSubclassOf(Super));
36 Expect.isFalse(Super.isSubclassOf(Sub2));
37
38 Expect.isFalse(Sub2.isSubclassOf(Sub1));
39 Expect.isFalse(Sub1.isSubclassOf(Sub2));
40
41 Expect.isTrue(Sub1.isSubclassOf(Obj));
42 Expect.isFalse(Obj.isSubclassOf(Sub1));
43
44 Expect.isTrue(Sub2.isSubclassOf(Obj));
45 Expect.isFalse(Obj.isSubclassOf(Sub2));
46
47 Expect.isTrue(Super.isSubclassOf(Obj));
48 Expect.isFalse(Obj.isSubclassOf(Super));
49
50 return; /// 01: ok
51
52 TypeMirror Func = reflectType(Function);
53 Expect.isTrue(Func.isSubclassOf(Obj));
54 Expect.isFalse(Obj.isSubclassOf(Func));
55
56 // Function typedef.
57 TypeMirror NumPred = reflectType(NumberPredicate);
58 TypeMirror IntPred = reflectType(IntegerPredicate);
59 TypeMirror DubPred = reflectType(DoublePredicate);
60 TypeMirror NumGen = reflectType(NumberGenerator);
61 TypeMirror IntGen = reflectType(IntegerGenerator);
62 TypeMirror DubGen = reflectType(DoubleGenerator);
63
64 Expect.throws(() => Func.isSubclassOf(NumPred), (e) => e is ArgumentError || e is TypeError);
65 Expect.throws(() => Func.isSubclassOf(IntPred), (e) => e is ArgumentError || e is TypeError);
66 Expect.throws(() => Func.isSubclassOf(DubPred), (e) => e is ArgumentError || e is TypeError);
67 Expect.throws(() => Func.isSubclassOf(NumGen), (e) => e is ArgumentError || e is TypeError);
68 Expect.throws(() => Func.isSubclassOf(IntGen), (e) => e is ArgumentError || e is TypeError);
69 Expect.throws(() => Func.isSubclassOf(DubGen), (e) => e is ArgumentError || e is TypeError);
70
71 Expect.throws(() => NumPred.isSubclassOf(Func), (e) => e is NoSuchMethodError) ;
72 Expect.throws(() => IntPred.isSubclassOf(Func), (e) => e is NoSuchMethodError) ;
73 Expect.throws(() => DubPred.isSubclassOf(Func), (e) => e is NoSuchMethodError) ;
74 Expect.throws(() => NumGen.isSubclassOf(Func), (e) => e is NoSuchMethodError);
75 Expect.throws(() => IntGen.isSubclassOf(Func), (e) => e is NoSuchMethodError);
76 Expect.throws(() => DubGen.isSubclassOf(Func), (e) => e is NoSuchMethodError);
77
78 // Function type.
79 TypeMirror NumPredRef = reflectType(NumberPredicate).referent;
80 TypeMirror IntPredRef = reflectType(IntegerPredicate).referent;
81 TypeMirror DubPredRef = reflectType(DoublePredicate).referent;
82 TypeMirror NumGenRef = reflectType(NumberGenerator).referent;
83 TypeMirror IntGenRef = reflectType(IntegerGenerator).referent;
84 TypeMirror DubGenRef = reflectType(DoubleGenerator).referent;
85
86 Expect.isFalse(Func.isSubclassOf(NumPredRef));
87 Expect.isFalse(Func.isSubclassOf(IntPredRef));
88 Expect.isFalse(Func.isSubclassOf(DubPredRef));
89 Expect.isFalse(Func.isSubclassOf(NumGenRef));
90 Expect.isFalse(Func.isSubclassOf(IntGenRef));
91 Expect.isFalse(Func.isSubclassOf(DubGenRef));
92
93 // The spec doesn't require these to be either value.
94 // NumPredRef.isSubclassOf(Func);
95 // IntPredRef.isSubclassOf(Func);
96 // DubPredRef.isSubclassOf(Func);
97 // NumGenRef.isSubclassOf(Func);
98 // IntGenRef.isSubclassOf(Func);
99 // DubGenRef.isSubclassOf(Func);
100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698