X-Git-Url: https://git.ladys.computer/Pisces/blobdiff_plain/e8d0d3ab4f8a844e15873345c0564929c80fdb74..3edbac676b6d5d58417dc813f9deefbe21d12a96:/object.test.js diff --git a/object.test.js b/object.test.js index da890d3..2b6cbb8 100644 --- a/object.test.js +++ b/object.test.js @@ -22,12 +22,251 @@ import { defineOwnProperties, deleteOwnProperty, frozenCopy, + LazyLoader, PropertyDescriptor, setPropertyValue, toObject, toPropertyKey, } from "./object.js"; +describe("LazyLoader", () => { + const symbol = Symbol(); + const prototype = {}; + const etaoinMethod = spy(() => "success"); + const shrdluMethod = spy(() => "success"); + const cmfwypMethod = spy(() => "success"); + const vbgkqjMethod = spy(() => "success"); + const methodsObject = Object.create( + prototype, + { + etaoin: { + configurable: false, + enumerable: true, + value: etaoinMethod, + writable: false, + }, + shrdlu: { + configurable: true, + enumerable: false, + value: shrdluMethod, + writable: false, + }, + cmfwyp: { + configurable: true, + enumerable: false, + get() { + return cmfwypMethod; + }, + }, + vbgkqj: { + configurable: false, + enumerable: true, + get() { + return vbgkqjMethod; + }, + set(_) {}, + }, + xzfiflffffi: { configurable: true, enumerable: false, set(_) {} }, + [symbol]: { + configurable: true, + enumerable: false, + value: "failure", + writable: true, + }, + }, + ); + + it("[[Construct]] creates a new object which inherits from the correct prototype", () => { + assertStrictEquals( + Object.getPrototypeOf(new LazyLoader(methodsObject)), + prototype, + ); + }); + + it("[[Construct]] creates a new object with the desired properties", () => { + assertEquals( + Reflect.ownKeys(new LazyLoader(methodsObject)), + ["etaoin", "shrdlu", "cmfwyp", "vbgkqj", "xzfiflffffi", symbol], + ); + }); + + it("[[Construct]] creates a new object with configurable properties", () => { + assertEquals( + Object.fromEntries( + function* (ll) { + for (const key of Reflect.ownKeys(ll)) { + yield [ + key, + Object.getOwnPropertyDescriptor(ll, key).configurable, + ]; + } + }(new LazyLoader(methodsObject)), + ), + { + etaoin: true, + shrdlu: true, + cmfwyp: true, + vbgkqj: true, + xzfiflffffi: true, + [symbol]: true, + }, + ); + }); + + it("[[Construct]] creates a new object with the correct enumerability", () => { + assertEquals( + Object.fromEntries( + function* (ll) { + for (const key of Reflect.ownKeys(ll)) { + yield [ + key, + Object.getOwnPropertyDescriptor(ll, key).enumerable, + ]; + } + }(new LazyLoader(methodsObject)), + ), + { + etaoin: true, + shrdlu: false, + cmfwyp: false, + vbgkqj: true, + xzfiflffffi: false, + [symbol]: false, + }, + ); + }); + + it("[[Construct]] creates a new object with defined getters", () => { + assertEquals( + Object.fromEntries( + function* (ll) { + for (const key of Reflect.ownKeys(ll)) { + yield [ + key, + Object.getOwnPropertyDescriptor(ll, key).get !== void {}, + ]; + } + }(new LazyLoader(methodsObject)), + ), + { + etaoin: true, + shrdlu: true, + cmfwyp: true, + vbgkqj: true, + xzfiflffffi: true, + [symbol]: true, + }, + ); + }); + + it("[[Construct]] creates a new object with defined setters for writable properties only", () => { + assertEquals( + Object.fromEntries( + function* (ll) { + for (const key of Reflect.ownKeys(ll)) { + yield [ + key, + Object.getOwnPropertyDescriptor(ll, key).set !== void {}, + ]; + } + }(new LazyLoader(methodsObject)), + ), + { + etaoin: false, + shrdlu: false, + cmfwyp: false, + vbgkqj: false, + xzfiflffffi: false, + [symbol]: true, + }, + ); + }); + + describe("[[Construct]] creates a new object with correct getter behaviour", () => { + const ll = new LazyLoader(methodsObject); + ll.etaoin; + assertEquals( + Object.getOwnPropertyDescriptor(ll, "etaoin"), + { + configurable: false, + enumerable: true, + value: "success", + writable: false, + }, + ); + assertSpyCalls(etaoinMethod, 1); + assertSpyCall(etaoinMethod, 0, { + args: [], + self: ll, + returned: "success", + }); + ll.shrdlu; + assertEquals( + Object.getOwnPropertyDescriptor(ll, "shrdlu"), + { + configurable: true, + enumerable: false, + value: "success", + writable: false, + }, + ); + assertSpyCalls(shrdluMethod, 1); + assertSpyCall(shrdluMethod, 0, { + args: [], + self: ll, + returned: "success", + }); + ll.cmfwyp; + assertEquals( + Object.getOwnPropertyDescriptor(ll, "cmfwyp"), + { + configurable: true, + enumerable: false, + value: "success", + writable: false, + }, + ); + assertSpyCalls(cmfwypMethod, 1); + assertSpyCall(cmfwypMethod, 0, { + args: [], + self: ll, + returned: "success", + }); + ll.vbgkqj; + assertEquals( + Object.getOwnPropertyDescriptor(ll, "vbgkqj"), + { + configurable: false, + enumerable: true, + value: "success", + writable: false, + }, + ); + assertSpyCalls(vbgkqjMethod, 1); + assertSpyCall(vbgkqjMethod, 0, { + args: [], + self: ll, + returned: "success", + }); + assertThrows(() => ll.xzfiflffffi); + assertThrows(() => ll[symbol]); + }); + + describe("[[Construct]] creates a new object with correct setter behaviour", () => { + const ll = new LazyLoader(methodsObject); + ll[symbol] = "success"; + assertEquals( + Object.getOwnPropertyDescriptor(ll, symbol), + { + configurable: true, + enumerable: false, + value: "success", + writable: true, + }, + ); + }); +}); + describe("PropertyDescriptor", () => { it("[[Construct]] creates a new PropertyDescriptor", () => { assertStrictEquals(