| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class GettersSettersTest { | 5 class GettersSettersTest { |
| 6 | 6 |
| 7 static int foo; | 7 static int foo; |
| 8 | 8 |
| 9 static get bar() { |
| 10 return foo; |
| 11 } |
| 12 |
| 13 static set bar(newValue) { |
| 14 foo = newValue; |
| 15 } |
| 16 |
| 9 static testMain() { | 17 static testMain() { |
| 10 A a = new A(); | 18 A a = new A(); |
| 11 a.x = 2; | 19 a.x = 2; |
| 12 Expect.equals(2, a.x); | 20 Expect.equals(2, a.x); |
| 13 Expect.equals(2, a.x_); | 21 Expect.equals(2, a.x_); |
| 14 | 22 |
| 15 // Test inheritance. | 23 // Test inheritance. |
| 16 a = new B(); | 24 a = new B(); |
| 17 a.x = 4; | 25 a.x = 4; |
| 18 Expect.equals(4, a.x); | 26 Expect.equals(4, a.x); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 35 a.x_ = 0; | 43 a.x_ = 0; |
| 36 a[2] += 8; | 44 a[2] += 8; |
| 37 Expect.equals(12, a[0]); | 45 Expect.equals(12, a[0]); |
| 38 | 46 |
| 39 // Test calling a function that internally uses getters. | 47 // Test calling a function that internally uses getters. |
| 40 Expect.equals(true, a.isXPositive()); | 48 Expect.equals(true, a.isXPositive()); |
| 41 | 49 |
| 42 // Test static fields. | 50 // Test static fields. |
| 43 foo = 42; | 51 foo = 42; |
| 44 Expect.equals(42, foo); | 52 Expect.equals(42, foo); |
| 53 Expect.equals(42, bar); |
| 45 A.foo = 43; | 54 A.foo = 43; |
| 46 Expect.equals(43, A.foo); | 55 Expect.equals(43, A.foo); |
| 56 Expect.equals(43, A.bar); |
| 47 | 57 |
| 48 new D().test(); | 58 bar = 42; |
| 49 | 59 Expect.equals(42, foo); |
| 50 OverrideField of = new OverrideField(); | 60 Expect.equals(42, bar); |
| 51 Expect.equals(27, of.getX_()); | 61 A.bar = 43; |
| 52 | 62 Expect.equals(43, A.foo); |
| 53 ReferenceField rf = new ReferenceField(); | 63 Expect.equals(43, A.bar); |
| 54 rf.x_ = 1; | |
| 55 Expect.equals(1, rf.getIt()); | |
| 56 rf.setIt(2); | |
| 57 Expect.equals(2, rf.x_); | |
| 58 } | 64 } |
| 59 } | 65 } |
| 60 | 66 |
| 61 class A { | 67 class A { |
| 62 // TODO(fabiofmv): consider removing once http://b/4254120 is fixed. | 68 A(); |
| 63 A() { } | |
| 64 int x_; | 69 int x_; |
| 65 static int foo; | 70 static int foo; |
| 66 | 71 |
| 67 static get bar() { | 72 static get bar() { |
| 68 return foo; | 73 return foo; |
| 69 } | 74 } |
| 70 | 75 |
| 71 static set bar(newValue) { | 76 static set bar(newValue) { |
| 72 foo = newValue; | 77 foo = newValue; |
| 73 } | 78 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 91 void operator []=(int index, int value) { | 96 void operator []=(int index, int value) { |
| 92 x_ = index + value; | 97 x_ = index + value; |
| 93 } | 98 } |
| 94 | 99 |
| 95 int getX_() { | 100 int getX_() { |
| 96 return x_; | 101 return x_; |
| 97 } | 102 } |
| 98 } | 103 } |
| 99 | 104 |
| 100 class B extends A { | 105 class B extends A { |
| 101 B() : super() {} | 106 B(); |
| 102 } | 107 } |
| 103 | 108 |
| 104 class C extends A { | 109 class C extends A { |
| 105 int y_; | 110 int y_; |
| 106 | 111 |
| 107 C() : super() { | 112 C() { |
| 108 this.x_ = 0; | 113 this.x_ = 0; |
| 109 } | 114 } |
| 110 | 115 |
| 111 int get x() { | 116 int get x() { |
| 112 return y_; | 117 return y_; |
| 113 } | 118 } |
| 114 | 119 |
| 115 void set x(int value) { | 120 void set x(int value) { |
| 116 y_ = value; | 121 y_ = value; |
| 117 } | 122 } |
| 118 } | 123 } |
| 119 | 124 |
| 120 class D extends A { | |
| 121 D() : super() {} | |
| 122 | |
| 123 var x2_; | |
| 124 | |
| 125 set x(new_x) { | |
| 126 x2_ = new_x; | |
| 127 } | |
| 128 | |
| 129 test() { | |
| 130 x = 87; | |
| 131 Expect.equals(87, x2_); | |
| 132 x = 42; | |
| 133 Expect.equals(42, x2_); | |
| 134 | |
| 135 foo = 0; | |
| 136 Expect.equals(0, bar); | |
| 137 bar = 1; | |
| 138 Expect.equals(1, foo); | |
| 139 var tmp = foo; | |
| 140 foo += 3; | |
| 141 Expect.equals(4, bar); | |
| 142 bar += 5; | |
| 143 Expect.equals(9, foo); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 class OverrideField extends A { | |
| 148 OverrideField() : super() {} | |
| 149 | |
| 150 int get x_() { | |
| 151 return 27; | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 class ReferenceField extends A { | |
| 156 ReferenceField() : super() {} | |
| 157 | |
| 158 setIt(a) { | |
| 159 super.x_ = a; | |
| 160 } | |
| 161 | |
| 162 int getIt() { | |
| 163 return super.x_; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 main() { | 125 main() { |
| 168 GettersSettersTest.testMain(); | 126 GettersSettersTest.testMain(); |
| 169 } | 127 } |
| OLD | NEW |