-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_9e.rb
147 lines (121 loc) · 2.8 KB
/
chapter_9e.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
require 'minitest/reporters'
MiniTest::Unit.runner = MiniTest::SuiteRunner.new
MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new
require 'minitest/autorun'
############## Page 219 ##############
class Mechanic
def prepare_bicycle(bicycle)
#...
end
end
class TripCoordinator
def buy_food(customers)
#...
end
end
class Driver
def gas_up(vehicle)
#...
end
def fill_water_tank(vehicle)
#...
end
end
############## Page 220 ##############
# The full class is below.
class Trip
attr_reader :bicycles, :customers, :vehicle
def prepare(preparers)
preparers.each {|preparer|
case preparer
when Mechanic
preparer.prepare_bicycles(bicycles)
when TripCoordinator
preparer.buy_food(customers)
when Driver
preparer.gas_up(vehicle)
preparer.fill_water_tank(vehicle)
end
}
end
end
############## Full example, so tests will run.
class Trip
attr_reader :bicycles, :customers, :vehicle
def initialize(args={})
@bicycles = args[:bicycles] ||= []
@customers = args[:customers] ||= []
@vehicls = args[:vehicle]
end
def prepare(preparers)
preparers.each {|preparer|
preparer.prepare_trip(self)}
end
end
############## Page 220 ##############
class Mechanic
def prepare_trip(trip)
trip.bicycles.each {|bicycle|
prepare_bicycle(bicycle)}
end
# ...
end
class TripCoordinator
def prepare_trip(trip)
buy_food(trip.customers)
end
# ...
end
class Driver
def prepare_trip(trip)
vehicle = trip.vehicle
gas_up(vehicle)
fill_water_tank(vehicle)
end
# ...
end
############## Page 221 ##############
class Trip
attr_reader :bicycles, :customers, :vehicle
def prepare(preparers)
preparers.each {|preparer|
preparer.prepare_trip(self)}
end
end
############## Page 222 ##############
module PreparerInterfaceTest
def test_implements_the_preparer_interface
assert_respond_to(@object, :prepare_trip)
end
end
############## Page 222 ##############
class MechanicTest < MiniTest::Unit::TestCase
include PreparerInterfaceTest
def setup
@mechanic = @object = Mechanic.new
end
# other tests which rely on @mechanic
end
############## Page 222 ##############
class TripCoordinatorTest < MiniTest::Unit::TestCase
include PreparerInterfaceTest
def setup
@trip_coordinator = @object = TripCoordinator.new
end
end
class DriverTest < MiniTest::Unit::TestCase
include PreparerInterfaceTest
def setup
@driver = @object = Driver.new
end
end
############## Page 223 ##############
class TripTest < MiniTest::Unit::TestCase
def test_requests_trip_preparation
@preparer = MiniTest::Mock.new
@trip = Trip.new
@preparer.expect(:prepare_trip, nil, [@trip])
@trip.prepare([@preparer])
@preparer.verify
end
end