summaryrefslogtreecommitdiffstats
path: root/CLI/ruby/spec/game_of_life/generators/input_spec.rb
blob: e05dbfb4294df1ad37d84d5b810a2dc9a946823c (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
# 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
  end
end