From ec490290fef07e7dad664f93bc3affab1e4bc848 Mon Sep 17 00:00:00 2001 From: Lucas Nestor Date: Fri, 11 May 2018 09:38:16 -0400 Subject: [PATCH 1/2] Initial commit --- lib/tree.rb | 33 +++++++++++-- spec/tree_spec.rb | 123 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 150 insertions(+), 6 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..c381b38 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,25 +1,48 @@ class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive def initialize + self.height ||= 0 + self.age ||= 0 + self.apples ||= [] + self.alive = true end def age! + if self.age < 50 + self.height += 1 + end + + if self.age == 99 + self.alive = false + else + add_apples + end + + self.age += 1 end def add_apples + 5.times do + diameter = Random.rand(2..4) + color = 'red' + apples[apples.length] = Apple.new(color, diameter) + end end def any_apples? + !apples.empty? end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + apples.shift end def dead? + !self.alive end end @@ -29,10 +52,12 @@ def initialize end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color, :diameter def initialize(color, diameter) + @color = color + @diameter = diameter end end @@ -61,7 +86,7 @@ def tree_data diameter_sum += apple.diameter end - avg_diameter = # It's up to you to calculate the average diameter for this harvest. + avg_diameter = diameter_sum / basket.size puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index b4f44c6..124b1c5 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,13 +2,132 @@ require_relative '../lib/tree' describe Tree do + let(:tree) { Tree.new } + + it 'is a brand new tree' do + expect(tree.age).to eq 0 + expect(tree.height).to eq 0 + expect(tree.alive).to be_truthy + expect(tree.apples).to be_empty + end + it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + + describe '#age!' do + it 'increases the trees age' do + expect { tree.age! }.to change { tree.age }.by 1 + end + + context 'when the tree is young' do + before { tree.age = 5 } + + it 'increases the trees height' do + expect { tree.age! }.to change { tree.height }.by 1 + end + + it 'adds apples to the tree' do + expect { tree.age! }.to change { tree.apples.count }.by 5 + end + end + + context 'when the tree is old' do + before { tree.age = 55 } + + it 'does not increase the trees height' do + expect { tree.age! }.to_not change { tree.height } + end + + it 'adds apples to the tree' do + expect { tree.age! }.to change { tree.apples.count }.by 5 + end + end + + context 'when the tree is about to die' do + before { tree.age = 99 } + + it 'does not increase the trees height' do + expect { tree.age! }.to_not change { tree.height } + end + + it 'does not add apples to the tree' do + expect { tree.age! }.to_not change { tree.apples.count } + end + + it 'kills the tree' do + tree.age! + expect(tree.alive).to be_falsey + end + end + end + + describe '#add_apples' do + it 'adds 5 apples to the tree' do + expect { tree.add_apples }.to change { tree.apples.count }.by 5 + end + end + + describe '#any_apples?' do + context 'when the tree doesnt have apples' do + before { tree.apples = [] } + + it 'returns false' do + expect(tree.any_apples?).to be_falsey + end + end + + context 'when the tree does have apples' do + before { tree.apples = [Apple.new('red', 2)]} + + it 'returns true' do + expect(tree.any_apples?).to be_truthy + end + end + end + + describe '#pick_an_apple!' do + before { tree.add_apples } + + it 'returns an apple object' do + expect(tree.pick_an_apple!).to be_instance_of Apple + end + + it 'removes the apple from the tree' do + expect { tree.pick_an_apple! }.to change { tree.apples.count } .by -1 + end + end + + describe '#dead?' do + context 'when the tree is alive' do + before { tree.alive = true } + + it 'returns it is alive' do + expect(tree.dead?).to be_falsey + end + end + + context 'when the tree is dead' do + before { tree.alive = false } + + it 'returns it is dead' do + expect(tree.dead?).to be_truthy + end + end + end end -describe 'Fruit' do +describe Fruit do end -describe 'Apple' do +describe Apple do + let(:apple) { Apple.new('red', 3) } + it 'is an instance of Fruit' do + expect(apple).to be_a Fruit + end + + it 'has a color and diameter' do + expect(apple.color).to eq 'red' + expect(apple.diameter).to eq 3 + end end From ce862e81e25c5d59527cb3d54023e2a5519f08d5 Mon Sep 17 00:00:00 2001 From: Lucas Nestor Date: Fri, 11 May 2018 12:37:43 -0400 Subject: [PATCH 2/2] Finish challenge --- spec/tree_spec.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 124b1c5..031ee2d 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -118,16 +118,21 @@ end describe Fruit do + # I don't really use the fruit class at all end describe Apple do - let(:apple) { Apple.new('red', 3) } - it 'is an instance of Fruit' do + let(:apple) { Apple.new('red', 2.5) } + + it 'should be a Fruit' do expect(apple).to be_a Fruit end - it 'has a color and diameter' do + it 'should have a color' do expect(apple.color).to eq 'red' - expect(apple.diameter).to eq 3 + end + + it 'should have a diameter' do + expect(apple.diameter).to eq 2.5 end end