Skip to content

Commit f99b59c

Browse files
committed
test: maximize coverage by ignoring else path when irrelevant
1 parent 981da74 commit f99b59c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/classes/recipe.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export class Recipe {
144144
if (this.sections.length === 0 && section.isBlank()) {
145145
section.name = line.substring(1).trim();
146146
} else {
147+
/* v8 ignore else -- @preserve */
147148
if (!section.isBlank()) {
148149
this.sections.push(section);
149150
}
@@ -178,6 +179,7 @@ export class Recipe {
178179
let cursor = 0;
179180
for (const match of line.matchAll(tokensRegex)) {
180181
const idx = match.index;
182+
/* v8 ignore else -- @preserve */
181183
if (idx > cursor) {
182184
items.push({ type: "text", value: line.slice(cursor, idx) });
183185
}
@@ -288,8 +290,10 @@ export class Recipe {
288290
index: idxsInList.cookwareIndex,
289291
quantityPartIndex: idxsInList.quantityPartIndex,
290292
} as CookwareItem);
291-
} else if (groups.timerQuantity !== undefined) {
292-
const durationStr = groups.timerQuantity.trim();
293+
}
294+
// Then it's necessarily a timer which was matched
295+
else {
296+
const durationStr = groups.timerQuantity!.trim();
293297
const unit = (groups.timerUnit || "").trim();
294298
if (!unit) {
295299
throw new Error("Timer missing unit");
@@ -396,6 +400,7 @@ export class Recipe {
396400

397401
newRecipe.servings = originalServings * factor;
398402

403+
/* v8 ignore else -- @preserve */
399404
if (newRecipe.metadata.servings && this.metadata.servings) {
400405
if (
401406
floatRegex.test(String(this.metadata.servings).replace(",", ".").trim())
@@ -407,6 +412,7 @@ export class Recipe {
407412
}
408413
}
409414

415+
/* v8 ignore else -- @preserve */
410416
if (newRecipe.metadata.yield && this.metadata.yield) {
411417
if (
412418
floatRegex.test(String(this.metadata.yield).replace(",", ".").trim())
@@ -418,6 +424,7 @@ export class Recipe {
418424
}
419425
}
420426

427+
/* v8 ignore else -- @preserve */
421428
if (newRecipe.metadata.serves && this.metadata.serves) {
422429
if (
423430
floatRegex.test(String(this.metadata.serves).replace(",", ".").trim())

src/classes/shopping_list.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export class ShoppingList {
6666
this.ingredients = [];
6767
for (const { recipe, factor } of this.recipes) {
6868
const scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
69+
6970
for (const ingredient of scaledRecipe.ingredients) {
71+
// Do not add hidden ingredients to the shopping list
7072
if (ingredient.flags && ingredient.flags.includes("hidden")) {
7173
continue;
7274
}
@@ -77,8 +79,8 @@ export class ShoppingList {
7779

7880
let addSeparate = false;
7981
try {
80-
if (existingIngredient) {
81-
if (existingIngredient.quantity && ingredient.quantity) {
82+
if (existingIngredient && ingredient.quantity) {
83+
if (existingIngredient.quantity) {
8284
const newQuantity: Quantity = addQuantities(
8385
{
8486
value: existingIngredient.quantity,
@@ -93,8 +95,10 @@ export class ShoppingList {
9395
if (newQuantity.unit) {
9496
existingIngredient.unit = newQuantity.unit;
9597
}
96-
} else if (ingredient.quantity) {
98+
} else {
9799
existingIngredient.quantity = ingredient.quantity;
100+
101+
/* v8 ignore else -- only set unit if it is given -- @preserve */
98102
if (ingredient.unit) {
99103
existingIngredient.unit = ingredient.unit;
100104
}

src/parser_helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export function findAndUpsertIngredient(
9696

9797
// Checking whether any provided flags are the same as the original ingredient
9898
for (const flag of newIngredient.flags!) {
99+
/* v8 ignore else -- @preserve */
99100
if (!existingIngredient.flags!.includes(flag)) {
100101
throw new ReferencedItemCannotBeRedefinedError(
101102
"ingredient",
@@ -125,6 +126,7 @@ export function findAndUpsertIngredient(
125126
}
126127
quantityPartIndex = existingIngredient.quantityParts!.length - 1;
127128
} catch (e) {
129+
/* v8 ignore else -- expliciting error types -- @preserve */
128130
if (
129131
e instanceof IncompatibleUnitsError ||
130132
e instanceof CannotAddTextValueError
@@ -175,6 +177,7 @@ export function findAndUpsertCookware(
175177

176178
// Checking whether any provided flags are the same as the original cookware
177179
for (const flag of newCookware.flags) {
180+
/* v8 ignore else -- @preserve */
178181
if (!existingCookware.flags.includes(flag)) {
179182
throw new ReferencedItemCannotBeRedefinedError(
180183
"cookware",
@@ -206,6 +209,7 @@ export function findAndUpsertCookware(
206209
) - 1;
207210
}
208211
} catch (e) {
212+
/* v8 ignore else -- expliciting error type -- @preserve */
209213
if (e instanceof CannotAddTextValueError) {
210214
return {
211215
cookwareIndex: cookware.push(newCookware) - 1,
@@ -301,6 +305,7 @@ export function parseListMetaVar(content: string, varName: string) {
301305
);
302306
if (!listMatch) return undefined;
303307

308+
/* v8 ignore else -- @preserve */
304309
if (listMatch[1] !== undefined) {
305310
// Inline list: tags: [one, two, three]
306311
return listMatch[1].split(",").map((tag) => tag.trim());

0 commit comments

Comments
 (0)