hangman-cli 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'answer'
4
+
5
+ # Class to model a player's name given as an answer
6
+ class PlayerName < Answer
7
+ attr_reader :answer
8
+
9
+ def initialize(answer)
10
+ super(answer)
11
+ valid?
12
+ end
13
+
14
+ def valid?
15
+ self.errors = []
16
+ errors << 'Name must be between 3 - 12 chars.' unless valid_size?
17
+ errors.empty?
18
+ end
19
+
20
+ private
21
+
22
+ def valid_size?
23
+ answer.size > 2 && answer.size < 13
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Class to model the word to guess
4
+ class WordToGuess
5
+ attr_reader :word
6
+
7
+ def initialize(word)
8
+ @word = word
9
+ end
10
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/answer'
4
+
5
+ RSpec.describe Answer do
6
+ describe '#answer' do
7
+ it 'returns the current guess' do
8
+ answer = described_class.new('e')
9
+ expect(answer.answer).to eql 'e'
10
+ end
11
+
12
+ it 'downcases any guess' do
13
+ answer = described_class.new('E')
14
+ expect(answer.answer).to eql 'e'
15
+ end
16
+
17
+ it 'removes any newlines' do
18
+ answer = described_class.new("e\n")
19
+ expect(answer.answer).to eql 'e'
20
+ end
21
+
22
+ it 'removes tabs' do
23
+ answer = described_class.new("e\r")
24
+ expect(answer.answer).to eql 'e'
25
+ end
26
+
27
+ it 'removes added white space' do
28
+ answer = described_class.new('e ')
29
+ expect(answer.answer).to eql 'e'
30
+ end
31
+ end
32
+
33
+ describe '#quit_game?' do
34
+ context 'when player chooses to quit game' do
35
+ it 'is true' do
36
+ answer = described_class.new('hello')
37
+ answer.answer = 'quit game'
38
+ expect(answer.quit_game?).to be true
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/dictionary'
4
+
5
+ RSpec.describe Dictionary do
6
+ subject(:dictionary) { described_class.new(file) }
7
+
8
+ let(:file) { 'test_file.txt' }
9
+
10
+ describe '#word' do
11
+ it 'returns a string' do
12
+ expect(dictionary.word).to be_a String
13
+ end
14
+
15
+ it 'returns a string only containing letters' do
16
+ word = dictionary.word
17
+ expect(word).to satisfy('only contain letters') do
18
+ word.chars.all? { |letter| letter =~ /[a-z]/i }
19
+ end
20
+ end
21
+
22
+ it 'returns a word between 5 and 12 characters' do
23
+ expect(dictionary.word.size).to be >= 5 and be <= 12
24
+ end
25
+
26
+ it 'returns a downcase string' do
27
+ word = dictionary.word
28
+ expect(word).to eql word.downcase
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,234 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/display'
4
+ require_relative '../lib/guess_logic'
5
+ require_relative '../lib/word_to_guess'
6
+ require_relative '../lib/player'
7
+ require_relative '../lib/player_name'
8
+ require_relative '../lib/guess'
9
+
10
+ RSpec.describe Display do
11
+ subject(:display) { described_class }
12
+
13
+ let(:guess_logic) do
14
+ instance_double(GuessLogic,
15
+ guessed_word: '-e---',
16
+ guessed_letters: ['e'],
17
+ incorrect_letters: %w[w y z],
18
+ incorrect_guesses: 2,
19
+ messages: ['Correct guess!'])
20
+ end
21
+
22
+ describe '.welcome_message' do
23
+ it 'displays welcome message' do
24
+ expect { display.welcome_message }.to output.to_stdout
25
+ end
26
+ end
27
+
28
+ describe '.instruction_intro' do
29
+ it 'displays instruction intro message' do
30
+ expect { display.instruction_intro }.to output.to_stdout
31
+ end
32
+ end
33
+
34
+ describe '.play_again' do
35
+ it 'displays instruction intro message' do
36
+ expect { display.play_again }.to output.to_stdout
37
+ end
38
+ end
39
+
40
+ describe '.leave' do
41
+ it 'displays goodbye message' do
42
+ expect { display.leave }.to output.to_stdout
43
+ end
44
+ end
45
+
46
+ describe '.instructions' do
47
+ it 'displays instruction intro message' do
48
+ expect { display.instructions }.to output.to_stdout
49
+ end
50
+ end
51
+
52
+ describe '.enter_name' do
53
+ it 'displays enter name text' do
54
+ expect { display.enter_name }.to output.to_stdout
55
+ end
56
+ end
57
+
58
+ describe '.thank_player' do
59
+ it 'display thank you message' do
60
+ player = instance_double(Player, name: 'Helen')
61
+ expect { display.thank_player(player) }.to output.to_stdout
62
+ end
63
+ end
64
+
65
+ describe '.word_to_guess' do
66
+ it 'displays the word to guess' do
67
+ expect { display.word_to_guess(guess_logic) }.to output(" Word to guess: -e---\n").to_stdout
68
+ end
69
+ end
70
+
71
+ describe '.beginning_of_guess_round' do
72
+ it 'displays beginning of round info' do
73
+ expect { display.beginning_of_guess_round(guess_logic) }.to output.to_stdout
74
+ end
75
+ end
76
+
77
+ describe '.end_of_guess_round' do
78
+ context 'when correct guess' do
79
+ it 'displays end of game round information' do
80
+ expect { display.end_of_guess_round(guess_logic) }.to output("Correct guess!\n").to_stdout
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '.gallows' do
86
+ context 'when 0 incorrect guesses' do
87
+ it 'displays the gallows' do
88
+ allow(guess_logic).to receive(:incorrect_guesses).and_return(0)
89
+ start = <<-START
90
+ ===========
91
+ |/ |
92
+ |
93
+ |
94
+ |
95
+ |\\
96
+ ============
97
+ START
98
+ expect { display.gallows(guess_logic) }.to output(start).to_stdout
99
+ end
100
+ end
101
+
102
+ context 'with 1 incorrect guess' do
103
+ it 'displays gallows and head' do
104
+ allow(guess_logic).to receive(:incorrect_guesses).and_return(1)
105
+ head = <<-HEAD
106
+ ===========
107
+ |/ |
108
+ | O
109
+ |
110
+ |
111
+ |\\
112
+ ============
113
+ HEAD
114
+ expect { display.gallows(guess_logic) }.to output(head).to_stdout
115
+ end
116
+ end
117
+
118
+ context 'with 2 incorrect guesses' do
119
+ it 'displays gallows, head and body' do
120
+ allow(guess_logic).to receive(:incorrect_guesses).and_return(2)
121
+ body = <<-BODY
122
+ ===========
123
+ |/ |
124
+ | O
125
+ | |
126
+ |
127
+ |\\
128
+ ============
129
+ BODY
130
+ expect { display.gallows(guess_logic) }.to output(body).to_stdout
131
+ end
132
+ end
133
+
134
+ context 'with 3 incorrect guess' do
135
+ it 'displays gallows, head, right-arm and body' do
136
+ allow(guess_logic).to receive(:incorrect_guesses).and_return(3)
137
+ right_arm = <<-RIGHT_ARM
138
+ ===========
139
+ |/ |
140
+ | O
141
+ | /|
142
+ |
143
+ |\\
144
+ ============
145
+ RIGHT_ARM
146
+ expect { display.gallows(guess_logic) }.to output(right_arm).to_stdout
147
+ end
148
+ end
149
+
150
+ context 'with 4 incorrect guesses' do
151
+ it 'displays the gallows, head, right-arm, left-arm and body' do
152
+ allow(guess_logic).to receive(:incorrect_guesses).and_return(4)
153
+ left_arm = <<-LEFT_ARM
154
+ ===========
155
+ |/ |
156
+ | O
157
+ | /|\\
158
+ |
159
+ |\\
160
+ ============
161
+ LEFT_ARM
162
+ expect { display.gallows(guess_logic) }.to output(left_arm).to_stdout
163
+ end
164
+ end
165
+
166
+ context 'with 5 incorrect guesses' do
167
+ it 'displays the gallows, head, right-arm, left-arm, right-leg and body' do
168
+ allow(guess_logic).to receive(:incorrect_guesses).and_return(5)
169
+ right_leg = <<-RIGHT_LEG
170
+ ===========
171
+ |/ |
172
+ | O
173
+ | /|\\
174
+ | /
175
+ |\\
176
+ ============
177
+ RIGHT_LEG
178
+ expect { display.gallows(guess_logic) }.to output(right_leg).to_stdout
179
+ end
180
+ end
181
+
182
+ context 'with 6 incorrect guesses' do
183
+ it 'displays the gallows and whole body' do
184
+ allow(guess_logic).to receive(:incorrect_guesses).and_return(6)
185
+ finish = <<-FINISH
186
+ ===========
187
+ |/ |
188
+ | O
189
+ | /|\\
190
+ | / \\
191
+ |\\
192
+ ============
193
+ FINISH
194
+ expect { display.gallows(guess_logic) }.to output(finish).to_stdout
195
+ end
196
+ end
197
+ end
198
+
199
+ describe '.validation_errors' do
200
+ context 'when validation error has been triggered' do
201
+ it 'displays validation errors' do
202
+ errors = ['Name must be between 3 and 12 characters.']
203
+ expect { display.validation_errors(errors) }.to output.to_stdout
204
+ end
205
+ end
206
+
207
+ context 'when validation error has not been triggered' do
208
+ it 'does not display an error message' do
209
+ errors = []
210
+ expect { display.validation_errors(errors) }.to_not output.to_stdout
211
+ end
212
+ end
213
+ end
214
+
215
+ describe '.victory' do
216
+ it 'displays victory text' do
217
+ word_to_guess = instance_double(WordToGuess, word: 'hello')
218
+ expect { display.victory(word_to_guess) }.to output.to_stdout
219
+ end
220
+ end
221
+
222
+ describe '.defeat' do
223
+ it 'displays defeat text' do
224
+ word_to_guess = instance_double(WordToGuess, word: 'hello')
225
+ expect { display.defeat(word_to_guess) }.to output.to_stdout
226
+ end
227
+ end
228
+
229
+ describe '.load' do
230
+ it 'displays load game text' do
231
+ expect { display.load_game }.to output.to_stdout
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/gallows'
4
+
5
+ RSpec.describe Gallows do
6
+ subject(:gallows) { described_class }
7
+
8
+ describe '#start' do
9
+ it 'returns the starting gallows' do
10
+ start = <<-START
11
+ ===========
12
+ |/ |
13
+ |
14
+ |
15
+ |
16
+ |\\
17
+ ============
18
+ START
19
+
20
+ expect(gallows.start).to eq start
21
+ end
22
+ end
23
+
24
+ describe '#head' do
25
+ it 'returns the gallows with head' do
26
+ head = <<-HEAD
27
+ ===========
28
+ |/ |
29
+ | O
30
+ |
31
+ |
32
+ |\\
33
+ ============
34
+ HEAD
35
+
36
+ expect(gallows.head).to eq head
37
+ end
38
+ end
39
+
40
+ describe '#body' do
41
+ it 'returns the gallows with body' do
42
+ body = <<-BODY
43
+ ===========
44
+ |/ |
45
+ | O
46
+ | |
47
+ |
48
+ |\\
49
+ ============
50
+ BODY
51
+
52
+ expect(gallows.body).to eq body
53
+ end
54
+ end
55
+
56
+ describe '#right_arm' do
57
+ it 'returns the gallows with right arm' do
58
+ right_arm = <<-RIGHT_ARM
59
+ ===========
60
+ |/ |
61
+ | O
62
+ | /|
63
+ |
64
+ |\\
65
+ ============
66
+ RIGHT_ARM
67
+
68
+ expect(gallows.right_arm).to eq right_arm
69
+ end
70
+ end
71
+
72
+ describe '#left_arm' do
73
+ it 'returns the gallows with left arm' do
74
+ left_arm = <<-LEFT_ARM
75
+ ===========
76
+ |/ |
77
+ | O
78
+ | /|\\
79
+ |
80
+ |\\
81
+ ============
82
+ LEFT_ARM
83
+
84
+ expect(gallows.left_arm).to eq left_arm
85
+ end
86
+ end
87
+
88
+ describe '#right_leg' do
89
+ it 'returns the gallows with right leg' do
90
+ right_leg = <<-RIGHT_LEG
91
+ ===========
92
+ |/ |
93
+ | O
94
+ | /|\\
95
+ | /
96
+ |\\
97
+ ============
98
+ RIGHT_LEG
99
+
100
+ expect(gallows.right_leg).to eq right_leg
101
+ end
102
+ end
103
+
104
+ describe '#left_leg' do
105
+ it 'returns the gallows with left leg' do
106
+ finish = <<-FINISH
107
+ ===========
108
+ |/ |
109
+ | O
110
+ | /|\\
111
+ | / \\
112
+ |\\
113
+ ============
114
+ FINISH
115
+
116
+ expect(gallows.finish).to eq finish
117
+ end
118
+ end
119
+ end