Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement _check #2407

Open
wants to merge 1 commit into
base: Ongkiboy
Choose a base branch
from
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
100 changes: 52 additions & 48 deletions src/calculator.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
exports._check = () => {
// DRY up the codebase with this function
// First, move the duplicate error checking code here
// Then, invoke this function inside each of the others
// HINT: you can invoke this function with exports._check()
};

exports.add = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
return x + y;
};

exports.subtract = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
return x - y;
};

exports.multiply = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
return x * y;
};

exports.divide = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
return x / y;
};

module.exports = exports;
exports._check = (x, y) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

// DRY up the codebase with this function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

// First, move the duplicate error checking code here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

// Then, invoke this function inside each of the others
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

// HINT: you can invoke this function with exports._check()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

if (typeof x !== 'number') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

throw new TypeError(`${x} is not a number`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

if (typeof y !== 'number') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

throw new TypeError(`${y} is not a number`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

exports.add = (x, y) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

exports._check(x, y);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

return x + y;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

exports.subtract = (x, y) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

exports._check(x, y);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected linebreaks to be 'LF' but found 'CRLF'.
(learn more)

if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
return x - y;
};

exports.multiply = (x, y) => {
exports._check(x, y);
if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
return x * y;
};

exports.divide = (x, y) => {
exports._check(x, y);
if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
return x / y;
};

module.exports = exports;
274 changes: 137 additions & 137 deletions src/calculator.test.js
Original file line number Diff line number Diff line change
@@ -1,137 +1,137 @@
/* eslint-disable no-unused-expressions */
const calculator = require('./calculator');

describe.skip('_check', () => {
beforeEach(() => {
sinon.spy(calculator, '_check');
});

afterEach(() => {
calculator._check.restore();
});

it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator._check(40, '2')).to.throw(TypeError);
expect(() => calculator._check(40, [])).to.throw(TypeError);
expect(() => calculator._check(40, {})).to.throw(TypeError);
expect(() => calculator._check('40', 2)).to.throw(TypeError);
expect(() => calculator._check([], 2)).to.throw(TypeError);
expect(() => calculator._check({}, 2)).to.throw(TypeError);
});

it('should be called once in "add"', () => {
calculator.add(40, 2);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(40, 2);
});

it('should be called once in "subtract"', () => {
calculator.subtract(44, 2);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(44, 2);
});

it('should be called once in "multiply"', () => {
calculator.multiply(6, 7);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(6, 7);
});

it('should be called once in "divide"', () => {
calculator.divide(84, 2);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(84, 2);
});
});

describe('add', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.add(40, '2')).to.throw(TypeError);
expect(() => calculator.add(40, [])).to.throw(TypeError);
expect(() => calculator.add(40, {})).to.throw(TypeError);
expect(() => calculator.add('40', 2)).to.throw(TypeError);
expect(() => calculator.add([], 2)).to.throw(TypeError);
expect(() => calculator.add({}, 2)).to.throw(TypeError);
});

it('should add two positive numbers', () => {
expect(calculator.add(40, 2)).to.equal(42);
});

it('should add two negative numbers', () => {
expect(calculator.add(-40, -2)).to.equal(-42);
});

it('should add one positive number and one negative number', () => {
expect(calculator.add(44, -2)).to.equal(42);
});
});

describe('subtract', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.subtract(40, '2')).to.throw(TypeError);
expect(() => calculator.subtract(40, [])).to.throw(TypeError);
expect(() => calculator.subtract(40, {})).to.throw(TypeError);
expect(() => calculator.subtract('40', 2)).to.throw(TypeError);
expect(() => calculator.subtract([], 2)).to.throw(TypeError);
expect(() => calculator.subtract({}, 2)).to.throw(TypeError);
});

it('should subtract two positive numbers', () => {
expect(calculator.subtract(44, 2)).to.equal(42);
});

it('should subtract two negative numbers', () => {
expect(calculator.subtract(-44, -2)).to.equal(-42);
});

it('should subtract one positive number and one negative number', () => {
expect(calculator.subtract(40, -2)).to.equal(42);
});
});

describe('multiply', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.multiply(40, '2')).to.throw(TypeError);
expect(() => calculator.multiply(40, [])).to.throw(TypeError);
expect(() => calculator.multiply(40, {})).to.throw(TypeError);
expect(() => calculator.multiply('40', 2)).to.throw(TypeError);
expect(() => calculator.multiply([], 2)).to.throw(TypeError);
expect(() => calculator.multiply({}, 2)).to.throw(TypeError);
});

it('should multiply two positive numbers', () => {
expect(calculator.multiply(6, 7)).to.equal(42);
});

it('should multiply two negative numbers', () => {
expect(calculator.multiply(-6, -7)).to.equal(42);
});

it('should multiply one positive number and one negative number', () => {
expect(calculator.multiply(6, -7)).to.equal(-42);
});
});

describe('divide', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.divide(40, '2')).to.throw(TypeError);
expect(() => calculator.divide(40, [])).to.throw(TypeError);
expect(() => calculator.divide(40, {})).to.throw(TypeError);
expect(() => calculator.divide('40', 2)).to.throw(TypeError);
expect(() => calculator.divide([], 2)).to.throw(TypeError);
expect(() => calculator.divide({}, 2)).to.throw(TypeError);
});

it('should divide two positive numbers', () => {
expect(calculator.divide(84, 2)).to.equal(42);
});

it('should divide two negative numbers', () => {
expect(calculator.divide(-84, -2)).to.equal(42);
});

it('should divide one positive number and one negative number', () => {
expect(calculator.divide(84, -2)).to.equal(-42);
});
});
/* eslint-disable no-unused-expressions */
const calculator = require('./calculator');
describe('_check', () => {
beforeEach(() => {
sinon.spy(calculator, '_check');
});
afterEach(() => {
calculator._check.restore();
});
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator._check(40, '2')).to.throw(TypeError);
expect(() => calculator._check(40, [])).to.throw(TypeError);
expect(() => calculator._check(40, {})).to.throw(TypeError);
expect(() => calculator._check('40', 2)).to.throw(TypeError);
expect(() => calculator._check([], 2)).to.throw(TypeError);
expect(() => calculator._check({}, 2)).to.throw(TypeError);
});
it('should be called once in "add"', () => {
calculator.add(40, 2);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(40, 2);
});
it('should be called once in "subtract"', () => {
calculator.subtract(44, 2);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(44, 2);
});
it('should be called once in "multiply"', () => {
calculator.multiply(6, 7);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(6, 7);
});
it('should be called once in "divide"', () => {
calculator.divide(84, 2);
expect(calculator._check).to.have.been.calledOnce;
expect(calculator._check).to.have.been.calledWith(84, 2);
});
});
describe('add', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.add(40, '2')).to.throw(TypeError);
expect(() => calculator.add(40, [])).to.throw(TypeError);
expect(() => calculator.add(40, {})).to.throw(TypeError);
expect(() => calculator.add('40', 2)).to.throw(TypeError);
expect(() => calculator.add([], 2)).to.throw(TypeError);
expect(() => calculator.add({}, 2)).to.throw(TypeError);
});
it('should add two positive numbers', () => {
expect(calculator.add(40, 2)).to.equal(42);
});
it('should add two negative numbers', () => {
expect(calculator.add(-40, -2)).to.equal(-42);
});
it('should add one positive number and one negative number', () => {
expect(calculator.add(44, -2)).to.equal(42);
});
});
describe('subtract', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.subtract(40, '2')).to.throw(TypeError);
expect(() => calculator.subtract(40, [])).to.throw(TypeError);
expect(() => calculator.subtract(40, {})).to.throw(TypeError);
expect(() => calculator.subtract('40', 2)).to.throw(TypeError);
expect(() => calculator.subtract([], 2)).to.throw(TypeError);
expect(() => calculator.subtract({}, 2)).to.throw(TypeError);
});
it('should subtract two positive numbers', () => {
expect(calculator.subtract(44, 2)).to.equal(42);
});
it('should subtract two negative numbers', () => {
expect(calculator.subtract(-44, -2)).to.equal(-42);
});
it('should subtract one positive number and one negative number', () => {
expect(calculator.subtract(40, -2)).to.equal(42);
});
});
describe('multiply', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.multiply(40, '2')).to.throw(TypeError);
expect(() => calculator.multiply(40, [])).to.throw(TypeError);
expect(() => calculator.multiply(40, {})).to.throw(TypeError);
expect(() => calculator.multiply('40', 2)).to.throw(TypeError);
expect(() => calculator.multiply([], 2)).to.throw(TypeError);
expect(() => calculator.multiply({}, 2)).to.throw(TypeError);
});
it('should multiply two positive numbers', () => {
expect(calculator.multiply(6, 7)).to.equal(42);
});
it('should multiply two negative numbers', () => {
expect(calculator.multiply(-6, -7)).to.equal(42);
});
it('should multiply one positive number and one negative number', () => {
expect(calculator.multiply(6, -7)).to.equal(-42);
});
});
describe('divide', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.divide(40, '2')).to.throw(TypeError);
expect(() => calculator.divide(40, [])).to.throw(TypeError);
expect(() => calculator.divide(40, {})).to.throw(TypeError);
expect(() => calculator.divide('40', 2)).to.throw(TypeError);
expect(() => calculator.divide([], 2)).to.throw(TypeError);
expect(() => calculator.divide({}, 2)).to.throw(TypeError);
});
it('should divide two positive numbers', () => {
expect(calculator.divide(84, 2)).to.equal(42);
});
it('should divide two negative numbers', () => {
expect(calculator.divide(-84, -2)).to.equal(42);
});
it('should divide one positive number and one negative number', () => {
expect(calculator.divide(84, -2)).to.equal(-42);
});
});