2013-07-10 10:49:04 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// Some methods are taken from v8/test/mjsunit/mjsunit.js
|
|
|
|
|
2016-09-06 22:56:52 +00:00
|
|
|
|
|
|
|
function classOf(object) {
|
|
|
|
// Argument must not be null or undefined.
|
|
|
|
var string = Object.prototype.toString.call(object);
|
|
|
|
// String has format [object <ClassName>].
|
|
|
|
return string.substring(8, string.length - 1);
|
|
|
|
}
|
|
|
|
|
2013-07-10 10:49:04 +00:00
|
|
|
/**
|
|
|
|
* Compares two objects for key/value equality.
|
|
|
|
* Returns true if they are equal, false otherwise.
|
|
|
|
*/
|
|
|
|
function deepObjectEquals(a, b) {
|
|
|
|
var aProps = Object.keys(a);
|
|
|
|
aProps.sort();
|
|
|
|
var bProps = Object.keys(b);
|
|
|
|
bProps.sort();
|
|
|
|
if (!deepEquals(aProps, bProps)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < aProps.length; i++) {
|
|
|
|
if (!deepEquals(a[aProps[i]], b[aProps[i]])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two JavaScript values for type and value equality.
|
|
|
|
* It checks internals of arrays and objects.
|
|
|
|
*/
|
|
|
|
function deepEquals(a, b) {
|
|
|
|
if (a === b) {
|
|
|
|
// Check for -0.
|
|
|
|
if (a === 0) return (1 / a) === (1 / b);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (typeof a != typeof b) return false;
|
|
|
|
if (typeof a == 'number') return isNaN(a) && isNaN(b);
|
|
|
|
if (typeof a !== 'object' && typeof a !== 'function') return false;
|
|
|
|
// Neither a nor b is primitive.
|
|
|
|
var objectClass = classOf(a);
|
|
|
|
if (objectClass !== classOf(b)) return false;
|
|
|
|
if (objectClass === 'RegExp') {
|
|
|
|
// For RegExp, just compare pattern and flags using its toString.
|
|
|
|
return (a.toString() === b.toString());
|
|
|
|
}
|
|
|
|
// Functions are only identical to themselves.
|
|
|
|
if (objectClass === 'Function') return false;
|
|
|
|
if (objectClass === 'Array') {
|
|
|
|
var elementCount = 0;
|
|
|
|
if (a.length != b.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < a.length; i++) {
|
|
|
|
if (!deepEquals(a[i], b[i])) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (objectClass == 'String' || objectClass == 'Number' ||
|
|
|
|
objectClass == 'Boolean' || objectClass == 'Date') {
|
|
|
|
if (a.valueOf() !== b.valueOf()) return false;
|
|
|
|
}
|
|
|
|
return deepObjectEquals(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 08:14:51 +00:00
|
|
|
* Throws an exception containing the user_message (if any) and the values.
|
2013-07-10 10:49:04 +00:00
|
|
|
*/
|
2016-04-07 08:14:51 +00:00
|
|
|
function fail(expected, found, user_message = '') {
|
2013-07-10 10:49:04 +00:00
|
|
|
// TODO(cira): Replace String with PrettyPrint for objects and arrays.
|
2016-04-07 08:14:51 +00:00
|
|
|
var message = 'Failure' + (user_message ? ' (' + user_message + ')' : '') +
|
|
|
|
': expected <' + String(expected) + '>, found <' + String(found) + '>.';
|
2013-07-10 10:49:04 +00:00
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws if two variables have different types or values.
|
|
|
|
*/
|
2016-04-07 08:14:51 +00:00
|
|
|
function assertEquals(expected, found, user_message = '') {
|
2013-07-10 10:49:04 +00:00
|
|
|
if (!deepEquals(expected, found)) {
|
2016-04-07 08:14:51 +00:00
|
|
|
fail(expected, found, user_message);
|
2013-07-10 10:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws if value is false.
|
|
|
|
*/
|
2016-04-07 08:14:51 +00:00
|
|
|
function assertTrue(value, user_message = '') {
|
|
|
|
assertEquals(true, value, user_message);
|
2013-07-10 10:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws if value is true.
|
|
|
|
*/
|
2016-04-07 08:14:51 +00:00
|
|
|
function assertFalse(value, user_message = '') {
|
|
|
|
assertEquals(false, value, user_message);
|
2013-07-10 10:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-29 03:11:16 +00:00
|
|
|
/**
|
|
|
|
* Throws if value is null.
|
|
|
|
*/
|
|
|
|
function assertNotNull(value, user_message = '') {
|
|
|
|
if (value === null) {
|
|
|
|
fail("not null", value, user_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 18:27:59 +00:00
|
|
|
/**
|
|
|
|
* Throws if value is undefined.
|
|
|
|
*/
|
|
|
|
function assertNotUndefined(value, user_message = '') {
|
|
|
|
if (value === undefined) {
|
|
|
|
fail("not undefined", value, user_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 03:11:16 +00:00
|
|
|
|
2013-07-10 10:49:04 +00:00
|
|
|
/**
|
2016-04-07 08:14:51 +00:00
|
|
|
* Runs code() and asserts that it throws the specified exception.
|
2013-07-10 10:49:04 +00:00
|
|
|
*/
|
|
|
|
function assertThrows(code, type_opt, cause_opt) {
|
|
|
|
try {
|
|
|
|
if (typeof code == 'function') {
|
|
|
|
code();
|
|
|
|
} else {
|
|
|
|
eval(code);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (typeof type_opt == 'function') {
|
|
|
|
assertInstanceof(e, type_opt);
|
|
|
|
}
|
|
|
|
if (arguments.length >= 3) {
|
2019-09-12 18:24:26 +00:00
|
|
|
assertEquals(cause_opt, e.message, 'thrown exception type mismatch');
|
2013-07-10 10:49:04 +00:00
|
|
|
}
|
|
|
|
// Success.
|
|
|
|
return;
|
|
|
|
}
|
2016-04-07 08:14:51 +00:00
|
|
|
var expected = arguments.length >= 3 ? cause_opt :
|
|
|
|
typeof type_opt == 'function' ? type_opt : 'any exception';
|
|
|
|
fail(expected, 'no exception', 'expected thrown exception');
|
2013-07-10 10:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-07 08:14:51 +00:00
|
|
|
* Runs code() and asserts that it does now throw any exception.
|
2013-07-10 10:49:04 +00:00
|
|
|
*/
|
2016-04-07 08:14:51 +00:00
|
|
|
function assertDoesNotThrow(code, user_message = '') {
|
2013-07-10 10:49:04 +00:00
|
|
|
try {
|
|
|
|
if (typeof code == 'function') {
|
|
|
|
code();
|
|
|
|
} else {
|
|
|
|
eval(code);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2016-04-07 08:14:51 +00:00
|
|
|
fail("no expection", "exception: " + String(e), user_message);
|
2013-07-10 10:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws if obj is not of given type.
|
|
|
|
*/
|
|
|
|
function assertInstanceof(obj, type) {
|
|
|
|
if (!(obj instanceof type)) {
|
|
|
|
var actualTypeName = null;
|
2016-12-23 14:32:16 +00:00
|
|
|
var actualConstructor = Object.getPrototypeOf(obj).constructor;
|
2013-07-10 10:49:04 +00:00
|
|
|
if (typeof actualConstructor == "function") {
|
|
|
|
actualTypeName = actualConstructor.name || String(actualConstructor);
|
|
|
|
}
|
|
|
|
throw new Error('Object <' + obj + '> is not an instance of <' +
|
2016-12-23 14:32:16 +00:00
|
|
|
(type.name || type) + '>' +
|
|
|
|
(actualTypeName ? ' but of < ' + actualTypeName + '>' : ''));
|
2013-07-10 10:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-29 03:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a BCP 47 language tag into locale and extension.
|
|
|
|
*/
|
|
|
|
function splitLanguageTag(tag) {
|
2019-02-14 14:01:43 +00:00
|
|
|
// Search for the beginning of one or more extension tags, each of which
|
|
|
|
// contains a singleton tag followed by one or more subtags. The equivalent
|
|
|
|
// regexp is: /(-[0-9A-Za-z](-[0-9A-Za-z]{2,8})+)+$/. For example, in
|
|
|
|
// 'de-DE-u-co-phonebk' the matched extension tags are '-u-co-phonebk'.
|
|
|
|
//
|
|
|
|
// The below is a mini-parser that reads backwards from the end of the string.
|
|
|
|
|
|
|
|
function charCode(char) { return char.charCodeAt(0); }
|
|
|
|
function isAlphaNumeric(code) {
|
|
|
|
return (charCode("0") <= code && code <= charCode("9")) ||
|
|
|
|
(charCode("A") <= code && code <= charCode("Z")) ||
|
|
|
|
(charCode("a") <= code && code <= charCode("z"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const MATCH_SUBTAG = 0;
|
|
|
|
const MATCH_SINGLETON_OR_SUBTAG = 1;
|
|
|
|
let state = MATCH_SUBTAG;
|
|
|
|
|
|
|
|
const MINIMUM_TAG_LENGTH = 2;
|
|
|
|
const MAXIMUM_TAG_LENGTH = 8;
|
|
|
|
let currentTagLength = 0;
|
|
|
|
|
|
|
|
// -1 signifies failure, a non-negative integer is the start index of the
|
|
|
|
// extension tag.
|
|
|
|
let extensionTagStartIndex = -1;
|
|
|
|
|
|
|
|
for (let i = tag.length - 1; i >= 0; i--) {
|
|
|
|
const currentCharCode = tag.charCodeAt(i);
|
|
|
|
if (currentCharCode == charCode("-")) {
|
|
|
|
if (state == MATCH_SINGLETON_OR_SUBTAG && currentTagLength == 1) {
|
|
|
|
// Found the singleton tag, the match succeeded.
|
|
|
|
// Save the matched index, and reset the state. After this point, we
|
|
|
|
// definitely have a match, but we may still find another extension tag
|
|
|
|
// sequence.
|
|
|
|
extensionTagStartIndex = i;
|
|
|
|
state = MATCH_SUBTAG;
|
|
|
|
currentTagLength = 0;
|
|
|
|
} else if (MINIMUM_TAG_LENGTH <= currentTagLength &&
|
|
|
|
currentTagLength <= MAXIMUM_TAG_LENGTH) {
|
|
|
|
// Found a valid subtag.
|
|
|
|
state = MATCH_SINGLETON_OR_SUBTAG;
|
|
|
|
currentTagLength = 0;
|
|
|
|
} else {
|
|
|
|
// Invalid subtag (too short or too long).
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (isAlphaNumeric(currentCharCode)) {
|
|
|
|
// An alphanumeric character is potentially part of a tag.
|
|
|
|
currentTagLength++;
|
|
|
|
} else {
|
|
|
|
// Any other character is invalid.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extensionTagStartIndex != -1) {
|
|
|
|
return { locale: tag.substring(0, extensionTagStartIndex),
|
|
|
|
extension: tag.substring(extensionTagStartIndex) };
|
2017-12-29 03:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { locale: tag, extension: '' };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw if |parent| is not a more general language tag of |child|, nor |child|
|
|
|
|
* itself, per BCP 47 rules.
|
|
|
|
*/
|
|
|
|
function assertLanguageTag(child, parent) {
|
|
|
|
var childSplit = splitLanguageTag(child);
|
|
|
|
var parentSplit = splitLanguageTag(parent);
|
|
|
|
|
|
|
|
// Do not compare extensions at this moment, as %GetDefaultICULocale()
|
|
|
|
// doesn't always output something we support.
|
|
|
|
if (childSplit.locale !== parentSplit.locale &&
|
|
|
|
!childSplit.locale.startsWith(parentSplit.locale + '-')) {
|
|
|
|
fail(child, parent, 'language tag comparison');
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 21:15:42 +00:00
|
|
|
|
|
|
|
function assertArrayEquals(expected, found, name_opt) {
|
|
|
|
var start = "";
|
|
|
|
if (name_opt) {
|
|
|
|
start = name_opt + " - ";
|
|
|
|
}
|
|
|
|
assertEquals(expected.length, found.length, start + "array length");
|
|
|
|
if (expected.length === found.length) {
|
|
|
|
for (var i = 0; i < expected.length; ++i) {
|
|
|
|
assertEquals(expected[i], found[i],
|
|
|
|
start + "array element at index " + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|