-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_solver_spec.rb
111 lines (93 loc) · 3.14 KB
/
maze_solver_spec.rb
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
require_relative 'spec_helper'
require_relative 'maze_solver'
describe 'MazeSolver' do
let(:small_maze){
<<-11x11
###########
# #
# ##### ###
→ # #
### # ### #
# # #
# ##### ###
# # # @
# ### #####
# #
###########
11x11
}
let(:maze_solver) { MazeSolver.new(small_maze) }
context "A new maze solver" do
it "instantiates with the maze as an attribute" do
expect(maze_solver.maze).to eq(small_maze)
end
it 'instantiates with empty arrays for traveled_path, visited_set, node_queue' do
expect(maze_solver.traveled_path).to eq([])
expect(maze_solver.visited_nodes).to eq([])
expect(maze_solver.node_queue).to eq([])
end
end
context 'Parsing a maze' do
let(:maze_array) {MazeSolver.new(small_maze).maze_array}
it 'converts the maze to an array' do
maze_array_fixture = [
["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", "#", "#", "#", "#", "#", " ", "#", "#", "#"],
["→", " ", " ", " ", "#", " ", " ", " ", " ", " ", "#"],
["#", "#", "#", " ", "#", " ", "#", "#", "#", " ", "#"],
["#", " ", " ", " ", " ", " ", "#", " ", " ", " ", "#"],
["#", " ", "#", "#", "#", "#", "#", " ", "#", "#", "#"],
["#", " ", "#", " ", " ", " ", "#", " ", " ", " ", "@"],
["#", " ", "#", "#", "#", " ", "#", "#", "#", "#", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"]
]
expect(maze_array).to eq(maze_array_fixture)
end
end
context 'Solving a maze' do
describe '#solve' do
it 'visits nodes and keeps track of them in the visited_nodes array' do
maze_solver.solve
# We're going to assume that at least 0,3 and 1,3 will be moved through
# in an at all functioning solve routine.
expect(maze_solver.visited_nodes).to include([0,3])
expect(maze_solver.visited_nodes).to include([1,3])
end
end
describe '#solution_path' do
it 'returns the solution path array' do
maze_solver.solve
# We're using nodes that absolutely must be traveled to in order to confirm a solution path,
# as mazes might include more than one solution.
solution_must_contain = [[0, 3], [7, 5], [7, 6], [7, 7], [8, 7], [9, 7], [10,7]]
solution_must_contain.each do |node|
expect(maze_solver.solution_path).to include(node)
end
end
end
# It is possible
describe '#display_solution_path' do
it 'prints out the solved maze' do
# There is another possible solution so don't worry
# if this test fails.
@solution_string = "
###########
# #
# ##### ###
→...#.....#
###.#.###.#
# ...#...#
# #####.###
# # #...@
# ### #####
# #
###########".strip
maze_solver.solve
expect($stdout).to receive(:puts).with(@solution_string)
maze_solver.display_solution_path
end
end
end
end