diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..0075b98 Binary files /dev/null and b/.DS_Store differ diff --git a/Gemfile.lock b/Gemfile.lock index 90f2f48..ee48ab5 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.13.7 diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..ed935b1 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,58 @@ 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=Array.new + @alive=true end def age! + if @age>5 + @alive=false + @apples.clear + else + @age+=1 + self.add_apples + @height+=1 + end end def add_apples + new_apple= Apple.new("red",2) + (1+Random.new.rand(5)).times{@apples << new_apple} end def any_apples? + @apples.count!=0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop end def dead? + !@alive end end class Fruit 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, :has_seeds def initialize(color, diameter) + @color=color + @diameter=diameter + super() end end @@ -61,7 +81,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.count puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +96,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..71cf1c2 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,94 @@ require 'rspec' require 'tree' -describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true +#run with rspec spec/tree_spec +shared_examples "class_check" do |tree_class| + it 'is a class' do + expect(tree_class.is_a? Class).to be true end end -describe 'Fruit' do +describe Tree do + before(:each) do + @tree=Tree.new + end + + include_examples "class_check", described_class + + it 'should be alive when born' do + expect(@tree.dead?).to be false + end + + it 'should have no apples at the start' do + expect(@tree.any_apples?).to be false + end + + it 'should have no height when born' do + expect(@tree.height==0) + end + + it 'should raise an error when popping no apples' do + expect(@tree.any_apples?).to be false + expect{@tree.pick_an_apple!}.to raise_error NoApplesError + end + + it 'should increase apples as it ages' do + apple_count_before = @tree.apples.count + @tree.age! + apple_count_after = @tree.apples.count + expect(apple_count_before