Add final test material
This commit is contained in:
50
tests/Final/Q3 Javascript/expression.js
Normal file
50
tests/Final/Q3 Javascript/expression.js
Normal file
@ -0,0 +1,50 @@
|
||||
class Expression {
|
||||
constructor(op, left, right) {
|
||||
this.op = op
|
||||
this.left = left
|
||||
this.right = right
|
||||
}
|
||||
|
||||
eval() {
|
||||
return evalExpression(this)
|
||||
}
|
||||
}
|
||||
|
||||
function evalExpression(expr) {
|
||||
let tempLeft
|
||||
let tempRight
|
||||
if (typeof(expr.left) === "object") { // Check if left type is another expression
|
||||
tempLeft = evalExpression(expr.left)
|
||||
}
|
||||
else {
|
||||
tempLeft = expr.left
|
||||
}
|
||||
if (typeof(expr.right) === "object") { // Check if right type is another expression
|
||||
tempRight = evalExpression(expr.right)
|
||||
}
|
||||
else {
|
||||
tempRight = expr.right
|
||||
}
|
||||
if (typeof(tempLeft) === "number" & typeof(tempRight) === "number") { // Make sure both inputs are number
|
||||
if (expr.op === "+") {
|
||||
return tempLeft + tempRight
|
||||
}
|
||||
else if(expr.op === "-") {
|
||||
return tempLeft - tempRight
|
||||
}
|
||||
else if(expr.op === "*") {
|
||||
return tempLeft * tempRight
|
||||
}
|
||||
else if(expr.op === "/") {
|
||||
return tempLeft / tempRight
|
||||
}
|
||||
else { // Case for when there is no valid provided operator
|
||||
return "Invalid operator syntax"
|
||||
}
|
||||
}
|
||||
else { //Case for when the left or right are not numbers
|
||||
return "Invalid number syntax"
|
||||
}
|
||||
}
|
||||
|
||||
exports.Expression = Expression;
|
71
tests/Final/Q3 Javascript/spec/expression.spec.js
Normal file
71
tests/Final/Q3 Javascript/spec/expression.spec.js
Normal file
@ -0,0 +1,71 @@
|
||||
Expression = require("../expression.js").Expression;
|
||||
|
||||
let six_plus_nine = new Expression('+', 6, 9);
|
||||
let six_times_nine = new Expression('*', 6, 9);
|
||||
let six_minus_nine = new Expression('-', 6, 9);
|
||||
let sixteen_div_eight = new Expression('/', 16, 8);
|
||||
let compound1 = new Expression('+', six_times_nine, six_plus_nine)
|
||||
let compound2 = new Expression('*', six_times_nine, compound1)
|
||||
let compound3 = new Expression('+', compound2, 3)
|
||||
|
||||
describe("constructor",
|
||||
function() {
|
||||
let one = new Expression("+",0,1);
|
||||
it("not null", () => expect(one).not.toBe(null));
|
||||
it("op", () => expect(one.op).toBe("+"));
|
||||
it("left", () => expect(one.left).toBe(0));
|
||||
it("right", () => expect(one.right).toBe(1));
|
||||
});
|
||||
|
||||
describe("simple",
|
||||
function() {
|
||||
it("+", () => expect(six_plus_nine.eval()).toBe(15));
|
||||
it("-", () => expect(six_minus_nine.eval()).toBe(-3));
|
||||
it("*", () => expect(six_times_nine.eval()).toBe(54));
|
||||
it("/", () => expect(sixteen_div_eight.eval()).toBe(2));
|
||||
});
|
||||
|
||||
describe("compound",
|
||||
function() {
|
||||
it("1", () => expect(compound1.eval()).toBe(69));
|
||||
it("2", () => expect(compound2.eval()).toBe(3726));
|
||||
it("3", () => expect(compound3.eval()).toBe(3729));
|
||||
});
|
||||
|
||||
describe("floating point", //Testing floating point
|
||||
function() {
|
||||
let addPointFive = new Expression("+", 0.5, 0.5)
|
||||
let timesPointFive = new Expression("*", 0.5, 10)
|
||||
let minusPointFive = new Expression("-", 10, 0.5)
|
||||
let dividePointFive = new Expression("/", 0.5, 0.1)
|
||||
|
||||
it("+", () => expect(addPointFive.eval()).toBe(1.0));
|
||||
it("*", () => expect(timesPointFive.eval()).toBe(5.0));
|
||||
it("-", () => expect(minusPointFive.eval()).toBe(9.5));
|
||||
it("/", () => expect(dividePointFive.eval()).toBe(5.0));
|
||||
});
|
||||
|
||||
|
||||
describe("bad input", //Testing bad inputs
|
||||
function() {
|
||||
let invalidStringLeft = new Expression("+", "five", 6)
|
||||
let invalidStringRight = new Expression("+", 5, "six")
|
||||
let invalidOperator = new Expression("^", 6, 9)
|
||||
|
||||
it("Invalid String on left side", () => expect(invalidStringLeft.eval()).toBe("Invalid number syntax"));
|
||||
it("Invalid String on right side", () => expect(invalidStringRight.eval()).toBe("Invalid number syntax"));
|
||||
it("invalid operator", () => expect(invalidOperator.eval()).toBe("Invalid operator syntax"));
|
||||
});
|
||||
|
||||
|
||||
describe("compound bad input", //Testing bad inputs in compound cases
|
||||
function() {
|
||||
let invalidNumber = new Expression("+", "five", 6)
|
||||
let invalidOperator = new Expression("^", 6, 9)
|
||||
|
||||
let semiValidCompound = new Expression("+", six_plus_nine, invalidNumber)
|
||||
let completlyBadCompound = new Expression("+", invalidNumber, invalidOperator)
|
||||
|
||||
it("semi-valid", () => expect(semiValidCompound.eval()).toBe("Invalid number syntax"));
|
||||
it("invalid", () => expect(completlyBadCompound.eval()).toBe("Invalid number syntax")); //Expected to be invalid number because it is the first error to be encountered
|
||||
});
|
13
tests/Final/Q3 Javascript/spec/support/jasmine.json
Normal file
13
tests/Final/Q3 Javascript/spec/support/jasmine.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"spec_dir": "spec",
|
||||
"spec_files": [
|
||||
"**/*[sS]pec.?(m)js"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/**/*.?(m)js"
|
||||
],
|
||||
"env": {
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user