diff options
| author | a14m <[email protected]> | 2020-12-14 13:00:30 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-12-14 13:00:30 +0100 |
| commit | 501eeba38abe984959205c9c17a99a27864e29cd (patch) | |
| tree | ec408dc4aa6064e6d12645e09296aedfa418429f | |
| parent | 3265911ce64d7ee6a7f1ffc55ac3a4d132367604 (diff) | |
Add the specs for the parsed_options method (fixing negative issues)
| -rw-r--r-- | CLI/ruby/spec/game_of_life_spec.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/CLI/ruby/spec/game_of_life_spec.rb b/CLI/ruby/spec/game_of_life_spec.rb index 6564327..00b58b8 100644 --- a/CLI/ruby/spec/game_of_life_spec.rb +++ b/CLI/ruby/spec/game_of_life_spec.rb @@ -1,6 +1,68 @@ # frozen_string_literal: true RSpec.describe GameOfLife do + describe ".parsed_options" do + context "options['delay']" do + it "raises GameOfLife::Error when negative delay" do + options = { "delay" => -13 } + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --delay value/) + end + + it "uses options['delay'] when provided" do + options = { "delay" => 15 } + expect(described_class.parsed_options(options)).to include("delay" => 15) + end + end + + context "options['width']" do + it "raises GameOfLife::Error when width < 1" do + options = { "width" => 0 } + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --width value/) + end + + it "raises GameOfLife::Error when width > IO.console.winsize[1]" do + options = { "width" => 23 } + allow(IO).to receive_message_chain(:console, :winsize).and_return([10, 20]) + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --width value/) + end + + it "uses options['width'] when provided" do + options = { "width" => 15 } + allow(IO).to receive_message_chain(:console, :winsize).and_return([10, 20]) + expect(described_class.parsed_options(options)).to include("width" => 15) + end + + it "uses IO.console.winsize[1] as default" do + allow(IO).to receive_message_chain(:console, :winsize).and_return([10, 20]) + expect(described_class.parsed_options({})).to include("width" => 20) + end + end + + context "options['height']" do + it "raises GameOfLife::Error when height < 1" do + options = { "height" => 0 } + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --height value/) + end + + it "raises GameOfLife::Error when height > winsize[0] - 2" do + options = { "height" => 9 } + allow(IO).to receive_message_chain(:console, :winsize).and_return([10, 20]) + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --height value/) + end + + it "uses options['height'] when provided" do + options = { "height" => 5 } + allow(IO).to receive_message_chain(:console, :winsize).and_return([10, 20]) + expect(described_class.parsed_options(options)).to include("height" => 5) + end + + it "uses IO.console.winsize[0] - 2 as default" do + allow(IO).to receive_message_chain(:console, :winsize).and_return([10, 20]) + expect(described_class.parsed_options({})).to include("height" => 8) + end + end + end + describe ".generate" do let(:options) do { |
