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

TreeTDD exercise dhatten #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
30 changes: 24 additions & 6 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
class NoApplesError < StandardError; end

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

def initialize
@height = 0
@age = 0
@apples = 0
Copy link

Choose a reason for hiding this comment

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

This makes it sound like @apples will be an array of apples. I'd say either switch the value or change the name to make clear that it's a count.

Copy link
Author

Choose a reason for hiding this comment

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

Rupert ended up doing the array design, it works out much better overall

@alive = true
end

def age!
@age+=1
@alive = false unless @age < 9
Copy link

Choose a reason for hiding this comment

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

@alive = false if @age > 9 is probably a bit clearer. if is generally clearer than unless, except when it isn't.

Copy link
Member

Choose a reason for hiding this comment

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

* if is clearer than unless, unless it isn't. 😁

@height+=rand(20)
self.add_apples
Copy link

Choose a reason for hiding this comment

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

The self shouldn't be necessary here - self is the default receiver for every command in ruby.

Copy link
Member

Choose a reason for hiding this comment

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

Joe is right. The only time self. is needed is calling a setter method, like self.foo = "bar". Ruby will assume foo = "bar" is local variable assignment instead of calling the foo= method.

@age
end

def add_apples
rand(25..100).times{@apples+=1}
end

def any_apples?
@apples != 0
end

def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
@apples-=1
Apple.new('Red', rand(3.0..5.0))
end

def dead?
!@alive
end
end

class Fruit
attr_reader :has_seeds
def initialize
has_seeds = true
@has_seeds = true
end
end

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

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

Expand All @@ -41,7 +58,7 @@ def initialize(color, diameter)
# it should calculate the diameter of the apples in the basket

def tree_data
tree = Tree.new
tree = AppleTree.new

tree.age! until tree.any_apples?

Expand All @@ -55,13 +72,14 @@ def tree_data
basket << tree.pick_an_apple!
end


diameter_sum = 0

basket.each do |apple|
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
Copy link

Choose a reason for hiding this comment

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

This seems to assume that users of the Apple class will always initialize it with a floating point diameter, as you did above.


puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand Down
76 changes: 71 additions & 5 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,80 @@
require 'rspec'
require 'tree'

describe 'Tree' do
it 'should be a Class' do
expect(described_class.is_a? 'Class').to be_true
describe AppleTree do
context 'when AppleTree exists' do
Copy link

Choose a reason for hiding this comment

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

I'm not sure adding this context helps to clarify anything.

subject(:tree) { AppleTree.new }
it 'should be a Class' do
expect(described_class.is_a? Class).to be true
end
Copy link

Choose a reason for hiding this comment

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

I know this is provided for you, but I really dislike this kind of test 😄 .


it 'should be a newborn tree' do
expect(tree.age).to eq 0
end

it 'should have no apples' do
expect(tree.any_apples?).to eq false
end
Copy link

@sjreich sjreich Jan 19, 2018

Choose a reason for hiding this comment

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

For these two tests: if it's part of the required setup for these guys that this be a brand-new tree, then the setup should go with the test. The options are either to do the necessary setup inside the it block itself, or else to use a context/describe block.

(and some other tests below too)


it 'should not be able to have an apple picked' do
expect{tree.pick_an_apple!}.to raise_error NoApplesError
end
Copy link

Choose a reason for hiding this comment

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

nice


it 'should be alive' do
expect(tree.alive).to eq true
end

it 'should age when told to' do
expect(tree.age!).to eq 1
end

it 'should get taller as it ages' do
tree.age!
expect(tree.height).to be > 0
end

it 'should die after 10 years' do
10.times {
tree.age!
}
expect(tree.dead?).to eq true
end
Copy link

Choose a reason for hiding this comment

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

Not that you need to add it, but think about testing the flip side here too: this only shows that tens years of aging kills the tree - but maybe 9 or 8 years would also kill it.


it 'should produce fruit after 1 year' do
tree.age!
expect(tree.any_apples?).to eq true
end

it 'should let me pick an apple' do
tree.age!
expect(tree.pick_an_apple!).to respond_to :diameter
end
end

end

describe 'Fruit' do
describe Fruit do
subject(:fruit) {Fruit.new}
it 'should be a Class' do
expect(described_class.is_a? Class).to be true
end

it 'should have seeds' do
expect(fruit.has_seeds).to eq true
end
end

describe 'Apple' do
describe Apple do
subject(:apple){Apple.new('Red', 5)}
it 'should be a Class' do
expect(described_class.is_a? Class).to be true
end

it 'should have a color' do
expect(apple.color).to eq 'Red'
end

it 'should have a diameter' do
expect(apple.diameter).to eq 5
end
Copy link

Choose a reason for hiding this comment

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

Nice tests overall.

end