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

LibJS: Implement String.prototype.isWellFormed

This commit is contained in:
Timothy Flynn 2022-12-01 10:36:44 -05:00 committed by Linus Groh
parent 24237ae5bf
commit 0bb46235a7
4 changed files with 87 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.isWellFormed.call(Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to string");
});
});
describe("basic functionality", () => {
test("ascii strings", () => {
expect("".isWellFormed()).toBeTrue();
expect("foo".isWellFormed()).toBeTrue();
expect("abcdefghi".isWellFormed()).toBeTrue();
});
test("valid UTF-16 strings", () => {
expect("😀".isWellFormed()).toBeTrue();
expect("\ud83d\ude00".isWellFormed()).toBeTrue();
});
test("invalid UTF-16 strings", () => {
expect("😀".slice(0, 1).isWellFormed()).toBeFalse();
expect("😀".slice(1, 2).isWellFormed()).toBeFalse();
expect("\ud83d".isWellFormed()).toBeFalse();
expect("\ude00".isWellFormed()).toBeFalse();
expect("a\ud83d".isWellFormed()).toBeFalse();
expect("a\ude00".isWellFormed()).toBeFalse();
expect("\ud83da".isWellFormed()).toBeFalse();
expect("\ude00a".isWellFormed()).toBeFalse();
expect("a\ud83da".isWellFormed()).toBeFalse();
expect("a\ude00a".isWellFormed()).toBeFalse();
});
test("object converted to a string", () => {
let toStringCalled = false;
const obj = {
toString: function () {
toStringCalled = true;
return "toString";
},
};
expect(String.prototype.isWellFormed.call(obj)).toBeTrue();
expect(toStringCalled).toBeTrue();
});
});