diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..8a04e78 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -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 + 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 diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index b4f44c6..d17dd47 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,13 +2,90 @@ require_relative '../lib/tree' describe Tree do + let(:tree) { Tree.new(1, [], true, 1) } + 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