Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/classes/shopping_cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ export class ShoppingCart {
this.cart.push(...optimumMatch);
this.match.push({ ingredient, selection: optimumMatch });
} catch (error) {
/* v8 ignore else -- @preserve */
if (error instanceof NoProductMatchError) {
this.misMatch.push({ ingredient, reason: error.reason });
} else {
throw error;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/parser_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,11 @@ export function findAndUpsertCookware(
quantity,
);
} catch (e) {
/* v8 ignore else -- expliciting error type -- @preserve */
if (e instanceof CannotAddTextValueError) {
diagnostics.push(createDiagnostic("COOKLANG_W0401", {}, span));
return cookware.push(newCookware) - 1;
} else {
throw e;
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/parser_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,29 @@
expect(diagnostics).toHaveDiagnostic("COOKLANG_W0401");
});

it("should rethrow errors other than CannotAddTextValueError", () => {
const cookware: Cookware[] = [
{
name: "oven",
quantity: {
type: "fixed",
value: { type: "fraction", num: 1, den: 0 },
},
},
];
const newCookware: Cookware = {
name: "oven",
quantity: {
type: "fixed",
value: { type: "fraction", num: 1, den: 1 },
},
};

expect(() => findAndUpsertCookware(cookware, newCookware, true)).toThrow(

Check failure on line 943 in test/parser_helpers.test.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Expected 4-5 arguments, but got 3.
DivisionByZeroError,
);
});

it("should throw a ReferencedCookwareNotFoundError if a reference cookware does not exist", () => {
const newCookware: Cookware = { name: "unreferenced-cookware", flags: [] };
expect(() => findAndUpsertCookware([], newCookware, true, [])).toThrow(
Expand Down
18 changes: 18 additions & 0 deletions test/shopping_cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe("initialisation", () => {
catalog: productCatalog,
list: shoppingList,
});

expect(shoppingCart.productCatalog).toBe(productCatalog);
expect(shoppingCart.shoppingList).toBe(shoppingList);
});
Expand All @@ -127,6 +128,23 @@ describe("initialisation", () => {
});
});

describe("unexpected matching errors", () => {
it("rethrows errors other than NoProductMatchError", () => {
const shoppingList = new ShoppingList();
shoppingList.addRecipe(new Recipe("@flour{1%g}"));
const shoppingCart = new ShoppingCart({
catalog: productCatalog,
list: shoppingList,
});
const unexpected = new Error("Unexpected matching failure");
Reflect.set(shoppingCart, "getOptimumMatch", () => {
throw unexpected;
});

expect(() => shoppingCart.buildCart()).toThrow(unexpected);
});
});

describe("buildCart", () => {
it("should handle ingredients with no matching products", () => {
const shoppingCart = new ShoppingCart();
Expand Down
Loading