Add tests for the tractorBeam function

This commit is contained in:
Oliver Akins 2022-06-28 23:59:40 -06:00
parent a7344e36e1
commit af5e74d4af
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -1,25 +1,108 @@
import { tractorBeam } from "./processCard"; import { tractorBeam } from "./processCard";
import { Board } from "../types/GameBoard";
import { expect } from "chai"; import { expect } from "chai";
import "mocha"; import "mocha";
interface test {
name: string;
args: [number, number];
input: Board,
expect: Board;
};
describe("The tractorBeam function", () => { 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; function stringifyBoard(b: Board) {
expect(nb[1]).to.be.null; return [...b].map(x => x != null ? x : `-`).join(`\t`);
expect(nb[2]).to.equal("p1"); };
expect(nb[3]).to.be.null;
expect(nb[4]).to.equal("p2"); for (const test of tests) {
expect(nb[5]).to.be.null; it(test.name, () => {
expect(nb[6]).to.equal("p3"); let b = [...test.input];
expect(nb[7]).to.be.null; tractorBeam(b, ...test.args);
expect(nb[8]).to.be.null; expect(b).to.have.length(test.expect.length);
}); expect(stringifyBoard(b)).to.equal(stringifyBoard(test.expect));
});
};
}); });