diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-07-02 16:09:21 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-03 19:30:13 +0200 |
commit | 4c48c9d69d3a949b7e434108173eee604a3235c1 (patch) | |
tree | bb574a33d977c6bf0d71f2fe6a26abb6f65cf8ff /Libraries/LibJS/Tests/builtins/Reflect | |
parent | 21064a1883d61624f4ec4069c80e0cd8aac0c1ea (diff) | |
download | serenity-4c48c9d69d3a949b7e434108173eee604a3235c1.zip |
LibJS: Reorganize tests into subfolders
Diffstat (limited to 'Libraries/LibJS/Tests/builtins/Reflect')
13 files changed, 562 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.apply.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.apply.js new file mode 100644 index 0000000000..15060eea4c --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.apply.js @@ -0,0 +1,37 @@ +load("test-common.js"); + +try { + assert(Reflect.apply.length === 3); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.apply(value); + }, { + error: TypeError, + message: "First argument of Reflect.apply() must be a function" + }); + + assertThrowsError(() => { + Reflect.apply(() => {}, undefined, value); + }, { + error: TypeError, + message: "Arguments list must be an object" + }); + }); + + assert(Reflect.apply(String.prototype.charAt, "foo", [0]) === "f"); + assert(Reflect.apply(Array.prototype.indexOf, ["hello", 123, "foo", "bar"], ["foo"]) === 2); + + function Foo(foo) { + this.foo = foo; + } + + var o = {}; + assert(o.foo === undefined); + assert(Reflect.apply(Foo, o, ["bar"]) === undefined); + assert(o.foo === "bar"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js new file mode 100644 index 0000000000..f65bcd3356 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js @@ -0,0 +1,54 @@ +load("test-common.js"); + +try { + assert(Reflect.construct.length === 2); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.construct(value); + }, { + error: TypeError, + message: "First argument of Reflect.construct() must be a function" + }); + + assertThrowsError(() => { + Reflect.construct(() => {}, value); + }, { + error: TypeError, + message: "Arguments list must be an object" + }); + + assertThrowsError(() => { + Reflect.construct(() => {}, [], value); + }, { + error: TypeError, + message: "Optional third argument of Reflect.construct() must be a constructor" + }); + }); + + var a = Reflect.construct(Array, [5]); + assert(a instanceof Array); + assert(a.length === 5); + + var s = Reflect.construct(String, [123]); + assert(s instanceof String); + assert(s.length === 3); + assert(s.toString() === "123"); + + function Foo() { + this.name = "foo"; + } + + function Bar() { + this.name = "bar"; + } + + var o = Reflect.construct(Foo, [], Bar); + assert(o.name === "foo"); + assert(o instanceof Foo === false); + assert(o instanceof Bar === true); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.defineProperty.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.defineProperty.js new file mode 100644 index 0000000000..3046357ccc --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.defineProperty.js @@ -0,0 +1,59 @@ +load("test-common.js"); + +try { + assert(Reflect.defineProperty.length === 3); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.defineProperty(value); + }, { + error: TypeError, + message: "First argument of Reflect.defineProperty() must be an object" + }); + + assertThrowsError(() => { + Reflect.defineProperty({}, "foo", value); + }, { + error: TypeError, + message: "Descriptor argument is not an object" + }); + }); + + var o = {}; + + assert(Reflect.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false }) === true); + assert(o.foo === 1); + o.foo = 2; + assert(o.foo === 1); + + assert(Reflect.defineProperty(o, "bar", { value: "hi", writable: true, enumerable: true }) === true); + assert(o.bar === "hi"); + o.bar = "ho"; + assert(o.bar === "ho"); + + assert(Reflect.defineProperty(o, "bar", { value: "xx", enumerable: false }) === false); + + var d = Reflect.getOwnPropertyDescriptor(o, "foo"); + assert(d.configurable === false); + assert(d.enumerable === false); + assert(d.writable === false); + assert(d.value === 1); + + d = Reflect.getOwnPropertyDescriptor(o, "bar"); + assert(d.configurable === false); + assert(d.enumerable === true); + assert(d.writable === true); + assert(d.value === "ho"); + + assert(Reflect.defineProperty(o, "baz", { value: 9, configurable: true, writable: false }) === true); + assert(Reflect.defineProperty(o, "baz", { configurable: true, writable: true }) === true); + + d = Reflect.getOwnPropertyDescriptor(o, "baz"); + assert(d.configurable === true); + assert(d.writable === true); + assert(d.value === 9); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.deleteProperty.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.deleteProperty.js new file mode 100644 index 0000000000..8dcf330fc1 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.deleteProperty.js @@ -0,0 +1,47 @@ +load("test-common.js"); + +try { + assert(Reflect.deleteProperty.length === 2); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.deleteProperty(value); + }, { + error: TypeError, + message: "First argument of Reflect.deleteProperty() must be an object" + }); + }); + + assert(Reflect.deleteProperty({}) === true); + assert(Reflect.deleteProperty({}, "foo") === true); + + var o = { foo: 1 }; + assert(o.foo === 1); + assert(Reflect.deleteProperty(o, "foo") === true); + assert(o.foo === undefined); + assert(Reflect.deleteProperty(o, "foo") === true); + assert(o.foo === undefined); + + Object.defineProperty(o, "bar", { value: 2, configurable: true, writable: false }); + assert(Reflect.deleteProperty(o, "bar") === true); + assert(o.bar === undefined); + + Object.defineProperty(o, "baz", { value: 3, configurable: false, writable: true }); + assert(Reflect.deleteProperty(o, "baz") === false); + assert(o.baz === 3); + + var a = [1, 2, 3]; + assert(a.length === 3); + assert(a[0] === 1); + assert(a[1] === 2); + assert(a[2] === 3); + assert(Reflect.deleteProperty(a, 1) === true); + assert(a.length === 3); + assert(a[0] === 1); + assert(a[1] === undefined); + assert(a[2] === 3); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.get.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.get.js new file mode 100644 index 0000000000..0ad987132f --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.get.js @@ -0,0 +1,56 @@ +load("test-common.js"); + +try { + assert(Reflect.get.length === 2); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.get(value); + }, { + error: TypeError, + message: "First argument of Reflect.get() must be an object" + }); + }); + + assert(Reflect.get({}) === undefined); + assert(Reflect.get({ undefined: 1 }) === 1); + assert(Reflect.get({ foo: 1 }) === undefined); + assert(Reflect.get({ foo: 1 }, "foo") === 1); + + assert(Reflect.get([]) === undefined); + assert(Reflect.get([1, 2, 3]) === undefined); + assert(Reflect.get([1, 2, 3], "0") === 1); + assert(Reflect.get([1, 2, 3], 0) === 1); + assert(Reflect.get([1, 2, 3], 1) === 2); + assert(Reflect.get([1, 2, 3], 2) === 3); + assert(Reflect.get([1, 2, 3], 4) === undefined); + + assert(Reflect.get(new String()) === undefined); + assert(Reflect.get(new String(), 0) === undefined); + assert(Reflect.get(new String("foo"), "0") === "f"); + assert(Reflect.get(new String("foo"), 0) === "f"); + assert(Reflect.get(new String("foo"), 1) === "o"); + assert(Reflect.get(new String("foo"), 2) === "o"); + assert(Reflect.get(new String("foo"), 3) === undefined); + + const foo = { + get prop() { + this.getPropCalled = true; + } + }; + const bar = {}; + Object.setPrototypeOf(bar, foo); + + assert(foo.getPropCalled === undefined); + assert(bar.getPropCalled === undefined); + Reflect.get(bar, "prop"); + assert(foo.getPropCalled === undefined); + assert(bar.getPropCalled === true); + Reflect.get(bar, "prop", foo); + assert(foo.getPropCalled === true); + assert(bar.getPropCalled === true); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.getOwnPropertyDescriptor.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.getOwnPropertyDescriptor.js new file mode 100644 index 0000000000..1ed500c6ae --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.getOwnPropertyDescriptor.js @@ -0,0 +1,35 @@ +load("test-common.js"); + +try { + assert(Reflect.getOwnPropertyDescriptor.length === 2); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.getOwnPropertyDescriptor(value); + }, { + error: TypeError, + message: "First argument of Reflect.getOwnPropertyDescriptor() must be an object" + }); + }); + + assert(Reflect.getOwnPropertyDescriptor({}) === undefined); + assert(Reflect.getOwnPropertyDescriptor({}, "foo") === undefined); + + var o = { foo: "bar" }; + var d = Reflect.getOwnPropertyDescriptor(o, "foo"); + assert(d.value === "bar"); + assert(d.writable === true); + assert(d.enumerable === true); + assert(d.configurable === true); + + var a = []; + d = Reflect.getOwnPropertyDescriptor(a, "length"); + assert(d.value === 0); + assert(d.writable === true); + assert(d.enumerable === false); + assert(d.configurable === false); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.getPrototypeOf.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.getPrototypeOf.js new file mode 100644 index 0000000000..7cd3999b7e --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.getPrototypeOf.js @@ -0,0 +1,26 @@ +load("test-common.js"); + +try { + assert(Reflect.getPrototypeOf.length === 1); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.getPrototypeOf(value); + }, { + error: TypeError, + message: "First argument of Reflect.getPrototypeOf() must be an object" + }); + }); + + assert(Reflect.getPrototypeOf({}) === Object.prototype); + assert(Reflect.getPrototypeOf([]) === Array.prototype); + assert(Reflect.getPrototypeOf(new String()) === String.prototype); + + var o = {}; + Reflect.setPrototypeOf(o, { foo: "bar" }); + assert(Reflect.getPrototypeOf(o).foo === "bar"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.has.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.has.js new file mode 100644 index 0000000000..74ec0e1c2d --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.has.js @@ -0,0 +1,42 @@ +load("test-common.js"); + +try { + assert(Reflect.has.length === 2); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.has(value); + }, { + error: TypeError, + message: "First argument of Reflect.has() must be an object" + }); + }); + + assert(Reflect.has({}) === false); + assert(Reflect.has({ undefined }) === true); + assert(Reflect.has({ 0: "1" }, "0") === true); + assert(Reflect.has({ foo: "bar" }, "foo") === true); + assert(Reflect.has({ bar: "baz" }, "foo") === false); + assert(Reflect.has({}, "toString") === true); + + assert(Reflect.has([]) === false); + assert(Reflect.has([], 0) === false); + assert(Reflect.has([1, 2, 3], "0") === true); + assert(Reflect.has([1, 2, 3], 0) === true); + assert(Reflect.has([1, 2, 3], 1) === true); + assert(Reflect.has([1, 2, 3], 2) === true); + assert(Reflect.has([1, 2, 3], 3) === false); + assert(Reflect.has([], "pop") === true); + + assert(Reflect.has(new String()) === false); + assert(Reflect.has(new String("foo"), "0") === true); + assert(Reflect.has(new String("foo"), 0) === true); + assert(Reflect.has(new String("foo"), 1) === true); + assert(Reflect.has(new String("foo"), 2) === true); + assert(Reflect.has(new String("foo"), 3) === false); + assert(Reflect.has(new String("foo"), "charAt") === true); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.isExtensible.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.isExtensible.js new file mode 100644 index 0000000000..104e725d7d --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.isExtensible.js @@ -0,0 +1,29 @@ +load("test-common.js"); + +try { + assert(Reflect.isExtensible.length === 1); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.isExtensible(value); + }, { + error: TypeError, + message: "First argument of Reflect.isExtensible() must be an object" + }); + }); + + assert(Reflect.isExtensible({}) === true); + + var o = {}; + o.foo = "foo"; + assert(o.foo === "foo"); + assert(Reflect.isExtensible(o) === true); + Reflect.preventExtensions(o); + o.bar = "bar"; + assert(o.bar === undefined); + assert(Reflect.isExtensible(o) === false); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.ownKeys.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.ownKeys.js new file mode 100644 index 0000000000..9f4551c1dc --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.ownKeys.js @@ -0,0 +1,39 @@ +load("test-common.js"); + +try { + assert(Reflect.ownKeys.length === 1); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.ownKeys(value); + }, { + error: TypeError, + message: "First argument of Reflect.ownKeys() must be an object" + }); + }); + + var objectOwnKeys = Reflect.ownKeys({}); + assert(objectOwnKeys.length === 0); + + objectOwnKeys = Reflect.ownKeys({ foo: "bar", bar: "baz", 0: 42 }); + assert(objectOwnKeys.length === 3); + assert(objectOwnKeys[0] === "0"); + assert(objectOwnKeys[1] === "foo"); + assert(objectOwnKeys[2] === "bar"); + + var arrayOwnKeys = Reflect.ownKeys([]); + assert(arrayOwnKeys.length === 1); + assert(arrayOwnKeys[0] === "length"); + + arrayOwnKeys = Reflect.ownKeys(["foo", [], 123, undefined]); + assert(arrayOwnKeys.length === 5); + assert(arrayOwnKeys[0] === "0"); + assert(arrayOwnKeys[1] === "1"); + assert(arrayOwnKeys[2] === "2"); + assert(arrayOwnKeys[3] === "3"); + assert(arrayOwnKeys[4] === "length"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.preventExtensions.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.preventExtensions.js new file mode 100644 index 0000000000..efbda277d4 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.preventExtensions.js @@ -0,0 +1,23 @@ +load("test-common.js"); + +try { + assert(Reflect.preventExtensions.length === 1); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.preventExtensions(value); + }, { + error: TypeError, + message: "First argument of Reflect.preventExtensions() must be an object" + }); + }); + + var o = {}; + assert(Reflect.isExtensible(o) === true); + assert(Reflect.preventExtensions(o) === true); + assert(Reflect.isExtensible(o) === false); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.set.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.set.js new file mode 100644 index 0000000000..08f095f191 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.set.js @@ -0,0 +1,72 @@ +load("test-common.js"); + +try { + assert(Reflect.set.length === 3); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.set(value); + }, { + error: TypeError, + message: "First argument of Reflect.set() must be an object" + }); + }); + + assert(Reflect.set({}) === true); + assert(Reflect.set({}, "foo") === true); + assert(Reflect.set({}, "foo", "bar") === true); + + var o = {}; + assert(o.foo === undefined); + assert(Reflect.set(o, "foo", 1) === true); + assert(o.foo === 1); + assert(Reflect.set(o, "foo", 2) === true); + assert(o.foo === 2); + + Object.defineProperty(o, "bar", { value: 2, configurable: true, writable: false }); + assert(Reflect.set(o, "bar") === false); + assert(o.bar === 2); + + Object.defineProperty(o, "baz", { value: 3, configurable: false, writable: true }); + assert(Reflect.set(o, "baz") === true); + assert(o.baz === undefined); + + var a = []; + assert(a.length === 0); + assert(Reflect.set(a, "0") === true); + assert(a.length === 1); + assert(a[0] === undefined); + assert(Reflect.set(a, 1, "foo") === true); + assert(a.length === 2); + assert(a[0] === undefined); + assert(a[1] === "foo"); + assert(Reflect.set(a, 4, "bar") === true); + assert(a.length === 5); + assert(a[0] === undefined); + assert(a[1] === "foo"); + assert(a[2] === undefined); + assert(a[3] === undefined); + assert(a[4] === "bar"); + + + const foo = { + set prop(value) { + this.setPropCalled = true; + } + }; + const bar = {}; + Object.setPrototypeOf(bar, foo); + + assert(foo.setPropCalled === undefined); + assert(bar.setPropCalled === undefined); + Reflect.set(bar, "prop", 42); + assert(foo.setPropCalled === undefined); + assert(bar.setPropCalled === true); + Reflect.set(bar, "prop", 42, foo); + assert(foo.setPropCalled === true); + assert(bar.setPropCalled === true); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/builtins/Reflect/Reflect.setPrototypeOf.js b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.setPrototypeOf.js new file mode 100644 index 0000000000..81758ff3ec --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Reflect/Reflect.setPrototypeOf.js @@ -0,0 +1,43 @@ +load("test-common.js"); + +try { + assert(Reflect.setPrototypeOf.length === 2); + + [null, undefined, "foo", 123, NaN, Infinity].forEach(value => { + assertThrowsError(() => { + Reflect.setPrototypeOf(value); + }, { + error: TypeError, + message: "First argument of Reflect.setPrototypeOf() must be an object" + }); + if (value === null) + return; + assertThrowsError(() => { + Reflect.setPrototypeOf({}, value); + }, { + error: TypeError, + message: "Prototype must be an object or null" + }); + }); + + assert(Reflect.setPrototypeOf({}, null) === true); + assert(Reflect.setPrototypeOf({}, {}) === true); + assert(Reflect.setPrototypeOf({}, Object.prototype) === true); + assert(Reflect.setPrototypeOf({}, Array.prototype) === true); + assert(Reflect.setPrototypeOf({}, String.prototype) === true); + assert(Reflect.setPrototypeOf({}, Reflect.getPrototypeOf({})) === true); + + var o = {}; + var p = { foo: "bar" }; + assert(o.foo === undefined); + assert(Reflect.setPrototypeOf(o, p) === true); + assert(o.foo === "bar"); + + Reflect.preventExtensions(o); + assert(Reflect.setPrototypeOf(o, {}) === false); + assert(Reflect.setPrototypeOf(o, p) === true); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |