From 80382d9eeac0bb0a8f3c4633e82da8b4f1fd81f2 Mon Sep 17 00:00:00 2001 From: Joseph Himes Date: Sat, 9 Dec 2017 18:34:24 -0500 Subject: [PATCH] tree.rb runs, added a few minor tests. Need to work on testing --- Gemfile.lock | 4 ++++ lib/tree.rb | 29 +++++++++++++++++++++++------ spec/tree_spec.rb | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 90f2f48..a259867 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.16.0 diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..bd33a0a 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,25 +1,39 @@ class NoApplesError < StandardError; end -class AppleTree - attr_#fill_in :height, :age, :apples, :alive +class Tree + attr_accessor :height, :age, :apples, :alive def initialize + @height = 0 + @age = 0 + @apples = [] + @alive = true end def age! + @age += 1 + @height += 1 + 5.times do + add_apples + end end def add_apples + apple = Apple.new("Red", rand(1..10)) + apples.push(apple) end def any_apples? + @apples != [] end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop end def dead? + self.age > 20 end end @@ -29,13 +43,16 @@ def initialize end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :diameter, :color def initialize(color, diameter) + @color = color + @diameter = diameter end end + #THERES ONLY ONE THING YOU NEED TO EDIT BELOW THIS LINE # avg_diameter (line 58) will raise an error. # it should calculate the diameter of the apples in the basket @@ -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.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" @@ -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..665e37e 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,48 @@ require 'rspec' require 'tree' -describe 'Tree' do +describe Tree do it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true + expect(described_class.is_a? Class).to eq true + end + + it 'should have height' do + tree = Tree.new + expect(tree.height).to_not eq nil + end + + it 'should age' do + tree = Tree.new + tree.age! + expect(tree.height).to eq 1 + expect(tree.age).to eq 1 + expect(tree.apples).to_not eq [] + end + + it 'should add apples' do + tree = Tree.new + tree.add_apples + expect(tree.apples).to_not eq [] + end + + it 'should have apples' do + tree = Tree.new + tree.add_apples + expect(tree.any_apples?).to eq true end end -describe 'Fruit' do +describe Fruit do end -describe 'Apple' do +describe Apple do + it 'should be a Class' do + expect(described_class.is_a? Class).to eq true + end + + it 'should create a new Apple' do + apple = Apple.new("Red", 5) + expect(apple.color).to eq "Red" + expect(apple.diameter).to eq 5 + end end