From ccec8204e892820af5cd98b5577bb51158f1fbf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ti=E1=BA=BFn=20Nguy=E1=BB=85n=20Kh=E1=BA=AFc?= Date: Tue, 22 Oct 2024 15:27:48 +1300 Subject: [PATCH] chore: remove useless tests grouping --- packages/utils/src/big-int.test.ts | 32 +- packages/utils/src/denominated-number.test.ts | 438 +++++++++--------- 2 files changed, 225 insertions(+), 245 deletions(-) diff --git a/packages/utils/src/big-int.test.ts b/packages/utils/src/big-int.test.ts index e3f0cc37..7dc96704 100644 --- a/packages/utils/src/big-int.test.ts +++ b/packages/utils/src/big-int.test.ts @@ -1,26 +1,24 @@ import { BigIntMath } from "./big-int"; import { describe, expect, it } from "vitest"; -describe("BigIntMath", () => { - describe("min", () => { - it("gets the correct minimum value", () => - expect(BigIntMath.min(-1n, 0n, 1n)).toBe(-1n)); +describe("min", () => { + it("gets the correct minimum value", () => + expect(BigIntMath.min(-1n, 0n, 1n)).toBe(-1n)); - it("returns the first value if only one value is passed", () => - expect(BigIntMath.min(1n)).toBe(1n)); + it("returns the first value if only one value is passed", () => + expect(BigIntMath.min(1n)).toBe(1n)); - it("returns 0n if no values is passed", () => - expect(BigIntMath.min()).toBe(0n)); - }); + it("returns 0n if no values is passed", () => + expect(BigIntMath.min()).toBe(0n)); +}); - describe("max", () => { - it("gets the correct maximum value", () => - expect(BigIntMath.max(-1n, 0n, 1n)).toBe(1n)); +describe("max", () => { + it("gets the correct maximum value", () => + expect(BigIntMath.max(-1n, 0n, 1n)).toBe(1n)); - it("returns the first value if only one value is passed", () => - expect(BigIntMath.max(-1n)).toBe(-1n)); + it("returns the first value if only one value is passed", () => + expect(BigIntMath.max(-1n)).toBe(-1n)); - it("returns 0n if no values is passed", () => - expect(BigIntMath.max()).toBe(0n)); - }); + it("returns 0n if no values is passed", () => + expect(BigIntMath.max()).toBe(0n)); }); diff --git a/packages/utils/src/denominated-number.test.ts b/packages/utils/src/denominated-number.test.ts index f0c5678d..4bbeeaaa 100644 --- a/packages/utils/src/denominated-number.test.ts +++ b/packages/utils/src/denominated-number.test.ts @@ -1,259 +1,241 @@ import { DenominatedNumber } from "./denominated-number.js"; import { describe, expect, it } from "vitest"; -describe("DenominatedNumber", () => { - describe("fromPlanck", () => { - it("leads to correct atomics value", () => { - expect(new DenominatedNumber("1", 0).planck).toEqual(1n); - expect(new DenominatedNumber("1", 1).planck).toEqual(1n); - expect(new DenominatedNumber("1", 2).planck).toEqual(1n); - - expect(new DenominatedNumber("1", 5).planck).toEqual(1n); - expect(new DenominatedNumber("2", 5).planck).toEqual(2n); - expect(new DenominatedNumber("3", 5).planck).toEqual(3n); - expect(new DenominatedNumber("10", 5).planck).toEqual(10n); - expect(new DenominatedNumber("20", 5).planck).toEqual(20n); - expect(new DenominatedNumber("30", 5).planck).toEqual(30n); - expect( - new DenominatedNumber("100000000000000000000000", 5).planck, - ).toEqual(100000000000000000000000n); - expect( - new DenominatedNumber("200000000000000000000000", 5).planck, - ).toEqual(200000000000000000000000n); - expect( - new DenominatedNumber("300000000000000000000000", 5).planck, - ).toEqual(300000000000000000000000n); - - expect(new DenominatedNumber("44", 5).planck).toEqual(44n); - expect(new DenominatedNumber("044", 5).planck).toEqual(44n); - expect(new DenominatedNumber("0044", 5).planck).toEqual(44n); - expect(new DenominatedNumber("00044", 5).planck).toEqual(44n); - }); - - it("reads DenominatedNumbers correctly", () => { - expect(new DenominatedNumber("44", 0).toString()).toEqual("44"); - expect(new DenominatedNumber("44", 1).toString()).toEqual("4.4"); - expect(new DenominatedNumber("44", 2).toString()).toEqual("0.44"); - expect(new DenominatedNumber("44", 3).toString()).toEqual("0.044"); - expect(new DenominatedNumber("44", 4).toString()).toEqual("0.0044"); - }); - - it("reads negative integer correctly", () => { - expect(new DenominatedNumber("-44", 0).toString()).toEqual("-44"); - expect(new DenominatedNumber("-44", 1).toString()).toEqual("-4.4"); - }); +describe("fromPlanck", () => { + it("leads to correct atomics value", () => { + expect(new DenominatedNumber("1", 0).planck).toEqual(1n); + expect(new DenominatedNumber("1", 1).planck).toEqual(1n); + expect(new DenominatedNumber("1", 2).planck).toEqual(1n); + + expect(new DenominatedNumber("1", 5).planck).toEqual(1n); + expect(new DenominatedNumber("2", 5).planck).toEqual(2n); + expect(new DenominatedNumber("3", 5).planck).toEqual(3n); + expect(new DenominatedNumber("10", 5).planck).toEqual(10n); + expect(new DenominatedNumber("20", 5).planck).toEqual(20n); + expect(new DenominatedNumber("30", 5).planck).toEqual(30n); + expect(new DenominatedNumber("100000000000000000000000", 5).planck).toEqual( + 100000000000000000000000n, + ); + expect(new DenominatedNumber("200000000000000000000000", 5).planck).toEqual( + 200000000000000000000000n, + ); + expect(new DenominatedNumber("300000000000000000000000", 5).planck).toEqual( + 300000000000000000000000n, + ); + + expect(new DenominatedNumber("44", 5).planck).toEqual(44n); + expect(new DenominatedNumber("044", 5).planck).toEqual(44n); + expect(new DenominatedNumber("0044", 5).planck).toEqual(44n); + expect(new DenominatedNumber("00044", 5).planck).toEqual(44n); }); - describe("fromNumber", () => { - it("throws helpful error message for invalid characters", () => { - expect(() => DenominatedNumber.fromNumber(" 13", 5)).toThrowError( - /invalid character at position 1/i, - ); - expect(() => DenominatedNumber.fromNumber("1,3", 5)).toThrowError( - /invalid character at position 2/i, - ); - expect(() => DenominatedNumber.fromNumber("13-", 5)).toThrowError( - /invalid character at position 3/i, - ); - expect(() => DenominatedNumber.fromNumber("13/", 5)).toThrowError( - /invalid character at position 3/i, - ); - expect(() => DenominatedNumber.fromNumber("13\\", 5)).toThrowError( - /invalid character at position 3/i, - ); - }); - - it("throws for more than one separator", () => { - expect(() => DenominatedNumber.fromNumber("1.3.5", 5)).toThrowError( - /more than one separator found/i, - ); - expect(() => DenominatedNumber.fromNumber("1..3", 5)).toThrowError( - /more than one separator found/i, - ); - expect(() => DenominatedNumber.fromNumber("..", 5)).toThrowError( - /more than one separator found/i, - ); - }); - - it("throws for separator only", () => { - expect(() => DenominatedNumber.fromNumber(".", 5)).toThrowError( - /fractional part missing/i, - ); - }); - - it.skip("throws for more decimals than supported", () => { - expect(() => DenominatedNumber.fromNumber("44.123456", 5)).toThrowError( - /got more DenominatedNumbers than supported/i, - ); - expect(() => DenominatedNumber.fromNumber("44.1", 0)).toThrowError( - /got more DenominatedNumbers than supported/i, - ); - }); + it("reads DenominatedNumbers correctly", () => { + expect(new DenominatedNumber("44", 0).toString()).toEqual("44"); + expect(new DenominatedNumber("44", 1).toString()).toEqual("4.4"); + expect(new DenominatedNumber("44", 2).toString()).toEqual("0.44"); + expect(new DenominatedNumber("44", 3).toString()).toEqual("0.044"); + expect(new DenominatedNumber("44", 4).toString()).toEqual("0.0044"); + }); - it("throws for decimals that are not non-negative integers", () => { - // no integer - expect(() => DenominatedNumber.fromNumber("1", Number.NaN)).toThrowError( - /Decimals is not an integer/i, - ); - expect(() => - DenominatedNumber.fromNumber("1", Number.POSITIVE_INFINITY), - ).toThrowError(/Decimals is not an integer/i); - expect(() => - DenominatedNumber.fromNumber("1", Number.NEGATIVE_INFINITY), - ).toThrowError(/Decimals is not an integer/i); - expect(() => - DenominatedNumber.fromNumber("1", 1.78945544484), - ).toThrowError(/Decimals is not an integer/i); + it("reads negative integer correctly", () => { + expect(new DenominatedNumber("-44", 0).toString()).toEqual("-44"); + expect(new DenominatedNumber("-44", 1).toString()).toEqual("-4.4"); + }); +}); - // negative - expect(() => DenominatedNumber.fromNumber("1", -1)).toThrowError( - /Decimals must not be negative/i, - ); - expect(() => - DenominatedNumber.fromNumber("1", Number.MIN_SAFE_INTEGER), - ).toThrowError(/Decimals must not be negative/i); +describe("fromNumber", () => { + it("throws helpful error message for invalid characters", () => { + expect(() => DenominatedNumber.fromNumber(" 13", 5)).toThrowError( + /invalid character at position 1/i, + ); + expect(() => DenominatedNumber.fromNumber("1,3", 5)).toThrowError( + /invalid character at position 2/i, + ); + expect(() => DenominatedNumber.fromNumber("13-", 5)).toThrowError( + /invalid character at position 3/i, + ); + expect(() => DenominatedNumber.fromNumber("13/", 5)).toThrowError( + /invalid character at position 3/i, + ); + expect(() => DenominatedNumber.fromNumber("13\\", 5)).toThrowError( + /invalid character at position 3/i, + ); + }); - // exceeds supported range - expect(() => DenominatedNumber.fromNumber("1", 101)).toThrowError( - /Decimals must not exceed 100/i, - ); - }); + it("throws for more than one separator", () => { + expect(() => DenominatedNumber.fromNumber("1.3.5", 5)).toThrowError( + /more than one separator found/i, + ); + expect(() => DenominatedNumber.fromNumber("1..3", 5)).toThrowError( + /more than one separator found/i, + ); + expect(() => DenominatedNumber.fromNumber("..", 5)).toThrowError( + /more than one separator found/i, + ); + }); - it("returns correct value", () => { - expect(DenominatedNumber.fromNumber("44", 0).planck).toEqual(44n); - expect(DenominatedNumber.fromNumber("44", 1).planck).toEqual(440n); - expect(DenominatedNumber.fromNumber("44", 2).planck).toEqual(4400n); - expect(DenominatedNumber.fromNumber("44", 3).planck).toEqual(44000n); + it("throws for separator only", () => { + expect(() => DenominatedNumber.fromNumber(".", 5)).toThrowError( + /fractional part missing/i, + ); + }); - expect(DenominatedNumber.fromNumber("44.2", 1).planck).toEqual(442n); - expect(DenominatedNumber.fromNumber("44.2", 2).planck).toEqual(4420n); - expect(DenominatedNumber.fromNumber("44.2", 3).planck).toEqual(44200n); + it.skip("throws for more decimals than supported", () => { + expect(() => DenominatedNumber.fromNumber("44.123456", 5)).toThrowError( + /got more DenominatedNumbers than supported/i, + ); + expect(() => DenominatedNumber.fromNumber("44.1", 0)).toThrowError( + /got more DenominatedNumbers than supported/i, + ); + }); - expect(DenominatedNumber.fromNumber("44.1", 6).planck).toEqual(44100000n); - expect(DenominatedNumber.fromNumber("44.12", 6).planck).toEqual( - 44120000n, - ); - expect(DenominatedNumber.fromNumber("44.123", 6).planck).toEqual( - 44123000n, - ); - expect(DenominatedNumber.fromNumber("44.1234", 6).planck).toEqual( - 44123400n, - ); - expect(DenominatedNumber.fromNumber("44.12345", 6).planck).toEqual( - 44123450n, - ); - expect(DenominatedNumber.fromNumber("44.123456", 6).planck).toEqual( - 44123456n, - ); - }); + it("throws for decimals that are not non-negative integers", () => { + // no integer + expect(() => DenominatedNumber.fromNumber("1", Number.NaN)).toThrowError( + /Decimals is not an integer/i, + ); + expect(() => + DenominatedNumber.fromNumber("1", Number.POSITIVE_INFINITY), + ).toThrowError(/Decimals is not an integer/i); + expect(() => + DenominatedNumber.fromNumber("1", Number.NEGATIVE_INFINITY), + ).toThrowError(/Decimals is not an integer/i); + expect(() => DenominatedNumber.fromNumber("1", 1.78945544484)).toThrowError( + /Decimals is not an integer/i, + ); + + // negative + expect(() => DenominatedNumber.fromNumber("1", -1)).toThrowError( + /Decimals must not be negative/i, + ); + expect(() => + DenominatedNumber.fromNumber("1", Number.MIN_SAFE_INTEGER), + ).toThrowError(/Decimals must not be negative/i); + + // exceeds supported range + expect(() => DenominatedNumber.fromNumber("1", 101)).toThrowError( + /Decimals must not exceed 100/i, + ); + }); - it("cuts leading zeros", () => { - expect(DenominatedNumber.fromNumber("4", 2).planck).toEqual(400n); - expect(DenominatedNumber.fromNumber("04", 2).planck).toEqual(400n); - expect(DenominatedNumber.fromNumber("004", 2).planck).toEqual(400n); - }); + it("returns correct value", () => { + expect(DenominatedNumber.fromNumber("44", 0).planck).toEqual(44n); + expect(DenominatedNumber.fromNumber("44", 1).planck).toEqual(440n); + expect(DenominatedNumber.fromNumber("44", 2).planck).toEqual(4400n); + expect(DenominatedNumber.fromNumber("44", 3).planck).toEqual(44000n); + + expect(DenominatedNumber.fromNumber("44.2", 1).planck).toEqual(442n); + expect(DenominatedNumber.fromNumber("44.2", 2).planck).toEqual(4420n); + expect(DenominatedNumber.fromNumber("44.2", 3).planck).toEqual(44200n); + + expect(DenominatedNumber.fromNumber("44.1", 6).planck).toEqual(44100000n); + expect(DenominatedNumber.fromNumber("44.12", 6).planck).toEqual(44120000n); + expect(DenominatedNumber.fromNumber("44.123", 6).planck).toEqual(44123000n); + expect(DenominatedNumber.fromNumber("44.1234", 6).planck).toEqual( + 44123400n, + ); + expect(DenominatedNumber.fromNumber("44.12345", 6).planck).toEqual( + 44123450n, + ); + expect(DenominatedNumber.fromNumber("44.123456", 6).planck).toEqual( + 44123456n, + ); + }); - it("cuts tailing zeros", () => { - expect(DenominatedNumber.fromNumber("4.12", 5).planck).toEqual(412000n); - expect(DenominatedNumber.fromNumber("4.120", 5).planck).toEqual(412000n); - expect(DenominatedNumber.fromNumber("4.1200", 5).planck).toEqual(412000n); - expect(DenominatedNumber.fromNumber("4.12000", 5).planck).toEqual( - 412000n, - ); - expect(DenominatedNumber.fromNumber("4.120000", 5).planck).toEqual( - 412000n, - ); - expect(DenominatedNumber.fromNumber("4.1200000", 5).planck).toEqual( - 412000n, - ); - }); + it("cuts leading zeros", () => { + expect(DenominatedNumber.fromNumber("4", 2).planck).toEqual(400n); + expect(DenominatedNumber.fromNumber("04", 2).planck).toEqual(400n); + expect(DenominatedNumber.fromNumber("004", 2).planck).toEqual(400n); + }); - it("interprets the empty string as zero", () => { - expect(DenominatedNumber.fromNumber("", 0).planck).toEqual(0n); - expect(DenominatedNumber.fromNumber("", 1).planck).toEqual(0n); - expect(DenominatedNumber.fromNumber("", 2).planck).toEqual(0n); - expect(DenominatedNumber.fromNumber("", 3).planck).toEqual(0n); - }); + it("cuts tailing zeros", () => { + expect(DenominatedNumber.fromNumber("4.12", 5).planck).toEqual(412000n); + expect(DenominatedNumber.fromNumber("4.120", 5).planck).toEqual(412000n); + expect(DenominatedNumber.fromNumber("4.1200", 5).planck).toEqual(412000n); + expect(DenominatedNumber.fromNumber("4.12000", 5).planck).toEqual(412000n); + expect(DenominatedNumber.fromNumber("4.120000", 5).planck).toEqual(412000n); + expect(DenominatedNumber.fromNumber("4.1200000", 5).planck).toEqual( + 412000n, + ); + }); - it("accepts american notation with skipped leading zero", () => { - expect(DenominatedNumber.fromNumber(".1", 3).planck).toEqual(100n); - expect(DenominatedNumber.fromNumber(".12", 3).planck).toEqual(120n); - expect(DenominatedNumber.fromNumber(".123", 3).planck).toEqual(123n); - }); + it("interprets the empty string as zero", () => { + expect(DenominatedNumber.fromNumber("", 0).planck).toEqual(0n); + expect(DenominatedNumber.fromNumber("", 1).planck).toEqual(0n); + expect(DenominatedNumber.fromNumber("", 2).planck).toEqual(0n); + expect(DenominatedNumber.fromNumber("", 3).planck).toEqual(0n); }); - describe("toString", () => { - it("displays no decimals point for full numbers", () => { - expect(DenominatedNumber.fromNumber("44", 0).toString()).toEqual("44"); - expect(DenominatedNumber.fromNumber("44", 1).toString()).toEqual("44"); - expect(DenominatedNumber.fromNumber("44", 2).toString()).toEqual("44"); + it("accepts american notation with skipped leading zero", () => { + expect(DenominatedNumber.fromNumber(".1", 3).planck).toEqual(100n); + expect(DenominatedNumber.fromNumber(".12", 3).planck).toEqual(120n); + expect(DenominatedNumber.fromNumber(".123", 3).planck).toEqual(123n); + }); +}); - expect(DenominatedNumber.fromNumber("44", 2).toString()).toEqual("44"); - expect(DenominatedNumber.fromNumber("44.0", 2).toString()).toEqual("44"); - expect(DenominatedNumber.fromNumber("44.00", 2).toString()).toEqual("44"); - expect(DenominatedNumber.fromNumber("44.000", 2).toString()).toEqual( - "44", - ); - }); +describe("toString", () => { + it("displays no decimals point for full numbers", () => { + expect(DenominatedNumber.fromNumber("44", 0).toString()).toEqual("44"); + expect(DenominatedNumber.fromNumber("44", 1).toString()).toEqual("44"); + expect(DenominatedNumber.fromNumber("44", 2).toString()).toEqual("44"); - it("only shows significant digits", () => { - expect(DenominatedNumber.fromNumber("44.1", 2).toString()).toEqual( - "44.1", - ); - expect(DenominatedNumber.fromNumber("44.10", 2).toString()).toEqual( - "44.1", - ); - expect(DenominatedNumber.fromNumber("44.100", 2).toString()).toEqual( - "44.1", - ); - }); + expect(DenominatedNumber.fromNumber("44", 2).toString()).toEqual("44"); + expect(DenominatedNumber.fromNumber("44.0", 2).toString()).toEqual("44"); + expect(DenominatedNumber.fromNumber("44.00", 2).toString()).toEqual("44"); + expect(DenominatedNumber.fromNumber("44.000", 2).toString()).toEqual("44"); + }); - it("fills up leading zeros", () => { - expect(new DenominatedNumber("3", 0).toString()).toEqual("3"); - expect(new DenominatedNumber("3", 1).toString()).toEqual("0.3"); - expect(new DenominatedNumber("3", 2).toString()).toEqual("0.03"); - expect(new DenominatedNumber("3", 3).toString()).toEqual("0.003"); - }); + it("only shows significant digits", () => { + expect(DenominatedNumber.fromNumber("44.1", 2).toString()).toEqual("44.1"); + expect(DenominatedNumber.fromNumber("44.10", 2).toString()).toEqual("44.1"); + expect(DenominatedNumber.fromNumber("44.100", 2).toString()).toEqual( + "44.1", + ); + }); - it("handles zero value", () => { - expect(new DenominatedNumber(0, 18).toString()).toEqual("0"); - }); + it("fills up leading zeros", () => { + expect(new DenominatedNumber("3", 0).toString()).toEqual("3"); + expect(new DenominatedNumber("3", 1).toString()).toEqual("0.3"); + expect(new DenominatedNumber("3", 2).toString()).toEqual("0.03"); + expect(new DenominatedNumber("3", 3).toString()).toEqual("0.003"); }); - describe("toNumber", () => { - it("works", () => { - expect(DenominatedNumber.fromNumber("0", 5).valueOf()).toEqual(0); - expect(DenominatedNumber.fromNumber("1", 5).valueOf()).toEqual(1); - expect(DenominatedNumber.fromNumber("1.5", 5).valueOf()).toEqual(1.5); - expect(DenominatedNumber.fromNumber("0.1", 5).valueOf()).toEqual(0.1); + it("handles zero value", () => { + expect(new DenominatedNumber(0, 18).toString()).toEqual("0"); + }); +}); - expect( - DenominatedNumber.fromNumber("1234500000000000", 5).valueOf(), - ).toEqual(1.2345e15); - expect( - DenominatedNumber.fromNumber("1234500000000000.002", 5).valueOf(), - ).toEqual(1.2345e15); - }); +describe("toNumber", () => { + it("works", () => { + expect(DenominatedNumber.fromNumber("0", 5).valueOf()).toEqual(0); + expect(DenominatedNumber.fromNumber("1", 5).valueOf()).toEqual(1); + expect(DenominatedNumber.fromNumber("1.5", 5).valueOf()).toEqual(1.5); + expect(DenominatedNumber.fromNumber("0.1", 5).valueOf()).toEqual(0.1); + + expect( + DenominatedNumber.fromNumber("1234500000000000", 5).valueOf(), + ).toEqual(1.2345e15); + expect( + DenominatedNumber.fromNumber("1234500000000000.002", 5).valueOf(), + ).toEqual(1.2345e15); }); +}); - describe("toLocaleString", () => { - it("add denomination to the string value", () => { - const string = new DenominatedNumber(30, 0, "DOT").toLocaleString( - "en-NZ", - ); +describe("toLocaleString", () => { + it("add denomination to the string value", () => { + const string = new DenominatedNumber(30, 0, "DOT").toLocaleString("en-NZ"); - expect(string).toContain("DOT"); - expect(string).toContain("30.00"); - }); + expect(string).toContain("DOT"); + expect(string).toContain("30.00"); + }); - it("keep compact notation", () => { - const string = new DenominatedNumber(30000, 0, "DOT").toLocaleString( - "en-NZ", - { notation: "compact" }, - ); + it("keep compact notation", () => { + const string = new DenominatedNumber(30000, 0, "DOT").toLocaleString( + "en-NZ", + { notation: "compact" }, + ); - expect(string).toContain("DOT"); - expect(string).toContain("30K"); - }); + expect(string).toContain("DOT"); + expect(string).toContain("30K"); }); });