diff --git a/common/src/algorithms/movementDirection.spec.ts b/common/src/algorithms/movementDirection.spec.ts new file mode 100644 index 0000000..a51dd2b --- /dev/null +++ b/common/src/algorithms/movementDirection.spec.ts @@ -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 (_) {} + }); +}); \ No newline at end of file