1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:27:35 +00:00

LibJS: Implement String.prototype.toWellFormed

This commit is contained in:
Timothy Flynn 2022-12-01 10:52:10 -05:00 committed by Linus Groh
parent 0bb46235a7
commit 3ee5217adc
4 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,47 @@
describe("errors", () => {
test("called with value that cannot be converted to a string", () => {
expect(() => {
String.prototype.toWellFormed.call(Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to string");
});
});
describe("basic functionality", () => {
test("ascii strings", () => {
expect("".toWellFormed()).toBe("");
expect("foo".toWellFormed()).toBe("foo");
expect("abcdefghi".toWellFormed()).toBe("abcdefghi");
});
test("valid UTF-16 strings", () => {
expect("😀".toWellFormed()).toBe("😀");
expect("\ud83d\ude00".toWellFormed()).toBe("\ud83d\ude00");
});
test("invalid UTF-16 strings", () => {
expect("😀".slice(0, 1).toWellFormed()).toBe("\ufffd");
expect("😀".slice(1, 2).toWellFormed()).toBe("\ufffd");
expect("\ud83d".toWellFormed()).toBe("\ufffd");
expect("\ude00".toWellFormed()).toBe("\ufffd");
expect("a\ud83d".toWellFormed()).toBe("a\ufffd");
expect("a\ude00".toWellFormed()).toBe("a\ufffd");
expect("\ud83da".toWellFormed()).toBe("\ufffda");
expect("\ude00a".toWellFormed()).toBe("\ufffda");
expect("a\ud83da".toWellFormed()).toBe("a\ufffda");
expect("a\ude00a".toWellFormed()).toBe("a\ufffda");
});
test("object converted to a string", () => {
let toStringCalled = false;
const obj = {
toString: function () {
toStringCalled = true;
return "toString";
},
};
expect(String.prototype.toWellFormed.call(obj)).toBe("toString");
expect(toStringCalled).toBeTrue();
});
});