-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorange_tree.rb
More file actions
145 lines (121 loc) Β· 3.53 KB
/
orange_tree.rb
File metadata and controls
145 lines (121 loc) Β· 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class OrangeTree
FULL_HEIGHT = 10.0 # meters
AGE_TO_REACH_FULL_HEIGHT = 15.0 # years
MAX_AGE = 40
attr_reader :age
# This is the same as these three lines of code:
# def age
# @age
# end
def initialize
@age = 0
@dead = false
@oranges = 0
end
def height
(FULL_HEIGHT / AGE_TO_REACH_FULL_HEIGHT) * @age
end
def oneYearPasses
@age += 1
if @age > 3
@oranges = @age * 2
end
if @age >= MAX_AGE
@dead = true
end
end
def dead?
@dead
end
def countTheOranges
@oranges
end
def pickAnOrange
if @oranges.zero?
"Oh noes! No more π!"
else
@oranges -= 1
"Yum! A delicious π!"
end
end
end
require 'rspec'
RSpec.describe OrangeTree do
it 'is a class' do
expect(OrangeTree).to be_a(Class)
end
describe '#height' do
it 'returns the height of the tree' do
expect(subject).to respond_to(:height)
expect(subject.height).to be_zero
end
end
describe '#oneYearPasses' do
it 'ages the tree one year' do
expect(subject).to respond_to(:oneYearPasses)
subject.oneYearPasses
expect(subject.age).to eq(1)
end
it 'causes the tree to grow one-tenth of its full height' do
subject.oneYearPasses
expect(subject.height).to eq(OrangeTree::FULL_HEIGHT / OrangeTree::AGE_TO_REACH_FULL_HEIGHT)
end
it 'dies after 40 years' do
39.times { subject.oneYearPasses }
expect(subject.dead?).to be false
subject.oneYearPasses
expect(subject.dead?).to be true
end
it 'does not produce fruit in the first three years' do
expect(subject.countTheOranges).to be_zero
subject.oneYearPasses
expect(subject.countTheOranges).to be_zero
subject.oneYearPasses
expect(subject.countTheOranges).to be_zero
subject.oneYearPasses
expect(subject.countTheOranges).to be_zero
end
it 'starts producing fruit in the fourth year' do
4.times { subject.oneYearPasses }
expect(subject.countTheOranges).to be > 0
end
it 'produces more fruit each year' do
4.times { subject.oneYearPasses }
last_years_fruit = subject.countTheOranges
until subject.dead?
subject.oneYearPasses
this_years_fruit = subject.countTheOranges
expect(this_years_fruit).to be > last_years_fruit
last_years_fruit = this_years_fruit
end
end
end
describe '#pickAnOrange' do
it 'reduces the orange count by one' do
4.times { subject.oneYearPasses }
before_count = subject.countTheOranges
subject.pickAnOrange
after_count = subject.countTheOranges
expect(after_count).to eq(before_count - 1)
end
it 'returns a string telling you how delicious the orange was' do
4.times { subject.oneYearPasses }
expect(subject.pickAnOrange).to eq("Yum! A delicious π!")
end
it 'tells you that there are no more oranges if there are no more oranges' do
4.times { subject.oneYearPasses }
subject.countTheOranges.times { subject.pickAnOrange }
expect(subject.pickAnOrange).to eq("Oh noes! No more π!")
end
it 'any oranges you don\'t pick one year fall off before the next year' do
orange_tree1 = OrangeTree.new
orange_tree2 = OrangeTree.new
4.times { orange_tree1.oneYearPasses }
4.times { orange_tree2.oneYearPasses }
orange_tree1.pickAnOrange
orange_tree1.oneYearPasses
orange_tree2.oneYearPasses
expect(orange_tree1.countTheOranges).to eq(orange_tree2.countTheOranges)
end
end
end