Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, it } from "@jest/globals";

import "./index";

describe("Number.prototype.chr", () => {
describe("converts ASCII codepoint to ASCII string", () => {
it.each([
{ codepoint: 65, expected: "A" },
{ codepoint: 10, expected: "\n" },
{ codepoint: 61, expected: "=" },
{ codepoint: 126, expected: "~" },
{ codepoint: 32, expected: " " },
{ codepoint: 48, expected: "0" },
])(
"converts ASCII codepoint $codepoint to ASCII string $expected",
({ codepoint, expected }) => {
expect(codepoint.chr()).toBe(expected);
},
);
});

describe("converts codepoint to emoji", () => {
it.each([
{ codepoint: 129302, expected: "🤖" },
{ codepoint: 129412, expected: "🦄" },
])(
"converts codepoint $codepoint to emoji $expected",
({ codepoint, expected }) => {
expect(codepoint.chr()).toBe(expected);
},
);
});

describe("converts hex codepoint to string", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hex only exists for the developer's benefit:

console.log(0x1f303); // prints 127747
console.log(0x1f303 === 127747); // prints true

So I'm not sure we gain from having a hex-specific section, it's just another way of writing valid integers.

Imo we can move the 0x24 into the ASCII codepoints test cases (and we'll just have one of the ASCII codepoints written as hex, while others aren't). Likewise we can move the emoji ones into the emoji test, and have some of those codepoints written as hex and some as decimal.

The Є test can either be removed entirely or have its own test (non-ASCII non-emoji?).

it.each([
{ codepoint: 0x404, expected: "Є" },
{ codepoint: 0x24, expected: "$" },
{ codepoint: 0x1f303, expected: "🌃" },
{ codepoint: 0x1f92a, expected: "🤪" },
])(
"converts hex codepoint $codepoint to string $expected",
({ codepoint, expected }) => {
expect(codepoint.chr()).toBe(expected);
},
);
});

describe("handles edgecases appropiately by returning null char or throwing error.", () => {
it("converts zero to null character", () => {
expect((0).chr()).toBe("\0");
});

it.each([
{ edgecase: -1 },
{ edgecase: 11120651 },
{ edgecase: Infinity },
{ edgecase: -Infinity },
{ edgecase: 1.5 },
{ edgecase: 1e-2 },
{ edgecase: 0xffffff },
])(
"handles number $edgecase outside unicode range by throwing range error",
({ edgecase }) => {
expect(() => edgecase.chr()).toThrow(RangeError);
},
);

it("handles incompatible types that resolve to NaN by throwing range error", () => {
expect(() => NaN.chr()).toThrow(RangeError);
});
Comment on lines +53 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think NaN can be merged with the other invalid values.

Suggested change
it.each([
{ edgecase: -1 },
{ edgecase: 11120651 },
{ edgecase: Infinity },
{ edgecase: -Infinity },
{ edgecase: 1.5 },
{ edgecase: 1e-2 },
{ edgecase: 0xffffff },
])(
"handles number $edgecase outside unicode range by throwing range error",
({ edgecase }) => {
expect(() => edgecase.chr()).toThrow(RangeError);
},
);
it("handles incompatible types that resolve to NaN by throwing range error", () => {
expect(() => NaN.chr()).toThrow(RangeError);
});
it.each([
-1,
11120651,
Infinity,
-Infinity,
1.5,
1e-2,
0xffffff,
NaN,
])(
"throws RangeError for non-Unicode value %p",
(value) => {
expect(() => value.chr()).toThrow(RangeError);
},
);

});
});
Loading