From f0276273448d77982a1653cb3d77df9b1a0ef6e8 Mon Sep 17 00:00:00 2001 From: Hari Dahal Date: Mon, 22 Jan 2018 09:58:40 -0500 Subject: [PATCH] fixed the tree with TDD --- lib/tree.rb | 28 ++++++++++++++++++----- spec/tree_spec.rb | 57 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..cf9a8c1 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,26 +1,40 @@ class NoApplesError < StandardError; end -class AppleTree - attr_#fill_in :height, :age, :apples, :alive +class Tree + attr_reader :age, :apples, :alive + attr_accessor :height def initialize + @height = 0 + @age = 0 + @apples = [] + @alive = true end def age! + @age += 1 if @alive + @height += rand(0..2) if @alive + add_apples if @alive and @age > 5 + @alive = false if @age > 30 or @height > 30 end def add_apples + rand(2..30).times{ |x| @apples << Apple.new('green', rand(1..4))} end def any_apples? + not @apples.empty? end def pick_an_apple! - raise NoApplesError, "This tree has no apples" unless self.any_apples? + raise NoApplesError, "This tree has no apples" unless any_apples? + @apples.pop end def dead? + not @alive end + end class Fruit @@ -29,10 +43,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 +77,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.length # 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 99c9184..072b2b4 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,13 +2,68 @@ require 'tree' describe 'Tree' do + + let(:tree) { Tree.new } + it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true + expect(Tree.is_a? Class).to eq true + end + + it 'should have no apples initially' do + expect(tree.any_apples?).to eq false + end + + it 'should be able to add/grow apples' do + tree.add_apples + expect(tree.any_apples?).to eq true + end + + it 'dies after living for 30 years' do + 32.times { |x| tree.age! } + expect(tree.dead?).to eq true + end + + it 'dies when it is 30 feets height' do + tree.height = 31 + tree.age! + expect(tree.dead?).to eq true + end + + it 'should age' do + tree.age! + expect(tree.age).to eq 1 + end + + it 'should be able to pick an apple' do + 6.times { |x| tree.age! } + expect(tree.pick_an_apple!.is_a? Apple).to eq true + end + + it 'should raise problem when no apples' do + expect{tree.pick_an_apple!}.to raise_error NoApplesError end + end describe 'Fruit' do + it 'should be a Class' do + expect(Fruit.is_a? Class).to eq true + end end describe 'Apple' do + it 'should be a Class' do + expect(Apple.is_a? Class).to eq true + end + + it 'should be a Fruit Class' do + expect(Apple.superclass).to eq Fruit + end + + it 'has color and diameter' do + apple = Apple.new('green', 2) + expect(apple.color).to eq 'green' + expect(apple.diameter).to eq 2 + end + end