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 arrays from const array literals are immutable. | 4 // Check that arrays from const array literals are immutable. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 class ListLiteral3Test { | 8 class ListLiteral3Test { |
9 | |
10 static const List<String> canonicalJoke = const ["knock", "knock"]; | 9 static const List<String> canonicalJoke = const ["knock", "knock"]; |
11 | 10 |
12 static testMain() { | 11 static testMain() { |
13 | |
14 List<String> joke = const ["knock", "knock"]; | 12 List<String> joke = const ["knock", "knock"]; |
15 // Elements of canonical lists are canonicalized. | 13 // Elements of canonical lists are canonicalized. |
16 Expect.identical(joke, canonicalJoke); | 14 Expect.identical(joke, canonicalJoke); |
17 Expect.identical(joke[0], joke[1]); | 15 Expect.identical(joke[0], joke[1]); |
18 Expect.identical(joke[0], canonicalJoke[0]); | 16 Expect.identical(joke[0], canonicalJoke[0]); |
19 | 17 |
20 // Lists from literals are immutable. | 18 // Lists from literals are immutable. |
21 Expect.throws(() { joke[0] = "sock"; }, (e) => e is UnsupportedError); | 19 Expect.throws(() { |
| 20 joke[0] = "sock"; |
| 21 }, (e) => e is UnsupportedError); |
22 Expect.identical(joke[0], joke[1]); | 22 Expect.identical(joke[0], joke[1]); |
23 | 23 |
24 // Make sure lists allocated at runtime are mutable and are | 24 // Make sure lists allocated at runtime are mutable and are |
25 // not canonicalized. | 25 // not canonicalized. |
26 List<String> lame_joke = ["knock", "knock"]; // Invokes operator new. | 26 List<String> lame_joke = ["knock", "knock"]; // Invokes operator new. |
27 Expect.identical(joke[1], lame_joke[1]); | 27 Expect.identical(joke[1], lame_joke[1]); |
28 // Operator new creates a mutable list. | 28 // Operator new creates a mutable list. |
29 Expect.equals(false, identical(joke, lame_joke)); | 29 Expect.equals(false, identical(joke, lame_joke)); |
30 lame_joke[1] = "who"; | 30 lame_joke[1] = "who"; |
31 Expect.identical("who", lame_joke[1]); | 31 Expect.identical("who", lame_joke[1]); |
32 | 32 |
33 // Elements of canonical lists are canonicalized. | 33 // Elements of canonical lists are canonicalized. |
34 List<List<int>> a = const <List<int>>[ const [1, 2], const [1, 2]]; | 34 List<List<int>> a = const <List<int>>[ |
| 35 const [1, 2], |
| 36 const [1, 2] |
| 37 ]; |
35 Expect.identical(a[0], a[1]); | 38 Expect.identical(a[0], a[1]); |
36 Expect.identical(a[0][0], a[1][0]); | 39 Expect.identical(a[0][0], a[1][0]); |
37 Expect.throws(() { a[0][0] = 42; }, (e) => e is UnsupportedError); | 40 Expect.throws(() { |
| 41 a[0][0] = 42; |
| 42 }, (e) => e is UnsupportedError); |
38 | 43 |
39 List<List<double>> b = const [ const [1.0, 2.0], const [1.0, 2.0]]; | 44 List<List<double>> b = const [ |
| 45 const [1.0, 2.0], |
| 46 const [1.0, 2.0] |
| 47 ]; |
40 Expect.identical(b[0], b[1]); | 48 Expect.identical(b[0], b[1]); |
41 Expect.equals(true, b[0][0] == 1.0); | 49 Expect.equals(true, b[0][0] == 1.0); |
42 Expect.identical(b[0][0], b[1][0]); | 50 Expect.identical(b[0][0], b[1][0]); |
43 Expect.throws(() { b[0][0] = 42.0; }, (e) => e is UnsupportedError); | 51 Expect.throws(() { |
| 52 b[0][0] = 42.0; |
| 53 }, (e) => e is UnsupportedError); |
44 } | 54 } |
45 } | 55 } |
46 | 56 |
47 main() { | 57 main() { |
48 ListLiteral3Test.testMain(); | 58 ListLiteral3Test.testMain(); |
49 } | 59 } |
OLD | NEW |