diff options
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/ordinary-to-primitive.js')
-rw-r--r-- | Userland/Libraries/LibJS/Tests/ordinary-to-primitive.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/ordinary-to-primitive.js b/Userland/Libraries/LibJS/Tests/ordinary-to-primitive.js new file mode 100644 index 0000000000..176beadb76 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/ordinary-to-primitive.js @@ -0,0 +1,23 @@ +test("object with custom toString", () => { + const o = { toString: () => "foo" }; + expect(o + "bar").toBe("foobar"); + expect([o, "bar"].toString()).toBe("foo,bar"); +}); + +test("object with uncallable toString and custom valueOf", () => { + const o = { toString: undefined, valueOf: () => "foo" }; + expect(o + "bar").toBe("foobar"); + expect([o, "bar"].toString()).toBe("foo,bar"); +}); + +test("object with custom valueOf", () => { + const o = { valueOf: () => 42 }; + expect(Number(o)).toBe(42); + expect(o + 1).toBe(43); +}); + +test("object with uncallable valueOf and custom toString", () => { + const o = { valueOf: undefined, toString: () => "42" }; + expect(Number(o)).toBe(42); + expect(o + 1).toBe("421"); +}); |