-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_9h.rb
219 lines (174 loc) · 4.42 KB
/
chapter_9h.rb
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
require 'minitest/reporters'
MiniTest::Unit.runner = MiniTest::SuiteRunner.new
MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new
require 'minitest/autorun'
############## Page 229 ##############
class Bicycle
attr_reader :size, :chain, :tire_size
def initialize(args={})
@size = args[:size]
@chain = args[:chain] || default_chain
@tire_size = args[:tire_size] || default_tire_size
post_initialize(args)
end
def spares
{ tire_size: tire_size,
chain: chain}.merge(local_spares)
end
def default_tire_size
raise NotImplementedError
end
# subclasses may override
def post_initialize(args)
nil
end
def local_spares
{}
end
def default_chain
'10-speed'
end
end
############## Page 230 ##############
class RoadBike < Bicycle
attr_reader :tape_color
def post_initialize(args)
@tape_color = args[:tape_color]
end
def local_spares
{tape_color: tape_color}
end
def default_tire_size
'23'
end
end
############## Page ??? ##############
class MountainBike < Bicycle
attr_reader :front_shock, :rear_shock
def post_initialize(args)
@front_shock = args[:front_shock]
@rear_shock = args[:rear_shock]
end
def local_spares
{rear_shock: rear_shock}
end
def default_tire_size
'2.1'
end
end
############## Page 231 ##############
module BicycleInterfaceTest
def test_responds_to_default_tire_size
assert_respond_to(@object, :default_tire_size)
end
def test_responds_to_default_chain
assert_respond_to(@object, :default_chain)
end
def test_responds_to_chain
assert_respond_to(@object, :chain)
end
def test_responds_to_size
assert_respond_to(@object, :size)
end
def test_responds_to_tire_size
assert_respond_to(@object, :tire_size)
end
def test_responds_to_spares
assert_respond_to(@object, :spares)
end
end
############## Page 231 ##############
class BicycleTest < MiniTest::Unit::TestCase
include BicycleInterfaceTest
def setup
@bike = @object = Bicycle.new({tire_size: 0})
end
end
class RoadBikeTest < MiniTest::Unit::TestCase
include BicycleInterfaceTest
def setup
@bike = @object = RoadBike.new
end
end
############## Page 233 ##############
module BicycleSubclassTest
def test_responds_to_post_initialize
assert_respond_to(@object, :post_initialize)
end
def test_responds_to_local_spares
assert_respond_to(@object, :local_spares)
end
def test_responds_to_default_tire_size
assert_respond_to(@object, :default_tire_size)
end
end
############## Page 233 ##############
class RoadBikeTest < MiniTest::Unit::TestCase
include BicycleInterfaceTest
include BicycleSubclassTest
def setup
@bike = @object = RoadBike.new
end
end
############## Page 234 ##############
class MountainBikeTest < MiniTest::Unit::TestCase
include BicycleInterfaceTest
include BicycleSubclassTest
def setup
@bike = @object = MountainBike.new
end
end
############## Page 235 ##############
class BicycleTest < MiniTest::Unit::TestCase
include BicycleInterfaceTest
def setup
@bike = @object = Bicycle.new({tire_size: 0})
end
def test_forces_subclasses_to_implement_default_tire_size
assert_raises(NotImplementedError) {@bike.default_tire_size}
end
end
############## Page 236 ##############
class RoadBikeTest < MiniTest::Unit::TestCase
include BicycleInterfaceTest
include BicycleSubclassTest
def setup
@bike = @object = RoadBike.new(tape_color: 'red')
end
def test_puts_tape_color_in_local_spares
assert_equal 'red', @bike.local_spares[:tape_color]
end
end
############## Page 238 ##############
class StubbedBike < Bicycle
def default_tire_size
0
end
def local_spares
{saddle: 'painful'}
end
end
class BicycleTest < MiniTest::Unit::TestCase
include BicycleInterfaceTest
def setup
@bike = @object = Bicycle.new({tire_size: 0})
@stubbed_bike = StubbedBike.new
end
def test_forces_subclasses_to_implement_default_tire_size
assert_raises(NotImplementedError) {
@bike.default_tire_size}
end
def test_includes_local_spares_in_spares
assert_equal @stubbed_bike.spares,
{ tire_size: 0,
chain: '10-speed',
saddle: 'painful'}
end
end
############## Page 239 ##############
class StubbedBikeTest < MiniTest::Unit::TestCase
include BicycleSubclassTest
def setup
@object = StubbedBike.new
end
end