mirror of
https://github.com/servo/servo.git
synced 2025-08-17 11:25:35 +01:00
Update web-platform-tests to revision 6c2d23b1b5e4dc00c944eedd16a11850e74a2d11
This commit is contained in:
parent
ad83faa745
commit
0114122fe0
140 changed files with 4328 additions and 1419 deletions
|
@ -0,0 +1,72 @@
|
|||
import { StorageArea, storage as defaultArea } from "std:async-local-storage";
|
||||
import { assertArrayCustomEquals } from "./equality-asserters.js";
|
||||
|
||||
export function testWithArea(testFn, description) {
|
||||
promise_test(t => {
|
||||
const area = new StorageArea(description);
|
||||
t.add_cleanup(t => area.clear());
|
||||
|
||||
return testFn(area, t);
|
||||
}, description);
|
||||
}
|
||||
|
||||
export function testWithDefaultArea(testFn, description) {
|
||||
promise_test(t => {
|
||||
t.add_cleanup(t => defaultArea.clear());
|
||||
|
||||
return testFn(defaultArea, t);
|
||||
}, description);
|
||||
}
|
||||
|
||||
// These two functions take a key/value and use them to test
|
||||
// set()/get()/delete()/has()/keys()/values()/entries(). The keyEqualityAsserter should be a
|
||||
// function from ./equality-asserters.js.
|
||||
|
||||
export function testVariousMethodsWithDefaultArea(label, key, value, keyEqualityAsserter) {
|
||||
testWithDefaultArea(testVariousMethodsInner(key, value, keyEqualityAsserter), label);
|
||||
}
|
||||
|
||||
export function testVariousMethods(label, key, value, keyEqualityAsserter) {
|
||||
testWithArea(testVariousMethodsInner(key, value, keyEqualityAsserter), label);
|
||||
}
|
||||
|
||||
function testVariousMethodsInner(key, value, keyEqualityAsserter) {
|
||||
return async area => {
|
||||
await assertPromiseEquals(area.set(key, value), undefined, "set()", "undefined");
|
||||
|
||||
await assertPromiseEquals(area.get(key), value, "get()", "the set value");
|
||||
await assertPromiseEquals(area.has(key), true, "has()", "true");
|
||||
|
||||
const keysPromise = area.keys();
|
||||
assertIsPromise(keysPromise, "keys()");
|
||||
assertArrayCustomEquals(await keysPromise, [key], keyEqualityAsserter, "keys() must have the key");
|
||||
|
||||
const valuesPromise = area.values();
|
||||
assertIsPromise(valuesPromise);
|
||||
assert_array_equals(await valuesPromise, [value], "values() must have the value");
|
||||
|
||||
const entriesPromise = area.entries();
|
||||
assertIsPromise(entriesPromise, "entries()");
|
||||
const entries = await entriesPromise;
|
||||
assert_true(Array.isArray(entries), "entries() must give an array");
|
||||
assert_equals(entries.length, 1, "entries() must have only one value");
|
||||
assert_true(Array.isArray(entries[0]), "entries() 0th element must be an array");
|
||||
assert_equals(entries[0].length, 2, "entries() 0th element must have 2 elements");
|
||||
keyEqualityAsserter(entries[0][0], key, "entries() 0th element's 0th element must be the key");
|
||||
assert_equals(entries[0][1], value, "entries() 0th element's 1st element must be the value");
|
||||
|
||||
await assertPromiseEquals(area.delete(key), undefined, "delete()", "undefined");
|
||||
|
||||
await assertPromiseEquals(area.get(key), undefined, "get()", "undefined after deleting");
|
||||
await assertPromiseEquals(area.has(key), false, "has()", "false after deleting");
|
||||
};
|
||||
}
|
||||
|
||||
async function assertPromiseEquals(promise, expected, label, expectedLabel) {
|
||||
assertIsPromise(promise, label);
|
||||
assert_equals(await promise, expected, label + " must fulfill with " + expectedLabel);
|
||||
}
|
||||
|
||||
function assertIsPromise(promise, label) {
|
||||
assert_equals(promise.constructor, Promise, label + " must return a promise");
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
export function isConstructor(o) {
|
||||
assert_equals(typeof o, "function", "Must be a function according to typeof");
|
||||
assert_true(isConstructorTest(o), "Must be a constructor according to the meta-object protocol");
|
||||
assert_throws(new TypeError(), () => o(), "Attempting to call (not construct) must throw");
|
||||
}
|
||||
|
||||
export function functionLength(o, expected, label) {
|
||||
const lengthExpected = { writable: false, enumerable: false, configurable: true };
|
||||
const { value } = propertyDescriptor(o, "length", lengthExpected);
|
||||
|
||||
assert_equals(value, expected, `${formatLabel(label)}length value`);
|
||||
}
|
||||
|
||||
export function functionName(o, expected, label) {
|
||||
const lengthExpected = { writable: false, enumerable: false, configurable: true };
|
||||
const { value } = propertyDescriptor(o, "name", lengthExpected);
|
||||
|
||||
assert_equals(value, expected, `${formatLabel(label)}name value`);
|
||||
}
|
||||
|
||||
export function hasClassPrototype(o) {
|
||||
const prototypeExpected = { writable: false, enumerable: false, configurable: false };
|
||||
const { value } = propertyDescriptor(o, "prototype", prototypeExpected);
|
||||
assert_equals(typeof value, "object", "prototype must be an object");
|
||||
assert_not_equals(value, null, "prototype must not be null");
|
||||
}
|
||||
|
||||
export function hasPrototypeConstructorLink(klass) {
|
||||
const constructorExpected = { writable: true, enumerable: false, configurable: true };
|
||||
const { value } = propertyDescriptor(klass.prototype, "constructor", constructorExpected);
|
||||
assert_equals(value, klass, "constructor property must match");
|
||||
}
|
||||
|
||||
export function propertyKeys(o, expectedNames, expectedSymbols, label) {
|
||||
label = formatLabel(label);
|
||||
assert_array_equals(Object.getOwnPropertyNames(o), expectedNames, `${label}property names`);
|
||||
assert_array_equals(Object.getOwnPropertySymbols(o), expectedSymbols,
|
||||
`${label}property symbols`);
|
||||
}
|
||||
|
||||
export function methods(o, expectedMethods) {
|
||||
for (const [name, length] of Object.entries(expectedMethods)) {
|
||||
method(o, name, length);
|
||||
}
|
||||
}
|
||||
|
||||
export function accessors(o, expectedAccessors) {
|
||||
for (const [name, accessorTypes] of Object.entries(expectedAccessors)) {
|
||||
accessor(o, name, accessorTypes);
|
||||
}
|
||||
}
|
||||
|
||||
function method(o, prop, length) {
|
||||
const methodExpected = { writable: true, enumerable: false, configurable: true };
|
||||
const { value } = propertyDescriptor(o, prop, methodExpected);
|
||||
|
||||
assert_equals(typeof value, "function", `${prop} method must be a function according to typeof`);
|
||||
assert_false(isConstructorTest(value),
|
||||
`${prop} method must not be a constructor according to the meta-object protocol`);
|
||||
functionLength(value, length, prop);
|
||||
functionName(value, prop, prop);
|
||||
propertyKeys(value, ["length", "name"], [], prop);
|
||||
}
|
||||
|
||||
function accessor(o, prop, expectedAccessorTypes) {
|
||||
const accessorExpected = { enumerable: false, configurable: true };
|
||||
const propDesc = propertyDescriptor(o, prop, accessorExpected);
|
||||
|
||||
for (const possibleType of ["get", "set"]) {
|
||||
const accessorFunc = propDesc[possibleType];
|
||||
if (expectedAccessorTypes.includes(possibleType)) {
|
||||
const label = `${prop}'s ${possibleType}ter`;
|
||||
|
||||
assert_equals(typeof accessorFunc, "function",
|
||||
`${label} must be a function according to typeof`);
|
||||
assert_false(isConstructorTest(accessorFunc),
|
||||
`${label} must not be a constructor according to the meta-object protocol`);
|
||||
|
||||
functionLength(accessorFunc, possibleType === "get" ? 0 : 1, label);
|
||||
functionName(accessorFunc, `${possibleType} ${prop}`, label);
|
||||
propertyKeys(accessorFunc, ["length", "name"], [], label);
|
||||
} else {
|
||||
assert_equals(accessorFunc, undefined, `${prop} must not have a ${possibleType}ter`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function propertyDescriptor(obj, prop, mustMatch) {
|
||||
const propDesc = Object.getOwnPropertyDescriptor(obj, prop);
|
||||
for (const key in Object.keys(mustMatch)) {
|
||||
assert_equals(propDesc[key], mustMatch[key], `${prop} ${key}`);
|
||||
}
|
||||
return propDesc;
|
||||
}
|
||||
|
||||
function isConstructorTest(o) {
|
||||
try {
|
||||
new (new Proxy(o, {construct: () => ({})}));
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function formatLabel(label) {
|
||||
return label !== undefined ? ` ${label}` : "";
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
export function assertEqualDates(actual, expected, label) {
|
||||
assert_equals(expected.constructor, Date,
|
||||
"assertEqualDates usage check: expected must be a Date");
|
||||
|
||||
const labelPart = label === undefined ? "" : `${label}: `;
|
||||
assert_equals(actual.constructor, Date, `${labelPart}must be a Date`);
|
||||
assert_equals(actual.valueOf(), expected.valueOf(), `${labelPart}timestamps must match`);
|
||||
}
|
||||
|
||||
export function assertEqualArrayBuffers(actual, expected, label) {
|
||||
assert_equals(expected.constructor, ArrayBuffer,
|
||||
"assertEqualArrayBuffers usage check: expected must be an ArrayBuffer");
|
||||
|
||||
const labelPart = label === undefined ? "" : `${label}: `;
|
||||
assert_equals(actual.constructor, ArrayBuffer, `${labelPart}must be an ArrayBuffer`);
|
||||
assert_array_equals(new Uint8Array(actual), new Uint8Array(expected), `${labelPart}must match`);
|
||||
}
|
||||
|
||||
export function assertArrayBufferEqualsABView(actual, expected, label) {
|
||||
assert_true(ArrayBuffer.isView(expected),
|
||||
"assertArrayBufferEqualsABView usage check: expected must be an ArrayBuffer view");
|
||||
|
||||
assertEqualArrayBuffers(actual, expected.buffer, label);
|
||||
}
|
||||
|
||||
export function assertArrayCustomEquals(actual, expected, equalityAsserter, label) {
|
||||
assert_true(Array.isArray(expected),
|
||||
"assertArrayCustomEquals usage check: expected must be an Array");
|
||||
|
||||
const labelPart = label === undefined ? "" : `${label}: `;
|
||||
assert_true(Array.isArray(actual), `${labelPart}must be an array`);
|
||||
assert_equals(actual.length, expected.length, `${labelPart}length must be as expected`);
|
||||
|
||||
for (let i = 0; i < actual.length; ++i) {
|
||||
equalityAsserter(actual[i], expected[i], `${labelPart}index ${i}`);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue