# frozen_string_literal: true RSpec.describe GameOfLife::Generators::Input do describe ".new" do let(:file) { instance_double(File) } it "returns a GameOfLife::Universe" do options = { "input" => "something", "width" => 1, "height" => 1 } allow(URI).to receive(:open).and_return(file) allow(file).to receive(:read).and_return(options["input"]) expect(described_class.new(options)).to be_a GameOfLife::Universe end it "parses alpha-numeric characters as living cells" do options = { "input" => "Conway's Game of Life", "width" => 21, "height" => 1 } allow(URI).to receive(:open).and_return(file) allow(file).to receive(:read).and_return(options["input"]) stub_const("GameOfLife::Cell::LIVE_CELL", "X") stub_const("GameOfLife::Cell::DEAD_CELL", ".") # Conway's Game of Life expect(described_class.new(options).to_s).to eq "XXXXXX.X.XXXX.XX.XXXX" end it "fills the rest of universe with dead cells" do options = { "input" => "Conway's Game of Life", "width" => 25, "height" => 2 } allow(URI).to receive(:open).and_return(file) allow(file).to receive(:read).and_return(options["input"]) stub_const("GameOfLife::Cell::LIVE_CELL", "X") stub_const("GameOfLife::Cell::DEAD_CELL", ".") # Conway's Game of Life universe = "XXXXXX.X.XXXX.XX.XXXX....\n" \ "........................." expect(described_class.new(options).to_s).to eq universe end it "truncates the content of the file to match the width" do options = { "input" => "Conway's Game of Life", "width" => 11, "height" => 3 } allow(URI).to receive(:open).and_return(file) allow(file).to receive(:read).and_return(options["input"]) stub_const("GameOfLife::Cell::LIVE_CELL", "X") stub_const("GameOfLife::Cell::DEAD_CELL", ".") # Conway's Ga universe = "XXXXXX.X.XX\n" \ "...........\n" \ "..........." expect(described_class.new(options).to_s).to eq universe end it "rescues Errno::ENOENT and raises GameOfLife::Error" do options = { "input" => "not-existing-file.txt", "width" => 25, "height" => 2 } expect { described_class.new(options) }.to raise_error( GameOfLife::Error, "File isn't avaialable, please check it again and make sure the path to the input file is correct", ) end it "rescues SocketError and raises GameOfLife::Error" do options = { "input" => "https://not-existing-url.com", "width" => 25, "height" => 2 } expect { described_class.new(options) }.to raise_error( GameOfLife::Error, "URL isn't avaialable, please check it again and make sure the URL is correct", ) end it "rescues OpenURI::HTTPError and raises GameOfLife::Error" do options = { "input" => "https://example.com/non-existing", "width" => 25, "height" => 2 } expect { described_class.new(options) }.to raise_error( GameOfLife::Error, "URL isn't avaialable, please check it again and make sure the URL is correct", ) end end end