Skip to content

Commit 76459ae

Browse files
committed
Added unit tests for map generation
1 parent 70fec26 commit 76459ae

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

spec/unit/MapGenerator.spec.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
const MapGenerator = require('../../');
2+
3+
function expectMapSize(map, width, height) {
4+
expect(map.length).toBe(height);
5+
for (let row of map) {
6+
expect(row.length).toBe(width);
7+
}
8+
}
9+
10+
function isMapEmpty(map) {
11+
let empty = true;
12+
for (let row of map) {
13+
for (let field of row) {
14+
if (field !== 0) {
15+
empty = false;
16+
break;
17+
}
18+
}
19+
if (empty === false) {
20+
break;
21+
}
22+
}
23+
return empty;
24+
}
25+
26+
describe('MapGenerator:', function() {
27+
const generator = new MapGenerator();
28+
29+
describe('generateEmptyMap method:', function() {
30+
it('should generate an empty 30x30 map', function() {
31+
const map = generator.generateEmptyMap(20, 30);
32+
expectMapSize(map, 20, 30);
33+
expect(isMapEmpty(map)).toBe(true);
34+
});
35+
});
36+
37+
describe('step method:', function() {
38+
it('should step 1 to left', function() {
39+
const [x, y] = generator.step(3, 3, [-1, 0]);
40+
expect(x).toBe(2);
41+
expect(y).toBe(3);
42+
});
43+
44+
it('should step 1 to right', function() {
45+
const [x, y] = generator.step(3, 3, [1, 0]);
46+
expect(x).toBe(4);
47+
expect(y).toBe(3);
48+
});
49+
50+
it('should step 1 down', function() {
51+
const [x, y] = generator.step(3, 3, [0, -1]);
52+
expect(x).toBe(3);
53+
expect(y).toBe(2);
54+
});
55+
56+
it('should step 1 up', function() {
57+
const [x, y] = generator.step(3, 3, [0, 1]);
58+
expect(x).toBe(3);
59+
expect(y).toBe(4);
60+
});
61+
});
62+
63+
describe('validatePosition method:', function() {
64+
const map3x3 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
65+
66+
it('smallest valid position', function() {
67+
const positionIsValid = generator.validatePosition(map3x3, 0, 0);
68+
expect(positionIsValid).toBe(true);
69+
});
70+
71+
it('hieghest valid position', function() {
72+
const positionIsValid = generator.validatePosition(map3x3, 2, 2);
73+
expect(positionIsValid).toBe(true);
74+
});
75+
76+
it('negative x position', function() {
77+
const positionIsValid = generator.validatePosition(map3x3, -1, 0);
78+
expect(positionIsValid).toBe(false);
79+
});
80+
81+
it('negative y position', function() {
82+
const positionIsValid = generator.validatePosition(map3x3, 0, -1);
83+
expect(positionIsValid).toBe(false);
84+
});
85+
86+
it('x position too great', function() {
87+
const positionIsValid = generator.validatePosition(map3x3, 3, 0);
88+
expect(positionIsValid).toBe(false);
89+
});
90+
91+
it('y position too great', function() {
92+
const positionIsValid = generator.validatePosition(map3x3, 0, 3);
93+
expect(positionIsValid).toBe(false);
94+
});
95+
});
96+
97+
describe('map generation', function() {
98+
it('default parameters', function() {
99+
const map = generator.generateMap();
100+
expectMapSize(map, 30, 30);
101+
expect(isMapEmpty(map)).toBe(false);
102+
});
103+
104+
it('custom parameters', function() {
105+
const map = generator.generateMap({ maxWidth: 14, maxHeight: 16, startPosition: { x: 2, y: 3 } });
106+
expectMapSize(map, 14, 16);
107+
expect(isMapEmpty(map)).toBe(false);
108+
expect(map[3][2]).toBe(1);
109+
});
110+
111+
it('seeded generation', function() {
112+
const maps = [];
113+
const config = { seed: 'seed_of_doom' };
114+
for (let i = 0; i < 25; i++) {
115+
maps.push(generator.generateMap(config));
116+
}
117+
for (let i = 1; i < 25; i++) {
118+
expect(maps[i]).toEqual(maps[0]);
119+
}
120+
});
121+
});
122+
});

0 commit comments

Comments
 (0)