Add a tests for countShips

This commit is contained in:
Oliver Akins 2022-06-19 12:41:02 -06:00
parent 18f58a6221
commit 762e2bdb1b
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -0,0 +1,47 @@
import { countShips } from "./movementDirection"
import { expect } from "chai";
import "mocha";
describe("The countShips function", () => {
it("should count the ships correctly when unbalanced", () => {
let r = countShips(["p3", null, "p1", "p2", null, "p4"], 2);
expect(r).to.have.keys(`left`, `right`);
expect(r.left).to.equal(1);
expect(r.right).to.equal(2);
});
it("should count the ships correctly when balanced", () => {
let r = countShips(["p3", null, "p1", "p2", null], 2);
expect(r).to.have.keys(`left`, `right`);
expect(r.left).to.equal(1);
expect(r.right).to.equal(1);
});
it("should count the ships correctly when shipLocation is at the start of the array", () => {
let r = countShips(["p3", null, "p1", "p2", null], 0);
expect(r).to.have.keys(`left`, `right`);
expect(r.left).to.equal(0);
expect(r.right).to.equal(2);
});
it("should count the ships correctly when shipLocation is at the end of the array", () => {
let r = countShips(["p3", null, "p1", "p2", null, "p4"], 5);
expect(r).to.have.keys(`left`, `right`);
expect(r.left).to.equal(3);
expect(r.right).to.equal(0);
});
it("should throw an error when shipLocation is larger than length", () => {
try {
countShips(["p3", null, "p1", "p2", null, "p4"], 8);
throw "Function didn't throw an error";
} catch (_) {}
});
it("should throw an error when shipLocation is less than 0", () => {
try {
countShips(["p3", null, "p1", "p2", null, "p4"], -3);
throw "Function didn't throw an error";
} catch (_) {}
});
});