From 762e2bdb1ba6a59d90476b24be7b3add8f8dd8c6 Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Sun, 19 Jun 2022 12:41:02 -0600 Subject: [PATCH] Add a tests for countShips --- .../src/algorithms/movementDirection.spec.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 common/src/algorithms/movementDirection.spec.ts 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