Add tests for the tractorBeam function
This commit is contained in:
parent
a7344e36e1
commit
af5e74d4af
1 changed files with 100 additions and 17 deletions
|
|
@ -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
|
||||
|
||||
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: [ ]
|
||||
// },
|
||||
];
|
||||
let nb = [...b];
|
||||
tractorBeam(nb, 4, 1);
|
||||
|
||||
expect(nb).to.have.length(9);
|
||||
function stringifyBoard(b: Board) {
|
||||
return [...b].map(x => x != null ? x : `-`).join(`\t`);
|
||||
};
|
||||
|
||||
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;
|
||||
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));
|
||||
});
|
||||
};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue