| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import "package:expect/expect.dart"; | |
| 6 | |
| 7 // Test that a final list literal is not expandable nor modifiable. | |
| 8 | |
| 9 class ConstListLiteralTest { | |
| 10 static void testMain() { | |
| 11 var list = const [4, 2, 3]; | |
| 12 Expect.equals(3, list.length); | |
| 13 | |
| 14 var exception = null; | |
| 15 try { | |
| 16 list.add(4); | |
| 17 } on UnsupportedError catch (e) { | |
| 18 exception = e; | |
| 19 } | |
| 20 Expect.equals(true, exception != null); | |
| 21 Expect.equals(3, list.length); | |
| 22 exception = null; | |
| 23 | |
| 24 exception = null; | |
| 25 try { | |
| 26 list.addAll([4, 5]); | |
| 27 } on UnsupportedError catch (e) { | |
| 28 exception = e; | |
| 29 } | |
| 30 Expect.equals(true, exception != null); | |
| 31 Expect.equals(3, list.length); | |
| 32 | |
| 33 exception = null; | |
| 34 try { | |
| 35 list[0] = 0; | |
| 36 } on UnsupportedError catch (e) { | |
| 37 exception = e; | |
| 38 } | |
| 39 Expect.equals(true, exception != null); | |
| 40 Expect.equals(3, list.length); | |
| 41 | |
| 42 exception = null; | |
| 43 try { | |
| 44 list.sort((a, b) => a - b); | |
| 45 } on UnsupportedError catch (e) { | |
| 46 exception = e; | |
| 47 } | |
| 48 Expect.equals(true, exception != null); | |
| 49 Expect.equals(3, list.length); | |
| 50 Expect.equals(4, list[0]); | |
| 51 Expect.equals(2, list[1]); | |
| 52 Expect.equals(3, list[2]); | |
| 53 | |
| 54 exception = null; | |
| 55 try { | |
| 56 list.setRange(0, 1, [1], 0); | |
| 57 } on UnsupportedError catch (e) { | |
| 58 exception = e; | |
| 59 } | |
| 60 Expect.equals(true, exception != null); | |
| 61 Expect.equals(3, list.length); | |
| 62 Expect.equals(4, list[0]); | |
| 63 Expect.equals(2, list[1]); | |
| 64 Expect.equals(3, list[2]); | |
| 65 | |
| 66 // Note: the next check is a regression test for dart2js. The immutable list | |
| 67 // overrides the 'length' property of List, but relies on using the native | |
| 68 // 'forEach' construct in Array. This test ensures that our strategy works | |
| 69 // correctly. | |
| 70 int x = 0; | |
| 71 list.forEach((e) { | |
| 72 x += e; | |
| 73 }); | |
| 74 Expect.equals(9, x); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 main() { | |
| 79 ConstListLiteralTest.testMain(); | |
| 80 } | |
| OLD | NEW |