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