Skip to content

Commit

Permalink
fix incorrect interface implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
soryy708 committed May 22, 2023
1 parent 0d54b4d commit 0aee5f4
Showing 1 changed file with 171 additions and 167 deletions.
338 changes: 171 additions & 167 deletions src/front/game/board/abstract-board.spec.ts
Original file line number Diff line number Diff line change
@@ -1,167 +1,171 @@
import { Engine } from '../../engine';
import { AbstractBoard, BoardPosition } from './abstract-board';
import { GamePiece } from '../piece/interface';

class TestingPiece implements GamePiece {
bootstrap(_engine: Engine): void {
// nop
}
}

describe('Board unit tests', () => {
describe('When checking if can stack', () => {
describe.each([0, 1, 2, 3])('When stack has %d items', (items) => {
it('Should return true', () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } = {
x: 0,
y: 0,
};
for (let i = 0; i < items; ++i) {
board.stack(position, new TestingPiece());
}

const actual = board.canStack(position.x, position.y);

expect(actual).toStrictEqual(true);
});
});

describe('When stack has 4 items', () => {
it('Should return false', () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } = {
x: 0,
y: 0,
};
for (let i = 0; i < 4; ++i) {
board.stack(position, new TestingPiece());
}

const actual = board.canStack(position.x, position.y);

expect(actual).toStrictEqual(false);
});
});
});

describe('When stacking', () => {
describe('If cannot stack', () => {
it('Should throw', () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } = {
x: 0,
y: 0,
};
for (let i = 0; i < 4; ++i) {
board.stack(position, new TestingPiece());
}

expect(() =>
board.stack(position, new TestingPiece()),
).toThrowError(new Error("Can't stack fully stacked column"));
});
});

describe('If can stack', () => {
describe.each([0, 1, 2, 3])(
'When stack has %d items',
(itemsBefore) => {
it(`Should stack on height ${itemsBefore}`, () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } =
{
x: 0,
y: 0,
};
for (let i = 0; i < itemsBefore; ++i) {
board.stack(position, new TestingPiece());
}

const piece = new TestingPiece();
board.stack(position, piece);

expect(
board.getCell(
position.x,
position.y,
itemsBefore as never,
),
).toStrictEqual(piece);
});
},
);
});
});

describe('When getting cell', () => {
describe("When there's nothing at the coordinates", () => {
it('Should return null', () => {
const board = new AbstractBoard();
const actual = board.getCell(0, 0, 0);
expect(actual).toStrictEqual(null);
});
});

describe("When there's only one item on the board", () => {
const itemCoordinates = { x: 2, y: 3, z: 0 } as const;
let board: AbstractBoard = null;
let piece: TestingPiece = null;

beforeEach(() => {
board = new AbstractBoard();
piece = new TestingPiece();
board.stack(itemCoordinates, piece);
});

it('Should return a reference to that item at its coordinates', () => {
const actual = board.getCell(
itemCoordinates.x,
itemCoordinates.y,
0,
);
expect(actual).toStrictEqual(piece);
});

it('Should return null for all other coordinates', () => {
for (let x = 0; x < 4; ++x) {
for (let y = 0; y < 4; ++y) {
for (let z = 0; z < 4; ++z) {
const atItemCoordinates =
x === itemCoordinates.x &&
y === itemCoordinates.y &&
z === itemCoordinates.z;
if (!atItemCoordinates) {
const actual = board.getCell(
x as never,
y as never,
z as never,
);
expect(actual).toStrictEqual(null);
}
}
}
}
});
});
});

describe('When getting stack height', () => {
describe.each([0, 1, 2, 3, 4])(
'When there are %d items in the stack',
(items) => {
it(`Should return ${items}`, () => {
const position = { x: 1, y: 2 } as const;
const board = new AbstractBoard();
for (let i = 0; i < items; ++i) {
board.stack(position, new TestingPiece());
}

const actual = board.getStackHeight(position);

expect(actual).toBe(items);
});
},
);
});
});
import { Engine } from '../../engine';
import { AbstractBoard, BoardPosition } from './abstract-board';
import { GamePiece } from '../piece/interface';

class TestingPiece implements GamePiece {
bootstrap(_engine: Engine): void {

Check warning on line 6 in src/front/game/board/abstract-board.spec.ts

View workflow job for this annotation

GitHub Actions / Continuous Integration

'_engine' is defined but never used
// nop
}

isSameTypeAs(other: GamePiece): boolean {

Check warning on line 10 in src/front/game/board/abstract-board.spec.ts

View workflow job for this annotation

GitHub Actions / Continuous Integration

'other' is defined but never used
return false;
}
}

describe('Board unit tests', () => {
describe('When checking if can stack', () => {
describe.each([0, 1, 2, 3])('When stack has %d items', (items) => {
it('Should return true', () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } = {
x: 0,
y: 0,
};
for (let i = 0; i < items; ++i) {
board.stack(position, new TestingPiece());
}

const actual = board.canStack(position.x, position.y);

expect(actual).toStrictEqual(true);
});
});

describe('When stack has 4 items', () => {
it('Should return false', () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } = {
x: 0,
y: 0,
};
for (let i = 0; i < 4; ++i) {
board.stack(position, new TestingPiece());
}

const actual = board.canStack(position.x, position.y);

expect(actual).toStrictEqual(false);
});
});
});

describe('When stacking', () => {
describe('If cannot stack', () => {
it('Should throw', () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } = {
x: 0,
y: 0,
};
for (let i = 0; i < 4; ++i) {
board.stack(position, new TestingPiece());
}

expect(() =>
board.stack(position, new TestingPiece()),
).toThrowError(new Error("Can't stack fully stacked column"));
});
});

describe('If can stack', () => {
describe.each([0, 1, 2, 3])(
'When stack has %d items',
(itemsBefore) => {
it(`Should stack on height ${itemsBefore}`, () => {
const board = new AbstractBoard();
const position: { x: BoardPosition; y: BoardPosition } =
{
x: 0,
y: 0,
};
for (let i = 0; i < itemsBefore; ++i) {
board.stack(position, new TestingPiece());
}

const piece = new TestingPiece();
board.stack(position, piece);

expect(
board.getCell(
position.x,
position.y,
itemsBefore as never,
),
).toStrictEqual(piece);
});
},
);
});
});

describe('When getting cell', () => {
describe("When there's nothing at the coordinates", () => {
it('Should return null', () => {
const board = new AbstractBoard();
const actual = board.getCell(0, 0, 0);
expect(actual).toStrictEqual(null);
});
});

describe("When there's only one item on the board", () => {
const itemCoordinates = { x: 2, y: 3, z: 0 } as const;
let board: AbstractBoard = null;
let piece: TestingPiece = null;

beforeEach(() => {
board = new AbstractBoard();
piece = new TestingPiece();
board.stack(itemCoordinates, piece);
});

it('Should return a reference to that item at its coordinates', () => {
const actual = board.getCell(
itemCoordinates.x,
itemCoordinates.y,
0,
);
expect(actual).toStrictEqual(piece);
});

it('Should return null for all other coordinates', () => {
for (let x = 0; x < 4; ++x) {
for (let y = 0; y < 4; ++y) {
for (let z = 0; z < 4; ++z) {
const atItemCoordinates =
x === itemCoordinates.x &&
y === itemCoordinates.y &&
z === itemCoordinates.z;
if (!atItemCoordinates) {
const actual = board.getCell(
x as never,
y as never,
z as never,
);
expect(actual).toStrictEqual(null);
}
}
}
}
});
});
});

describe('When getting stack height', () => {
describe.each([0, 1, 2, 3, 4])(
'When there are %d items in the stack',
(items) => {
it(`Should return ${items}`, () => {
const position = { x: 1, y: 2 } as const;
const board = new AbstractBoard();
for (let i = 0; i < items; ++i) {
board.stack(position, new TestingPiece());
}

const actual = board.getStackHeight(position);

expect(actual).toBe(items);
});
},
);
});
});

0 comments on commit 0aee5f4

Please sign in to comment.