From fe20671323dea7f9b5d6625b79b8cff2077eeafb Mon Sep 17 00:00:00 2001 From: David Hatten Date: Fri, 19 Jan 2018 14:08:08 -0500 Subject: [PATCH 1/6] Tests for tree growing --- spec/tree_spec.rb | 72 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 99c9184..3c946f2 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,76 @@ require 'rspec' require 'tree' -describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true +describe AppleTree do + context 'when AppleTree exists' do + subject(:tree) { AppleTree.new } + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should be a newborn tree' do + expect(tree.age).to eq 0 + end + + it 'should have no apples' do + expect(tree.any_apples?).to eq false + end + + it 'should be alive' do + expect(tree.alive).to eq true + end + + it 'should age when told to' do + expect(tree.age!).to eq 1 + end + + it 'should get taller as it ages' do + tree.age! + expect(tree.height).to be > 0 + end + + it 'should die after 10 years' do + 10.times { + tree.age! + } + expect(tree.dead?).to eq true + end + + it 'should produce fruit after 1 year' do + tree.age! + expect(tree.any_apples?).to eq true + end + + it 'should let me pick an apple' do + tree.age! + expect(tree.pick_an_apple!).to respond_to :diameter + end end + end -describe 'Fruit' do +describe Fruit do + subject(:fruit) {Fruit.new} + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should have seeds' do + expect(fruit.has_seeds).to eq true + end end -describe 'Apple' do +describe Apple do + subject(:apple){Apple.new('Red', 5)} + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should have a color' do + expect(apple.color).to eq 'Red' + end + + it 'should have a diameter' do + expect(apple.diameter).to eq 5 + end end From 535d3d88adbacf0de22ced111147a438c73becdf Mon Sep 17 00:00:00 2001 From: David Hatten Date: Fri, 19 Jan 2018 14:08:27 -0500 Subject: [PATCH 2/6] A healthy apple tree object --- lib/tree.rb | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..2b96a7e 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,55 @@ class NoApplesError < StandardError; end class AppleTree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive def initialize + @height = 0 + @age = 0 + @apples = 0 + @alive = true end def age! + @age+=1 + @alive = false unless @age < 9 + @height+=rand(20) + rand(10).times {self.add_apples} if @age > 0 + @age end def add_apples + @apples+=1 end def any_apples? + @apples != 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples-=1 + Apple.new('Red', rand(100)) end def dead? + !@alive end end class Fruit + attr_reader :has_seeds def initialize - has_seeds = true + @has_seeds = true end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color, :diameter #what should go here def initialize(color, diameter) + @color = color + @diameter = diameter end end @@ -41,7 +58,7 @@ def initialize(color, diameter) # it should calculate the diameter of the apples in the basket def tree_data - tree = Tree.new + tree = AppleTree.new tree.age! until tree.any_apples? @@ -55,13 +72,14 @@ def tree_data basket << tree.pick_an_apple! end + diameter_sum = 0 basket.each do |apple| 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.count puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" From 9ab7d4d7373a543095fd3b4f87043705854fb0d4 Mon Sep 17 00:00:00 2001 From: David Hatten Date: Fri, 19 Jan 2018 14:40:18 -0500 Subject: [PATCH 3/6] added ranges to rand calls --- lib/tree.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 2b96a7e..49bfa8a 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -14,7 +14,7 @@ def age! @age+=1 @alive = false unless @age < 9 @height+=rand(20) - rand(10).times {self.add_apples} if @age > 0 + rand(1..10).times {self.add_apples} @age end @@ -29,7 +29,7 @@ def any_apples? def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? @apples-=1 - Apple.new('Red', rand(100)) + Apple.new('Red', rand(1..100)) end def dead? From 19ef714c35df15a60186efbe1032296a021f0b5a Mon Sep 17 00:00:00 2001 From: David Hatten Date: Fri, 19 Jan 2018 14:40:52 -0500 Subject: [PATCH 4/6] whitespace I guess? --- spec/tree_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 3c946f2..bdf80ac 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -16,6 +16,10 @@ expect(tree.any_apples?).to eq false end + it 'should not be able to have an apple picked' do + expect{tree.pick_an_apple!}.to raise_error NoApplesError + end + it 'should be alive' do expect(tree.alive).to eq true end From e86de669fc279ae78a7401989674dcb65cd05546 Mon Sep 17 00:00:00 2001 From: David Hatten Date: Fri, 19 Jan 2018 14:42:52 -0500 Subject: [PATCH 5/6] Refactored how apples get added --- lib/tree.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 49bfa8a..8b25e64 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -14,12 +14,12 @@ def age! @age+=1 @alive = false unless @age < 9 @height+=rand(20) - rand(1..10).times {self.add_apples} + self.add_apples @age end def add_apples - @apples+=1 + rand(25..100).times{@apples+=1} end def any_apples? @@ -94,4 +94,4 @@ def tree_data end # Uncomment this line to run the script, but BE SURE to comment it before you try to run your tests! -# tree_data +tree_data From 9093118935719e731e19d8a352ed106a6a8d2fa0 Mon Sep 17 00:00:00 2001 From: David Hatten Date: Fri, 19 Jan 2018 14:51:57 -0500 Subject: [PATCH 6/6] changing apple diameter to be sensible --- lib/tree.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 8b25e64..8874240 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -29,7 +29,7 @@ def any_apples? def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? @apples-=1 - Apple.new('Red', rand(1..100)) + Apple.new('Red', rand(3.0..5.0)) end def dead? @@ -94,4 +94,4 @@ def tree_data end # Uncomment this line to run the script, but BE SURE to comment it before you try to run your tests! -tree_data +# tree_data