|
| 1 | +import { describe, expect, it } from "@jest/globals"; |
| 2 | + |
| 3 | +import "./index"; |
| 4 | + |
| 5 | +describe("Number.prototype.chr", () => { |
| 6 | + describe("converts ASCII codepoint to ASCII string", () => { |
| 7 | + it.each([ |
| 8 | + { codepoint: 65, expected: "A" }, |
| 9 | + { codepoint: 10, expected: "\n" }, |
| 10 | + { codepoint: 61, expected: "=" }, |
| 11 | + { codepoint: 126, expected: "~" }, |
| 12 | + { codepoint: 32, expected: " " }, |
| 13 | + { codepoint: 48, expected: "0" }, |
| 14 | + ])( |
| 15 | + "converts ASCII codepoint $codepoint to ASCII string $expected", |
| 16 | + ({ codepoint, expected }) => { |
| 17 | + expect(codepoint.chr()).toBe(expected); |
| 18 | + }, |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("converts codepoint to emoji", () => { |
| 23 | + it.each([ |
| 24 | + { codepoint: 129302, expected: "🤖" }, |
| 25 | + { codepoint: 129412, expected: "🦄" }, |
| 26 | + ])( |
| 27 | + "converts codepoint $codepoint to emoji $expected", |
| 28 | + ({ codepoint, expected }) => { |
| 29 | + expect(codepoint.chr()).toBe(expected); |
| 30 | + }, |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("converts hex codepoint to string", () => { |
| 35 | + it.each([ |
| 36 | + { codepoint: 0x404, expected: "Є" }, |
| 37 | + { codepoint: 0x24, expected: "$" }, |
| 38 | + { codepoint: 0x1f303, expected: "🌃" }, |
| 39 | + { codepoint: 0x1f92a, expected: "🤪" }, |
| 40 | + ])( |
| 41 | + "converts hex codepoint $codepoint to string $expected", |
| 42 | + ({ codepoint, expected }) => { |
| 43 | + expect(codepoint.chr()).toBe(expected); |
| 44 | + }, |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("handles edgecases appropiately by returning null char or throwing error.", () => { |
| 49 | + it("converts zero to null character", () => { |
| 50 | + expect((0).chr()).toBe("\0"); |
| 51 | + }); |
| 52 | + |
| 53 | + it.each([ |
| 54 | + { edgecase: -1 }, |
| 55 | + { edgecase: 11120651 }, |
| 56 | + { edgecase: Infinity }, |
| 57 | + { edgecase: -Infinity }, |
| 58 | + { edgecase: 1.5 }, |
| 59 | + { edgecase: 1e-2 }, |
| 60 | + { edgecase: 0xffffff }, |
| 61 | + ])( |
| 62 | + "handles number $edgecase outside unicode range by throwing range error", |
| 63 | + ({ edgecase }) => { |
| 64 | + expect(() => edgecase.chr()).toThrow(RangeError); |
| 65 | + }, |
| 66 | + ); |
| 67 | + |
| 68 | + it("handles incompatible types that resolve to NaN by throwing range error", () => { |
| 69 | + expect(() => NaN.chr()).toThrow(RangeError); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments