ladybird/Libraries/LibJS/Tests/object-spread.js
mattco98 104969a9f5 LibJS: Add spreading in object literals
Supports spreading strings, arrays, and other objects within object
literals.
2020-04-28 20:37:21 +02:00

72 lines
No EOL
1.4 KiB
JavaScript

load("test-common.js");
function testObjSpread(obj) {
return obj.foo === 0 &&
obj.bar === 1 &&
obj.baz === 2 &&
obj.qux === 3;
}
function testObjStrSpread(obj) {
return obj[0] === "a" &&
obj[1] === "b" &&
obj[2] === "c" &&
obj[3] === "d";
}
try {
let obj = {
foo: 0,
...{ bar: 1, baz: 2 },
qux: 3,
};
assert(testObjSpread(obj));
obj = { foo: 0, bar: 1, baz: 2 };
obj.qux = 3;
assert(testObjSpread({ ...obj }));
let a = { bar: 1, baz: 2 };
obj = { foo: 0, ...a, qux: 3 };
assert(testObjSpread(obj));
obj = {
...{},
...{
...{ foo: 0, bar: 1, baz: 2 },
},
qux: 3,
};
assert(testObjSpread(obj));
obj = { ..."abcd" };
assert(testObjStrSpread(obj));
obj = { ...["a", "b", "c", "d"] };
assert(testObjStrSpread(obj));
obj = { ...String("abcd") };
assert(testObjStrSpread(obj));
a = { foo: 0 };
Object.defineProperty(a, 'bar', {
value: 1,
enumerable: false,
});
obj = { ...a };
assert(obj.foo === 0 && obj.bar === undefined);
let empty = ({
...undefined,
...null,
...1,
...true,
...function(){},
...Date,
});
assert(Object.getOwnPropertyNames(empty).length === 0);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}