CS2613/tests/Final/Q3 Javascript/spec/expression.spec.js

72 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2022-12-20 21:49:28 -04:00
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
});