diff --git a/Gemfile.lock b/Gemfile.lock index 90f2f48..8e77bde 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: https://rubygems.org/ specs: diff-lcs (1.2.5) rspec (3.1.0) @@ -19,3 +20,6 @@ PLATFORMS DEPENDENCIES rspec + +BUNDLED WITH + 1.15.1 diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..390b156 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 +class Tree + attr_reader :height, :age, :apples, :alive def initialize + @height = 0 + @age = 0 + @apples = [] + @alive = true end def age! + @age = age + 1 + @height = height + 1 + @alive = false if @age > 25 + add_apples end def add_apples + apple = 12.times { @apples.push Apple.new("Red", Random.rand(1..6)) } #creates 12 Red apples with ramdom diameters each year end def any_apples? + @apples.length > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop #to remove it 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 def initialize(color, diameter) + @color = color + @diameter = diameter + super() end end @@ -61,7 +78,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" @@ -76,4 +93,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 99c9184..cbb9f79 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,79 @@ require 'rspec' require 'tree' -describe 'Tree' do +tree = Tree.new + +describe Tree do it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true + expect(Tree.is_a? Class).to eq true + end + + context 'when grows one foot' do + it "equals 1" do + tree.age! + expect(tree.height).to eq(1) + end + end + + context 'when ages one year' do + it "equals 1" do + expect(tree.age).to eq(1) + end + end + + context 'when has no fruit' do + it "equals true" do + expect(tree.any_apples?).to eq true + end + end + + context 'when has apples after 3 years' do + it "equals true" do + tree.age! + expect(tree.any_apples?).to be true + end + end + + context 'when dies after 25 years' do + it "equals false" do + (1..13).each{ |idx| tree.age!} + expect(tree.dead?).to be false + end end end -describe 'Fruit' do +describe Fruit do + it 'should be a Class' do + expect(Fruit.is_a? Class).to eq true + end end -describe 'Apple' do +apple = Apple.new("Red", 30) + +describe Apple do + it 'should be a Class' do + expect(Apple.is_a? Class).to eq true + end + + it 'is a Fruit' do + expect(Apple < Fruit).to be true # expects to be inheriting from apple if it is a fruit + end + + context 'when sets a color' do + it "equals red" do + expect(apple.color).to eq("Red") + end + end + + context 'when sets a diameter' do + it "equals 30" do + expect(apple.diameter).to eq(30) + end + end + + context 'when calls super()' do + it "equals true" do + expect(apple.has_seeds).to eq true + end + end end