Skip to content
Open
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
24 changes: 24 additions & 0 deletions __tests__/entities/Drug.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Drug from '../../entities/Drug';

describe('Drug', () => {
// Test case for the constructor
it('should create a drug with the given name, expiresIn, and benefit', () => {
const drug = new Drug('Aspirin', 10, 5);
expect(drug.name).toBe('Aspirin');
expect(drug.expiresIn).toBe(10);
expect(drug.benefit).toBe(5);
});

// Test case for isExpired method
describe('isExpired', () => {
it('should return true if expiresIn is less than 0', () => {
const expiredDrug = new Drug('ExpiredDrug', -1, 5);
expect(expiredDrug.isExpired()).toBe(true);
});

it('should return false if expiresIn is greater than or equal to 0', () => {
const nonExpiredDrug = new Drug('NonExpiredDrug', 5, 10);
expect(nonExpiredDrug.isExpired()).toBe(false);
});
});
});
55 changes: 55 additions & 0 deletions __tests__/entities/Pharmacy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Pharmacy from '../../entities/Pharmacy';
import HerbalTeaStrategy from '../../strategies/HerbalTeaStrategy';
import MagicPillStrategy from '../../strategies/MagicPillStrategy';
import FervexStrategy from '../../strategies/FervexStrategy';
import DafalganStrategy from '../../strategies/DafalganStrategy';
import DefaultStrategy from '../../strategies/DefaultStrategy';

jest.mock('../../strategies/HerbalTeaStrategy');
jest.mock('../../strategies/MagicPillStrategy');
jest.mock('../../strategies/FervexStrategy');
jest.mock('../../strategies/DafalganStrategy');
jest.mock('../../strategies/DefaultStrategy');

describe('Pharmacy', () => {
describe('updateBenefitValue method', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should update benefit value using HerbalTeaStrategy for Herbal Tea drug', () => {
const herbalTeaDrug = { name: 'Herbal Tea', expiresIn: 5, benefit: 10 };
const pharmacy = new Pharmacy([herbalTeaDrug]);
pharmacy.updateBenefitValue();
expect(HerbalTeaStrategy.mock.instances[0].update).toHaveBeenCalledWith(herbalTeaDrug);
});

it('should update benefit value using MagicPillStrategy for Magic Pill drug', () => {
const magicPillDrug = { name: 'Magic Pill', expiresIn: 10, benefit: 20 };
const pharmacy = new Pharmacy([magicPillDrug]);
pharmacy.updateBenefitValue();
expect(MagicPillStrategy.mock.instances[0].update).toHaveBeenCalledWith(magicPillDrug);
});

it('should update benefit value using FervexStrategy for Fervex drug', () => {
const fervexDrug = { name: 'Fervex', expiresIn: 8, benefit: 30 };
const pharmacy = new Pharmacy([fervexDrug]);
pharmacy.updateBenefitValue();
expect(FervexStrategy.mock.instances[0].update).toHaveBeenCalledWith(fervexDrug);
});

it('should update benefit value using DafalganStrategy for Dafalgan drug', () => {
const dafalganDrug = { name: 'Dafalgan', expiresIn: 3, benefit: 25 };
const pharmacy = new Pharmacy([dafalganDrug]);
pharmacy.updateBenefitValue();
expect(DafalganStrategy.mock.instances[0].update).toHaveBeenCalledWith(dafalganDrug);
});

it('should update benefit value using DefaultStrategy for unknown drug', () => {
const unknownDrug = { name: 'UnknownDrug', expiresIn: 5, benefit: 15 };
const pharmacy = new Pharmacy([unknownDrug]);
pharmacy.updateBenefitValue();
expect(DefaultStrategy.mock.instances[0].update).toHaveBeenCalledWith(unknownDrug);
});
});
});
22 changes: 22 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

describe('Generated File Test', () => {
it('should match the content of the expected file', () => {
// Run script to generate the file
execSync('babel-node index.js', { stdio: 'inherit' });

const generatedFilePath = path.join(__dirname, '../output.txt');
const expectedFilePath = path.join(__dirname, '../expectedOutput.txt');

// Read the contents of the generated file
const generatedFileContent = fs.readFileSync(generatedFilePath, 'utf8');

// Read the contents of the expected file
const expectedFileContent = fs.readFileSync(expectedFilePath, 'utf8');

// Compare the contents
expect(generatedFileContent).toEqual(expectedFileContent);
});
});
27 changes: 27 additions & 0 deletions __tests__/strategies/DafalganStrategy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Drug from '../../entities/Drug';
import DafalganStrategy from '../../strategies/DafalganStrategy';

describe('DafalganStrategy', () => {
describe('update method', () => {
it('should decrement expiresIn by 1', () => {
let drug = new Drug("Dafalgan", 6, 10);
const strategy = new DafalganStrategy();
drug = strategy.update(drug);
expect(drug.expiresIn).toBe(5);
});

it('should decrement benefit by 4 when expiresIn is less than or equal to 0', () => {
let drug = new Drug("Dafalgan", 0, 10);
const strategy = new DafalganStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(6);
});

it('should decrement benefit by 2 when expiresIn is greater than 0', () => {
let drug = new Drug("Dafalgan", 5, 10);
const strategy = new DafalganStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(8);
});
});
});
27 changes: 27 additions & 0 deletions __tests__/strategies/DefaultStrategy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Drug from '../../entities/Drug';
import DefaultStrategy from '../../strategies/DefaultStrategy';

describe('DefaultStrategy', () => {
describe('update method', () => {
it('should decrement expiresIn by 1', () => {
const drug = new Drug("Doliprane", 5, 10);
const strategy = new DefaultStrategy();
strategy.update(drug);
expect(drug.expiresIn).toBe(4);
});

it('should decrement benefit by 1 when expiresIn is greater than 0', () => {
const drug = new Drug("Doliprane", 5, 10);
const strategy = new DefaultStrategy();
strategy.update(drug);
expect(drug.benefit).toBe(9);
});

it('should decrement benefit by 2 when expiresIn is less than or equal to 0', () => {
const drug = new Drug("Doliprane", 0, 10);
const strategy = new DefaultStrategy();
strategy.update(drug);
expect(drug.benefit).toBe(8);
});
});
});
48 changes: 48 additions & 0 deletions __tests__/strategies/FervexStrategy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Drug from '../../entities/Drug';
import FervexStrategy from '../../strategies/FervexStrategy';

describe('FervexStrategy', () => {
describe('update method', () => {
it('should decrement expiresIn by 1', () => {
let drug = new Drug('Fervex', 6, 10);
const strategy = new FervexStrategy();
drug = strategy.update(drug);
expect(drug.expiresIn).toBe(5);
});

it('should set benefit to 0 when expiresIn is less than or equal to 0', () => {
let drug = new Drug('Fervex', 0, 10);
const strategy = new FervexStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(0);
});

it('should increase benefit by 3 when expiresIn is 5 or less, up to a maximum of 50', () => {
let drug = new Drug('Fervex', 5, 10);
const strategy = new FervexStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(13);
});

it('should increase benefit by 2 when expiresIn is 10 or less, up to a maximum of 50', () => {
let drug = new Drug('Fervex', 8, 10);
const strategy = new FervexStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(12);
});

it('should increase benefit by 1 when expiresIn is more than 10, up to a maximum of 50', () => {
let drug = new Drug('Fervex', 15, 10);
const strategy = new FervexStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(11);
});

it('should not exceed maximum benefit of 50', () => {
let drug = new Drug('Fervex', 5, 48);
const strategy = new FervexStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(50);
});
});
});
34 changes: 34 additions & 0 deletions __tests__/strategies/HerbalTeaStrategy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Drug from '../../entities/Drug';
import HerbalTeaStrategy from '../../strategies/HerbalTeaStrategy';

describe('HerbalTeaStrategy', () => {
describe('update method', () => {
it('should decrement expiresIn by 1', () => {
let drug = new Drug('Herbal Tea', 6, 10);
const strategy = new HerbalTeaStrategy();
drug = strategy.update(drug);
expect(drug.expiresIn).toBe(5);
});

it('should increase benefit by 2 when expiresIn is less than or equal to 0, up to a maximum of 50', () => {
let drug = new Drug('Herbal Tea', 0, 10);
const strategy = new HerbalTeaStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(12);
});

it('should increase benefit by 1 when expiresIn is greater than 0, up to a maximum of 50', () => {
let drug = new Drug('Herbal Tea', 5, 10);
const strategy = new HerbalTeaStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(11);
});

it('should not exceed maximum benefit of 50', () => {
let drug = new Drug('Herbal Tea', 0, 49);
const strategy = new HerbalTeaStrategy();
drug = strategy.update(drug);
expect(drug.benefit).toBe(50);
});
});
});
12 changes: 12 additions & 0 deletions __tests__/strategies/MagicPillStrategy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import MagicPillStrategy from '../../strategies/MagicPillStrategy';

describe('MagicPillStrategy', () => {
describe('update method', () => {
it('should return the same drug without any modifications', () => {
const originalDrug = { name: 'Magic Pill', expiresIn: 10, benefit: 20 };
const strategy = new MagicPillStrategy();
const updatedDrug = strategy.update(originalDrug);
expect(updatedDrug).toEqual(originalDrug);
});
});
});
42 changes: 42 additions & 0 deletions __tests__/utils/forceRange.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import forceRange from '../../utils/forceRange';

describe('forceRange', () => {
it('should return the value when it is within the specified range', () => {
const result = forceRange(10, 5, 20);
expect(result).toBe(10);
});

it('should adjust the value to the minimum when it is below the specified range', () => {
const result = forceRange(3, 5, 20);
expect(result).toBe(5);
});

it('should adjust the value to the maximum when it is above the specified range', () => {
const result = forceRange(25, 5, 20);
expect(result).toBe(20);
});

it('should throw an error for undefined value', () => {
expect(() => forceRange(undefined, 5, 20)).toThrowError('Invalid value');
});

it('should throw an error for null value', () => {
expect(() => forceRange(null, 5, 20)).toThrowError('Invalid value');
});

it('should throw an error for undefined min', () => {
expect(() => forceRange(10, undefined, 20)).toThrowError('Invalid min');
});

it('should throw an error for null min', () => {
expect(() => forceRange(10, null, 20)).toThrowError('Invalid min');
});

it('should throw an error for undefined max', () => {
expect(() => forceRange(10, 5, undefined)).toThrowError('Invalid max');
});

it('should throw an error for null max', () => {
expect(() => forceRange(10, 5, null)).toThrowError('Invalid max');
});
});
34 changes: 34 additions & 0 deletions entities/Drug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Represents a medicinal drug.
*
* @class Drug
* @param {string} name The name of the drug.
* @param {number} expiresIn The duration in days after which the drug expires.
* @param {number} benefit The initial benefit of the drug.
*/
export default class Drug {
/**
* Constructor for the Drug class.
*
* @param {string} name The name of the drug.
* @param {number} expiresIn The duration in days after which the drug expires.
* @param {number} benefit The initial benefit of the drug.
*/
constructor(name, expiresIn, benefit) {
this.name = name;
this.expiresIn = expiresIn;
this.benefit = benefit;
}

/**
* Checks if the drug has expired.
*
* @method
* @name isExpired
* @returns {boolean} True if the drug has expired, false otherwise.
*/
isExpired() {
return this.expiresIn < 0;
}
}

52 changes: 52 additions & 0 deletions entities/Pharmacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Imports the strategy classes from the strategies directory.
*/
import DafalganStrategy from "../strategies/DafalganStrategy";
import DefaultStrategy from "../strategies/DefaultStrategy";
import FervexStrategy from "../strategies/FervexStrategy";
import HerbalTeaStrategy from "../strategies/HerbalTeaStrategy";
import MagicPillStrategy from "../strategies/MagicPillStrategy";

/**
* Represents a pharmacy that manages and updates the benefit values of drugs.
*
* @class Pharmacy
* @param {Array<Drug>} drugs An array of drugs to manage.
*/
export default class Pharmacy {
/**
* Constructor for the Pharmacy class.
*
* @param {Array<Drug>} drugs An array of drugs to manage.
*/
constructor(drugs = []) {
this.drugs = drugs;
this.strategies = {
"Herbal Tea": new HerbalTeaStrategy(),
"Magic Pill": new MagicPillStrategy(),
"Fervex": new FervexStrategy(),
"Dafalgan": new DafalganStrategy(),
"default": new DefaultStrategy()
}
}

/**
* Updates the benefit value of each drug based on its specific strategy.
*
* @returns {Array<Drug>} The updated list of drugs.
*/
updateBenefitValue() {
for (let i = 0; i < this.drugs.length; i++) {
let drug = this.drugs[i];
const strategy = this.strategies[drug.name] || this.strategies.default;

try {
drug = strategy.update(drug);
} catch (error) {
console.error(`Error updating drug "${drug.name}": ${error.message}`);
}
}

return this.drugs;
}
}
1 change: 1 addition & 0 deletions expectedOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"Doliprane","expiresIn":19,"benefit":29},{"name":"Herbal Tea","expiresIn":9,"benefit":6},{"name":"Fervex","expiresIn":4,"benefit":43},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":18,"benefit":28},{"name":"Herbal Tea","expiresIn":8,"benefit":7},{"name":"Fervex","expiresIn":3,"benefit":46},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":17,"benefit":27},{"name":"Herbal Tea","expiresIn":7,"benefit":8},{"name":"Fervex","expiresIn":2,"benefit":49},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":16,"benefit":26},{"name":"Herbal Tea","expiresIn":6,"benefit":9},{"name":"Fervex","expiresIn":1,"benefit":50},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":15,"benefit":25},{"name":"Herbal Tea","expiresIn":5,"benefit":10},{"name":"Fervex","expiresIn":0,"benefit":50},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":14,"benefit":24},{"name":"Herbal Tea","expiresIn":4,"benefit":11},{"name":"Fervex","expiresIn":-1,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":13,"benefit":23},{"name":"Herbal Tea","expiresIn":3,"benefit":12},{"name":"Fervex","expiresIn":-2,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":12,"benefit":22},{"name":"Herbal Tea","expiresIn":2,"benefit":13},{"name":"Fervex","expiresIn":-3,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":11,"benefit":21},{"name":"Herbal Tea","expiresIn":1,"benefit":14},{"name":"Fervex","expiresIn":-4,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":10,"benefit":20},{"name":"Herbal Tea","expiresIn":0,"benefit":15},{"name":"Fervex","expiresIn":-5,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":9,"benefit":19},{"name":"Herbal Tea","expiresIn":-1,"benefit":17},{"name":"Fervex","expiresIn":-6,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":8,"benefit":18},{"name":"Herbal Tea","expiresIn":-2,"benefit":19},{"name":"Fervex","expiresIn":-7,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":7,"benefit":17},{"name":"Herbal Tea","expiresIn":-3,"benefit":21},{"name":"Fervex","expiresIn":-8,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":6,"benefit":16},{"name":"Herbal Tea","expiresIn":-4,"benefit":23},{"name":"Fervex","expiresIn":-9,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":5,"benefit":15},{"name":"Herbal Tea","expiresIn":-5,"benefit":25},{"name":"Fervex","expiresIn":-10,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":4,"benefit":14},{"name":"Herbal Tea","expiresIn":-6,"benefit":27},{"name":"Fervex","expiresIn":-11,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":3,"benefit":13},{"name":"Herbal Tea","expiresIn":-7,"benefit":29},{"name":"Fervex","expiresIn":-12,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":2,"benefit":12},{"name":"Herbal Tea","expiresIn":-8,"benefit":31},{"name":"Fervex","expiresIn":-13,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":1,"benefit":11},{"name":"Herbal Tea","expiresIn":-9,"benefit":33},{"name":"Fervex","expiresIn":-14,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":0,"benefit":10},{"name":"Herbal Tea","expiresIn":-10,"benefit":35},{"name":"Fervex","expiresIn":-15,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-1,"benefit":8},{"name":"Herbal Tea","expiresIn":-11,"benefit":37},{"name":"Fervex","expiresIn":-16,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-2,"benefit":6},{"name":"Herbal Tea","expiresIn":-12,"benefit":39},{"name":"Fervex","expiresIn":-17,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-3,"benefit":4},{"name":"Herbal Tea","expiresIn":-13,"benefit":41},{"name":"Fervex","expiresIn":-18,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-4,"benefit":2},{"name":"Herbal Tea","expiresIn":-14,"benefit":43},{"name":"Fervex","expiresIn":-19,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-5,"benefit":0},{"name":"Herbal Tea","expiresIn":-15,"benefit":45},{"name":"Fervex","expiresIn":-20,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-6,"benefit":0},{"name":"Herbal Tea","expiresIn":-16,"benefit":47},{"name":"Fervex","expiresIn":-21,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-7,"benefit":0},{"name":"Herbal Tea","expiresIn":-17,"benefit":49},{"name":"Fervex","expiresIn":-22,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-8,"benefit":0},{"name":"Herbal Tea","expiresIn":-18,"benefit":50},{"name":"Fervex","expiresIn":-23,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-9,"benefit":0},{"name":"Herbal Tea","expiresIn":-19,"benefit":50},{"name":"Fervex","expiresIn":-24,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-10,"benefit":0},{"name":"Herbal Tea","expiresIn":-20,"benefit":50},{"name":"Fervex","expiresIn":-25,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}]
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Drug, Pharmacy } from "./pharmacy";
import fs from "fs";
import Drug from "./entities/Drug";
import Pharmacy from "./entities/Pharmacy";

const drugs = [
new Drug("Doliprane", 20, 30),
Expand Down
Loading