summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibJS/Tests')
-rw-r--r--Libraries/LibJS/Tests/object-spread.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/object-spread.js b/Libraries/LibJS/Tests/object-spread.js
new file mode 100644
index 0000000000..18fab7abb8
--- /dev/null
+++ b/Libraries/LibJS/Tests/object-spread.js
@@ -0,0 +1,72 @@
+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);
+} \ No newline at end of file