OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // Tests classes with getters and setters that do not have the same type. | 7 // Tests classes with getters and setters that do not have the same type. |
8 | 8 |
9 class A { | 9 class A { |
10 int a() { | 10 int a() { return 37;} |
11 return 37; | |
12 } | |
13 } | 11 } |
14 | 12 |
15 class B extends A { | 13 class B extends A{ |
16 int b() { | 14 int b() { return 38; } |
17 return 38; | |
18 } | |
19 } | 15 } |
20 | 16 |
21 class C {} | 17 class C {} |
22 | 18 |
23 class T1 { | 19 class T1 { |
24 A getterField; | 20 A getterField; |
25 A get field { | 21 A get field { return getterField; } |
26 return getterField; | |
27 } | |
28 | |
29 // OK, B is assignable to A | 22 // OK, B is assignable to A |
30 void set field(B arg) { | 23 void set field(B arg) { getterField = arg; } |
31 getterField = arg; | |
32 } | |
33 } | 24 } |
34 | 25 |
35 class T2 { | 26 class T2 { |
36 A getterField; | 27 A getterField; |
37 C setterField; | 28 C setterField; |
38 A get field { | 29 A get field { return getterField; } |
39 return getterField; | |
40 } | |
41 | 30 |
42 // Type C is not assignable to A | 31 // Type C is not assignable to A |
43 void set field(C arg) { setterField = arg; } //# 01: static type warning | 32 void set field(C arg) { setterField = arg; } //# 01: static type warning |
44 } | 33 } |
45 | 34 |
46 class T3 { | 35 class T3 { |
47 B getterField; | 36 B getterField; |
48 B get field { | 37 B get field { return getterField; } |
49 return getterField; | 38 // OK, A is assignable to B |
50 } | 39 void set field(A arg) { getterField = arg; } |
| 40 } |
51 | 41 |
52 // OK, A is assignable to B | |
53 void set field(A arg) { | |
54 getterField = arg; | |
55 } | |
56 } | |
57 | 42 |
58 main() { | 43 main() { |
59 T1 instance1 = new T1(); | 44 T1 instance1 = new T1(); |
60 T2 instance2 = new T2(); //# 01: continued | 45 T2 instance2 = new T2(); //# 01: continued |
61 T3 instance3 = new T3(); | 46 T3 instance3 = new T3(); |
62 | 47 |
63 instance1.field = new B(); | 48 instance1.field = new B(); |
64 A resultA = instance1.field; | 49 A resultA = instance1.field; |
65 instance1.field = new A(); //# 03: dynamic type error | 50 instance1.field = new A(); //# 03: dynamic type error |
66 B resultB = instance1.field; | 51 B resultB = instance1.field; |
67 | 52 |
68 int result; | 53 int result; |
69 result = instance1.field.a(); | 54 result = instance1.field.a(); |
70 Expect.equals(37, result); | 55 Expect.equals(37, result); |
71 | 56 |
72 // Type 'A' has no method named 'b' | 57 // Type 'A' has no method named 'b' |
73 instance1.field.b(); //# 02: static type warning | 58 instance1.field.b(); //# 02: static type warning |
74 | 59 |
75 instance3.field = new B(); | 60 instance3.field = new B(); |
76 result = instance3.field.a(); | 61 result = instance3.field.a(); |
77 Expect.equals(37, result); | 62 Expect.equals(37, result); |
78 result = instance3.field.b(); | 63 result = instance3.field.b(); |
79 Expect.equals(38, result); | 64 Expect.equals(38, result); |
80 } | 65 } |
OLD | NEW |