Skip to content

Commit

Permalink
Adds unit tests for collision detection and utilities functions
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrypula committed Nov 25, 2018
1 parent 9ca44f4 commit 96c8d3a
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 3 deletions.
103 changes: 101 additions & 2 deletions src/lib/game/selectors/game.selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { TetriminoIndex, TetriminoRotation } from '../models/tetrimino.model';
import { getTestingState } from '../utilities';
import * as fromGameSelectors from './game.selectors';

fdescribe('Game Selectors', () => {
describe('isNotCollidingSelector', () => {
describe('Game Selectors', () => {
describe('isNotCollidingSelector - movement on empty matrix', () => {
let state: IState;

beforeEach(() => {
Expand Down Expand Up @@ -50,4 +50,103 @@ fdescribe('Game Selectors', () => {
expect(fromGameSelectors.isNotCollidingSelector(2, 2)(state)).toEqual(false);
});
});

describe('isNotCollidingSelector - movement on matrix with blocks', () => {
let state: IState;

beforeEach(() => {
const blocks = [
_, X, _, _, _,
X, f, f, f, X,
_, _, f, _, _,
_, _, X, _, _
];
state = getTestingState(blocks, 5, 4, TetriminoIndex.T, TetriminoRotation.DEGREE_0, 1, 0);
});

it('should allow to remain in the same position', () => {
expect(fromGameSelectors.isNotCollidingSelector()(state)).toEqual(true);
});

it('should NOT allow to move one unit in the direction that will cause collision with existing block', () => {
expect(fromGameSelectors.isNotCollidingSelector(-1, -1)(state)).toEqual(false);
expect(fromGameSelectors.isNotCollidingSelector(0, -1)(state)).toEqual(false);
expect(fromGameSelectors.isNotCollidingSelector(1, -1)(state)).toEqual(true);

expect(fromGameSelectors.isNotCollidingSelector(-1)(state)).toEqual(false);
expect(fromGameSelectors.isNotCollidingSelector(1)(state)).toEqual(false);

expect(fromGameSelectors.isNotCollidingSelector(-1, 1)(state)).toEqual(true);
expect(fromGameSelectors.isNotCollidingSelector(0, 1)(state)).toEqual(false);
expect(fromGameSelectors.isNotCollidingSelector(1, 1)(state)).toEqual(true);
});
});

describe('isNotCollidingSelector - rotations on empty matrix', () => {
let state: IState;

beforeEach(() => {
const blocks = [
_, _, _, _, f,
_, _, _, _, f,
_, _, _, f, f,
_, _, _, _, _
];
state = getTestingState(blocks, 5, 4, TetriminoIndex.J, TetriminoRotation.DEGREE_90_CW, 3, 0);
});

it('should allow to remain in the same position', () => {
expect(fromGameSelectors.isNotCollidingSelector()(state)).toEqual(true);
});

it('should NOT allow rotation when it will cause collision with matrix boundary', () => {
expect(fromGameSelectors.isNotCollidingSelector(0, 0, 1)(state)).toEqual(false);
});
});

describe('isNotCollidingSelector - rotations on matrix with blocks', () => {
let state: IState;

beforeEach(() => {
const blocks = [
_, _, X, f, _,
_, _, _, f, _,
_, _, f, f, _,
_, _, _, _, _
];
state = getTestingState(blocks, 5, 4, TetriminoIndex.J, TetriminoRotation.DEGREE_90_CW, 2, 0);
});

it('should allow to remain in the same position', () => {
expect(fromGameSelectors.isNotCollidingSelector()(state)).toEqual(true);
});

it('should NOT allow rotation when it will cause collision with existing blocks', () => {
expect(fromGameSelectors.isNotCollidingSelector(0, 0, 1)(state)).toEqual(false);
expect(fromGameSelectors.isNotCollidingSelector(0, 0, -1)(state)).toEqual(true);
});
});

describe('matrixBlocksToRenderSelector', () => {
let state: IState;

beforeEach(() => {
const blocks = [
_, _, _, _, _,
_, f, f, f, _,
X, _, f, _, _,
X, X, X, _, X
];
state = getTestingState(blocks, 5, 4, TetriminoIndex.T, TetriminoRotation.DEGREE_0, 1, 0);
});

it('should properly include tetrimino blocks into the current matrix blocks', () => {
expect(fromGameSelectors.matrixBlocksToRenderSelector(state)).toEqual([
_, _, _, _, _,
_, X, X, X, _,
X, _, X, _, _,
X, X, X, _, X
].map((item) => item ? 1 : 0));
});
});
});
38 changes: 37 additions & 1 deletion src/lib/game/utilities.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright (c) 2018 Robert Rypuła - https://github.com/robertrypula

import { _, f, X } from './constants';
import { IState } from './models/state.model';
import { TetriminoIndex, TetriminoRotation } from './models/tetrimino.model';
import * as utilities from './utilities';

describe('utilities', () => {
describe('Utilities', () => {
describe('generateBlocks', () => {
it('should generate blocks array for given dimensions and default fill', () => {
const blocks = utilities.generateBlocks(2, 3);
Expand All @@ -23,4 +26,37 @@ describe('utilities', () => {
]);
});
});

describe('getTestingState', () => {
it('should return full simple-tetris state from data provided in simpler form', () => {
const blocks = [
_, _, _, _, f,
_, _, _, _, f,
X, _, _, f, f,
X, X, _, _, _
];
const state: IState = utilities.getTestingState(
blocks, 5, 4, TetriminoIndex.J, TetriminoRotation.DEGREE_90_CW, 3, 0
);

expect(state).toEqual({
matrix: {
blocks: [
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 0, 0, 0, 0,
1, 1, 0, 0, 0
],
sizeX: 5,
sizeY: 4
},
tetrimino: {
index: TetriminoIndex.J,
rotation: TetriminoRotation.DEGREE_90_CW,
x: 3,
y: 0
}
});
});
});
});

0 comments on commit 96c8d3a

Please sign in to comment.