From 9c5624e0f7edb235e903bd503baf7d4e934ac5cd Mon Sep 17 00:00:00 2001 From: Nick Caley Date: Mon, 5 Mar 2018 09:46:29 -0500 Subject: [PATCH 1/2] Completed Tree with tests --- lib/tree.rb | 48 +++++++++++++++++++++---- spec/tree_spec.rb | 90 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 8 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..4ea1dea 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,41 +1,77 @@ class NoApplesError < StandardError; end +class DeadTreeError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive - def initialize + attr_reader :height + attr_reader :age + attr_reader :apples + attr_accessor :alive + + def initialize(height = 0, age = 0, apples = [], alive = true) + @height = height + @age = age + @apples = apples + @alive = alive end def age! + raise DeadTreeError, "This tree is dead" if self.dead? + @age += 1 + + # Simple check if a tree has become too old + if @age >= 10 + @alive = false + end + + # What to do when a tree is alive and is aging + if @alive + @height += 1 + add_apples + end + end def add_apples + raise DeadTreeError, "This tree is dead" if self.dead? + # Change this if you want to change how apples are added/removed + count = @height + count.times { @apples.push(Apple.new('red', 1)) } end def any_apples? + @apples.size > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.shift end def dead? + @alive == false 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 def initialize(color, diameter) + super() + @color = color + @diameter = diameter end end + #THERES ONLY ONE THING YOU NEED TO EDIT BELOW THIS LINE # avg_diameter (line 58) will raise an error. # it should calculate the diameter of the apples in the basket @@ -61,7 +97,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.to_f # It's up to you to calculate the average diameter for this harvest. puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 6394020..93d1084 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,100 @@ require 'rspec' require 'tree' +def kill_tree(tree) + 10.times { tree.age! } +end + describe Tree do it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + + describe '#pick_an_apple!' do + it 'should raise when there are no apples' do + tree = Tree.new + expect { tree.pick_an_apple! }.to raise_error(NoApplesError) + end + + it 'shoud remove an apple' do + tree = Tree.new + tree.age! + tree.pick_an_apple! + expect(tree.any_apples?).to eq false + end + end + + describe '#any_apples?' do + it 'should return true when there are apples' do + tree = Tree.new + tree.age! + expect(tree.any_apples?).to eq true + end + + it 'should return false when there are not apples' do + tree = Tree.new + expect(tree.any_apples?).to eq false + end + end + + describe '#age!' do + it 'should increase the age of the tree' do + tree = Tree.new + tree.age! + expect(tree.age).to eq 1 + end + + it 'should increase the height of the tree' do + tree = Tree.new + tree.age! + expect(tree.height).to eq 1 + end + + it 'should raise when the tree is dead' do + tree = Tree.new + kill_tree(tree) + expect { tree.age! }.to raise_error(DeadTreeError) + end + end + + describe '#dead?' do + it 'should return true when the tree is not alive' do + tree = Tree.new + kill_tree(tree) + expect(tree.dead?).to eq true + end + + it 'should return false when the tree is alive' do + tree = Tree.new + expect(tree.dead?).to eq false + end + end + + describe '#add_apples' do + it 'should raise when the tree is dead' do + tree = Tree.new + kill_tree(tree) + expect { tree.add_apples }.to raise_error(DeadTreeError) + end + + it 'should add apples ' do + tree = Tree.new + expect(tree.any_apples?).to eq false + tree.age! + expect(tree.any_apples?).to eq true + end + end + end -describe 'Fruit' do +describe Fruit do + end -describe 'Apple' do +describe Apple do + it 'should have seeds when created' do + apple = Apple.new(1, 'blue') + has_seeds = apple.has_seeds + expect(has_seeds).to eq true + end end From 9000a309910a9e9fbbeb031092cf69d45605ff9c Mon Sep 17 00:00:00 2001 From: Nick Caley Date: Mon, 5 Mar 2018 13:32:44 -0500 Subject: [PATCH 2/2] Add in some randomness to trees and update tests. --- lib/tree.rb | 38 ++++++++++++++++++-------- spec/tree_spec.rb | 69 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 4ea1dea..00fa947 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -3,40 +3,46 @@ class DeadTreeError < StandardError; end class Tree - attr_reader :height - attr_reader :age - attr_reader :apples + attr_reader :height, :age, :apples attr_accessor :alive - def initialize(height = 0, age = 0, apples = [], alive = true) + def initialize(height: 0, age: 0, apples: [], alive: true, rand_seed: nil) @height = height @age = age @apples = apples @alive = alive + @prng = Random.new(rand_seed || Random.new_seed) end def age! raise DeadTreeError, "This tree is dead" if self.dead? @age += 1 - # Simple check if a tree has become too old - if @age >= 10 + + # Simple check if a tree has become too old. + if @age >= 10 && age + @prng.rand(0..10) > 20 @alive = false end # What to do when a tree is alive and is aging if @alive - @height += 1 + inc_height! add_apples end + end + def inc_height! + @height += @prng.rand(1..3) end def add_apples raise DeadTreeError, "This tree is dead" if self.dead? - # Change this if you want to change how apples are added/removed - count = @height - count.times { @apples.push(Apple.new('red', 1)) } + + count = @height * @prng.rand(1..3) + color = Apple.colors.sample + diameter = @prng.rand(4..8) + + count.times { @apples.push(Apple.new(color, diameter)) } end def any_apples? @@ -53,6 +59,7 @@ def dead? end end + class Fruit attr_reader :has_seeds @@ -61,9 +68,18 @@ def initialize end end + class Apple < Fruit attr_reader :color, :diameter + @@colors = ['red', 'yellow', 'green'] + + class << self + def colors + @@colors + end + end + def initialize(color, diameter) super() @color = color @@ -112,4 +128,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 diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 93d1084..72e6883 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,7 +2,13 @@ require 'tree' def kill_tree(tree) - 10.times { tree.age! } + # The 12 here is based off of the rand_seed the below + 12.times { tree.age! } +end + +def new_test_tree + # Use a random seed here to make our tests predictable + tree = Tree.new(rand_seed: 100) end describe Tree do @@ -12,12 +18,12 @@ def kill_tree(tree) describe '#pick_an_apple!' do it 'should raise when there are no apples' do - tree = Tree.new + tree = new_test_tree expect { tree.pick_an_apple! }.to raise_error(NoApplesError) end it 'shoud remove an apple' do - tree = Tree.new + tree = new_test_tree tree.age! tree.pick_an_apple! expect(tree.any_apples?).to eq false @@ -26,75 +32,106 @@ def kill_tree(tree) describe '#any_apples?' do it 'should return true when there are apples' do - tree = Tree.new + tree = new_test_tree tree.age! expect(tree.any_apples?).to eq true end it 'should return false when there are not apples' do - tree = Tree.new + tree = new_test_tree expect(tree.any_apples?).to eq false end end describe '#age!' do it 'should increase the age of the tree' do - tree = Tree.new + tree = new_test_tree tree.age! expect(tree.age).to eq 1 end it 'should increase the height of the tree' do - tree = Tree.new + tree = new_test_tree tree.age! expect(tree.height).to eq 1 end it 'should raise when the tree is dead' do - tree = Tree.new + tree = new_test_tree kill_tree(tree) expect { tree.age! }.to raise_error(DeadTreeError) end + + it 'should set alive to false when the tree is too old' do + tree = new_test_tree + kill_tree(tree) + expect(tree.age).to eq 12 + expect(tree.dead?).to eq true + end + + it 'should grow in height' do + tree = new_test_tree + previous_height = tree.height + tree.age! + expect(tree.height).to be > previous_height + end + + it 'should grow more apples' do + tree = new_test_tree + previous_count = tree.apples.size + tree.age! + expect(tree.apples.size).to be > previous_count + end end describe '#dead?' do it 'should return true when the tree is not alive' do - tree = Tree.new + tree = new_test_tree kill_tree(tree) expect(tree.dead?).to eq true end it 'should return false when the tree is alive' do - tree = Tree.new + tree = new_test_tree expect(tree.dead?).to eq false end end describe '#add_apples' do it 'should raise when the tree is dead' do - tree = Tree.new + tree = new_test_tree kill_tree(tree) expect { tree.add_apples }.to raise_error(DeadTreeError) end it 'should add apples ' do - tree = Tree.new + tree = new_test_tree expect(tree.any_apples?).to eq false tree.age! expect(tree.any_apples?).to eq true end end + describe '#inc_height!' do + it 'should increase the tree\'s height' do + tree = new_test_tree + tree.age! + expect(tree.height).to eq 1 + end + end + end describe Fruit do - + it 'should have seeds when created' do + f = Fruit.new + expect(f.has_seeds).to eq true + end end describe Apple do it 'should have seeds when created' do - apple = Apple.new(1, 'blue') - has_seeds = apple.has_seeds - expect(has_seeds).to eq true + f = Apple.new('red', 1) + expect(f.has_seeds).to eq true end end