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