forked from joinpursuit/8-0-javascript-on-your-machine-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
7,295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: 2.1 | ||
orbs: | ||
node: circleci/node@3.0.0 | ||
workflows: | ||
node-tests: | ||
jobs: | ||
- node/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const calculator = require("../"); | ||
const resetArgV = () => { | ||
process.argv = ["/path/to/node", "/path/to/file"]; | ||
}; | ||
|
||
describe("calculator()", () => { | ||
describe("error handling", () => { | ||
beforeEach(resetArgV); | ||
test("should return an error message if no operation is provided", () => { | ||
const actual = calculator(); | ||
const expected = "No operation provided..."; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should return an error message if no numbers are provided", () => { | ||
const command = "plus"; | ||
process.argv.push(command); | ||
|
||
const actual = calculator(); | ||
const expected = "No numbers provided..."; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should return an error message if the operation does not match 'plus' or 'minus'", () => { | ||
const command = "modulo"; | ||
const numbers = ["5", "10"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = "Invalid operation: modulo"; | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe("plus", () => { | ||
beforeEach(resetArgV); | ||
test("should add up two numbers", () => { | ||
const command = "plus"; | ||
const numbers = ["5", "10"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = 15; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should add up multiple numbers", () => { | ||
const command = "plus"; | ||
const numbers = ["5", "10", "15", "30", "60"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = 120; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should add negative numbers", () => { | ||
const command = "plus"; | ||
const numbers = ["5", "-10", "15", "30", "-60"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = -20; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should return the first number if there is only one number", () => { | ||
const command = "plus"; | ||
const numbers = ["5"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = 5; | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe("minus", () => { | ||
beforeEach(resetArgV); | ||
test("should subtract two numbers", () => { | ||
const command = "minus"; | ||
const numbers = ["15", "10"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = 5; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should subtract multiple numbers", () => { | ||
const command = "minus"; | ||
const numbers = ["60", "10", "15", "30", "5"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = 0; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should subtract negative numbers", () => { | ||
const command = "minus"; | ||
const numbers = ["5", "-10", "15", "30", "-60"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = 30; | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("should return the first number if there is only one number", () => { | ||
const command = "minus"; | ||
const numbers = ["5"]; | ||
process.argv.push(command, ...numbers); | ||
|
||
const actual = calculator(); | ||
const expected = 5; | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* The function below has no parameters. Instead, access the arguments from the command line. | ||
* The first argument passed after the filename should be either "plus" or "minus", which represents the kind of calculation that will be done. Every argument afterwards should be a number. | ||
* Depending on the operation, either add up all of the numbers or subtract all of the numbers, from left to right. | ||
* @returns {number} The result of either adding all numbers or subtracting all numbers, depending on the arguments added to the command line. | ||
*/ | ||
function calculator() {} | ||
|
||
// Don't change anything below this line. | ||
module.exports = calculator; |
Oops, something went wrong.