ff5f391c0a
Make API more consistent for a few methods. Reduce the number of language features used in tests: * Never iterating an iterable, always converting it using `.toList()` first and iterating using indices (fx `setEquals`). Also require a `List` in places where an `Iterable` wasn't necessary. * Avoid doing complicated computations that are also used for the error message. Do simple check first, then recompute to get better error messages (fx `allDistinct`). Renamed some rarely used members for consistency (`stringContainsInOrder`->`containsInOrder`, where other string-contains functions just start with `contains`, and `containsOneOf` -> `containsAny` to match `Iterable.any` phrasing, and also it accepts if containing at least one, not precisely one.) Removed a function that wasn't used anywhere. Moved `assertStatementsEnabled` to `variations.dart` as `asserts`. Removed `typeAssertionsEnabled` and `checkedModeEnabled`. The former used in one place, where it was replaced with `checkedImplicitDowncasts` from `variations.dart`, the latter wasn't used anywhere. Deprecates `package:expect/minitest.dart`. It was never intended to be used for new tests, only as a help to convert existing tests written against `package:unit_test`. All existing imports marked as `// ignore: deprecated_member_use`. Change-Id: I07e21d4c0f3ccf11b82ee34af2668fdbb22264d2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352360 Reviewed-by: Slava Egorov <vegorov@google.com> Reviewed-by: Ömer Ağacan <omersa@google.com> Reviewed-by: Nate Bosch <nbosch@google.com> Reviewed-by: Stephen Adams <sra@google.com> Commit-Queue: Lasse Nielsen <lrn@google.com>
156 lines
4.3 KiB
Dart
156 lines
4.3 KiB
Dart
// Copyright (c) 2013, 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 'dart:html';
|
|
import 'dart:indexed_db' show IdbFactory, KeyRange;
|
|
import 'dart:typed_data' show Int32List;
|
|
import 'dart:js';
|
|
|
|
import 'package:js/js_util.dart' as js_util;
|
|
import 'package:expect/minitest.dart'; // ignore: deprecated_member_use_from_same_package
|
|
|
|
import 'js_test_util.dart';
|
|
|
|
main() {
|
|
injectJs();
|
|
|
|
test('new JsArray()', () {
|
|
var array = new JsArray();
|
|
var arrayType = context['Array'];
|
|
expect(array.instanceof(arrayType), true);
|
|
expect(array, []);
|
|
// basic check that it behaves like a List
|
|
array.addAll([1, 2, 3]);
|
|
expect(array, [1, 2, 3]);
|
|
});
|
|
|
|
test('new JsArray.from()', () {
|
|
var array = new JsArray.from([1, 2, 3]);
|
|
var arrayType = context['Array'];
|
|
expect(array.instanceof(arrayType), true);
|
|
expect(array, [1, 2, 3]);
|
|
});
|
|
|
|
test('get Array from JS', () {
|
|
context['a'] = new JsObject(context['Array'], [1, 2, 3]);
|
|
expect(context.callMethod('isPropertyInstanceOf', ['a', context['Array']]),
|
|
isTrue);
|
|
var a = context['a'];
|
|
expect(a is JsArray, isTrue);
|
|
expect(a, [1, 2, 3]);
|
|
context.deleteProperty('a');
|
|
});
|
|
|
|
test('pass Array to JS', () {
|
|
context['a'] = [1, 2, 3];
|
|
var a = context['a'];
|
|
expect(a is List, isTrue);
|
|
expect(a is JsArray, isFalse);
|
|
expect(a, [1, 2, 3]);
|
|
context.deleteProperty('a');
|
|
});
|
|
|
|
test('[]', () {
|
|
var array = new JsArray.from([1, 2]);
|
|
expect(array[0], 1);
|
|
expect(array[1], 2);
|
|
expect(() => array[-1], throwsRangeError);
|
|
expect(() => array[2], throwsRangeError);
|
|
});
|
|
|
|
test('[]=', () {
|
|
var array = new JsArray<Object>.from([1, 2]);
|
|
array[0] = 'd';
|
|
array[1] = 'e';
|
|
expect(array, ['d', 'e']);
|
|
expect(() => array[-1] = 3, throwsRangeError);
|
|
expect(() => array[2] = 3, throwsRangeError);
|
|
});
|
|
|
|
test('length', () {
|
|
var array = new JsArray<int?>.from([1, 2, 3]);
|
|
expect(array.length, 3);
|
|
array.add(4);
|
|
expect(array.length, 4);
|
|
array.length = 2;
|
|
expect(array, [1, 2]);
|
|
array.length = 3;
|
|
expect(array, [1, 2, null]);
|
|
});
|
|
|
|
test('add', () {
|
|
var array = new JsArray();
|
|
array.add('a');
|
|
expect(array, ['a']);
|
|
array.add('b');
|
|
expect(array, ['a', 'b']);
|
|
});
|
|
|
|
test('addAll', () {
|
|
var array = new JsArray();
|
|
array.addAll(['a', 'b']);
|
|
expect(array, ['a', 'b']);
|
|
// make sure addAll can handle Iterables
|
|
array.addAll(new Set.from(['c']));
|
|
expect(array, ['a', 'b', 'c']);
|
|
});
|
|
|
|
test('insert', () {
|
|
var array = new JsArray.from([]);
|
|
array.insert(0, 'b');
|
|
expect(array, ['b']);
|
|
array.insert(0, 'a');
|
|
expect(array, ['a', 'b']);
|
|
array.insert(2, 'c');
|
|
expect(array, ['a', 'b', 'c']);
|
|
expect(() => array.insert(4, 'e'), throwsRangeError);
|
|
expect(() => array.insert(-1, 'e'), throwsRangeError);
|
|
});
|
|
|
|
test('removeAt', () {
|
|
var array = new JsArray.from(['a', 'b', 'c']);
|
|
expect(array.removeAt(1), 'b');
|
|
expect(array, ['a', 'c']);
|
|
expect(() => array.removeAt(2), throwsRangeError);
|
|
expect(() => array.removeAt(-1), throwsRangeError);
|
|
});
|
|
|
|
test('removeLast', () {
|
|
var array = new JsArray.from(['a', 'b', 'c']);
|
|
expect(array.removeLast(), 'c');
|
|
expect(array, ['a', 'b']);
|
|
array.length = 0;
|
|
expect(() => array.removeLast(), throwsRangeError);
|
|
});
|
|
|
|
test('removeRange', () {
|
|
var array = new JsArray.from(['a', 'b', 'c', 'd']);
|
|
array.removeRange(1, 3);
|
|
expect(array, ['a', 'd']);
|
|
expect(() => array.removeRange(-1, 2), throwsRangeError);
|
|
expect(() => array.removeRange(0, 3), throwsRangeError);
|
|
expect(() => array.removeRange(2, 1), throwsRangeError);
|
|
});
|
|
|
|
test('setRange', () {
|
|
var array = new JsArray.from(['a', 'b', 'c', 'd']);
|
|
array.setRange(1, 3, ['e', 'f']);
|
|
expect(array, ['a', 'e', 'f', 'd']);
|
|
array.setRange(3, 4, ['g', 'h', 'i'], 1);
|
|
expect(array, ['a', 'e', 'f', 'h']);
|
|
});
|
|
|
|
test('sort', () {
|
|
var array = new JsArray.from(['c', 'a', 'b']);
|
|
array.sort();
|
|
expect(array, ['a', 'b', 'c']);
|
|
});
|
|
|
|
test('sort with a Comparator', () {
|
|
var array = new JsArray.from(['c', 'a', 'b']);
|
|
array.sort((a, b) => -(a.compareTo(b)));
|
|
expect(array, ['c', 'b', 'a']);
|
|
});
|
|
}
|