From 4c3a415dc3247e6dc4b62baffd62c952d2b9e8f7 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Sun, 12 Jul 2020 12:51:12 -0700 Subject: [PATCH] LibJS: Add String Iterator tests --- .../LibJS/Tests/iterators/array-iterator.js | 2 +- .../LibJS/Tests/iterators/string-iterator.js | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Libraries/LibJS/Tests/iterators/string-iterator.js diff --git a/Libraries/LibJS/Tests/iterators/array-iterator.js b/Libraries/LibJS/Tests/iterators/array-iterator.js index 8f02845ebe..92966d54d4 100644 --- a/Libraries/LibJS/Tests/iterators/array-iterator.js +++ b/Libraries/LibJS/Tests/iterators/array-iterator.js @@ -1,5 +1,5 @@ test("length", () => { - expect(Array.prototype[Symbol.iterator].length).toBe(0); + expect(Array.prototype[Symbol.iterator]).toHaveLength(0); }); test("@@toStringTag", () => { diff --git a/Libraries/LibJS/Tests/iterators/string-iterator.js b/Libraries/LibJS/Tests/iterators/string-iterator.js new file mode 100644 index 0000000000..379f6f05fc --- /dev/null +++ b/Libraries/LibJS/Tests/iterators/string-iterator.js @@ -0,0 +1,48 @@ +test("length", () => { + expect(String.prototype[Symbol.iterator]).toHaveLength(0); +}); + +test("basic functionality", () => { + const s = "abcd"; + const it = s[Symbol.iterator](); + expect(it.next()).toEqual({ value: "a", done: false }); + expect(it.next()).toEqual({ value: "b", done: false }); + expect(it.next()).toEqual({ value: "c", done: false }); + expect(it.next()).toEqual({ value: "d", done: false }); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); +}); + +test("casts |this| to string", () => { + const it = String.prototype[Symbol.iterator].call(45); + expect(it.next()).toEqual({ value: "4", done: false }); + expect(it.next()).toEqual({ value: "5", done: false }); + expect(it.next()).toEqual({ value: undefined, done: true }); + + const it = String.prototype[Symbol.iterator].call(false); + expect(it.next()).toEqual({ value: "f", done: false }); + expect(it.next()).toEqual({ value: "a", done: false }); + expect(it.next()).toEqual({ value: "l", done: false }); + expect(it.next()).toEqual({ value: "s", done: false }); + expect(it.next()).toEqual({ value: "e", done: false }); + expect(it.next()).toEqual({ value: undefined, done: true }); + + expect(() => { + String.prototype[Symbol.iterator].call(null); + }).toThrowWithMessage(TypeError, "ToObject on null or undefined"); + expect(() => { + String.prototype[Symbol.iterator].call(undefined); + }).toThrowWithMessage(TypeError, "ToObject on null or undefined"); +}); + +test("utf8 compatible", () => { + const it = "ab\u{1f41e}cde"[Symbol.iterator](); + expect(it.next()).toEqual({ value: "a", done: false }); + expect(it.next()).toEqual({ value: "b", done: false }); + expect(it.next()).toEqual({ value: "🐞", done: false }); + expect(it.next()).toEqual({ value: "c", done: false }); + expect(it.next()).toEqual({ value: "d", done: false }); + expect(it.next()).toEqual({ value: "e", done: false }); + expect(it.next()).toEqual({ value: undefined, done: true }); +});