summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/builtins
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-11-16 19:35:40 -0500
committerLinus Groh <mail@linusgroh.de>2021-11-17 09:01:32 +0000
commitc19c3205ffe0747fa299950a725a57ebd6667e95 (patch)
treec2f28a77357ecd7a43b604d5383e17e89d9b63bc /Userland/Libraries/LibJS/Tests/builtins
parent8fe1c1f78876c8ef189a12ac9174488ed9576384 (diff)
downloadserenity-c19c3205ffe0747fa299950a725a57ebd6667e95.zip
LibJS: Implement ECMA-402 Number.prototype.toLocaleString
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/builtins')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toLocaleString.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toLocaleString.js
new file mode 100644
index 0000000000..72ffb24573
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toLocaleString.js
@@ -0,0 +1,78 @@
+describe("errors", () => {
+ test("must be called with numeric |this|", () => {
+ [true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
+ expect(() => Number.prototype.toLocaleString.call(value)).toThrowWithMessage(
+ TypeError,
+ "Not an object of type Number"
+ );
+ });
+ });
+});
+
+describe("correct behavior", () => {
+ test("length", () => {
+ expect(Number.prototype.toLocaleString).toHaveLength(0);
+ });
+});
+
+describe("special values", () => {
+ test("NaN", () => {
+ expect(NaN.toLocaleString()).toBe("NaN");
+ expect(NaN.toLocaleString("en")).toBe("NaN");
+ expect(NaN.toLocaleString("ar")).toBe("ليس رقم");
+ });
+
+ test("Infinity", () => {
+ expect(Infinity.toLocaleString()).toBe("∞");
+ expect(Infinity.toLocaleString("en")).toBe("∞");
+ expect(Infinity.toLocaleString("ar")).toBe("∞");
+ });
+});
+
+describe("styles", () => {
+ test("decimal", () => {
+ expect((12).toLocaleString("en")).toBe("12");
+ expect((12).toLocaleString("ar")).toBe("\u0661\u0662");
+ });
+
+ test("percent", () => {
+ expect((0.234).toLocaleString("en", { style: "percent" })).toBe("23%");
+ expect((0.234).toLocaleString("ar", { style: "percent" })).toBe("\u0662\u0663\u066a\u061c");
+ });
+
+ test("currency", () => {
+ expect(
+ (1.23).toLocaleString("en", {
+ style: "currency",
+ currency: "USD",
+ currencyDisplay: "name",
+ })
+ ).toBe("1.23 US dollars");
+
+ expect(
+ (1.23).toLocaleString("ar", {
+ style: "currency",
+ currency: "USD",
+ currencyDisplay: "name",
+ })
+ ).toBe("\u0661\u066b\u0662\u0663 دولار أمريكي");
+ });
+
+ test("unit", () => {
+ expect(
+ (1.23).toLocaleString("en", {
+ style: "unit",
+ unit: "kilometer-per-hour",
+ unitDisplay: "long",
+ })
+ ).toBe("1.23 kilometers per hour");
+
+ expect(
+ (1.23).toLocaleString("ar", {
+ style: "unit",
+ unit: "kilometer-per-hour",
+ unitDisplay: "long",
+ })
+ ).toBe("\u0661\u066b\u0662\u0663 كيلومتر في الساعة");
+ });
+});