-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: Ongkiboy
Are you sure you want to change the base?
Implement _check #2407
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) => { | ||
// DRY up the codebase with this function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
// First, move the duplicate error checking code here | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
// Then, invoke this function inside each of the others | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
// HINT: you can invoke this function with exports._check() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
if (typeof x !== 'number') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
throw new TypeError(`${x} is not a number`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
if (typeof y !== 'number') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
throw new TypeError(`${y} is not a number`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
exports.add = (x, y) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
exports._check(x, y); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
return x + y; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
exports.subtract = (x, y) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
exports._check(x, y); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected linebreaks to be 'LF' but found 'CRLF'. |
||
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; |
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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)