summaryrefslogtreecommitdiffstats
path: root/CLI/ruby/spec/game_of_life/generators/input_spec.rb
blob: 6986787d5b69493edd89b5121e4f5cf1e79beae5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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