From 501eeba38abe984959205c9c17a99a27864e29cd Mon Sep 17 00:00:00 2001 From: a14m Date: Mon, 14 Dec 2020 13:00:30 +0100 Subject: Add the specs for the parsed_options method (fixing negative issues) --- CLI/ruby/spec/game_of_life_spec.rb | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'CLI/ruby') 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 { -- cgit v1.2.3