summaryrefslogtreecommitdiffstats
path: root/CLI/ruby/spec/game_of_life/cell_spec.rb
blob: 00f55b319e9831a826294ede23e0a93633413720 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# frozen_string_literal: true

RSpec.describe GameOfLife::Cell do
  subject(:living_cell) { described_class.new(x: nil, y: nil, alive: true) }
  subject(:dead_cell)   { described_class.new(x: nil, y: nil, alive: false) }

  describe "#to_s" do
    context "when alive" do
      it "returns LIVE_CELL representation" do
        stub_const("LIVE_CELL", "X")
        expect(living_cell.to_s).to eq "X"
      end
    end

    context "when dead" do
      it "returns DEAD_CELL representation" do
        stub_const("DEAD_CELL", ".")
        expect(dead_cell.to_s).to eq "."
      end
    end
  end

  describe "#evolve!" do
    it "calls #living_neighbors with param: neighbors" do
      neighbors = [1, 2, 3, 4, 5]
      expect(living_cell).to receive(:living_neighbors).with(neighbors)
      living_cell.evolve!(neighbors)
    end

    context "when dead" do
      it "stays dead" do
        expect(dead_cell).to receive(:living_neighbors).with([]).and_return 0
        next_generation = dead_cell.evolve!([])
        expect(next_generation.object_id).to eq dead_cell.object_id
        expect(next_generation.dead?).to be_truthy
      end

      it "becomes alive when having 3 living neighbors" do
        expect(dead_cell).to receive(:living_neighbors).with([]).and_return 3
        next_generation = dead_cell.evolve!([])
        expect(next_generation.object_id).not_to eq dead_cell.object_id
        expect(next_generation.alive?).to be_truthy
      end
    end

    context "when alive" do
      it "survives when having 2 neighbors" do
        expect(living_cell).to receive(:living_neighbors).with([]).and_return 2
        next_generation = living_cell.evolve!([])
        expect(next_generation.object_id).to eq living_cell.object_id
        expect(next_generation.alive?).to be_truthy
      end

      it "survives when having 3 neighbors" do
        expect(living_cell).to receive(:living_neighbors).with([]).and_return 3
        next_generation = living_cell.evolve!([])
        expect(next_generation.object_id).to eq living_cell.object_id
        expect(next_generation.alive?).to be_truthy
      end

      it "dies when having less than 2 neighbors" do
        expect(living_cell).to receive(:living_neighbors).with([]).and_return 1
        next_generation = living_cell.evolve!([])
        expect(next_generation.object_id).not_to eq living_cell.object_id
        expect(next_generation.dead?).to be_truthy
      end

      it "dies when having more than 3 neighbors" do
        expect(living_cell).to receive(:living_neighbors).with([]).and_return 4
        next_generation = living_cell.evolve!([])
        expect(next_generation).not_to eq living_cell
        expect(next_generation.dead?).to be_truthy
      end
    end
  end

  describe "#alive?" do
    it { expect(living_cell.alive?).to be_truthy }
    it { expect(dead_cell.alive?).to be_falsy }
  end

  describe "#dead?" do
    it { expect(living_cell.dead?).to be_falsy }
    it { expect(dead_cell.dead?).to be_truthy }
  end

  describe "#living_neighbors" do
    subject(:cell) { [living_cell, dead_cell].sample }

    let(:living_neighbors) { (0...rand(100)).map { described_class.new(x: nil, y: nil, alive: true) } }
    let(:dead_neighbors)   { (0...rand(100)).map { described_class.new(x: nil, y: nil, alive: false) } }
    let(:neighbors)        { (living_neighbors + dead_neighbors).shuffle }

    it "returns the count of living neighbors cells" do
      expect(cell.send(:living_neighbors, neighbors)).to eq living_neighbors.size
    end
  end

  describe "#repopulate!" do
    # evaluate subject to avoid multiple calls on first evaluation in the test
    subject!(:cell) { [dead_cell, living_cell].sample }

    it "returns living cell" do
      expect(described_class).to receive(:new).with(hash_including(alive: true))
      cell.send(:repopulate!)
    end
  end

  describe "#survive!" do
    subject(:cell) { [dead_cell, living_cell].sample }

    it "returns self" do
      expect(cell.send(:survive!).object_id).to eq cell.object_id
    end
  end

  describe "#die!" do
    context "when dead" do
      subject(:cell) { dead_cell }

      it "returns self" do
        expect(cell.send(:die!).object_id).to eq cell.object_id
      end
    end

    context "when alive" do
      # evaluate subject to avoid multiple calls on first evaluation in the test
      subject!(:cell) { living_cell }

      it "returns new dead cell" do
        expect(described_class).to receive(:new).with(hash_including(alive: false))
        cell.send(:die!)
      end
    end
  end
end