Skip to content

Commit

Permalink
Add Lab content
Browse files Browse the repository at this point in the history
  • Loading branch information
bwreid committed Jun 30, 2021
1 parent bafe748 commit 5808b09
Show file tree
Hide file tree
Showing 6 changed files with 7,295 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .circleci/config.yaml
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
120 changes: 120 additions & 0 deletions __tests__/index.test.js
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);
});
});
});
10 changes: 10 additions & 0 deletions index.js
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;
Loading

0 comments on commit 5808b09

Please sign in to comment.