-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtests.js
More file actions
99 lines (93 loc) · 2.79 KB
/
tests.js
File metadata and controls
99 lines (93 loc) · 2.79 KB
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
var showDetails = false
chai.should()
var expect = chai.expect;
const testFunctions = [testGenerateNewBoard, testCalculateNearbyMines]
// test boundle, testing generate new board
function testGenerateNewBoard(){
function emptyBoard(){
let board = generateNewBoard(0, 0, 0)
board.should.be.an('array').that.is.empty
}
function moreMinesThanFieds(){
try{
let board = generateNewBoard(0, 0, 1)
}catch(err) {
return
}
throw new Error('Expecting error if more mines than cells!')
}
function someBoard(){
let board = generateNewBoard(9, 10, 90)
board.should.be.an('array')
board.should.have.lengthOf(9)
board.forEach((row)=>{
row.should.have.lengthOf(10)
row.forEach((cell)=>{
cell.should.not.be.empty
expect(cell).to.have.own.property('mine')
expect(cell.mine).to.be.true
})
})
}
return runTests('generateNewBoard()', [emptyBoard, moreMinesThanFieds, someBoard])
}
// test boundle, test calculateNearbyMines
function testCalculateNearbyMines(){
function noNearby(){
const board = [[{}]]
const cell = calculateNearbyMines(0, 0, board)
cell.should.be.an('object')
expect(cell).to.have.property('nearbyMines')
expect(cell.nearbyMines).to.be.a('number').and.to.be.equal(0)
}
function allNearby(){
const board = [[{mine: true},{mine: true},{mine: true}],
[{mine: true},{},{mine: true}],
[{mine: true},{mine: true},{mine: true}]]
const cell = calculateNearbyMines(1, 1, board)
cell.should.be.an('object')
expect(cell).to.have.property('nearbyMines')
expect(cell.nearbyMines).to.be.a('number').and.to.be.equal(8)
}
function testCorner(){
const board = [[{mine: true},{},{}],
[{mine: true},{mine: true},{}],
[{mine: true},{mine: true},{mine: true}]]
const cell = calculateNearbyMines(0, 2, board)
cell.should.be.an('object')
expect(cell).to.have.property('nearbyMines')
expect(cell.nearbyMines).to.be.a('number').and.to.be.equal(1)
}
return runTests('calculateNearbyMines()', [noNearby, allNearby, testCorner])
}
// takes care of running the tests of test bundles
function runTests(testName, testList){
let res = {
message: `Tests for ${testName}`,
passCount: 0,
totalCount: 0
}
testList.forEach((fn)=>{
res.totalCount += 1
try{
fn()
res.passCount += 1
}catch(err){
if(showDetails) console.error(err)
}
})
return res
}
// runs the test bundles
(function testRunner(){
testFunctions.forEach((fn) => {
colors = {
'pass': 'green',
'fail': 'red'
}
// res = {message: '', passCount, totalCount}
res = fn()
result = res.totalCount - res.passCount === 0? 'pass' : 'fail'
console.log(`%c ${res.message} (${res.passCount}/${res.totalCount})`, `color: ${colors[result]}`);
})
})()