From 798e8ee4e77a2467b54c75cbeaf28e8bd68ba419 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Tue, 12 Jun 2018 11:15:03 -0400 Subject: [PATCH 1/4] Add tree initialization and its tests --- lib/tree.rb | 16 +++++++++++----- spec/tree_spec.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..3a23d03 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,12 +1,16 @@ class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive - def initialize + 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 end def add_apples @@ -29,10 +33,12 @@ def initialize 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..5261162 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,13 +2,56 @@ 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 ?! + # TODO any_apples should return true if apples isn't empty + # TODO any_apples should return true if apples is empty + # TODO pick_an_apple should pick an apple, remove it from the list ?! + # TODO pick_an_apple should raise NoApplesError if apples is empty + # TODO dead should return true if the tree is dead + # TODO dead should return false if the tree is alive 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 From d2de97b5463f2747a432fe2d11d1e0a809135a9a Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Tue, 12 Jun 2018 12:38:11 -0400 Subject: [PATCH 2/4] add implementation to different tree functionalities and their tests" " --- lib/tree.rb | 19 +++++++++++++++--- spec/tree_spec.rb | 49 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 3a23d03..c7d944a 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -11,25 +11,38 @@ def initialize(age, apples=[], alive, height) end 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 ? true : false end def pick_an_apple! - raise NoApplesError, "This tree has no apples" unless self.any_apples? + if self.any_apples? + return @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 diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 5261162..3bd1a71 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -25,12 +25,49 @@ end # TODO age! should modify age ?! - # TODO any_apples should return true if apples isn't empty - # TODO any_apples should return true if apples is empty - # TODO pick_an_apple should pick an apple, remove it from the list ?! - # TODO pick_an_apple should raise NoApplesError if apples is empty - # TODO dead should return true if the tree is dead - # TODO dead should return false if the tree is alive + 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.apples = [] + tree.add_apples 10 + expect(tree.apples.length).to be 10 + end + + it 'returns true if the tree has grown apples' do + tree.apples = [] + tree.add_apples(1) + expect(tree.any_apples?).to be true + end + + it 'returns false if the tree has no apples' do + tree.apples = [] + 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 + tree.apples = [] + expect { tree.pick_an_apple! }.to raise_error(NoApplesError) + end + + it 'is dead when calling dead? is returns true' do + tree.alive = false + 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 From fb105d382c1df9f6096857ed4871454573062c74 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Mon, 18 Jun 2018 13:50:23 -0400 Subject: [PATCH 3/4] Update tree instance variables accessors Update tests --- lib/tree.rb | 5 +++-- spec/tree_spec.rb | 7 ++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c7d944a..5341f25 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,7 +1,8 @@ class NoApplesError < StandardError; end class Tree - attr_accessor :height, :age, :apples, :alive + attr_accessor :age + attr_reader :alive, :height, :apples def initialize(age, apples=[], alive, height) @age = age @@ -11,7 +12,7 @@ def initialize(age, apples=[], alive, height) end def age! # I'm not sure why this has a bang - if @age == 50 + if @age >= 50 @alive = false elsif @age = @age + 1 diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 3bd1a71..d17dd47 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -32,19 +32,16 @@ end it 'grows n apples if add_apples is called with n number of apples' do - tree.apples = [] tree.add_apples 10 expect(tree.apples.length).to be 10 end it 'returns true if the tree has grown apples' do - tree.apples = [] tree.add_apples(1) expect(tree.any_apples?).to be true end it 'returns false if the tree has no apples' do - tree.apples = [] expect(tree.any_apples?).to be false end @@ -56,12 +53,12 @@ end it 'raises an error if the tree has no apples' do - tree.apples = [] expect { tree.pick_an_apple! }.to raise_error(NoApplesError) end it 'is dead when calling dead? is returns true' do - tree.alive = false + tree.age = 50 + tree.age! expect(tree.dead?).to be true end From b2c0d6fa966ee82392e723e6e30b034e3a8bcfd1 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Mon, 18 Jun 2018 13:56:41 -0400 Subject: [PATCH 4/4] Remove unnecessary return statement Remove unnecessary ternary operator --- lib/tree.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 5341f25..8a04e78 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -24,12 +24,12 @@ def add_apples(number_of_apples) end def any_apples? - @apples.length >= 1 ? true : false + @apples.length >= 1 end def pick_an_apple! if self.any_apples? - return @apples.pop + @apples.pop else raise NoApplesError, "This tree has no apples" end