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
38 changes: 29 additions & 9 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
class NoApplesError < StandardError; end

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

def initialize
attr_accessor :age
attr_reader :alive, :height, :apples

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

def age!
def age! # I'm not sure why this has a bang
Copy link
Member

Choose a reason for hiding this comment

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

if @age >= 50
@alive = false
elsif
@age = @age + 1
end
end

def add_apples
def add_apples(number_of_apples)
@apples.concat([Apple.new('red', 9)] * number_of_apples)
end

def any_apples?
@apples.length >= 1
end

def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
if self.any_apples?
@apples.pop
else
raise NoApplesError, "This tree has no apples"
end

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 :has_seeds #what should go here

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

Expand Down
81 changes: 79 additions & 2 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,90 @@
require_relative '../lib/tree'

describe Tree do
let(:tree) { Tree.new(1, [], true, 1) }
Copy link
Member

Choose a reason for hiding this comment

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

nice use of .let. in rails apps, you'll often see use of a library like factory_bot for building factories to use in test. there are also fixture libraries that are popular.

https://github.com/thoughtbot/factory_bot


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

it 'is born with the age of 1' do
expect(tree.age).to be 1
end

it 'is born with the height of 1 foot' do
expect(tree.height).to be 1
end

it 'is born alive' do
expect(tree.alive).to be true
end

it 'is born with no apples' do
expect(tree.apples.length).to be 0
end

# TODO age! should modify age ?!
it 'ages by one year everytime age! is called' do
tree.age = 1
tree.age!
expect(tree.age).to be 2
end

it 'grows n apples if add_apples is called with n number of apples' do
tree.add_apples 10
expect(tree.apples.length).to be 10
end

it 'returns true if the tree has grown apples' do
tree.add_apples(1)
expect(tree.any_apples?).to be true
end

it 'returns false if the tree has no apples' do
expect(tree.any_apples?).to be false
end

it 'returns an apple when pick_an_apple is called' do
expect(tree.any_apples?).to be false
tree.add_apples 1
expect(tree.pick_an_apple!).to be_an_instance_of(Apple)
expect(tree.any_apples?).to be false
end

it 'raises an error if the tree has no apples' do
expect { tree.pick_an_apple! }.to raise_error(NoApplesError)
end

it 'is dead when calling dead? is returns true' do
tree.age = 50
tree.age!
expect(tree.dead?).to be true
end

it 'is alive when calling dead? returns false' do
expect(tree.dead?).to be false
end
end

describe 'Fruit' do
describe Fruit do

context 'when initialized' do
it { is_expected.to be_an_instance_of(Fruit) }
end

end

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

it 'is a Fruit' do
expect(apple).to be_a_kind_of(Fruit)
end

it 'has seeds' do
# TODO Apple should be a subclass of Fruit
expect(apple).to be_an_instance_of(Apple)
#expect(apple::has_seeds).to be_true
end

end