Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial add of TreeTDD project #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
42 changes: 34 additions & 8 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
class NoApplesError < StandardError; end
class DeadTreeError < StandardError; end

class Tree
attr_#fill_in :height, :age, :apples, :alive
attr_reader :height, :age, :apples, :alive

def initialize
DEATH_AGE = 60
FRUIT_AGE = 5

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

def age!
end
raise DeadTreeError, "This tree is dead" unless self.alive

def add_apples
@age += 1
@height += rand(2..4)
@alive = @age < DEATH_AGE

add_apples if @alive && @age >= FRUIT_AGE
end

def any_apples?
@apples.any?
end

def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?

@apples.pop
end

def dead?
!@alive
end

def add_apples
apples_to_add = rand(10..15)

apples_to_add.times do
@apples << Apple.new('red', rand(1..4))
end
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

def initialize(color, diameter)
@color = color
@diameter = diameter
end
end

Expand Down Expand Up @@ -61,7 +87,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.to_f / basket.length

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand Down
80 changes: 78 additions & 2 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,86 @@
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end

describe '#age!' do
subject { tree }

context 'new tree' do
let(:tree) { Tree.new }

it 'should age 1 years' do
tree.age!
expect(tree.age).to eq 1
end

it 'should grow at least 2 feet a year' do
tree.age!
expect(tree.height).to be >= 2
end
end

context 'a fruit bearing tree' do
let(:tree) { Tree.new(0, Tree::FRUIT_AGE, [], true) }

it 'should add apples every year' do
tree.age!
expect(tree.any_apples?).to eq true
end
end

context 'almost dead tree' do
let(:tree) { Tree.new(0, Tree::DEATH_AGE - 1, [], true) }

it "should die when it when hits #{Tree::DEATH_AGE} years old" do
tree.age!
expect(tree.dead?).to eq true
end
end

context 'a dead tree' do
let(:tree) { Tree.new(0, Tree::DEATH_AGE, [], false) }

it 'should throw an error if aged after tree is dead' do
expect { tree.age! }.to raise_error DeadTreeError
end
end
end

describe '#pick_an_apple!' do
subject { tree }

context 'tree without apples' do
let(:tree) { Tree.new }

it 'should throw an error' do
expect { tree.pick_an_apple! }.to raise_error NoApplesError
end
end

context 'tree with one apple' do
let(:tree) { Tree.new(0, Tree::FRUIT_AGE, [Apple.new('red', 2)], true) }

it 'should return the last apple' do
picked_apple = tree.pick_an_apple!
expect(picked_apple.color).to eq 'red'
end

it 'should remove the apple from the tree' do
tree.pick_an_apple!
expect(tree.any_apples?).to eq false
end
end
end
end

describe 'Fruit' do
describe Fruit do
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end
end

describe 'Apple' do
describe Apple do
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end
end