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 // Check that const objects (including literals) are immutable. | 4 // Check that const objects (including literals) are immutable. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 class A { | 8 class A { |
9 const A(this.x, this.y); | 9 const A(this.x, this.y); |
10 final num x, y; | 10 final num x, y; |
11 } | 11 } |
12 | 12 |
13 main() { | 13 main() { |
14 var list = const [1, 2]; | 14 var list = const [1, 2]; |
15 Expect.throws(() => list[0] = 3); | 15 Expect.throws(() => list[0] = 3); |
16 Expect.equals(1, list[0]); | 16 Expect.equals(1, list[0]); |
17 | 17 |
18 var m = const { 'foo': 499 }; | 18 var m = const {'foo': 499}; |
19 Expect.throws(() => m['foo'] = 42); | 19 Expect.throws(() => m['foo'] = 42); |
20 Expect.equals(499, m['foo']); | 20 Expect.equals(499, m['foo']); |
21 | 21 |
22 var a1 = const A(1, 2); | 22 var a1 = const A(1, 2); |
23 Expect.throws(() => a1.x = 499); | 23 Expect.throws(() => a1.x = 499); |
24 Expect.equals(1, a1.x); | 24 Expect.equals(1, a1.x); |
25 | 25 |
26 A a2 = const A(1, 2); | 26 A a2 = const A(1, 2); |
27 Expect.throws(() => a2.x = 499); //# 01: static type warning | 27 Expect.throws(() => a2.x = 499); //# 01: static type warning |
28 Expect.equals(1, a2.x); | 28 Expect.equals(1, a2.x); |
29 } | 29 } |
OLD | NEW |