diff options
| author | a14m <[email protected]> | 2021-01-30 16:10:55 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2021-01-30 16:10:55 +0100 |
| commit | 602d200b9fd0ebde60a2391ffd6bb2408689f3a5 (patch) | |
| tree | 73832ef271c277aa317e7bf81e51336fbd88b425 /CLI | |
| parent | d1fc54d9c8e10f0aced6de3c275bf494f6867abf (diff) | |
Fix Living/Dead cells can have more than 1 char or have the same char
Diffstat (limited to 'CLI')
| -rw-r--r-- | CLI/ruby/lib/game_of_life.rb | 8 | ||||
| -rw-r--r-- | CLI/ruby/spec/game_of_life_spec.rb | 50 |
2 files changed, 46 insertions, 12 deletions
diff --git a/CLI/ruby/lib/game_of_life.rb b/CLI/ruby/lib/game_of_life.rb index a2ce27a..2ff548c 100644 --- a/CLI/ruby/lib/game_of_life.rb +++ b/CLI/ruby/lib/game_of_life.rb @@ -43,6 +43,14 @@ module GameOfLife "must be between 1 and #{max_height} (current terminal height)" end + fail GameOfLife::Error, "Invalid --live-cell value. must be a single character" if options["live-cell"].length > 1 + + fail GameOfLife::Error, "Invalid --dead-cell value. must be a single character" if options["dead-cell"].length > 1 + + if options["dead-cell"] == options["live-cell"] + fail GameOfLife::Error, "Invalid --dead-cell value. must be a different character than --live-cell" + end + { # Defaults the hight to less then the height of the terminal to allow banner info # and an extra emtpy line to avoid triggering terminal scroll while flushing diff --git a/CLI/ruby/spec/game_of_life_spec.rb b/CLI/ruby/spec/game_of_life_spec.rb index 5e99ca8..89a0c87 100644 --- a/CLI/ruby/spec/game_of_life_spec.rb +++ b/CLI/ruby/spec/game_of_life_spec.rb @@ -2,6 +2,13 @@ RSpec.describe GameOfLife do describe ".parsed_options" do + let(:options) do + { + "live-cell" => "L", + "dead-cell" => "D", + } + end + before do console = instance_double("<File:/dev/tty>") allow(IO).to receive(:console).and_return(console) @@ -10,68 +17,87 @@ RSpec.describe GameOfLife do context "with options['delay']" do it "raises GameOfLife::Error when negative" do - options = { "delay" => -13 } + options["delay"] = -13 expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --delay value/) end it "uses options['delay'] when posotive" do - options = { "delay" => 15 } + options["delay"] = 15 expect(described_class.parsed_options(options)).to include("delay" => 15) end end context "with options['width']" do it "raises GameOfLife::Error when < 1" do - options = { "width" => 0 } + options["width"] = 0 expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --width value/) end it "raises GameOfLife::Error when > IO.console.winsize[1]" do - options = { "width" => 23 } + options["width"] = 23 expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --width value/) end it "uses options['width']" do - options = { "width" => 15 } + options["width"] = 15 expect(described_class.parsed_options(options)).to include("width" => 15) end it "uses IO.console.winsize[1] as default" do - expect(described_class.parsed_options({})).to include("width" => 20) + expect(described_class.parsed_options(options)).to include("width" => 20) end end context "with options['height']" do it "raises GameOfLife::Error < 1" do - options = { "height" => 0 } + options["height"] = 0 expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --height value/) end it "raises GameOfLife::Error when > winsize[0] - 2" do - options = { "height" => 9 } + options["height"] = 9 expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --height value/) end it "uses options['height']" do - options = { "height" => 5 } + options["height"] = 5 expect(described_class.parsed_options(options)).to include("height" => 5) end it "uses IO.console.winsize[0] - 2 as default" do - expect(described_class.parsed_options({})).to include("height" => 8) + expect(described_class.parsed_options(options)).to include("height" => 8) + end + end + + context "with options['live-cell']" do + it "raises GameOfLife::Error if live-cell is multiple characters" do + options["live-cell"] = "LL" + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --live-cell /) + end + end + + context "with options['dead-cell']" do + it "raises GameOfLife::Error if dead-cell is multiple characters" do + options["dead-cell"] = "DD" + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --dead-cell /) + end + + it "raises GameOfLife::Error if dead-cell is same as live-cell character" do + options = { "live-cell" => "X", "dead-cell" => "X" } + expect { described_class.parsed_options(options) }.to raise_error(GameOfLife::Error, /Invalid --dead-cell /) end end context "with options['seed']" do it "uses options['seed']" do - options = { "seed" => 1337 } + options["seed"] = 1337 expect(described_class.parsed_options(options)).to include("seed" => 1337) end # rubocop:disable RSpec/AnyInstance it "defaults to Kernel.random" do allow_any_instance_of(Kernel).to receive(:rand).and_return "random" - expect(described_class.parsed_options({})).to include("seed" => "random") + expect(described_class.parsed_options(options)).to include("seed" => "random") end # rubocop:enable RSpec/AnyInstance end |
