diff --git a/common/src/algorithms/processCard.spec.ts b/common/src/algorithms/processCard.spec.ts index 542fe7c..aef5df1 100644 --- a/common/src/algorithms/processCard.spec.ts +++ b/common/src/algorithms/processCard.spec.ts @@ -1,25 +1,108 @@ import { tractorBeam } from "./processCard"; +import { Board } from "../types/GameBoard"; import { expect } from "chai"; import "mocha"; +interface test { + name: string; + args: [number, number]; + input: Board, + expect: Board; +}; + describe("The tractorBeam function", () => { - it("should move the ships closer", () => { - let b = [ - null, "p1", null, null, "p2", null, null, "p3", null - ]; - let nb = [...b]; - tractorBeam(nb, 4, 1); - expect(nb).to.have.length(9); + const tests: test[] = [ + { + name: `should move the ships closer when equidistant`, + args: [4, 1], + input: [ null, "p1", null, null, "p2", null, null, "p3", null ], + expect: [ null, null, "p1", null, "p2", null, "p3", null, null ] + }, + { + name: `should move the ships closer when not equidistant`, + args: [4, 1], + input: [ null, "p1", null, null, "p2", null, null, null, "p3" ], + expect: [ null, null, "p1", null, "p2", null, null, "p3", null ] + }, + { + name: `should move the ships closer when only on the left side`, + args: [4, 1], + input: [ null, "p1", null, null, "p2", null, null, null, null ], + expect: [ null, null, "p1", null, "p2", null, null, null, null ] + }, + { + name: `should move the ships closer when only on the right side`, + args: [4, 1], + input: [ null, null, null, null, "p2", null, null, "p3", null ], + expect: [ null, null, null, null, "p2", null, "p3", null, null ] + }, + { + name: `should move the ships closer when pulling the ship from the left edge`, + args: [4, 1], + input: [ "p1", null, null, null, "p2", null, null, null, null ], + expect: [ null, "p1", null, null, "p2", null, null, null, null ] + }, + { + name: `should move the ships closer when pulling the ship from the right edge`, + args: [4, 1], + input: [ null, null, null, null, "p2", null, null, null, "p3" ], + expect: [ null, null, null, null, "p2", null, null, "p3", null ] + }, + { + name: `should move the ship closer when the origin is on the left edge`, + args: [0, 1], + input: [ "p1", null, null, null, "p2", null, null, null, null ], + expect: [ "p1", null, null, "p2", null, null, null, null, null ] + }, + { + name: `should move the ship closer when the origin is on the right edge`, + args: [8, 1], + input: [ null, null, null, null, "p2", null, null, null, "p1" ], + expect: [ null, null, null, null, null, "p2", null, null, "p1" ] + }, + { + name: `should move the ship correctly when it needs to drift (single ship drift)`, + args: [4, 1], + input: [ null, null, null, "p1", "p2", null, null, null, null ], + expect: [ null, null, null, null, "p2", "p1", null, null, null ] + }, + { + name: `should move the ship correctly when it needs to drift (multi-ship drift)`, + args: [4, 1], + input: [ null, null, null, "p1", "p2", null, "p3", null, null ], + expect: [ null, null, null, "p3", "p2", "p1", null, null, null ] + }, + { + name: `should move the ship correctly when it needs to drift (multi-ship drift unbalanced)`, + args: [4, 1], + input: [ null, null, null, "p1", "p2", null, "p3", "p4", null ], + expect: [ null, null, null, "p3", "p2", "p1", "p4", null, null ] + }, + { + name: `should move the ship correctly when it needs to cross-over`, + args: [4, 1], + input: [ null, null, null, "p1", "p2", "p3", null, null, null ], + expect: [ null, null, null, "p3", "p2", "p1", null, null, null ] + }, + // { + // name: ``, + // args: [4, 1], + // input: [ ], + // expect: [ ] + // }, + ]; - expect(nb[0]).to.be.null; - expect(nb[1]).to.be.null; - expect(nb[2]).to.equal("p1"); - expect(nb[3]).to.be.null; - expect(nb[4]).to.equal("p2"); - expect(nb[5]).to.be.null; - expect(nb[6]).to.equal("p3"); - expect(nb[7]).to.be.null; - expect(nb[8]).to.be.null; - }); + function stringifyBoard(b: Board) { + return [...b].map(x => x != null ? x : `-`).join(`\t`); + }; + + for (const test of tests) { + it(test.name, () => { + let b = [...test.input]; + tractorBeam(b, ...test.args); + expect(b).to.have.length(test.expect.length); + expect(stringifyBoard(b)).to.equal(stringifyBoard(test.expect)); + }); + }; }); \ No newline at end of file