-
Notifications
You must be signed in to change notification settings - Fork 74
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
@alive = true | ||
end | ||
|
||
def age! | ||
@age+=1 | ||
@alive = false unless @age < 9 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. * |
||
@height+=rand(20) | ||
self.add_apples | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Joe is right. The only time |
||
@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 | ||
|
||
|
@@ -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? | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to assume that users of the |
||
|
||
puts "Year #{tree.age} Report" | ||
puts "Tree height: #{tree.height} feet" | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 (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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tests overall. |
||
end |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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