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,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/game_save'
4
+ require_relative '../lib/game'
5
+
6
+ RSpec.describe GameSave do
7
+ subject(:game_save) { described_class }
8
+
9
+ let(:game) { instance_double(Game) }
10
+
11
+ describe '.save' do
12
+ it 'saves the a game object' do
13
+ file = StringIO.new
14
+ game_data = 'game data'
15
+ allow(YAML).to receive(:dump).and_return(game_data)
16
+ allow(File).to receive(:join).and_return('a file')
17
+ allow(File).to receive(:directory?).and_return(true)
18
+ allow(File).to receive(:open).and_yield(file)
19
+ game_save.save('game')
20
+ expect(file.string).to eql game_data
21
+ end
22
+ end
23
+
24
+ describe '.load' do
25
+ it 'loads saved game' do
26
+ file = StringIO.new
27
+ file.puts 'game data'
28
+ file.rewind
29
+ allow(File).to receive(:join).and_return('a file')
30
+ allow(File).to receive(:exist?).and_return(true)
31
+ allow(File).to receive(:open).and_yield(file)
32
+ expect(game_save.load).to eql 'game data'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/game'
4
+
5
+ RSpec.describe Game do
6
+ subject(:game) { described_class.new }
7
+
8
+ around do |example|
9
+ example.run
10
+ rescue SystemExit
11
+ end
12
+
13
+ before do
14
+ # Suppresses puts output, comment out to reveal output again.
15
+ allow($stdout).to receive(:write)
16
+ # Suppresses auto game save
17
+ allow(GameSave).to receive(:save).and_return(nil)
18
+ end
19
+
20
+ describe 'play through' do
21
+ context 'with victory' do
22
+ it 'by guessing the correct word' do
23
+ word = game.word_to_guess.word
24
+ allow($stdin).to receive(:gets).and_return('no', 'no', 'Beth', word, 'no')
25
+ expect(game).to receive(:victory).and_call_original
26
+ game.start
27
+ end
28
+
29
+ it 'by guessing each letter' do
30
+ letters = game.word_to_guess.word.chars.uniq
31
+ allow($stdin).to receive(:gets).and_return('no', 'no', 'Beth', *letters, 'no')
32
+ expect(game).to receive(:victory).and_call_original
33
+ game.start
34
+ end
35
+ end
36
+
37
+ context 'with defeat' do
38
+ def wrong_letter
39
+ let = 'a'
40
+ word = game.word_to_guess.word
41
+ while word.include?(let)
42
+ let = let.next
43
+ end
44
+ let
45
+ end
46
+
47
+ it 'by guessing an incorrect word' do
48
+ word = game.word_to_guess.word.chars.shuffle.join
49
+ word = word.chars.shuffle.join
50
+ allow($stdin).to receive(:gets).and_return('no', 'no', 'Beth', word, 'no')
51
+ expect(game).to receive(:defeat).and_call_original
52
+ game.start
53
+ end
54
+
55
+ it 'by running out of lives' do
56
+ let = wrong_letter
57
+ letters = (1..6).map { let }
58
+ allow($stdin).to receive(:gets).and_return('no', 'no', 'Beth', *letters, 'no')
59
+ expect(game).to receive(:defeat).and_call_original
60
+ game.start
61
+ end
62
+ end
63
+ end
64
+
65
+ describe 'invaldation is triggered' do
66
+ context 'with invalid name' do
67
+ it 're-tries the loop if player name is invalid' do
68
+ allow($stdin).to receive(:gets).and_return('no', 'no', 'be', 'bee', 'quit game')
69
+ expect(Display).to receive(:enter_name).twice
70
+ game.start
71
+ end
72
+ end
73
+
74
+ context 'with invalid guess' do
75
+ it 're-tries the loop if guess is invalid' do
76
+ allow($stdin).to receive(:gets).and_return('no', 'no', 'Dan', '4', 'quit game')
77
+ expect(Display).to receive(:beginning_of_guess_round).twice
78
+ game.start
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'load' do
84
+ it 'loads previously saved game when selected' do
85
+ allow($stdin).to receive(:gets).and_return('load', 'quit game')
86
+ expect(GameSave).to receive(:load).and_call_original
87
+ game.start
88
+ end
89
+ end
90
+
91
+ describe 'instructions' do
92
+ it 'instructions are shown when selected' do
93
+ allow($stdin).to receive(:gets).and_return('no', 'i', 'quit game')
94
+ expect(Display).to receive(:instructions).and_call_original
95
+ game.start
96
+ end
97
+ end
98
+
99
+ describe 'reload' do
100
+ it 'starts a new game at game end' do
101
+ word = game.word_to_guess.word
102
+ allow($stdin).to receive(:gets).and_return('no', 'no', 'Beth', word, 'yes', 'quit game')
103
+ expect(described_class).to receive(:new).and_call_original
104
+ game.start
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/guess_logic'
4
+ require_relative '../lib/word_to_guess'
5
+ require_relative '../lib/guess'
6
+
7
+ RSpec.describe GuessLogic do
8
+ subject(:guess_logic) { described_class.new(word_to_guess) }
9
+
10
+ let(:word_to_guess) { instance_double(WordToGuess, word: 'hello') }
11
+
12
+ describe '#word_to_guess' do
13
+ it 'returns the word to guess' do
14
+ expect(guess_logic.word_to_guess).to eq 'hello'
15
+ end
16
+ end
17
+
18
+ describe '#compare' do
19
+ context 'when guess is correct' do
20
+ it 'adds the correctly guessed letters' do
21
+ guess = instance_double(Guess, answer: 'h')
22
+ guess_logic.compare(guess)
23
+ expect(guess_logic.guessed_letters).to eq ['h']
24
+ end
25
+
26
+ it 'adds the correctly guessed letter and returns all guessed letters' do
27
+ guess = instance_double(Guess, answer: 'e')
28
+ allow(guess_logic).to receive(:guessed_letters).and_return ['h']
29
+ guess_logic.compare(guess)
30
+ expect(guess_logic.guessed_letters).to eq %w[h e]
31
+ end
32
+
33
+ it 'adds positive message to messages' do
34
+ guess = instance_double(Guess, answer: 'o')
35
+ expect { guess_logic.compare(guess) }.to change { guess_logic.messages.size }.by(1)
36
+ end
37
+ end
38
+
39
+ context 'when guess is incorrect' do
40
+ it 'adds the incorrectly guessed letters' do
41
+ guess = instance_double(Guess, answer: 'q')
42
+ guess_logic.compare(guess)
43
+ expect(guess_logic.incorrect_letters).to eq ['q']
44
+ end
45
+
46
+ it 'adds the incorrectly guessed letters and returns all incorrectly guessed' do
47
+ guess = instance_double(Guess, answer: 'w')
48
+ allow(guess_logic).to receive(:incorrect_letters).and_return ['q']
49
+ guess_logic.compare(guess)
50
+ expect(guess_logic.incorrect_letters).to eq %w[q w]
51
+ end
52
+
53
+ it 'adds a negative message to messages' do
54
+ guess = instance_double(Guess, answer: 'w')
55
+ expect { guess_logic.compare(guess) }.to change { guess_logic.messages.size }.by(1)
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#guessed_word' do
61
+ context 'with no guesses' do
62
+ it 'returns the blank word to guess' do
63
+ expect(guess_logic.guessed_word).to eq '-----'
64
+ end
65
+ end
66
+
67
+ context 'with 1 guess right' do
68
+ it 'returns the correctly guessed letter filled in' do
69
+ guess = instance_double(Guess, answer: 'e')
70
+ guess_logic.compare(guess)
71
+ expect(guess_logic.guessed_word).to eq '-e---'
72
+ end
73
+
74
+ it 'returns all of the same correctly guessed letter filled in' do
75
+ guess = instance_double(Guess, answer: 'l')
76
+ guess_logic.compare(guess)
77
+ expect(guess_logic.guessed_word).to eq '--ll-'
78
+ end
79
+ end
80
+
81
+ context 'with 2 correct guesses' do
82
+ it 'returns all the correctly filled in letters' do
83
+ guess1 = instance_double(Guess, answer: 'h')
84
+ guess2 = instance_double(Guess, answer: 'o')
85
+ guess_logic.compare(guess1)
86
+ guess_logic.compare(guess2)
87
+ expect(guess_logic.guessed_word).to eq 'h---o'
88
+ end
89
+ end
90
+
91
+ context 'with incorrect guess' do
92
+ it "doesn't fill in any incorrect guesses" do
93
+ guess = instance_double(Guess, answer: 'x')
94
+ guess_logic.compare(guess)
95
+ expect(guess_logic.guessed_word).to eq '-----'
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '#incorrect_guesses' do
101
+ it 'increases by 1 if guess is incorrect' do
102
+ guess = instance_double(Guess, answer: 'w')
103
+ expect { guess_logic.compare(guess) }.to change(guess_logic, :incorrect_guesses).by 1
104
+ end
105
+ end
106
+
107
+ describe '#full_word_guess' do
108
+ context 'when guess is same length as word to guess' do
109
+ it 'returns true' do
110
+ guess = instance_double(Guess, answer: 'threw')
111
+ expect(guess_logic.full_word_guess?(guess)).to be true
112
+ end
113
+ end
114
+
115
+ context 'when guess is not same length as word to guess' do
116
+ it 'returns false' do
117
+ guess = instance_double(Guess, answer: 'thrw')
118
+ expect(guess_logic.full_word_guess?(guess)).to be false
119
+ end
120
+ end
121
+ end
122
+
123
+ describe '#correct_word?' do
124
+ context 'when full word guess is correct' do
125
+ it 'returns true' do
126
+ guess = instance_double(Guess, answer: 'hello')
127
+ expect(guess_logic.correct_word?(guess)).to be true
128
+ end
129
+ end
130
+
131
+ context 'when full word guess is incorrect' do
132
+ it 'returns true' do
133
+ guess = instance_double(Guess, answer: 'hippo')
134
+ expect(guess_logic.correct_word?(guess)).to be false
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/guess'
4
+ require_relative '../lib/word_to_guess'
5
+
6
+ RSpec.describe Guess do
7
+ subject(:guess) { described_class.new('e', word_to_guess) }
8
+
9
+ let(:word_to_guess) { instance_double(WordToGuess, word: 'hello') }
10
+
11
+ describe '#valid?' do
12
+ context 'with valid guess' do
13
+ it 'is true for a single letter' do
14
+ expect(guess.valid?).to be true
15
+ end
16
+
17
+ it 'is true for a word the same length as the word to guess' do
18
+ guess = described_class.new('fudge', word_to_guess)
19
+ expect(guess.valid?).to be true
20
+ end
21
+ end
22
+
23
+ context 'with invalid guess' do
24
+ it 'is false for a number' do
25
+ guess.answer = '3'
26
+ expect(guess.valid?).to be false
27
+ end
28
+
29
+ it 'is false for an array' do
30
+ guess.answer = '[]'
31
+ expect(guess.valid?).to be false
32
+ end
33
+
34
+ it 'is false if more than 1 letter (and not a full word guess)' do
35
+ guess.answer = 'he'
36
+ expect(guess.valid?).to be false
37
+ end
38
+
39
+ it 'adds an error message to errors' do
40
+ guess.answer = '888'
41
+ guess.valid?
42
+ expect(guess.errors).to_not be_empty
43
+ end
44
+
45
+ it 'is false for a full word guess that is numbers' do
46
+ guess.answer = '99999'
47
+ expect(guess.valid?).to be false
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/player_name'
4
+
5
+ RSpec.describe PlayerName do
6
+ subject(:player_name) { described_class.new('beth') }
7
+
8
+ describe '#initialize' do
9
+ describe '#name' do
10
+ it 'sets the given name' do
11
+ expect(player_name.answer).to eql 'beth'
12
+ end
13
+ end
14
+
15
+ describe '#errors' do
16
+ it 'sets an empty array' do
17
+ expect(player_name.errors).to eql []
18
+ end
19
+ end
20
+
21
+ describe '#valid?' do
22
+ it 'calls the valid? method' do
23
+ expect_any_instance_of(described_class).to receive(:valid?)
24
+ player_name
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#valid?' do
30
+ context 'when player name is valid' do
31
+ it 'is true if name is between 3 and 12 chars' do
32
+ expect(player_name.valid?).to be true
33
+ end
34
+
35
+ it 'does not add an error message to errors' do
36
+ expect(player_name.errors).to be_empty
37
+ end
38
+ end
39
+
40
+ context 'when player name is invalid' do
41
+ subject(:player_name) { described_class.new('po') }
42
+
43
+ it 'is false' do
44
+ expect(player_name.valid?).to be false
45
+ end
46
+
47
+ it 'is false if name is below 3 chars' do
48
+ player_name = described_class.new('po')
49
+ expect(player_name.valid?).to be false
50
+ end
51
+
52
+ it 'is false if name is longer than 12 chars' do
53
+ player_name = described_class.new('montgomery the third esquire')
54
+ expect(player_name.valid?).to be false
55
+ end
56
+
57
+ it 'adds an error message to errors' do
58
+ expect(player_name.errors).to_not be_empty
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/player'
4
+
5
+ RSpec.describe Player do
6
+ subject(:player) { described_class.new('jess') }
7
+
8
+ it 'has a capitalized name' do
9
+ expect(player.name).to eql 'Jess'
10
+ end
11
+ end
@@ -0,0 +1,105 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ enable_coverage :branch
4
+ end
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
9
+ # this file to always be loaded, without a need to explicitly require it in any
10
+ # files.
11
+ #
12
+ # Given that it is always loaded, you are encouraged to keep this file as
13
+ # light-weight as possible. Requiring heavyweight dependencies from this file
14
+ # will add to the boot time of your test suite on EVERY test run, even for an
15
+ # individual file that may not need all of that loaded. Instead, consider making
16
+ # a separate helper file that requires the additional dependencies and performs
17
+ # the additional setup, and require it from the spec files that actually need
18
+ # it.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+ RSpec.configure do |config|
22
+ # rspec-expectations config goes here. You can use an alternate
23
+ # assertion/expectation library such as wrong or the stdlib/minitest
24
+ # assertions if you prefer.
25
+
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
47
+ # have no way to turn it off -- the option exists only for backwards
48
+ # compatibility in RSpec 3). It causes shared context metadata to be
49
+ # inherited by the metadata hash of host groups and examples, rather than
50
+ # triggering implicit auto-inclusion in groups with matching metadata.
51
+ config.shared_context_metadata_behavior = :apply_to_host_groups
52
+
53
+ # The settings below are suggested to provide a good initial experience
54
+ # with RSpec, but feel free to customize to your heart's content.
55
+
56
+ # This allows you to limit a spec run to individual examples or groups
57
+ # you care about by tagging them with `:focus` metadata. When nothing
58
+ # is tagged with `:focus`, all examples get run. RSpec also provides
59
+ # aliases for `it`, `describe`, and `context` that include `:focus`
60
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
61
+ #config.filter_run_when_matching :focus
62
+
63
+ # Allows RSpec to persist some state between runs in order to support
64
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
65
+ # you configure your source control system to ignore this file.
66
+ config.example_status_persistence_file_path = "spec/examples.txt"
67
+
68
+ # Limits the available syntax to the non-monkey patched syntax that is
69
+ # recommended. For more details, see:
70
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
71
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
73
+ config.disable_monkey_patching!
74
+
75
+ # This setting enables warnings. It's recommended, but in some cases may
76
+ # be too noisy due to issues in dependencies.
77
+ config.warnings = true
78
+
79
+ # Many RSpec users commonly either run the entire suite or an individual
80
+ # file, and it's useful to allow more verbose output when running an
81
+ # individual spec file.
82
+ if config.files_to_run.one?
83
+ # Use the documentation formatter for detailed output,
84
+ # unless a formatter has already been configured
85
+ # (e.g. via a command-line flag).
86
+ config.default_formatter = "doc"
87
+ end
88
+
89
+ # Print the 10 slowest examples and example groups at the
90
+ # end of the spec run, to help surface which specs are running
91
+ # particularly slow.
92
+ config.profile_examples = 3
93
+
94
+ # Run specs in random order to surface order dependencies. If you find an
95
+ # order dependency and want to debug it, you can fix the order by providing
96
+ # the seed, which is printed after each run.
97
+ # --seed 1234
98
+ config.order = :random
99
+
100
+ # Seed global randomization in this process using the `--seed` CLI option.
101
+ # Setting this allows you to use `--seed` to deterministically reproduce
102
+ # test failures related to randomization by passing the same `--seed` value
103
+ # as the one that triggered the failure.
104
+ Kernel.srand config.seed
105
+ end