-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
RatInAMaze.test.js
103 lines (90 loc) · 2.63 KB
/
RatInAMaze.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
import { RatInAMaze } from '../RatInAMaze'
describe('RatInAMaze', () => {
it('should fail for non-arrays', () => {
const values = [undefined, null, {}, 42, 'hello, world']
for (const value of values) {
// we deliberately want to check whether this constructor call fails or not
expect(() => {
new RatInAMaze(value)
}).toThrow()
}
})
it('should fail for an empty array', () => {
// we deliberately want to check whether this constructor call fails or not
expect(() => {
new RatInAMaze([])
}).toThrow()
})
it('should fail for a non-square array', () => {
const array = [
[0, 0, 0],
[0, 0]
]
// we deliberately want to check whether this constructor call fails or not
expect(() => {
new RatInAMaze(array)
}).toThrow()
})
it('should fail for arrays containing invalid values', () => {
const values = [[[2]], [['a']]]
for (const value of values) {
// we deliberately want to check whether this constructor call fails or not
expect(() => {
new RatInAMaze(value)
}).toThrow()
}
})
it('should work for a single-cell maze', () => {
const maze = new RatInAMaze([[1]])
expect(maze.solved).toBe(true)
expect(maze.path).toBe('')
})
it('should work for a single-cell maze that can not be solved', () => {
const maze = new RatInAMaze([[0]])
expect(maze.solved).toBe(false)
expect(maze.path).toBe('')
})
it('should work for a simple 3x3 maze', () => {
const maze = new RatInAMaze([
[1, 1, 0],
[0, 1, 0],
[0, 1, 1]
])
expect(maze.solved).toBe(true)
expect(maze.path).toBe('RDDR')
})
it('should work for a simple 2x2 that can not be solved', () => {
const maze = new RatInAMaze([
[1, 0],
[0, 1]
])
expect(maze.solved).toBe(false)
expect(maze.path).toBe('')
})
it('should work for a more complex maze', () => {
const maze = new RatInAMaze([
[1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[1, 1, 1, 0, 1, 0, 0],
[1, 0, 1, 0, 1, 0, 0],
[1, 0, 1, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1]
])
expect(maze.solved).toBe(true)
expect(maze.path).toBe('RRRRDDDDLLUULLDDDDRRRRRR')
})
it('should work for a more complex maze that can not be solved', () => {
const maze = new RatInAMaze([
[1, 1, 1, 1, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 1],
[1, 1, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1]
])
expect(maze.solved).toBe(false)
expect(maze.path).toBe('')
})
})