Skip to content

Commit 10cc9cd

Browse files
committed
test(parser_helper): increase coverage
1 parent 3a912b9 commit 10cc9cd

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/parser_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export function findAndUpsertCookware(
156156
}
157157

158158
// Parser when we know the input is either a number-like value
159-
const parseFixedValue = (
159+
export const parseFixedValue = (
160160
input_str: string,
161161
): TextValue | DecimalValue | FractionValue => {
162162
if (!numberLikeRegex.test(input_str)) {

test/parser_helpers.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
parseSimpleMetaVar,
99
parseScalingMetaVar,
1010
parseListMetaVar,
11+
parseFixedValue,
12+
parseQuantityInput,
1113
extractMetadata,
1214
findAndUpsertCookware,
1315
findAndUpsertIngredient,
@@ -370,6 +372,21 @@ describe("findAndUpsertIngredient", () => {
370372
expect(ingredients_noqtt[0]!.quantity).toBe(undefined);
371373
});
372374

375+
it("should insert new ingredient if referenced ingredient has a text quantity", () => {
376+
const ingredients: Ingredient[] = [
377+
{
378+
name: "eggs",
379+
quantity: { type: "fixed", value: { type: "text", value: "one" } },
380+
},
381+
];
382+
const newIngredient: Ingredient = {
383+
name: "eggs",
384+
quantity: { type: "fixed", value: { type: "decimal", value: 1 } },
385+
};
386+
expect(findAndUpsertIngredient(ingredients, newIngredient, true)).toBe(1);
387+
expect(ingredients).toHaveLength(2);
388+
});
389+
373390
it("should adopt quantity of new ingredient if referenced one has none", () => {
374391
const ingredients: Ingredient[] = [{ name: "eggs" }];
375392
const newIngredient: Ingredient = {
@@ -402,3 +419,23 @@ describe("findAndUpsertIngredient", () => {
402419
);
403420
});
404421
});
422+
423+
describe("parseFixedValue", () => {
424+
it("parses non numerical value as text", () => {
425+
expect(parseFixedValue("1-ish")).toEqual({ type: "text", value: "1-ish" });
426+
});
427+
428+
it("parses fractions as such", () => {
429+
expect(parseFixedValue("1/2")).toEqual({
430+
type: "fraction",
431+
num: 1,
432+
den: 2,
433+
});
434+
});
435+
436+
it("parses decimal values as such", () => {
437+
expect(parseFixedValue("1.5")).toEqual({ type: "decimal", value: 1.5 });
438+
expect(parseFixedValue("0.1")).toEqual({ type: "decimal", value: 0.1 });
439+
expect(parseFixedValue("1")).toEqual({ type: "decimal", value: 1 });
440+
});
441+
});

0 commit comments

Comments
 (0)