3
05/11/2024 9:27 am
Topic starter
Your task is to test a variable named mathEnforcer, which represents an object that should have the following functionality:
- addFive(num) - A function that accepts a single parameter:
- If the parameter is not a number, the funtion should return undefined.
- If the parameter is a number, add 5 to it, and return the result.
- subtractTen(num) - A function that accepts a single parameter:
- If the parameter is not a number, the function should return undefined.
- If the parameter is a number, subtract 10 from it, and return the result.
- sum(num1, num2) - A function that should accepts two parameters:
- If any of the 2 parameters is not a number, the function should return undefined.
- If both parameters are numbers, the function should return their sum.
JS Code
To ease you in the process, you are provided with an implementation which meets all of the specification requirements for the mathEnforcer object:
let mathEnforcer = { addFive: function (num) { if (typeof(num) !== 'number') { return undefined; } return num + 5; }, subtractTen: function (num) { if (typeof(num) !== 'number') { return undefined; } return num - 10; }, sum: function (num1, num2) { if (typeof(num1) !== 'number' || typeof(num2) !== 'number') { return undefined; } return num1 + num2; } };
The methods should function correctly for positive, negative and floating point numbers. In case of floating point numbers the result should be considered correct if it is within 0.01 of the correct value.
1 Answer
2
05/11/2024 9:28 am
Here are my testing requirements:
addFive(): -check if the input is number (with string) -check if the input is positive number -check if the input is negative number -check if the input is floating point number subtractTen(): -check if the input is number (with string) -check if the input is positive number -check if the input is negative number -check if the input is floating point number sum(): -check if the input 1 is number (with string) -check if the input 2 is number (with string) -check if the input is positive number -check if the input is negative number -check if the input is floating point number
And my Mocha and Chai tests (in one file - in the JavaScript file):
let expect = require("chai").expect; let mathEnforcer = { addFive: function (num) { if (typeof(num) !== 'number') { return undefined; } return num + 5; }, subtractTen: function (num) { if (typeof(num) !== 'number') { return undefined; } return num - 10; }, sum: function (num1, num2) { if (typeof(num1) !== 'number' || typeof(num2) !== 'number') { return undefined; } return num1 + num2; } }; describe("Tests for this task", function () { //addFive() tests: describe("Check the addFive function input", function () { it("with a string - undefined", function () { let result = mathEnforcer.addFive("5"); expect(result).to.be.undefined; }); it("with positive number", function () { let result = mathEnforcer.addFive(5); expect(result).to.equal(10); }); it("with negative number", function () { let result = mathEnforcer.addFive(-5); expect(result).to.equal(0); }); it("with floating number", function () { let result = mathEnforcer.addFive(3.12); expect(result).to.be.closeTo(8.12, 0.01); }); }); //subtractTen() tests: describe("Check the subtractTen function input", function () { it("with a string - undefined", function () { let result = mathEnforcer.subtractTen("5"); expect(result).to.be.undefined; }); it("with positive number", function () { let result = mathEnforcer.subtractTen(20); expect(result).to.equal(10); }); it("with negative number", function () { let result = mathEnforcer.subtractTen(-20); expect(result).to.equal(-30); }); it("with floating number", function () { let result = mathEnforcer.subtractTen(3.12); expect(result).to.equal(-6.88, 0.01); }); }); //sum() tests: describe("Check the sum function input", function () { it("with a 1st input incorrect - undefined", function () { let result = mathEnforcer.sum(1, "5"); expect(result).to.be.undefined; }); it("with a 2nd input incorrect - undefined", function () { let result = mathEnforcer.sum("1", 5); expect(result).to.be.undefined; }); it("with positive number", function () { let result = mathEnforcer.sum(20, 10); expect(result).to.equal(30); }); it("with negative number", function () { let result = mathEnforcer.sum(-20, -10); expect(result).to.equal(-30); }); it("with floating number", function () { let result = mathEnforcer.sum(3.12, 3.12); expect(result).to.equal(6.24, 0.01); }); it("with floating number", function () { let result = mathEnforcer.sum(-3.12, -3.12); expect(result).to.equal(-6.24, 0.01); }); }); });