OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 import "package:meta/meta.dart" show virtual; | |
7 | |
8 class GettersSettersTest { | |
9 static int foo; | |
10 | |
11 static testMain() { | |
12 A a = new A(); | |
13 a.x = 2; | |
14 Expect.equals(2, a.x); | |
15 Expect.equals(2, a.x_); | |
16 | |
17 // Test inheritance. | |
18 a = new B(); | |
19 a.x = 4; | |
20 Expect.equals(4, a.x); | |
21 Expect.equals(4, a.x_); | |
22 | |
23 // Test overriding. | |
24 C c = new C(); | |
25 c.x = 8; | |
26 Expect.equals(8, c.x); | |
27 Expect.equals(0, c.x_); | |
28 Expect.equals(8, c.y_); | |
29 | |
30 // Test keyed getters and setters. | |
31 a.x_ = 0; | |
32 Expect.equals(2, a[2]); | |
33 a[2] = 4; | |
34 Expect.equals(6, a[0]); | |
35 | |
36 // Test assignment operators. | |
37 a.x_ = 0; | |
38 a[2] += 8; | |
39 Expect.equals(12, a[0]); | |
40 | |
41 // Test calling a function that internally uses getters. | |
42 Expect.equals(true, a.isXPositive()); | |
43 | |
44 // Test static fields. | |
45 foo = 42; | |
46 Expect.equals(42, foo); | |
47 A.foo = 43; | |
48 Expect.equals(43, A.foo); | |
49 | |
50 new D().test(); | |
51 | |
52 OverrideField of = new OverrideField(); | |
53 Expect.equals(27, of.getX_()); | |
54 | |
55 ReferenceField rf = new ReferenceField(); | |
56 rf.x_ = 1; | |
57 Expect.equals(1, rf.getIt()); | |
58 rf.setIt(2); | |
59 Expect.equals(2, rf.x_); | |
60 } | |
61 } | |
62 | |
63 class A { | |
64 @virtual | |
65 int x_; | |
66 static int foo; | |
67 | |
68 static get bar { | |
69 return foo; | |
70 } | |
71 | |
72 static set bar(newValue) { | |
73 foo = newValue; | |
74 } | |
75 | |
76 int get x { | |
77 return x_; | |
78 } | |
79 | |
80 void set x(int value) { | |
81 x_ = value; | |
82 } | |
83 | |
84 bool isXPositive() { | |
85 return x > 0; | |
86 } | |
87 | |
88 int operator [](int index) { | |
89 return x_ + index; | |
90 } | |
91 | |
92 void operator []=(int index, int value) { | |
93 x_ = index + value; | |
94 } | |
95 | |
96 int getX_() { | |
97 return x_; | |
98 } | |
99 } | |
100 | |
101 class B extends A {} | |
102 | |
103 class C extends A { | |
104 int y_; | |
105 | |
106 C() : super() { | |
107 this.x_ = 0; | |
108 } | |
109 | |
110 int get x { | |
111 return y_; | |
112 } | |
113 | |
114 void set x(int value) { | |
115 y_ = value; | |
116 } | |
117 } | |
118 | |
119 class D extends A { | |
120 var x2_; | |
121 | |
122 set x(new_x) { | |
123 x2_ = new_x; | |
124 } | |
125 | |
126 test() { | |
127 x = 87; | |
128 Expect.equals(87, x2_); | |
129 x = 42; | |
130 Expect.equals(42, x2_); | |
131 } | |
132 } | |
133 | |
134 class OverrideField extends A { | |
135 int get x_ { | |
136 return 27; | |
137 } | |
138 } | |
139 | |
140 class ReferenceField extends A { | |
141 setIt(a) { | |
142 super.x_ = a; | |
143 } | |
144 | |
145 int getIt() { | |
146 return super.x_; | |
147 } | |
148 } | |
149 | |
150 main() { | |
151 GettersSettersTest.testMain(); | |
152 } | |
OLD | NEW |