diff options
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/builtins/String')
25 files changed, 520 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.fromCharCode.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.fromCharCode.js new file mode 100644 index 0000000000..c6b52fd2e1 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.fromCharCode.js @@ -0,0 +1,17 @@ +test("basic functionality", () => { + expect(String.fromCharCode).toHaveLength(1); + + expect(String.fromCharCode()).toBe(""); + expect(String.fromCharCode(0)).toBe("\u0000"); + expect(String.fromCharCode(false)).toBe("\u0000"); + expect(String.fromCharCode(null)).toBe("\u0000"); + expect(String.fromCharCode(undefined)).toBe("\u0000"); + expect(String.fromCharCode(1)).toBe("\u0001"); + expect(String.fromCharCode(true)).toBe("\u0001"); + expect(String.fromCharCode(-1)).toBe("\uffff"); + expect(String.fromCharCode(0xffff)).toBe("\uffff"); + expect(String.fromCharCode(0x123ffff)).toBe("\uffff"); + expect(String.fromCharCode(65)).toBe("A"); + expect(String.fromCharCode(65, 66, 67)).toBe("ABC"); + expect(String.fromCharCode(228, 246, 252)).toBe("äöü"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.js new file mode 100644 index 0000000000..687e0929d4 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.js @@ -0,0 +1,9 @@ +test("constructor properties", () => { + expect(String).toHaveLength(1); + expect(String.name).toBe("String"); +}); + +test("typeof", () => { + expect(typeof String()).toBe("string"); + expect(typeof new String()).toBe("object"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype-generic-functions.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype-generic-functions.js new file mode 100644 index 0000000000..a05ca53d8c --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype-generic-functions.js @@ -0,0 +1,39 @@ +test("basic functionality", () => { + const genericStringPrototypeFunctions = [ + "charAt", + "charCodeAt", + "repeat", + "startsWith", + "endsWith", + "indexOf", + "toLowerCase", + "toUpperCase", + "padStart", + "padEnd", + "trim", + "trimStart", + "trimEnd", + "concat", + "substring", + "includes", + "slice", + ]; + + genericStringPrototypeFunctions.forEach(name => { + String.prototype[name].call({ toString: () => "hello friends" }); + String.prototype[name].call({ toString: () => 123 }); + String.prototype[name].call({ toString: () => undefined }); + + expect(() => { + String.prototype[name].call({ toString: () => new String() }); + }).toThrowWithMessage(TypeError, "Cannot convert object to string"); + + expect(() => { + String.prototype[name].call({ toString: () => [] }); + }).toThrowWithMessage(TypeError, "Cannot convert object to string"); + + expect(() => { + String.prototype[name].call({ toString: () => ({}) }); + }).toThrowWithMessage(TypeError, "Cannot convert object to string"); + }); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charAt.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charAt.js new file mode 100644 index 0000000000..7747331885 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charAt.js @@ -0,0 +1,20 @@ +test("basic functionality", () => { + expect(String.prototype.charAt).toHaveLength(1); + + var s = "foobar"; + expect(typeof s).toBe("string"); + expect(s).toHaveLength(6); + + expect(s.charAt(0)).toBe("f"); + expect(s.charAt(1)).toBe("o"); + expect(s.charAt(2)).toBe("o"); + expect(s.charAt(3)).toBe("b"); + expect(s.charAt(4)).toBe("a"); + expect(s.charAt(5)).toBe("r"); + expect(s.charAt(6)).toBe(""); + + expect(s.charAt()).toBe("f"); + expect(s.charAt(NaN)).toBe("f"); + expect(s.charAt("foo")).toBe("f"); + expect(s.charAt(undefined)).toBe("f"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js new file mode 100644 index 0000000000..e7ebd9d0d3 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js @@ -0,0 +1,21 @@ +test("basic functionality", () => { + expect(String.prototype.charAt).toHaveLength(1); + + var s = "Foobar"; + expect(typeof s).toBe("string"); + expect(s).toHaveLength(6); + + expect(s.charCodeAt(0)).toBe(70); + expect(s.charCodeAt(1)).toBe(111); + expect(s.charCodeAt(2)).toBe(111); + expect(s.charCodeAt(3)).toBe(98); + expect(s.charCodeAt(4)).toBe(97); + expect(s.charCodeAt(5)).toBe(114); + expect(s.charCodeAt(6)).toBe(NaN); + expect(s.charCodeAt(-1)).toBe(NaN); + + expect(s.charCodeAt()).toBe(70); + expect(s.charCodeAt(NaN)).toBe(70); + expect(s.charCodeAt("foo")).toBe(70); + expect(s.charCodeAt(undefined)).toBe(70); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.concat.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.concat.js new file mode 100644 index 0000000000..7a38877e9e --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.concat.js @@ -0,0 +1,17 @@ +test("basic functionality", () => { + expect(String.prototype.concat).toHaveLength(1); + + expect("".concat(1)).toBe("1"); + expect("".concat(3, 2, 1)).toBe("321"); + expect("hello".concat(" ", "friends")).toBe("hello friends"); + expect("".concat(null)).toBe("null"); + expect("".concat(false)).toBe("false"); + expect("".concat(true)).toBe("true"); + expect("".concat([])).toBe(""); + expect("".concat([1, 2, 3, "hello"])).toBe("1,2,3,hello"); + expect("".concat(true, [])).toBe("true"); + expect("".concat(true, false)).toBe("truefalse"); + expect("".concat({})).toBe("[object Object]"); + expect("".concat(1, {})).toBe("1[object Object]"); + expect("".concat(1, {}, false)).toBe("1[object Object]false"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.endsWith.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.endsWith.js new file mode 100644 index 0000000000..286cc6b1b2 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.endsWith.js @@ -0,0 +1,42 @@ +test("basic functionality", () => { + expect(String.prototype.endsWith).toHaveLength(1); + + var s = "foobar"; + expect(s.endsWith("r")).toBeTrue(); + expect(s.endsWith("ar")).toBeTrue(); + expect(s.endsWith("bar")).toBeTrue(); + expect(s.endsWith("obar")).toBeTrue(); + expect(s.endsWith("oobar")).toBeTrue(); + expect(s.endsWith("foobar")).toBeTrue(); + expect(s.endsWith("1foobar")).toBeFalse(); + expect(s.endsWith("r", 6)).toBeTrue(); + expect(s.endsWith("ar", 6)).toBeTrue(); + expect(s.endsWith("bar", 6)).toBeTrue(); + expect(s.endsWith("obar", 6)).toBeTrue(); + expect(s.endsWith("oobar", 6)).toBeTrue(); + expect(s.endsWith("foobar", 6)).toBeTrue(); + expect(s.endsWith("1foobar", 6)).toBeFalse(); + expect(s.endsWith("bar", [])).toBeFalse(); + expect(s.endsWith("bar", null)).toBeFalse(); + expect(s.endsWith("bar", false)).toBeFalse(); + expect(s.endsWith("bar", true)).toBeFalse(); + expect(s.endsWith("f", true)).toBeTrue(); + expect(s.endsWith("bar", -1)).toBeFalse(); + expect(s.endsWith("bar", 42)).toBeTrue(); + expect(s.endsWith("foo", 3)).toBeTrue(); + expect(s.endsWith("foo", "3")).toBeTrue(); + expect(s.endsWith("foo1", 3)).toBeFalse(); + expect(s.endsWith("foo", 3.7)).toBeTrue(); + expect(s.endsWith()).toBeFalse(); + expect(s.endsWith("")).toBeTrue(); + expect(s.endsWith("", 0)).toBeTrue(); + expect(s.endsWith("", 1)).toBeTrue(); + expect(s.endsWith("", -1)).toBeTrue(); + expect(s.endsWith("", 42)).toBeTrue(); + expect("12undefined".endsWith()).toBeTrue(); + expect(() => s.endsWith(/foobar/)).toThrowWithMessage( + TypeError, + "searchString is not a string, but a regular expression" + ); + expect(s.endsWith("bar", undefined)).toBeTrue(); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.includes.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.includes.js new file mode 100644 index 0000000000..14a2230530 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.includes.js @@ -0,0 +1,12 @@ +test("basic functionality", () => { + expect(String.prototype.includes).toHaveLength(1); + + expect("hello friends".includes("hello")).toBeTrue(); + expect("hello friends".includes("hello", 100)).toBeFalse(); + expect("hello friends".includes("hello", -10)).toBeTrue(); + expect("hello friends".includes("friends", 6)).toBeTrue(); + expect("hello friends".includes("hello", 6)).toBeFalse(); + expect("hello friends false".includes(false)).toBeTrue(); + expect("hello 10 friends".includes(10)).toBeTrue(); + expect("hello friends undefined".includes()).toBeTrue(); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.indexOf.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.indexOf.js new file mode 100644 index 0000000000..8e2ca2f892 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.indexOf.js @@ -0,0 +1,8 @@ +test("basic functionality", () => { + expect(String.prototype.indexOf).toHaveLength(1); + + var s = "hello friends"; + + expect(s.indexOf("friends")).toBe(6); + expect(s.indexOf("enemies")).toBe(-1); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.js new file mode 100644 index 0000000000..a66207e96c --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.js @@ -0,0 +1,9 @@ +test("basic functionality", () => { + expect(String.prototype).toHaveLength(0); + + expect(typeof Object.getPrototypeOf("")).toBe("object"); + expect(Object.getPrototypeOf("").valueOf()).toBe(""); + + expect(typeof String.prototype).toBe("object"); + expect(String.prototype.valueOf()).toBe(""); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.lastIndexOf.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.lastIndexOf.js new file mode 100644 index 0000000000..ce69fc3214 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.lastIndexOf.js @@ -0,0 +1,22 @@ +test("basic functionality", () => { + expect(String.prototype.lastIndexOf).toHaveLength(1); + + expect("hello friends".lastIndexOf()).toBe(-1); + expect("hello friends".lastIndexOf("e")).toBe(9); + expect("hello friends".lastIndexOf("e", -7)).toBe(-1); + expect("hello friends".lastIndexOf("e", 100)).toBe(9); + expect("hello friends".lastIndexOf("")).toBe(13); + expect("hello friends".lastIndexOf("Z")).toBe(-1); + expect("hello friends".lastIndexOf("serenity")).toBe(-1); + expect("hello friends".lastIndexOf("", 4)).toBe(4); + expect("hello serenity friends".lastIndexOf("serenity")).toBe(6); + expect("hello serenity friends serenity".lastIndexOf("serenity")).toBe(23); + expect("hello serenity friends serenity".lastIndexOf("serenity", 14)).toBe(6); + expect("".lastIndexOf("")).toBe(0); + expect("".lastIndexOf("", 1)).toBe(0); + expect("".lastIndexOf("", -1)).toBe(0); + expect("hello friends serenity".lastIndexOf("h", 10)).toBe(0); + expect("hello friends serenity".lastIndexOf("l", 4)).toBe(3); + expect("hello friends serenity".lastIndexOf("s", 13)).toBe(12); + expect("hello".lastIndexOf("serenity")).toBe(-1); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.padEnd.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.padEnd.js new file mode 100644 index 0000000000..a103084c3a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.padEnd.js @@ -0,0 +1,18 @@ +test("basic functionality", () => { + expect(String.prototype.padEnd).toHaveLength(1); + + var s = "foo"; + expect(s.padEnd(-1)).toBe("foo"); + expect(s.padEnd(0)).toBe("foo"); + expect(s.padEnd(3)).toBe("foo"); + expect(s.padEnd(5)).toBe("foo "); + expect(s.padEnd(10)).toBe("foo "); + expect(s.padEnd("5")).toBe("foo "); + expect(s.padEnd([[["5"]]])).toBe("foo "); + expect(s.padEnd(2, "+")).toBe("foo"); + expect(s.padEnd(5, "+")).toBe("foo++"); + expect(s.padEnd(5, 1)).toBe("foo11"); + expect(s.padEnd(10, null)).toBe("foonullnul"); + expect(s.padEnd(10, "bar")).toBe("foobarbarb"); + expect(s.padEnd(10, "123456789")).toBe("foo1234567"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.padStart.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.padStart.js new file mode 100644 index 0000000000..a2f0f0b710 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.padStart.js @@ -0,0 +1,18 @@ +test("basic functionality", () => { + expect(String.prototype.padStart).toHaveLength(1); + + var s = "foo"; + expect(s.padStart(-1)).toBe("foo"); + expect(s.padStart(0)).toBe("foo"); + expect(s.padStart(3)).toBe("foo"); + expect(s.padStart(5)).toBe(" foo"); + expect(s.padStart(10)).toBe(" foo"); + expect(s.padStart("5")).toBe(" foo"); + expect(s.padStart([[["5"]]])).toBe(" foo"); + expect(s.padStart(2, "+")).toBe("foo"); + expect(s.padStart(5, "+")).toBe("++foo"); + expect(s.padStart(5, 1)).toBe("11foo"); + expect(s.padStart(10, null)).toBe("nullnulfoo"); + expect(s.padStart(10, "bar")).toBe("barbarbfoo"); + expect(s.padStart(10, "123456789")).toBe("1234567foo"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.repeat.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.repeat.js new file mode 100644 index 0000000000..8427698509 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.repeat.js @@ -0,0 +1,25 @@ +test("basic functionality", () => { + expect(String.prototype.repeat).toHaveLength(1); + + expect("foo".repeat(0)).toBe(""); + expect("foo".repeat(1)).toBe("foo"); + expect("foo".repeat(2)).toBe("foofoo"); + expect("foo".repeat(3)).toBe("foofoofoo"); + expect("foo".repeat(3.1)).toBe("foofoofoo"); + expect("foo".repeat(3.5)).toBe("foofoofoo"); + expect("foo".repeat(3.9)).toBe("foofoofoo"); + expect("foo".repeat(null)).toBe(""); + expect("foo".repeat(undefined)).toBe(""); + expect("foo".repeat([])).toBe(""); + expect("foo".repeat("")).toBe(""); +}); + +test("throws correct range errors", () => { + expect(() => { + "foo".repeat(-1); + }).toThrowWithMessage(RangeError, "repeat count must be a positive number"); + + expect(() => { + "foo".repeat(Infinity); + }).toThrowWithMessage(RangeError, "repeat count must be a finite number"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.slice.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.slice.js new file mode 100644 index 0000000000..2141e0f6f3 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.slice.js @@ -0,0 +1,17 @@ +test("basic functionality", () => { + expect(String.prototype.slice).toHaveLength(2); + + expect("hello friends".slice()).toBe("hello friends"); + expect("hello friends".slice(1)).toBe("ello friends"); + expect("hello friends".slice(0, 5)).toBe("hello"); + expect("hello friends".slice(13, 6)).toBe(""); + expect("hello friends".slice("", 5)).toBe("hello"); + expect("hello friends".slice(3, 3)).toBe(""); + expect("hello friends".slice(-1, 13)).toBe("s"); + expect("hello friends".slice(0, 50)).toBe("hello friends"); + expect("hello friends".slice(0, "5")).toBe("hello"); + expect("hello friends".slice("6", "13")).toBe("friends"); + expect("hello friends".slice(-7)).toBe("friends"); + expect("hello friends".slice(1000)).toBe(""); + expect("hello friends".slice(-1000)).toBe("hello friends"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.split.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.split.js new file mode 100644 index 0000000000..63d6c69a97 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.split.js @@ -0,0 +1,35 @@ +test("basic functionality", () => { + expect(String.prototype.split).toHaveLength(2); + + expect("hello friends".split()).toEqual(["hello friends"]); + expect("hello friends".split("")).toEqual([ + "h", + "e", + "l", + "l", + "o", + " ", + "f", + "r", + "i", + "e", + "n", + "d", + "s", + ]); + expect("hello friends".split(" ")).toEqual(["hello", "friends"]); + + expect("a,b,c,d".split(",")).toEqual(["a", "b", "c", "d"]); + expect(",a,b,c,d".split(",")).toEqual(["", "a", "b", "c", "d"]); + expect("a,b,c,d,".split(",")).toEqual(["a", "b", "c", "d", ""]); + expect("a,b,,c,d".split(",")).toEqual(["a", "b", "", "c", "d"]); + expect(",a,b,,c,d,".split(",")).toEqual(["", "a", "b", "", "c", "d", ""]); + expect(",a,b,,,c,d,".split(",,")).toEqual([",a,b", ",c,d,"]); +}); + +test("limits", () => { + expect("a b c d".split(" ", 0)).toEqual([]); + expect("a b c d".split(" ", 1)).toEqual(["a"]); + expect("a b c d".split(" ", 3)).toEqual(["a", "b", "c"]); + expect("a b c d".split(" ", 100)).toEqual(["a", "b", "c", "d"]); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.startsWith.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.startsWith.js new file mode 100644 index 0000000000..5114da78f9 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.startsWith.js @@ -0,0 +1,36 @@ +test("basic functionality", () => { + expect(String.prototype.startsWith).toHaveLength(1); + + var s = "foobar"; + expect(s.startsWith("f")).toBeTrue(); + expect(s.startsWith("fo")).toBeTrue(); + expect(s.startsWith("foo")).toBeTrue(); + expect(s.startsWith("foob")).toBeTrue(); + expect(s.startsWith("fooba")).toBeTrue(); + expect(s.startsWith("foobar")).toBeTrue(); + expect(s.startsWith("foobar1")).toBeFalse(); + expect(s.startsWith("f", 0)).toBeTrue(); + expect(s.startsWith("fo", 0)).toBeTrue(); + expect(s.startsWith("foo", 0)).toBeTrue(); + expect(s.startsWith("foob", 0)).toBeTrue(); + expect(s.startsWith("fooba", 0)).toBeTrue(); + expect(s.startsWith("foobar", 0)).toBeTrue(); + expect(s.startsWith("foobar1", 0)).toBeFalse(); + expect(s.startsWith("foo", [])).toBeTrue(); + expect(s.startsWith("foo", null)).toBeTrue(); + expect(s.startsWith("foo", undefined)).toBeTrue(); + expect(s.startsWith("foo", false)).toBeTrue(); + expect(s.startsWith("foo", true)).toBeFalse(); + expect(s.startsWith("foo", "foo")).toBeTrue(); + expect(s.startsWith("foo", -1)).toBeTrue(); + expect(s.startsWith("foo", 42)).toBeFalse(); + expect(s.startsWith("bar", 3)).toBeTrue(); + expect(s.startsWith("bar", "3")).toBeTrue(); + expect(s.startsWith("bar1", 3)).toBeFalse(); + expect(s.startsWith()).toBeFalse(); + expect(s.startsWith("")).toBeTrue(); + expect(s.startsWith("", 0)).toBeTrue(); + expect(s.startsWith("", 1)).toBeTrue(); + expect(s.startsWith("", -1)).toBeTrue(); + expect(s.startsWith("", 42)).toBeTrue(); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substr.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substr.js new file mode 100644 index 0000000000..ade6c3c60a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substr.js @@ -0,0 +1,16 @@ +test("basic functionality", () => { + expect(String.prototype.substr).toHaveLength(2); + + expect("hello friends".substr()).toBe("hello friends"); + expect("hello friends".substr(1)).toBe("ello friends"); + expect("hello friends".substr(0, 5)).toBe("hello"); + expect("hello friends".substr(5, 6)).toBe(" frien"); + expect("hello friends".substr("", 5)).toBe("hello"); + expect("hello friends".substr(3, 3)).toBe("lo "); + expect("hello friends".substr(-1, 13)).toBe("s"); + expect("hello friends".substr(0, 50)).toBe("hello friends"); + expect("hello friends".substr(0, "5")).toBe("hello"); + expect("hello friends".substr("2", "2")).toBe("ll"); + expect("hello friends".substr(-7)).toBe("friends"); + expect("hello friends".substr(-3, -5)).toBe(""); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substring.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substring.js new file mode 100644 index 0000000000..49a9888d7f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substring.js @@ -0,0 +1,14 @@ +test("basic functionality", () => { + expect(String.prototype.substring).toHaveLength(2); + + expect("hello friends".substring()).toBe("hello friends"); + expect("hello friends".substring(1)).toBe("ello friends"); + expect("hello friends".substring(0, 5)).toBe("hello"); + expect("hello friends".substring(13, 6)).toBe("friends"); + expect("hello friends".substring("", 5)).toBe("hello"); + expect("hello friends".substring(3, 3)).toBe(""); + expect("hello friends".substring(-1, 13)).toBe("hello friends"); + expect("hello friends".substring(0, 50)).toBe("hello friends"); + expect("hello friends".substring(0, "5")).toBe("hello"); + expect("hello friends".substring("6", "13")).toBe("friends"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toLowerCase.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toLowerCase.js new file mode 100644 index 0000000000..4c7cbad50a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toLowerCase.js @@ -0,0 +1,9 @@ +test("basic functionality", () => { + expect(String.prototype.toLowerCase).toHaveLength(0); + + expect("foo".toLowerCase()).toBe("foo"); + expect("Foo".toLowerCase()).toBe("foo"); + expect("FOO".toLowerCase()).toBe("foo"); + + expect(("b" + "a" + +"a" + "a").toLowerCase()).toBe("banana"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toString.js new file mode 100644 index 0000000000..418f2da5f8 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toString.js @@ -0,0 +1,6 @@ +test("basic functionality", () => { + expect(String.prototype.toString).toHaveLength(0); + + expect("".toString()).toBe(""); + expect("hello friends".toString()).toBe("hello friends"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toUpperCase.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toUpperCase.js new file mode 100644 index 0000000000..03f463c6e1 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.toUpperCase.js @@ -0,0 +1,9 @@ +test("basic functionality", () => { + expect(String.prototype.toUpperCase).toHaveLength(0); + + expect("foo".toUpperCase()).toBe("FOO"); + expect("Foo".toUpperCase()).toBe("FOO"); + expect("FOO".toUpperCase()).toBe("FOO"); + + expect(("b" + "a" + +"n" + "a").toUpperCase()).toBe("BANANA"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.trim.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.trim.js new file mode 100644 index 0000000000..d60522cad4 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.trim.js @@ -0,0 +1,58 @@ +test("trim", () => { + expect(String.prototype.trim).toHaveLength(0); + + expect(" hello friends ".trim()).toBe("hello friends"); + expect("hello friends ".trim()).toBe("hello friends"); + expect(" hello friends".trim()).toBe("hello friends"); + + expect("\thello friends\t".trim()).toBe("hello friends"); + expect("\thello friends".trim()).toBe("hello friends"); + expect("hello friends\t".trim()).toBe("hello friends"); + + expect("\rhello friends\r".trim()).toBe("hello friends"); + expect("\rhello friends".trim()).toBe("hello friends"); + expect("hello friends\r".trim()).toBe("hello friends"); + + expect("\rhello friends\n".trim()).toBe("hello friends"); + expect("\r\thello friends".trim()).toBe("hello friends"); + expect("hello friends\r\n".trim()).toBe("hello friends"); + expect(" \thello friends\r\n".trim()).toBe("hello friends"); + expect("\n\t\thello friends\r\n".trim()).toBe("hello friends"); + expect("\n\t\thello friends\t\t".trim()).toBe("hello friends"); +}); + +test("trimStart", () => { + expect(String.prototype.trimStart).toHaveLength(0); + + expect(" hello friends".trimStart()).toBe("hello friends"); + expect("hello friends ".trimStart()).toBe("hello friends "); + expect(" hello friends ".trimStart()).toBe("hello friends "); + + expect("\thello friends".trimStart()).toBe("hello friends"); + expect("hello friends\t".trimStart()).toBe("hello friends\t"); + expect("\thello friends\t".trimStart()).toBe("hello friends\t"); + + expect("\rhello friends".trimStart()).toBe("hello friends"); + expect("hello friends\r".trimStart()).toBe("hello friends\r"); + expect("\rhello friends\r".trimStart()).toBe("hello friends\r"); +}); + +test("trimEnd", () => { + expect(String.prototype.trimEnd).toHaveLength(0); + + expect("hello friends ".trimEnd()).toBe("hello friends"); + expect(" hello friends".trimEnd()).toBe(" hello friends"); + expect(" hello friends ".trimEnd()).toBe(" hello friends"); + + expect("hello friends\t".trimEnd()).toBe("hello friends"); + expect("\thello friends".trimEnd()).toBe("\thello friends"); + expect("\thello friends\t".trimEnd()).toBe("\thello friends"); + + expect("hello friends\r".trimEnd()).toBe("hello friends"); + expect("\rhello friends".trimEnd()).toBe("\rhello friends"); + expect("\rhello friends\r".trimEnd()).toBe("\rhello friends"); + + expect("hello friends\n".trimEnd()).toBe("hello friends"); + expect("\r\nhello friends".trimEnd()).toBe("\r\nhello friends"); + expect("\rhello friends\r\n".trimEnd()).toBe("\rhello friends"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.valueOf.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.valueOf.js new file mode 100644 index 0000000000..87fb50fa21 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.valueOf.js @@ -0,0 +1,12 @@ +test("basic functionality", () => { + expect(String.prototype.valueOf).toHaveLength(0); + + expect(String()).toBe(""); + expect(new String().valueOf()).toBe(""); + expect(String("foo")).toBe("foo"); + expect(new String("foo").valueOf()).toBe("foo"); + expect(String(123)).toBe("123"); + expect(new String(123).valueOf()).toBe("123"); + expect(String(123)).toBe("123"); + expect(new String(123).valueOf()).toBe("123"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.raw.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.raw.js new file mode 100644 index 0000000000..a866276034 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.raw.js @@ -0,0 +1,31 @@ +test("basic functionality", () => { + expect(String.raw).toHaveLength(1); + + let str = String.raw`foo\nbar`; + expect(str).toHaveLength(8); + expect(str).toBe("foo\\nbar"); + + str = String.raw`foo ${1 + 9}\nbar${"hf!"}`; + expect(str).toBe("foo 10\\nbarhf!"); + + str = String.raw`${10}${20}${30}`; + expect(str).toBe("102030"); + + str = String.raw({ raw: ["foo ", "\\nbar"] }, 10, "hf!"); + expect(str).toBe("foo 10\\nbar"); + + str = String.raw({ raw: ["foo ", "\\nbar"] }); + expect(str).toBe("foo \\nbar"); + + str = String.raw({ raw: [] }, 10, "hf!"); + expect(str).toBe(""); + + str = String.raw({ raw: 1 }); + expect(str).toBe(""); +}); + +test("passing object with no 'raw' property", () => { + expect(() => { + String.raw({}); + }).toThrowWithMessage(TypeError, "Cannot convert property 'raw' to object from undefined"); +}); |