From 73ff6a605f38adc8eb6b824c1ad70cb6e3c54f22 Mon Sep 17 00:00:00 2001 From: Levi Klingler Date: Mon, 22 Jan 2018 12:32:22 -0500 Subject: [PATCH 1/3] Levi's solution --- .rspec | 3 + Gemfile.lock | 4 ++ lib/tree.rb | 49 +++++++++++--- spec/spec_helper.rb | 3 + spec/tree_spec.rb | 159 ++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 205 insertions(+), 13 deletions(-) create mode 100644 .rspec create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..45c00a8 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--color +--require ./spec/spec_helper.rb +--format doc diff --git a/Gemfile.lock b/Gemfile.lock index 90f2f48..6025109 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: https://rubygems.org/ specs: diff-lcs (1.2.5) rspec (3.1.0) @@ -19,3 +20,6 @@ PLATFORMS DEPENDENCIES rspec + +BUNDLED WITH + 1.16.1 diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..e8d8f8f 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -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? + 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 @@ -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" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..bf6db62 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.warnings = true +end diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 99c9184..73d018b 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -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 + raise unless tree(height: 15).height == 15 + end + + it 'has an age' do + raise unless tree(age: 75).age == 75 + end + + context 'is alive' do + subject { tree } + + it 'can have apples' do + raise unless tree.any_apples? + end + + it 'can have no apples' do + raise unless not tree(apples: []).any_apples? + end + + it 'can grow apples' do + original_apples_count = subject.apples.count + subject.add_apples + new_apples_count = subject.apples.count + raise unless original_apples_count < new_apples_count + end + + context 'has apples' do + it 'can be picked' do + apple = subject.pick_an_apple! + expect(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 + raise unless original_tree_height < 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 + raise unless original_tree_height == 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 + raise unless original_tree_age < new_tree_age + end + end + + context 'is 200 years old' do + subject { tree(age: 200) } + + it 'dies' do + subject.age! + raise unless subject.dead? + 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 + raise unless @dead_tree.apples.count == 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 + raise unless Fruit.new.has_seeds? + 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 + raise unless apple(color: "green").color == "green" + end + + it 'has a diameter' do + raise unless apple(diameter: 2).diameter == 2 + end + + it 'has seeds' do + raise unless Apple.new.has_seeds? + end end From b6c24eda1200921e1037565a0deaa4a3fb0bd6e7 Mon Sep 17 00:00:00 2001 From: Levi Klingler Date: Mon, 22 Jan 2018 14:50:10 -0500 Subject: [PATCH 2/3] Changed all appearances of 'raise' to 'expect' syntax --- spec/tree_spec.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 73d018b..60016bd 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -29,35 +29,35 @@ def tree(params = {}) end it 'has a height' do - raise unless tree(height: 15).height == 15 + expect(tree(height: 15).height).to eq(15) end it 'has an age' do - raise unless tree(age: 75).age == 75 + expect(tree(age: 75).age).to eq(75) end context 'is alive' do subject { tree } it 'can have apples' do - raise unless tree.any_apples? + expect(tree.any_apples?).to be true end it 'can have no apples' do - raise unless not tree(apples: []).any_apples? + 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 - raise unless original_apples_count < new_apples_count + expect(original_apples_count).to be < new_apples_count end context 'has apples' do it 'can be picked' do - apple = subject.pick_an_apple! - expect(apple.class).to eq(Apple) + picked_apple = subject.pick_an_apple! + expect(picked_apple.class).to eq(Apple) end end @@ -74,7 +74,7 @@ def tree(params = {}) original_tree_height = subject.height subject.age! new_tree_height = subject.height - raise unless original_tree_height < new_tree_height + expect(original_tree_height).to be < new_tree_height end end @@ -85,7 +85,7 @@ def tree(params = {}) original_tree_height = subject.height subject.age! new_tree_height = subject.height - raise unless original_tree_height == new_tree_height + expect(original_tree_height).to eq new_tree_height end end @@ -94,7 +94,7 @@ def tree(params = {}) original_tree_age = subject.age subject.age! new_tree_age = subject.age - raise unless original_tree_age < new_tree_age + expect(original_tree_age).to be < new_tree_age end end @@ -103,7 +103,7 @@ def tree(params = {}) it 'dies' do subject.age! - raise unless subject.dead? + expect(subject.dead?).to be true end end end @@ -119,7 +119,7 @@ def tree(params = {}) end it 'has no apples' do - raise unless @dead_tree.apples.count == 0 + expect(@dead_tree.apples.count).to eq(0) end it 'cannot grow apples' do @@ -138,7 +138,7 @@ def tree(params = {}) end it 'has seeds' do - raise unless Fruit.new.has_seeds? + expect(Fruit.new.has_seeds?).to be true end end @@ -152,14 +152,14 @@ def tree(params = {}) end it 'has a color' do - raise unless apple(color: "green").color == "green" + expect(apple(color: "green").color).to eq("green") end it 'has a diameter' do - raise unless apple(diameter: 2).diameter == 2 + expect(apple(diameter: 2).diameter).to eq(2) end it 'has seeds' do - raise unless Apple.new.has_seeds? + expect(apple.has_seeds?).to be true end end From 370882266248cb34ae51be56249ce9ad2d77ca75 Mon Sep 17 00:00:00 2001 From: Levi Klingler Date: Tue, 23 Jan 2018 15:52:55 -0500 Subject: [PATCH 3/3] Apply recommended changes --- lib/tree.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index e8d8f8f..6934634 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -12,7 +12,7 @@ def initialize(height: 0, age: 0, apples: [], alive: true) end def age! - raise DeadTreeError, "This tree is dead" unless not self.dead? + raise DeadTreeError, "This tree is dead" if self.dead? @age += 1 @height += 1 if @height < 25 add_apples if @age > 5 @@ -20,7 +20,7 @@ def age! end def add_apples - raise DeadTreeError, "This tree is dead" unless not self.dead? + raise DeadTreeError, "This tree is dead" if self.dead? rand(50..100).times { @apples << Apple.new(color: "red", diameter: rand(2..4)) } end @@ -29,7 +29,7 @@ def any_apples? end def pick_an_apple! - raise DeadTreeError, "This tree is dead" unless not self.dead? + raise DeadTreeError, "This tree is dead" if self.dead? raise NoApplesError, "This tree has no apples" unless self.any_apples? @apples.shift end