dart-sdk/tests/corelib_2/const_list_literal_test.dart
Janice Collins b1da48a29d Migrate test block 4 to Dart 2.0.
Unusual things in this block:
- corelib_strong/data_uri_test.dart was behind corelib, so migrated
that file from corelib.
- core_runtime_types_test was split per the document into static
and non-static versions
- data_resource_test.dart is now pointless since class Resource
was moved from dart:core to http://github.com/dart-lang/resource,
so deleted.

BUG=
R=rnystrom@google.com

Review-Url: https://codereview.chromium.org/2986503002 .
2017-07-21 08:24:26 -07:00

81 lines
2 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Test that a final list literal is not expandable nor modifiable.
class ConstListLiteralTest {
static void testMain() {
var list = const [4, 2, 3];
Expect.equals(3, list.length);
var exception = null;
try {
list.add(4);
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
exception = null;
exception = null;
try {
list.addAll([4, 5]);
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
exception = null;
try {
list[0] = 0;
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
exception = null;
try {
list.sort((a, b) => a - b);
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
Expect.equals(4, list[0]);
Expect.equals(2, list[1]);
Expect.equals(3, list[2]);
exception = null;
try {
list.setRange(0, 1, [1], 0);
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
Expect.equals(4, list[0]);
Expect.equals(2, list[1]);
Expect.equals(3, list[2]);
// Note: the next check is a regression test for dart2js. The immutable list
// overrides the 'length' property of List, but relies on using the native
// 'forEach' construct in Array. This test ensures that our strategy works
// correctly.
int x = 0;
list.forEach((e) {
x += e;
});
Expect.equals(9, x);
}
}
main() {
ConstListLiteralTest.testMain();
}