Skip to content

Commit f1873b6

Browse files
committed
emmas comments
1 parent 193461f commit f1873b6

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/interfaces/generators/imports/io.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const readReal = <TParams extends Record<string, unknown>, ParName extends strin
2222
const par = params[parName];
2323
errorIfMissingValueAndDefault(par, parName, defaultValue);
2424

25-
if (isNullish(par)) return defaultValue!;
25+
if (isNullish(par)) {
26+
checkScalar(defaultValue, parName);
27+
return defaultValue!;
28+
}
2629

2730
checkScalar(par, parName);
2831
return par;
@@ -34,7 +37,10 @@ const readInt = <TParams extends Record<string, unknown>, ParName extends string
3437
defaultValue?: TParams[ParName]
3538
) => {
3639
const par = readReal(params, parName, defaultValue);
37-
return Math.round(par as number);
40+
if (!Number.isInteger(par)) {
41+
throw new Error(`'${parName}' must be an integer`);
42+
}
43+
return par;
3844
};
3945

4046
const readSize = <TParams extends Record<string, unknown>, ParName extends string>(

tests/importsIo.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ describe("io", () => {
1212
e: -20.1,
1313
f: true,
1414
g: false,
15-
h: [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
15+
h: [1.1, 2.2, 3.3, 4.4, 5.5, 6.6],
16+
i: 2
1617
} as const;
1718
type Par = keyof typeof params;
1819

@@ -47,9 +48,23 @@ describe("io", () => {
4748
expect(io.readReal(params, par, def as any)).toBe(def);
4849
});
4950

50-
test("readInt rounds number", () => {
51+
test("readReal throws if default is not scalar", () => {
52+
const par: Par = "c";
53+
expect(() => {
54+
io.readReal(params, par, "hello" as any);
55+
}).toThrow(`'${par}' must be a scalar`);
56+
});
57+
58+
test("readInt throws if number is not an integer", () => {
5159
const par: Par = "a";
52-
expect(io.readInt(params, par)).toBe(10);
60+
expect(() => {
61+
io.readInt(params, par);
62+
}).toThrow(`'${par}' must be an integer`);
63+
});
64+
65+
test("readInt returns number if integer", () => {
66+
const par: Par = "i";
67+
expect(io.readInt(params, par)).toBe(2);
5368
});
5469

5570
test("readSize errors if number is negative", () => {

0 commit comments

Comments
 (0)