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

Levi's solution #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--require ./spec/spec_helper.rb
--format doc
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
49 changes: 40 additions & 9 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,69 @@
class NoApplesError < StandardError; end
class DeadTreeError < StandardError; end

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

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

def age!
raise DeadTreeError, "This tree is dead" unless not self.dead?
@age += 1
@height += 1 if @height < 25
add_apples if @age > 5
kill! if @age > 200
end

def add_apples
raise DeadTreeError, "This tree is dead" unless not self.dead?
Copy link
Member

Choose a reason for hiding this comment

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

unless not == if

rand(50..100).times { @apples << Apple.new(color: "red", diameter: rand(2..4)) }
end

def any_apples?
@apples.count > 0
end

def pick_an_apple!
raise DeadTreeError, "This tree is dead" unless not self.dead?
raise NoApplesError, "This tree has no apples" unless self.any_apples?
@apples.shift
end

def dead?
!@alive
end

def kill!
@alive = false
@apples = []
end
end

class Fruit
def initialize
has_seeds = true
attr_reader :has_seeds

def initialize(has_seeds: true)
@has_seeds = has_seeds
end

def has_seeds?
@has_seeds
end
end

class Apple <
attr_reader #what should go here
class Apple < Fruit
attr_reader :color, :diameter

def initialize(color: "red", diameter: 3)
super()

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

Expand Down Expand Up @@ -61,7 +92,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"
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.warnings = true
end
159 changes: 155 additions & 4 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,165 @@
require 'rspec'
require 'tree'

describe 'Tree' do
def apple(params = {})
defaults = {
color: "red",
diameter: 3
}

Apple.new(**defaults.merge(params))
end

def tree(params = {})
defaults = {
height: 20,
age: 10,
apples: [apple(diameter: 3),
apple(diameter: 2),
apple(diameter: 3)],
alive: true
}

Tree.new(**defaults.merge(params))
end

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 be true
end

it 'has a height' do
expect(tree(height: 15).height).to eq(15)
end

it 'has an age' do
expect(tree(age: 75).age).to eq(75)
end

context 'is alive' do
subject { tree }

it 'can have apples' do
expect(tree.any_apples?).to be true
end

it 'can have no apples' do
expect(tree(apples: []).any_apples?).to be false
end

it 'can grow apples' do
original_apples_count = subject.apples.count
subject.add_apples
new_apples_count = subject.apples.count
expect(original_apples_count).to be < new_apples_count
end

context 'has apples' do
it 'can be picked' do
picked_apple = subject.pick_an_apple!
expect(picked_apple.class).to eq(Apple)
end
end

context 'has no apples' do
subject { tree(apples: []) }

it 'cannot be picked' do
expect { subject.pick_an_apple! }.to raise_error(NoApplesError)
end
end

context 'is less than 25 feet tall' do
it 'can grow taller' do
original_tree_height = subject.height
subject.age!
new_tree_height = subject.height
expect(original_tree_height).to be < new_tree_height
end
end

context 'is 25 feet tall' do
subject { tree(height: 25) }

it 'cannot grow taller' do
original_tree_height = subject.height
subject.age!
new_tree_height = subject.height
expect(original_tree_height).to eq new_tree_height
end
end

context 'is less than 200 years old' do
it 'can grow older' do
original_tree_age = subject.age
subject.age!
new_tree_age = subject.age
expect(original_tree_age).to be < new_tree_age
end
end

context 'is 200 years old' do
subject { tree(age: 200) }

it 'dies' do
subject.age!
expect(subject.dead?).to be true
end
end
end

before(:context) do
@dead_tree = tree
@dead_tree.kill!
end

context 'is dead' do
it 'cannot age' do
expect { @dead_tree.age! }.to raise_error(DeadTreeError)
end

it 'has no apples' do
expect(@dead_tree.apples.count).to eq(0)
end

it 'cannot grow apples' do
expect { @dead_tree.add_apples }.to raise_error(DeadTreeError)
end

it 'cannot be picked' do
expect { @dead_tree.pick_an_apple! }.to raise_error(DeadTreeError)
end
end
end

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

it 'has seeds' do
expect(Fruit.new.has_seeds?).to be true
end
end

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

it 'should be a Fruit' do
expect(described_class.superclass).to eq(Fruit)
end

it 'has a color' do
expect(apple(color: "green").color).to eq("green")
end

it 'has a diameter' do
expect(apple(diameter: 2).diameter).to eq(2)
end

it 'has seeds' do
expect(apple.has_seeds?).to be true
end
end