-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
139 lines (124 loc) · 3.7 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*global test expect*/
const buildBoard = require('../build-board')
const populateBoard = require('../populate-board')
const generateLife = require('../generate-life')
const countNeighbours = require('../count-neighbours')
const checkEachCell = require('../check-each-cell')
const nextGeneration = require('../next-generation')
const assignLetters = require('../assign-letters')
const typeOutBoard = require('../type-out-board')
const displayBoard = require('../display-board')
//build-board
test('generate rectangular board', () => {
const board = buildBoard(3, 5)
const expected = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
const actual = board
expect(actual).toEqual(expected)
})
test('generate default square board', () => {
const board = buildBoard(5)
const expected = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
const actual = board
expect(actual).toEqual(expected)
})
//generate-life // re-write
test('life is generated randomly', () => {
const actual = generateLife()
expect(actual).toBeGreaterThanOrEqual(0)
})
//populate-board
test('board is populated with live cells', () => {
const board = buildBoard(5)
const lifeyBoard = populateBoard(board)
const expected = lifeyBoard.includes(1)
expect(expected).toBeTruthy
})
//count-neighbours
test('counts live neighbours around cell', () => {
const board = [
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 1, 1],
[0, 1, 0, 1, 1],
[0, 0, 0, 0, 0]]
const actual = countNeighbours(board, 2, 3)
expect(actual).toBe(5)
})
//check-each-cell
test('loop through cells and return neighbours array', () => {
const board = [
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 1, 1],
[0, 1, 0, 1, 1],
[0, 0, 0, 0, 0]]
const expected = [
[1, 1, 2, 1, 1],
[1, 1, 4, 3, 3],
[2, 3, 5, 5, 4],
[1, 1, 4, 4, 3],
[1, 1, 2, 2, 2]]
const actual = checkEachCell(board)
expect(actual).toEqual(expected)
})
//next-generation
test('next generation is generated according to rules', () => {
const board = [
[1, 1, 0, 1, 1],
[0, 0, 1, 0, 0],
[1, 0, 0, 1, 0],
[1, 0, 0, 1, 1],
[1, 1, 0, 0, 0]
]
const neighboursBoard = [
[1, 2, 3, 2, 1],
[3, 4, 3, 4, 3],
[1, 3, 3, 3, 3],
[3, 4, 3, 2, 2],
[2, 2, 2, 2, 2]
]
const expected = [
[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 1, 1, 1],
[1, 0, 1, 1, 1],
[1, 1, 0, 0, 0]]
const actual = nextGeneration(board, neighboursBoard)
expect(actual).toEqual(expected)
})
//assign-letters
test('input letter is returned as a board', () => {
const expected =
[
[0, 1, 1, 1, 1],
[0, 1, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 1],
[0, 1, 1, 1, 1]
]
const actual = assignLetters('s')
expect(actual).toEqual(expected)
})
//type-out-board
test('string of letters is returned as concatenated board', () => {
const actual = typeOutBoard('hello')
const expected =
[
[0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1]
]
expect(actual).toEqual(expected)
})