From 2bcc56d15a18ce0b3e78cfeb54bc5836f2d48188 Mon Sep 17 00:00:00 2001 From: Erik Thiem Date: Fri, 19 May 2017 10:20:58 -0400 Subject: [PATCH 1/2] Got some initial tests to pass after fixing small errors --- lib/tree.rb | 15 ++++++++++----- spec/tree_spec.rb | 23 +++++++++++++++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..87347de 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,9 +1,13 @@ class NoApplesError < StandardError; end -class AppleTree - attr_#fill_in :height, :age, :apples, :alive +class Tree + attr_reader :height + attr_reader :age + attr_reader :apples + attr_reader :alive def initialize + @age = 0 end def age! @@ -29,8 +33,9 @@ def initialize end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color + attr_reader :diameter def initialize(color, diameter) end @@ -61,7 +66,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 / tree.apples.count.to_f puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 99c9184..10c9ec4 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,25 @@ require 'rspec' require 'tree' -describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true - end +describe Tree do + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should be a newborn when planted' do + tree = Tree.new + expect(tree.age).to eq 0 + end end -describe 'Fruit' do +describe Fruit do + it 'should be a Class' do + expect(described_class.is_a?(Class)).to be true + end end -describe 'Apple' do +describe Apple do + it 'should be a Class' do + expect(described_class.is_a?(Class)).to be true + end end From e03e52256fd55d92b6e3c254ab1e2ca6480252ca Mon Sep 17 00:00:00 2001 From: Erik Thiem Date: Fri, 19 May 2017 14:16:09 -0400 Subject: [PATCH 2/2] Implemented tests and methods --- lib/tree.rb | 42 +++++++++++++++++++++++--- spec/tree_spec.rb | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 5 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 87347de..64576cb 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -5,32 +5,62 @@ class Tree attr_reader :age attr_reader :apples attr_reader :alive + attr_reader :treeAppleColor def initialize @age = 0 + @height = 1 + @apples = Array.new + @alive = true + @treeAppleColor = "Red" end def age! + @age += 1 + + self.add_apples + self.grow + + if @age >= 50 + @alive = false + end + end + + def grow + @height += Random.new.rand(1..10) end def add_apples + numApples = Random.new.rand(1..20) * @height + + numApples.times do + diameter = Random.new.rand(2.0..4.0) + apple = Apple.new(@treeAppleColor, diameter) + @apples << apple + end end def any_apples? + return @apples.count > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + + apple = @apples.delete_at(rand(@apples.length)) + return apple end def dead? + return !@alive end end class Fruit - def initialize - has_seeds = true - end + attr_reader :has_seeds + def initialize + @has_seeds = true + end end class Apple < Fruit @@ -38,6 +68,8 @@ class Apple < Fruit attr_reader :diameter def initialize(color, diameter) + @color = color + @diameter = diameter end end @@ -66,7 +98,7 @@ def tree_data diameter_sum += apple.diameter end - avg_diameter = diameter_sum / tree.apples.count.to_f + avg_diameter = diameter_sum / basket.count.to_f puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -81,4 +113,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 10c9ec4..8604379 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,24 +2,101 @@ require 'tree' describe Tree do + it 'should be a Class' do expect(described_class.is_a? Class).to be true end it 'should be a newborn when planted' do tree = Tree.new + expect(tree.age).to eq 0 end + + it 'should have zero apples when planted' do + tree = Tree.new + + expect(tree.any_apples?).to eq false + end + + it 'should be alive when planted' do + tree = Tree.new + + expect(tree.dead?).to eq false + end + + it 'should age a year' do + tree = Tree.new + tree.age! + + expect(tree.age).to eq 1 + end + + it 'should die after 50 years' do + tree = Tree.new + 50.times do tree.age! end + + expect(tree.dead?).to eq true + end + + it 'should add at least one apple each times it ages' do + tree = Tree.new + startApples = tree.apples.count + tree.age! + endApples = tree.apples.count + + expect(endApples).to be > startApples + end + + it 'should decrease by one apple when an apple is picked' do + tree = Tree.new + tree.age! + startApples = tree.apples.count + tree.pick_an_apple! + endApples = tree.apples.count + + expect(endApples).to eq (startApples-1) + end + + it 'should give an apple when one is picked' do + tree = Tree.new + tree.age! + apple = tree.pick_an_apple! + + expect(apple).to be_instance_of(Apple) + end + + it 'should grow a positive value each year' do + tree = Tree.new + startHeight = tree.height + tree.age! + endHeight = tree.height + + expect(endHeight).to be > startHeight + end end describe Fruit do it 'should be a Class' do expect(described_class.is_a?(Class)).to be true end + + it 'should have seeds' do + fruit = Fruit.new + expect(fruit.has_seeds).to eq true + end end describe Apple do it 'should be a Class' do expect(described_class.is_a?(Class)).to be true end + + it 'should set the color and diameter properties' do + color = "blue" + diameter = 25.3 + apple = Apple.new(color, diameter) + expect(apple.color).to eq color + expect(apple.diameter).to eq diameter + end end