Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (3.1.0)
Expand All @@ -19,3 +20,6 @@ PLATFORMS

DEPENDENCIES
rspec

BUNDLED WITH
1.16.1
35 changes: 27 additions & 8 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
class NoApplesError < StandardError; end


class AppleTree
attr_#fill_in :height, :age, :apples, :alive
attr_accessor :height, :age, :apples, :alive

def initialize
@height = 0
@age = 0
@apples = []
@alive = true
end

def age!
@age += 1
@height += rand(1..(2*@age))
self.add_apples
end

def add_apples
rand(1..(4*@age)).times do
@apples << Apple.new
end
end

def any_apples?
@apples.empty? ? false : true
end

def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
@apples.slice!(rand(0...@apples.length))
end

def dead?
@height > 60 || @age > 13
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)
def initialize
colors = ['red', 'green', 'yellow']
@color = colors.sample
@diameter = rand(1..5).to_f
end
end

Expand All @@ -41,7 +60,7 @@ def initialize(color, diameter)
# it should calculate the diameter of the apples in the basket

def tree_data
tree = Tree.new
tree = AppleTree.new

tree.age! until tree.any_apples?

Expand All @@ -61,7 +80,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).round(2)

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand All @@ -76,4 +95,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
54 changes: 49 additions & 5 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
require 'rspec'
require 'tree'

describe 'Tree' do
it 'should be a Class' do
expect(described_class.is_a? 'Class').to be_true
describe AppleTree do
let(:tree) { AppleTree.new }

it { expect(tree.class).to be AppleTree }

it 'will age' do
tree.age = rand(1..10)
previous_tree_age = tree.age
tree.age!
expect(tree.age).to eq (previous_tree_age + 1)
end

it 'can add apples' do
tree.age = 4
tree.add_apples
expect(tree.any_apples?).to be true
end

context 'when picking apples' do

it 'raises an error for no apples' do
expect { tree.pick_an_apple! }.to raise_error(NoApplesError, "This tree has no apples")
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test.


it 'removes an apple when present' do
tree.age = rand(1..12)
tree.add_apples
remaining_apples_count = tree.apples.length - 1
tree.pick_an_apple!
expect(tree.apples.length).to eq remaining_apples_count
end

end
end

describe 'Fruit' do
describe Fruit do
let(:fruit) { Fruit.new }

it { expect(fruit.class).to be Fruit }

it { expect(fruit.has_seeds).to be true }
end

describe 'Apple' do
describe Apple do
let(:apple) { Apple.new }

it "should have a color" do
colors = ['red', 'green', 'yellow']
expect(colors).to include apple.color
end

it 'should have a diameter' do
expect(apple.diameter).to be_between(1,5).inclusive
end
end