-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy path9_45.rb
174 lines (134 loc) · 3.57 KB
/
9_45.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
# testing duck types
# ...
# ...
# This is a near copy of 5_15.rb
class Trip
attr_reader :bicycles, :customers, :vehicle
def initialize(bicycles, customers, vehicle)
@bicycles = bicycles
@customers = customers
@vehicle = vehicle
end
def prepare(preparers)
preparers.each {|preparer|
preparer.prepare_trip(self)}
end
# ...
end
class Mechanic
def prepare_trip(trip)
trip.bicycles.each {|bicycle|
prepare_bicycle(bicycle)}
end
def prepare_bicycles(bicycles)
bicycles.each {|bicycle| prepare_bicycle(bicycle)}
end
def prepare_bicycle(bicycle)
#...
puts "Mechanic prepare_bicycle #{bicycle}"
end
end
class TripCoordinator
def prepare_trip(trip)
buy_food(trip.customers)
end
def buy_food(customers)
# ...
puts "TripCoordinator buy_food"
end
end
class Driver
def prepare_trip(trip)
vehicle = trip.vehicle
gas_up(vehicle)
fill_water_tank(vehicle)
end
def gas_up(vehicle)
#...
puts "Driver gas_up"
end
def fill_water_tank(vehicle)
#...
puts "Driver fill_water_tank"
end
end
class Bicycle
end
class Customer
end
class Vehicle
end
# trip = Trip.new(
# [Bicycle.new, Bicycle.new, Bicycle.new],
# [Customer.new, Customer.new],
# Vehicle.new )
# preparers = [TripCoordinator.new, Driver.new, Mechanic.new]
#
# trip.prepare(preparers)
##########################################################
require "minitest"
require 'minitest/mock'
require "minitest/reporters"
require "minitest/asciidoc_plugin"
Minitest::Reporters.use! Minitest::Reporters::Asciidoc.new
module PreparerInterfaceTest
def test_implements_the_preparer_interface
assert_respond_to(@object, :prepare_trip)
end
end
##########################################################
class MechanicTest < Minitest::Test
include PreparerInterfaceTest
def setup
@mechanic = @object = Mechanic.new
end
# other tests which rely on @mechanic
end
Minitest.run
Minitest::Runnable.reset
**[rubyclass]#MechanicTest#**
**[green]#PASS#** __(0.00s)__ test_implements_the_preparer_interface
Finished in 0.00014s
1 tests, 1 assertions, [green]#0 failures, 0 errors#, [yellow]#0 skips#
##########################################################
class TripCoordinatorTest < Minitest::Test
include PreparerInterfaceTest
def setup
@trip_coordinator = @object = TripCoordinator.new
end
end
Minitest.run
Minitest::Runnable.reset
**[rubyclass]#TripCoordinatorTest#**
**[green]#PASS#** __(0.00s)__ test_implements_the_preparer_interface
Finished in 0.00012s
1 tests, 1 assertions, [green]#0 failures, 0 errors#, [yellow]#0 skips#
##########################################################
class DriverTest < Minitest::Test
include PreparerInterfaceTest
def setup
@driver = @object = Driver.new
end
end
Minitest.run
Minitest::Runnable.reset
**[rubyclass]#DriverTest#**
**[green]#PASS#** __(0.00s)__ test_implements_the_preparer_interface
Finished in 0.00014s
1 tests, 1 assertions, [green]#0 failures, 0 errors#, [yellow]#0 skips#
##########################################################
class TripTest < Minitest::Test
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
Minitest.run
Minitest::Runnable.reset
**[rubyclass]#TripTest#**
**[green]#PASS#** __(0.00s)__ test_requests_trip_preparation
Finished in 0.00016s
1 tests, 0 assertions, [green]#0 failures, 0 errors#, [yellow]#0 skips#