-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
105 lines (95 loc) · 3.11 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
const test = require('tape')
const tictactoe = require('./tictactoe')
const removeDuplicates = tictactoe.removeDuplicates
const allTheSame = tictactoe.allTheSame
const winner = tictactoe.winner
const rowWinner = tictactoe.rowWinner
const columnWinner = tictactoe.columnWinner
const diagonalWinner = tictactoe.diagonalWinner
const togglePlayer = tictactoe.togglePlayer
const emptyField = tictactoe.emptyField
const swapArrayElements = tictactoe.swapArrayElements
test('removeDuplicates removes all duplicates from an array', t => {
t.deepEquals(removeDuplicates([1, 2, 3]), [1, 2, 3])
t.deepEquals(removeDuplicates([1, 1, '1']), [1, '1'], 'data types matter')
t.deepEquals(removeDuplicates([0, 'a', null, undefined, 'a', null]), [
0,
'a',
null,
undefined
])
t.deepEquals(removeDuplicates([]), [])
t.end()
})
test('allTheSame checks if all elements in array are the same', t => {
t.equal(allTheSame([1, 1, 1]), true)
t.equal(allTheSame([2, 2, '2']), false, 'data types matter')
t.equal(
allTheSame([null, undefined]),
false,
'null and undefined are not the same thing'
)
t.equal(allTheSame([]), true, 'returns true for an empty array')
t.end()
})
test('checks rows, returns who wins the game', t => {
t.equal(rowWinner([['o', 'x', 'o'], ['x', 'x', 'x'], [null, null, 'o']]), 'x')
t.end()
})
test('checks columns, returns who wins the game', t => {
t.equal(
columnWinner([['x', 'o', 'o'], ['x', 'o', 'x'], ['o', 'o', 'x']]),
'o'
)
t.end()
})
test('checks diagonal, returns who wins the game', t => {
t.equal(
diagonalWinner([['x', 'o', 'o'], ['x', 'x', 'o'], ['o', 'o', 'x']]),
'x'
)
t.equal(
diagonalWinner([['x', 'x', 'o'], ['x', 'o', 'x'], ['o', 'o', 'x']]),
'o'
)
t.end()
})
test('returns who wins the game, if no one wins it returns null', t => {
t.equal(winner([['x', 'x', 'o'], ['o', 'o', 'x'], [null, 'o', 'x']]), null)
t.equal(winner([['o', 'x', 'o'], ['x', 'x', 'x'], [null, null, 'o']]), 'x')
t.equal(winner([['x', 'o', 'o'], ['x', 'o', 'x'], ['o', 'o', 'x']]), 'o')
t.equal(winner([['x', 'o', 'o'], ['x', 'x', 'o'], ['o', 'o', 'x']]), 'x')
t.equal(winner([['x', 'x', 'o'], ['x', 'o', 'x'], ['o', 'o', 'x']]), 'o')
t.equal(winner([['x', 'o', 'o'], ['x', 'x', 'o'], ['o', 'x', 'o']]), 'o')
t.equal(winner([['x', 'o', 'x'], ['o', 'x', 'o'], ['x', 'o', 'x']]), 'x')
t.equal(
winner([[null, null, null], [null, null, null], [null, null, null]]),
null
)
t.equal(winner([['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]), 'x')
t.equal(winner([['o', 'o', 'o'], ['o', 'o', 'o'], ['o', 'o', 'o']]), 'o')
t.end()
})
test('if player is x, it returns o', t => {
t.equal(togglePlayer('x'), 'o')
t.equal(togglePlayer('o'), 'x')
t.end()
})
test('returns the blank playing field', t => {
t.deepEqual(emptyField(), [
[null, null, null],
[null, null, null],
[null, null, null]
])
t.end()
})
test('swap elements in array', t => {
t.deepEqual(swapArrayElements(['abc', 'def'], 0, 1), ['def', 'abc'])
t.deepEqual(swapArrayElements(['abc', 'def', 'ghi', 'jkl'], 2, 3), [
'abc',
'def',
'jkl',
'ghi'
])
t.end()
})