From 33c60f715319da5e40689b24eb274923fbbaa236 Mon Sep 17 00:00:00 2001 From: Ellmo Rivetti Date: Sun, 22 May 2022 17:06:56 +0200 Subject: [PATCH 1/5] test(pharmacy): add tests for every drug --- pharmacy.test.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/pharmacy.test.js b/pharmacy.test.js index f0925fc1..62f55f33 100644 --- a/pharmacy.test.js +++ b/pharmacy.test.js @@ -1,9 +1,87 @@ import { Drug, Pharmacy } from "./pharmacy"; -describe("Pharmacy", () => { +describe("Pharmacy (Drug)", () => { it("should decrease the benefit and expiresIn", () => { expect(new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()).toEqual( [new Drug("test", 1, 2)] ); }); + + it("should not decrease the benefit and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("test", 0, 0)]).updateBenefitValue()).toEqual( + [new Drug("test", -1, 0)] + ); + }); + + it("should decrease the benefit twice as fast and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("test", 0, 10)]).updateBenefitValue()).toEqual( + [new Drug("test", -1, 8)] + ); + }); + + it("should decrease the benefit and expiresIn for both drugs", () => { + expect(new Pharmacy([new Drug("test", 2, 3), new Drug("test", 3, 4)]).updateBenefitValue()).toEqual( + [new Drug("test", 1, 2), new Drug("test", 2, 3)] + ); + }); +}) + +describe("Pharmacy (Herbal Tea)", () => { + it("should increase the benefit and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 5, 3)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", 4, 4)] + ); + }); + + it("should increase the benefit twice as fast and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 0, 3)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", -1, 5)] + ); + }); + + it("should increase the benefit to 50 and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 0, 49)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", -1, 50)] + ); + }); +}) + +describe("Pharmacy (Magic Pill)", () => { + it("should not change benefit and expiresIn (Magic Pill)", () => { + expect(new Pharmacy([new Drug("Magic Pill", 10, 40)]).updateBenefitValue()).toEqual( + [new Drug("Magic Pill", 10, 40)] + ); + }); +}) + +describe("Pharmacy (Fervex)", () => { + it("should decrease the benefit and expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 20, 10)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 19, 9)] + ); + }); + + it("should increase the benefit by two and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 10, 10)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 9, 12)] + ); + }); + + it("should increase the benefit by three and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 4, 10)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 3, 13)] + ); + }); + + it("should decrease the benefit to zero and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 0, 25)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", -1, 0)] + ); + }); + + it("should increase the benefit to 50 and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 3, 49)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 2, 50)] + ); + }); }); From 8d89fb078497300b83dd051da4ef89b8108e4198 Mon Sep 17 00:00:00 2001 From: Ellmo Rivetti Date: Sun, 22 May 2022 18:56:39 +0200 Subject: [PATCH 2/5] feat(drugLogics): add pure function and tests for each drug logic --- drugLogics.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++ drugLogics.test.js | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 drugLogics.js create mode 100644 drugLogics.test.js diff --git a/drugLogics.js b/drugLogics.js new file mode 100644 index 00000000..cbd0251f --- /dev/null +++ b/drugLogics.js @@ -0,0 +1,90 @@ +/** + * Used to increase the benefit value according to general drug rules + * @param {int} currentBenefitValue the benefit value before the increase + * @param {int} increaseValue the number used to increase + * @returns the increased benefit + */ +function increaseBenefit(currentBenefitValue, increaseValue) { + let increasedBenefit = currentBenefitValue + increaseValue; + if (increasedBenefit > 50) { + increasedBenefit = 50; + } + return increasedBenefit; +} + +/** + * Used to decrease the benefit value according to general drug rules + * @param {int} currentBenefitValue the benefit value before the decrease + * @param {int} decreaseValue the number used to decrease + * @returns the decreased benefit + */ +function decreaseBenefit(currentBenefitValue, decreaseValue) { + let decreasedBenefit = currentBenefitValue - decreaseValue; + if (decreasedBenefit < 0) { + decreasedBenefit = 0; + } + return decreasedBenefit; +} +/** + * Decrease the ExpiresIn value + * @param {int} currentExpiresInValue the ExpiresIn value to decrease + * @param {int} decreaseValue the number used to decrease + * @returns the decreased ExpiresIn + */ +function decreaseExpiresIn(currentExpiresInValue, decreaseValue) { + return currentExpiresInValue - decreaseValue; +} + +/** + * Used to know if a drug is expired or not + * @param {*} expiresIn the number of days left before expiration + * @returns true if the drug is expired + */ +function isExpired(expiresIn) { + return expiresIn <= 0; +} + +function herbalTeaLogic(expiresIn, benefit) { + if (isExpired(expiresIn)) { + benefit = increaseBenefit(benefit, 2); + } else { + benefit = increaseBenefit(benefit, 1); + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + +function magicPillLogic(expiresIn, benefit) { + return [expiresIn, benefit]; +} + +function fervexLogic(expiresIn, benefit) { + if (expiresIn > 10) { + return drugLogic(expiresIn, benefit); + } else if (expiresIn <= 10 && expiresIn > 5) { + benefit = increaseBenefit(benefit, 2); + } else if (expiresIn <= 5 && expiresIn > 0) { + benefit = increaseBenefit(benefit, 3); + } else { + benefit = 0; + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + +function drugLogic(expiresIn, benefit) { + if (isExpired(expiresIn)) { + benefit = decreaseBenefit(benefit, 2); + } else { + benefit = decreaseBenefit(benefit, 1); + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + +export { + herbalTeaLogic, + magicPillLogic, + fervexLogic, + drugLogic, + increaseBenefit, + decreaseBenefit, + isExpired +}; diff --git a/drugLogics.test.js b/drugLogics.test.js new file mode 100644 index 00000000..0a054944 --- /dev/null +++ b/drugLogics.test.js @@ -0,0 +1,75 @@ +import { + isExpired, + increaseBenefit, + decreaseBenefit, + magicPillLogic, + herbalTeaLogic, + fervexLogic, + drugLogic +} from "./drugLogics"; + +describe("Usual methods", () => { + it("should return false", () => { + expect(isExpired(1)).toEqual(false); + }); + + it("should return true", () => { + expect(isExpired(-1)).toEqual(true); + }); + + it("should increase value by one", () => { + expect(increaseBenefit(10, 1)).toEqual(11); + }); + + it("should not increase more than 50", () => { + expect(increaseBenefit(10, 100)).toEqual(50); + }); + + it("should decrease value by 2", () => { + expect(decreaseBenefit(10, 2)).toEqual(8); + }); + + it("should not decrease more than 0", () => { + expect(decreaseBenefit(10, 100)).toEqual(0); + }); + + it("should not change benefit", () => { + expect(magicPillLogic(10, 20)).toEqual([10, 20]); + }); + + it("should increase benefit by one ", () => { + expect(herbalTeaLogic(10, 20)).toEqual([9, 21]); + }); + + it("should increase benefit by two ", () => { + expect(herbalTeaLogic(-1, 20)).toEqual([-2, 22]); + }); + + it("should not increase benefit more than fifty ", () => { + expect(herbalTeaLogic(-1, 49)).toEqual([-2, 50]); + }); + + it("should decrease benefit by one", () => { + expect(fervexLogic(15, 10)).toEqual([14, 9]); + }); + + it("should increase benefit by two", () => { + expect(fervexLogic(10, 10)).toEqual([9, 12]); + }); + + it("should increase benefit by three", () => { + expect(fervexLogic(5, 10)).toEqual([4, 13]); + }); + + it("should decrease benefit to zero", () => { + expect(fervexLogic(0, 10)).toEqual([-1, 0]); + }); + + it("should decrease benefit by one", () => { + expect(drugLogic(5, 10)).toEqual([4, 9]); + }); + + it("should decrease benefit by two", () => { + expect(drugLogic(0, 10)).toEqual([-1, 8]); + }); +}); From a2a0a9cc23ddaf74bf20b9581c574fb7be214f78 Mon Sep 17 00:00:00 2001 From: Ellmo Rivetti Date: Sun, 22 May 2022 18:58:06 +0200 Subject: [PATCH 3/5] refactor(pharmacy): use drug logic functions in updateBenefitValue --- pharmacy.js | 67 +++++++++++++++-------------------------------------- 1 file changed, 19 insertions(+), 48 deletions(-) diff --git a/pharmacy.js b/pharmacy.js index cda44c41..6b2de78f 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -1,3 +1,5 @@ +import { herbalTeaLogic, magicPillLogic, fervexLogic, drugLogic } from "./drugLogics"; + export class Drug { constructor(name, expiresIn, benefit) { this.name = name; @@ -11,56 +13,25 @@ export class Pharmacy { this.drugs = drugs; } updateBenefitValue() { - for (var i = 0; i < this.drugs.length; i++) { - if ( - this.drugs[i].name != "Herbal Tea" && - this.drugs[i].name != "Fervex" - ) { - if (this.drugs[i].benefit > 0) { - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].benefit = this.drugs[i].benefit - 1; - } - } - } else { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - if (this.drugs[i].name == "Fervex") { - if (this.drugs[i].expiresIn < 11) { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - } - } - if (this.drugs[i].expiresIn < 6) { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - } - } - } - } - } - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1; - } - if (this.drugs[i].expiresIn < 0) { - if (this.drugs[i].name != "Herbal Tea") { - if (this.drugs[i].name != "Fervex") { - if (this.drugs[i].benefit > 0) { - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].benefit = this.drugs[i].benefit - 1; - } - } - } else { - this.drugs[i].benefit = - this.drugs[i].benefit - this.drugs[i].benefit; - } - } else { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - } - } + for (let i = 0; i < this.drugs.length; i++) { + let resultLogic; + switch (this.drugs[i].name) { + case "Herbal Tea": + resultLogic = herbalTeaLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; + case "Magic Pill": + resultLogic = magicPillLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; + case "Fervex": + resultLogic = fervexLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; + default: + resultLogic = drugLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; } + this.drugs[i].expiresIn = resultLogic[0]; + this.drugs[i].benefit = resultLogic[1]; } - return this.drugs; } } From c0198d74d9b831bd529a7e940334c8bcdc4e1266 Mon Sep 17 00:00:00 2001 From: Ellmo Rivetti Date: Sun, 22 May 2022 19:14:26 +0200 Subject: [PATCH 4/5] test(drugLogics): re-arranged drugLogics tests --- drugLogics.test.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/drugLogics.test.js b/drugLogics.test.js index 0a054944..aa47ea2a 100644 --- a/drugLogics.test.js +++ b/drugLogics.test.js @@ -5,7 +5,8 @@ import { magicPillLogic, herbalTeaLogic, fervexLogic, - drugLogic + drugLogic, + decreaseExpiresIn } from "./drugLogics"; describe("Usual methods", () => { @@ -33,11 +34,19 @@ describe("Usual methods", () => { expect(decreaseBenefit(10, 100)).toEqual(0); }); + it("should decrease by one", () => { + expect(decreaseExpiresIn(10, 1)).toEqual(9); + }); +}); + +describe("Logic methods - Magic Pill", () => { it("should not change benefit", () => { expect(magicPillLogic(10, 20)).toEqual([10, 20]); }); +}); - it("should increase benefit by one ", () => { +describe("Logic methods - Herbal Tea", () => { + it("should increase benefit by one", () => { expect(herbalTeaLogic(10, 20)).toEqual([9, 21]); }); @@ -48,7 +57,9 @@ describe("Usual methods", () => { it("should not increase benefit more than fifty ", () => { expect(herbalTeaLogic(-1, 49)).toEqual([-2, 50]); }); +}); +describe("Logic methods - Fervex Pill", () => { it("should decrease benefit by one", () => { expect(fervexLogic(15, 10)).toEqual([14, 9]); }); @@ -64,7 +75,9 @@ describe("Usual methods", () => { it("should decrease benefit to zero", () => { expect(fervexLogic(0, 10)).toEqual([-1, 0]); }); +}); +describe("Logic methods - Drug", () => { it("should decrease benefit by one", () => { expect(drugLogic(5, 10)).toEqual([4, 9]); }); From ad5a3a6fa11d71b5b53dccdda05f676b0606f19b Mon Sep 17 00:00:00 2001 From: Ellmo Rivetti Date: Sun, 22 May 2022 19:15:12 +0200 Subject: [PATCH 5/5] feat(dafalgan): add new drug dafalgan --- drugLogics.js | 11 +++++++++++ drugLogics.test.js | 11 +++++++++++ pharmacy.js | 5 ++++- pharmacy.test.js | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/drugLogics.js b/drugLogics.js index cbd0251f..cd1afb86 100644 --- a/drugLogics.js +++ b/drugLogics.js @@ -70,6 +70,15 @@ function fervexLogic(expiresIn, benefit) { return [decreaseExpiresIn(expiresIn, 1), benefit]; } +function dafalganLogic(expiresIn, benefit) { + if (isExpired(expiresIn)) { + benefit = decreaseBenefit(benefit, 4); + } else { + benefit = decreaseBenefit(benefit, 2); + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + function drugLogic(expiresIn, benefit) { if (isExpired(expiresIn)) { benefit = decreaseBenefit(benefit, 2); @@ -83,8 +92,10 @@ export { herbalTeaLogic, magicPillLogic, fervexLogic, + dafalganLogic, drugLogic, increaseBenefit, decreaseBenefit, + decreaseExpiresIn, isExpired }; diff --git a/drugLogics.test.js b/drugLogics.test.js index aa47ea2a..99a19904 100644 --- a/drugLogics.test.js +++ b/drugLogics.test.js @@ -6,6 +6,7 @@ import { herbalTeaLogic, fervexLogic, drugLogic, + dafalganLogic, decreaseExpiresIn } from "./drugLogics"; @@ -86,3 +87,13 @@ describe("Logic methods - Drug", () => { expect(drugLogic(0, 10)).toEqual([-1, 8]); }); }); + +describe("Logic methods - Dafalgan", () => { + it("should decrease benefit by two", () => { + expect(dafalganLogic(10, 10)).toEqual([9, 8]); + }); + + it("should decrease benefit by four", () => { + expect(dafalganLogic(0, 10)).toEqual([-1, 6]); + }); +}); diff --git a/pharmacy.js b/pharmacy.js index 6b2de78f..57a5c599 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -1,4 +1,4 @@ -import { herbalTeaLogic, magicPillLogic, fervexLogic, drugLogic } from "./drugLogics"; +import { herbalTeaLogic, magicPillLogic, fervexLogic, dafalganLogic, drugLogic } from "./drugLogics"; export class Drug { constructor(name, expiresIn, benefit) { @@ -25,6 +25,9 @@ export class Pharmacy { case "Fervex": resultLogic = fervexLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); break; + case "Dafalgan": + resultLogic = dafalganLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; default: resultLogic = drugLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); break; diff --git a/pharmacy.test.js b/pharmacy.test.js index 62f55f33..94bdd945 100644 --- a/pharmacy.test.js +++ b/pharmacy.test.js @@ -85,3 +85,17 @@ describe("Pharmacy (Fervex)", () => { ); }); }); + +describe("Pharmacy (Dafalgan)", () => { + it("should decrease benefit by two and expiresIn (Dafalgan)", () => { + expect(new Pharmacy([new Drug("Dafalgan", 10, 40)]).updateBenefitValue()).toEqual( + [new Drug("Dafalgan", 9, 38)] + ); + }); + + it("should decrease benefit by four and expiresIn (Dafalgan)", () => { + expect(new Pharmacy([new Drug("Dafalgan", 0, 40)]).updateBenefitValue()).toEqual( + [new Drug("Dafalgan", -1, 36)] + ); + }); +})