From 6d22b72b10560640b919ea1c06c97a07ba708e97 Mon Sep 17 00:00:00 2001 From: Leah Musie Date: Tue, 17 Jan 2017 14:06:08 -0500 Subject: [PATCH 1/2] added tests and fixed deliberate errors in tree.rb --- .DS_Store | Bin 0 -> 6148 bytes Gemfile.lock | 4 +++ lib/tree.rb | 32 +++++++++++++---- spec/tree_spec.rb | 90 +++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0075b98d822fb6b020719c5f424af538b498181f GIT binary patch literal 6148 zcmeHKF=_)r43uma1~)EK?iccd#W*kU2aN3$fddCj`m6G;JS{Vl5@L>!#%at5w6m+# z>~d3_R%Yg#x6`B9+RT=4qP=sN8u#fFn^lD9JmWYV&gX&AMQ!~F5 + @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" 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 Date: Tue, 17 Jan 2017 14:16:19 -0500 Subject: [PATCH 2/2] ran with tree_data uncommented after running tests --- lib/tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tree.rb b/lib/tree.rb index c55d5c8..ed935b1 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -96,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