diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-07-12 12:51:12 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-13 15:07:29 +0200 |
commit | 4c3a415dc3247e6dc4b62baffd62c952d2b9e8f7 (patch) | |
tree | edef78f80b04add236bef0771bfe5b4a86814260 /Libraries | |
parent | c831fb17bff563bd543a1e51dd14dc55b05615c1 (diff) | |
download | serenity-4c3a415dc3247e6dc4b62baffd62c952d2b9e8f7.zip |
LibJS: Add String Iterator tests
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Tests/iterators/array-iterator.js | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/iterators/string-iterator.js | 48 |
2 files changed, 49 insertions, 1 deletions
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 }); +}); |