From cc3846d1b57e074bb75065608bfa4846b4c6e227 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 18:28:05 +0000 Subject: [PATCH] test: simplify numeric parsing coverage --- src/infra/parse-finite-number.test.ts | 59 +++++++++++++++------------ 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/src/infra/parse-finite-number.test.ts b/src/infra/parse-finite-number.test.ts index 99b093dfe3b..d3c838cf61a 100644 --- a/src/infra/parse-finite-number.test.ts +++ b/src/infra/parse-finite-number.test.ts @@ -7,47 +7,52 @@ import { } from "./parse-finite-number.js"; describe("parseFiniteNumber", () => { - it("returns finite numbers", () => { - expect(parseFiniteNumber(42)).toBe(42); + it.each([ + { value: 42, expected: 42 }, + { value: "3.14", expected: 3.14 }, + { value: " 3.14ms", expected: 3.14 }, + ])("parses %j", ({ value, expected }) => { + expect(parseFiniteNumber(value)).toBe(expected); }); - it("parses numeric strings", () => { - expect(parseFiniteNumber("3.14")).toBe(3.14); - }); - - it("returns undefined for non-finite or non-numeric values", () => { - expect(parseFiniteNumber(Number.NaN)).toBeUndefined(); - expect(parseFiniteNumber(Number.POSITIVE_INFINITY)).toBeUndefined(); - expect(parseFiniteNumber("not-a-number")).toBeUndefined(); - expect(parseFiniteNumber(null)).toBeUndefined(); - }); + it.each([Number.NaN, Number.POSITIVE_INFINITY, "not-a-number", " ", null])( + "returns undefined for %j", + (value) => { + expect(parseFiniteNumber(value)).toBeUndefined(); + }, + ); }); describe("parseStrictInteger", () => { - it("parses exact integers", () => { - expect(parseStrictInteger("42")).toBe(42); - expect(parseStrictInteger(" -7 ")).toBe(-7); + it.each([ + { value: "42", expected: 42 }, + { value: " -7 ", expected: -7 }, + { value: 12, expected: 12 }, + ])("parses %j", ({ value, expected }) => { + expect(parseStrictInteger(value)).toBe(expected); }); - it("rejects junk prefixes and suffixes", () => { - expect(parseStrictInteger("42ms")).toBeUndefined(); - expect(parseStrictInteger("0abc")).toBeUndefined(); - expect(parseStrictInteger("1.5")).toBeUndefined(); + it.each(["42ms", "0abc", "1.5", " ", Number.MAX_SAFE_INTEGER + 1])("rejects %j", (value) => { + expect(parseStrictInteger(value)).toBeUndefined(); }); }); describe("parseStrictPositiveInteger", () => { - it("accepts only positive integers", () => { - expect(parseStrictPositiveInteger("9")).toBe(9); - expect(parseStrictPositiveInteger("0")).toBeUndefined(); - expect(parseStrictPositiveInteger("-1")).toBeUndefined(); + it.each([ + { value: "9", expected: 9 }, + { value: "0", expected: undefined }, + { value: "-1", expected: undefined }, + ])("parses %j", ({ value, expected }) => { + expect(parseStrictPositiveInteger(value)).toBe(expected); }); }); describe("parseStrictNonNegativeInteger", () => { - it("accepts zero and positive integers only", () => { - expect(parseStrictNonNegativeInteger("0")).toBe(0); - expect(parseStrictNonNegativeInteger("9")).toBe(9); - expect(parseStrictNonNegativeInteger("-1")).toBeUndefined(); + it.each([ + { value: "0", expected: 0 }, + { value: "9", expected: 9 }, + { value: "-1", expected: undefined }, + ])("parses %j", ({ value, expected }) => { + expect(parseStrictNonNegativeInteger(value)).toBe(expected); }); });