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