sanford 0.10.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/README.md +41 -56
- data/Rakefile +0 -1
- data/bench/client.rb +8 -3
- data/bench/{services.rb → config.sanford} +11 -6
- data/bench/{runner.rb → report.rb} +2 -2
- data/bench/report.txt +32 -32
- data/lib/sanford/cli.rb +42 -28
- data/lib/sanford/config_file.rb +79 -0
- data/lib/sanford/{worker.rb → connection_handler.rb} +28 -20
- data/lib/sanford/error_handler.rb +7 -7
- data/lib/sanford/pid_file.rb +42 -0
- data/lib/sanford/process.rb +136 -0
- data/lib/sanford/process_signal.rb +20 -0
- data/lib/sanford/route.rb +48 -0
- data/lib/sanford/router.rb +36 -0
- data/lib/sanford/runner.rb +30 -58
- data/lib/sanford/sanford_runner.rb +19 -9
- data/lib/sanford/server.rb +211 -42
- data/lib/sanford/server_data.rb +47 -0
- data/lib/sanford/service_handler.rb +8 -46
- data/lib/sanford/template_source.rb +19 -2
- data/lib/sanford/test_runner.rb +27 -28
- data/lib/sanford/version.rb +1 -1
- data/lib/sanford.rb +1 -23
- data/sanford.gemspec +4 -5
- data/test/helper.rb +3 -20
- data/test/support/app_server.rb +142 -0
- data/test/support/config.sanford +7 -0
- data/test/support/config_invalid_run.sanford +3 -0
- data/test/support/config_no_run.sanford +0 -0
- data/test/support/fake_server_connection.rb +58 -0
- data/test/support/pid_file_spy.rb +19 -0
- data/test/support/template.erb +1 -0
- data/test/system/server_tests.rb +378 -0
- data/test/system/service_handler_tests.rb +224 -0
- data/test/unit/cli_tests.rb +187 -0
- data/test/unit/config_file_tests.rb +59 -0
- data/test/unit/connection_handler_tests.rb +254 -0
- data/test/unit/error_handler_tests.rb +30 -35
- data/test/unit/pid_file_tests.rb +70 -0
- data/test/unit/process_signal_tests.rb +61 -0
- data/test/unit/process_tests.rb +428 -0
- data/test/unit/route_tests.rb +92 -0
- data/test/unit/router_tests.rb +65 -0
- data/test/unit/runner_tests.rb +61 -15
- data/test/unit/sanford_runner_tests.rb +162 -28
- data/test/unit/sanford_tests.rb +0 -8
- data/test/unit/server_data_tests.rb +87 -0
- data/test/unit/server_tests.rb +502 -21
- data/test/unit/service_handler_tests.rb +114 -219
- data/test/unit/template_engine_tests.rb +1 -1
- data/test/unit/template_source_tests.rb +56 -16
- data/test/unit/test_runner_tests.rb +206 -0
- metadata +67 -67
- data/bench/tasks.rb +0 -41
- data/lib/sanford/config.rb +0 -28
- data/lib/sanford/host.rb +0 -129
- data/lib/sanford/host_data.rb +0 -65
- data/lib/sanford/hosts.rb +0 -38
- data/lib/sanford/manager.rb +0 -275
- data/test/support/fake_connection.rb +0 -36
- data/test/support/helpers.rb +0 -17
- data/test/support/service_handlers.rb +0 -154
- data/test/support/services.rb +0 -123
- data/test/support/simple_client.rb +0 -62
- data/test/system/request_handling_tests.rb +0 -306
- data/test/unit/config_tests.rb +0 -56
- data/test/unit/host_data_tests.rb +0 -71
- data/test/unit/host_tests.rb +0 -141
- data/test/unit/hosts_tests.rb +0 -50
- data/test/unit/manager_tests.rb +0 -195
- data/test/unit/worker_tests.rb +0 -24
@@ -1,23 +1,18 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'sanford/service_handler'
|
3
3
|
|
4
|
-
require '
|
4
|
+
require 'sanford/template_engine'
|
5
5
|
require 'sanford/template_source'
|
6
|
-
require 'sanford/test_helpers'
|
7
|
-
require 'test/support/service_handlers'
|
8
6
|
|
9
7
|
module Sanford::ServiceHandler
|
10
8
|
|
11
9
|
class UnitTests < Assert::Context
|
12
|
-
include Sanford::TestHelpers
|
13
|
-
|
14
10
|
desc "Sanford::ServiceHandler"
|
15
11
|
setup do
|
16
12
|
@handler_class = Class.new{ include Sanford::ServiceHandler }
|
17
13
|
end
|
18
14
|
subject{ @handler_class }
|
19
15
|
|
20
|
-
should have_imeths :run
|
21
16
|
should have_imeths :before_callbacks, :after_callbacks
|
22
17
|
should have_imeths :before_init_callbacks, :after_init_callbacks
|
23
18
|
should have_imeths :before_run_callbacks, :after_run_callbacks
|
@@ -28,20 +23,6 @@ module Sanford::ServiceHandler
|
|
28
23
|
should have_imeths :prepend_before_init, :prepend_after_init
|
29
24
|
should have_imeths :prepend_before_run, :prepend_after_run
|
30
25
|
|
31
|
-
should "disallow certain template extensions" do
|
32
|
-
exp = Sanford::TemplateSource::DISALLOWED_ENGINE_EXTS
|
33
|
-
assert_equal exp, subject::DISALLOWED_TEMPLATE_EXTS
|
34
|
-
end
|
35
|
-
|
36
|
-
should "allow running a handler class with the class method #run" do
|
37
|
-
response = HaltServiceHandler.run({
|
38
|
-
'code' => 648,
|
39
|
-
'data' => true
|
40
|
-
})
|
41
|
-
assert_equal 648, response.code
|
42
|
-
assert_equal true, response.data
|
43
|
-
end
|
44
|
-
|
45
26
|
should "return an empty array by default using `before_callbacks`" do
|
46
27
|
assert_equal [], subject.before_callbacks
|
47
28
|
end
|
@@ -67,85 +48,85 @@ module Sanford::ServiceHandler
|
|
67
48
|
end
|
68
49
|
|
69
50
|
should "append a block to the before callbacks using `before`" do
|
70
|
-
subject.before_callbacks << proc{ }
|
71
|
-
block = Proc.new{}
|
51
|
+
subject.before_callbacks << proc{ Factory.string }
|
52
|
+
block = Proc.new{ Factory.string }
|
72
53
|
subject.before(&block)
|
73
54
|
assert_equal block, subject.before_callbacks.last
|
74
55
|
end
|
75
56
|
|
76
57
|
should "append a block to the after callbacks using `after`" do
|
77
|
-
subject.after_callbacks << proc{ }
|
78
|
-
block = Proc.new{}
|
58
|
+
subject.after_callbacks << proc{ Factory.string }
|
59
|
+
block = Proc.new{ Factory.string }
|
79
60
|
subject.after(&block)
|
80
61
|
assert_equal block, subject.after_callbacks.last
|
81
62
|
end
|
82
63
|
|
83
64
|
should "append a block to the before init callbacks using `before_init`" do
|
84
|
-
subject.before_init_callbacks << proc{ }
|
85
|
-
block = Proc.new{}
|
65
|
+
subject.before_init_callbacks << proc{ Factory.string }
|
66
|
+
block = Proc.new{ Factory.string }
|
86
67
|
subject.before_init(&block)
|
87
68
|
assert_equal block, subject.before_init_callbacks.last
|
88
69
|
end
|
89
70
|
|
90
71
|
should "append a block to the after init callbacks using `after_init`" do
|
91
|
-
subject.after_init_callbacks << proc{ }
|
92
|
-
block = Proc.new{}
|
72
|
+
subject.after_init_callbacks << proc{ Factory.string }
|
73
|
+
block = Proc.new{ Factory.string }
|
93
74
|
subject.after_init(&block)
|
94
75
|
assert_equal block, subject.after_init_callbacks.last
|
95
76
|
end
|
96
77
|
|
97
78
|
should "append a block to the before run callbacks using `before_run`" do
|
98
|
-
subject.before_run_callbacks << proc{ }
|
99
|
-
block = Proc.new{}
|
79
|
+
subject.before_run_callbacks << proc{ Factory.string }
|
80
|
+
block = Proc.new{ Factory.string }
|
100
81
|
subject.before_run(&block)
|
101
82
|
assert_equal block, subject.before_run_callbacks.last
|
102
83
|
end
|
103
84
|
|
104
85
|
should "append a block to the after run callbacks using `after_run`" do
|
105
|
-
subject.after_run_callbacks << proc{ }
|
106
|
-
block = Proc.new{}
|
86
|
+
subject.after_run_callbacks << proc{ Factory.string }
|
87
|
+
block = Proc.new{ Factory.string }
|
107
88
|
subject.after_run(&block)
|
108
89
|
assert_equal block, subject.after_run_callbacks.last
|
109
90
|
end
|
110
91
|
|
111
92
|
should "prepend a block to the before callbacks using `prepend_before`" do
|
112
|
-
subject.before_callbacks << proc{ }
|
113
|
-
block = Proc.new{}
|
93
|
+
subject.before_callbacks << proc{ Factory.string }
|
94
|
+
block = Proc.new{ Factory.string }
|
114
95
|
subject.prepend_before(&block)
|
115
96
|
assert_equal block, subject.before_callbacks.first
|
116
97
|
end
|
117
98
|
|
118
99
|
should "prepend a block to the after callbacks using `prepend_after`" do
|
119
|
-
subject.after_callbacks << proc{ }
|
120
|
-
block = Proc.new{}
|
100
|
+
subject.after_callbacks << proc{ Factory.string }
|
101
|
+
block = Proc.new{ Factory.string }
|
121
102
|
subject.prepend_after(&block)
|
122
103
|
assert_equal block, subject.after_callbacks.first
|
123
104
|
end
|
124
105
|
|
125
106
|
should "prepend a block to the before init callbacks using `prepend_before_init`" do
|
126
|
-
subject.before_init_callbacks << proc{ }
|
127
|
-
block = Proc.new{}
|
107
|
+
subject.before_init_callbacks << proc{ Factory.string }
|
108
|
+
block = Proc.new{ Factory.string }
|
128
109
|
subject.prepend_before_init(&block)
|
129
110
|
assert_equal block, subject.before_init_callbacks.first
|
130
111
|
end
|
131
112
|
|
132
113
|
should "prepend a block to the after init callbacks using `prepend_after_init`" do
|
133
|
-
subject.after_init_callbacks << proc{ }
|
134
|
-
block = Proc.new{}
|
114
|
+
subject.after_init_callbacks << proc{ Factory.string }
|
115
|
+
block = Proc.new{ Factory.string }
|
135
116
|
subject.prepend_after_init(&block)
|
136
117
|
assert_equal block, subject.after_init_callbacks.first
|
137
118
|
end
|
138
119
|
|
139
120
|
should "prepend a block to the before run callbacks using `prepend_before_run`" do
|
140
|
-
subject.before_run_callbacks << proc{ }
|
141
|
-
block = Proc.new{}
|
121
|
+
subject.before_run_callbacks << proc{ Factory.string }
|
122
|
+
block = Proc.new{ Factory.string }
|
142
123
|
subject.prepend_before_run(&block)
|
143
124
|
assert_equal block, subject.before_run_callbacks.first
|
144
125
|
end
|
145
126
|
|
146
127
|
should "prepend a block to the after run callbacks using `prepend_after_run`" do
|
147
|
-
subject.after_run_callbacks << proc{ }
|
148
|
-
block = Proc.new{}
|
128
|
+
subject.after_run_callbacks << proc{ Factory.string }
|
129
|
+
block = Proc.new{ Factory.string }
|
149
130
|
subject.prepend_after_run(&block)
|
150
131
|
assert_equal block, subject.after_run_callbacks.first
|
151
132
|
end
|
@@ -155,227 +136,141 @@ module Sanford::ServiceHandler
|
|
155
136
|
class InitTests < UnitTests
|
156
137
|
desc "when init"
|
157
138
|
setup do
|
158
|
-
@
|
139
|
+
@runner = FakeRunner.new
|
140
|
+
@handler = TestServiceHandler.new(@runner)
|
159
141
|
end
|
160
142
|
subject{ @handler }
|
161
143
|
|
162
144
|
should have_imeths :init, :init!, :run, :run!
|
163
145
|
|
164
|
-
should "
|
165
|
-
|
166
|
-
|
146
|
+
should "know its request, params and logger" do
|
147
|
+
assert_equal @runner.request, subject.request
|
148
|
+
assert_equal @runner.params, subject.params
|
149
|
+
assert_equal @runner.logger, subject.logger
|
167
150
|
end
|
168
151
|
|
169
|
-
should "
|
170
|
-
|
152
|
+
should "call `init!` and its before/after init callbacks using `init`" do
|
153
|
+
subject.init
|
154
|
+
assert_equal 1, subject.first_before_init_call_order
|
155
|
+
assert_equal 2, subject.second_before_init_call_order
|
156
|
+
assert_equal 3, subject.init_call_order
|
157
|
+
assert_equal 4, subject.first_after_init_call_order
|
158
|
+
assert_equal 5, subject.second_after_init_call_order
|
171
159
|
end
|
172
160
|
|
173
|
-
should "
|
174
|
-
assert_true subject.before_init_called
|
175
|
-
assert_true subject.second_before_init_called
|
176
|
-
assert_true subject.init_bang_called
|
177
|
-
assert_true subject.after_init_called
|
178
|
-
end
|
179
|
-
|
180
|
-
should "not have called `run!` or its callbacks when initialized" do
|
181
|
-
assert_nil subject.before_run_called
|
182
|
-
assert_nil subject.run_bang_called
|
183
|
-
assert_nil subject.after_run_called
|
184
|
-
assert_nil subject.second_after_run_called
|
185
|
-
end
|
186
|
-
|
187
|
-
should "call `run!` and its callbacks when its run" do
|
161
|
+
should "call `run!` and its before/after run callbacks using `run`" do
|
188
162
|
subject.run
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
163
|
+
assert_equal 1, subject.first_before_run_call_order
|
164
|
+
assert_equal 2, subject.second_before_run_call_order
|
165
|
+
assert_equal 3, subject.run_call_order
|
166
|
+
assert_equal 4, subject.first_after_run_call_order
|
167
|
+
assert_equal 5, subject.second_after_run_call_order
|
194
168
|
end
|
195
169
|
|
196
|
-
should "
|
197
|
-
subject.
|
198
|
-
|
170
|
+
should "have a custom inspect" do
|
171
|
+
reference = '0x0%x' % (subject.object_id << 1)
|
172
|
+
expected = "#<#{subject.class}:#{reference} " \
|
173
|
+
"@request=#{subject.request.inspect}>"
|
174
|
+
assert_equal expected, subject.inspect
|
199
175
|
end
|
200
176
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
should "return a response with the status code and the passed data" do
|
207
|
-
runner = test_runner(HaltServiceHandler, {
|
208
|
-
'code' => 648,
|
209
|
-
'data' => true
|
210
|
-
})
|
211
|
-
runner.run
|
212
|
-
|
213
|
-
assert_equal 648, runner.response.code
|
214
|
-
assert_true runner.response.data
|
215
|
-
assert_nil runner.response.status.message
|
177
|
+
should "demeter its runner's halt" do
|
178
|
+
code = Factory.integer
|
179
|
+
result = subject.halt(code)
|
180
|
+
assert_includes [ code ], @runner.halt_calls
|
216
181
|
end
|
217
182
|
|
218
|
-
should "
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
runner.run
|
224
|
-
|
225
|
-
assert_equal 200, runner.response.code
|
226
|
-
assert_equal 'test message', runner.response.status.message
|
227
|
-
assert_nil runner.response.data
|
183
|
+
should "use the runner template source to render the template by default" do
|
184
|
+
path = Factory.file_path
|
185
|
+
locals = { 'something' => Factory.string }
|
186
|
+
result = subject.render(path, 'locals' => locals)
|
187
|
+
assert_equal [ path, TestServiceHandler.to_s, locals ], result
|
228
188
|
end
|
229
189
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
should "not call `init!, `after_init`, `run!` or run's callbacks when `before_init` halts" do
|
236
|
-
runner = test_runner(HaltingBehaviorServiceHandler, {
|
237
|
-
'when' => 'before_init'
|
238
|
-
})
|
239
|
-
response = runner.response
|
240
|
-
|
241
|
-
assert_equal true, response.data[:before_init_called]
|
242
|
-
assert_equal nil, response.data[:init_bang_called]
|
243
|
-
assert_equal nil, response.data[:after_init_called]
|
244
|
-
assert_equal nil, response.data[:before_run_called]
|
245
|
-
assert_equal nil, response.data[:run_bang_called]
|
246
|
-
assert_equal nil, response.data[:after_run_called]
|
247
|
-
|
248
|
-
assert_equal 'before_init halting', response.status.message
|
190
|
+
should "default its locals to an empty hash using `render`" do
|
191
|
+
path = Factory.file_path
|
192
|
+
result = subject.render(path)
|
193
|
+
assert_equal [ path, TestServiceHandler.to_s, {} ], result
|
249
194
|
end
|
250
195
|
|
251
|
-
should "
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
assert_equal
|
258
|
-
assert_equal true, response.data[:init_bang_called]
|
259
|
-
assert_equal nil, response.data[:after_init_called]
|
260
|
-
assert_equal nil, response.data[:before_run_called]
|
261
|
-
assert_equal nil, response.data[:run_bang_called]
|
262
|
-
assert_equal nil, response.data[:after_run_called]
|
263
|
-
|
264
|
-
assert_equal 'init! halting', response.status.message
|
196
|
+
should "allow passing a source to its options using `render`" do
|
197
|
+
path = Factory.file_path
|
198
|
+
source_class = Class.new do
|
199
|
+
def render(path, service_handler, locals); path; end
|
200
|
+
end
|
201
|
+
result = subject.render(path, 'source' => source_class.new)
|
202
|
+
assert_equal path, result
|
265
203
|
end
|
266
204
|
|
267
|
-
should "
|
268
|
-
|
269
|
-
'when' => 'after_init'
|
270
|
-
})
|
271
|
-
response = runner.response
|
272
|
-
|
273
|
-
assert_equal true, response.data[:before_init_called]
|
274
|
-
assert_equal true, response.data[:init_bang_called]
|
275
|
-
assert_equal true, response.data[:after_init_called]
|
276
|
-
assert_equal nil, response.data[:before_run_called]
|
277
|
-
assert_equal nil, response.data[:run_bang_called]
|
278
|
-
assert_equal nil, response.data[:after_run_called]
|
279
|
-
|
280
|
-
assert_equal 'after_init halting', response.status.message
|
205
|
+
should "raise a not implemented error when `run!` by default" do
|
206
|
+
assert_raises(NotImplementedError){ @handler_class.new(@runner).run! }
|
281
207
|
end
|
282
208
|
|
283
|
-
|
284
|
-
runner = test_runner(HaltingBehaviorServiceHandler, {
|
285
|
-
'when' => 'before_run'
|
286
|
-
})
|
287
|
-
response = runner.run
|
209
|
+
end
|
288
210
|
|
289
|
-
|
290
|
-
|
291
|
-
assert_equal true, response.data[:after_init_called]
|
292
|
-
assert_equal true, response.data[:before_run_called]
|
293
|
-
assert_equal nil, response.data[:run_bang_called]
|
294
|
-
assert_equal nil, response.data[:after_run_called]
|
211
|
+
class TestServiceHandler
|
212
|
+
include Sanford::ServiceHandler
|
295
213
|
|
296
|
-
|
297
|
-
|
214
|
+
attr_reader :first_before_init_call_order, :second_before_init_call_order
|
215
|
+
attr_reader :first_after_init_call_order, :second_after_init_call_order
|
216
|
+
attr_reader :first_before_run_call_order, :second_before_run_call_order
|
217
|
+
attr_reader :first_after_run_call_order, :second_after_run_call_order
|
218
|
+
attr_reader :init_call_order, :run_call_order
|
298
219
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
response = runner.run
|
220
|
+
# these methods are made public so they can be tested, they are being tested
|
221
|
+
# because they are used by classes that mixin this, essentially they are
|
222
|
+
# "public" to classes that use the mixin
|
223
|
+
public :render, :halt, :request, :params, :logger
|
304
224
|
|
305
|
-
|
306
|
-
|
307
|
-
assert_equal true, response.data[:after_init_called]
|
308
|
-
assert_equal true, response.data[:before_run_called]
|
309
|
-
assert_equal true, response.data[:run_bang_called]
|
310
|
-
assert_equal nil, response.data[:after_run_called]
|
225
|
+
before_init{ @first_before_init_call_order = next_call_order }
|
226
|
+
before_init{ @second_before_init_call_order = next_call_order }
|
311
227
|
|
312
|
-
|
313
|
-
|
228
|
+
after_init{ @first_after_init_call_order = next_call_order }
|
229
|
+
after_init{ @second_after_init_call_order = next_call_order }
|
314
230
|
|
315
|
-
|
316
|
-
|
317
|
-
'when' => 'after_run'
|
318
|
-
})
|
319
|
-
response = runner.run
|
231
|
+
before_run{ @first_before_run_call_order = next_call_order }
|
232
|
+
before_run{ @second_before_run_call_order = next_call_order }
|
320
233
|
|
321
|
-
|
322
|
-
|
323
|
-
assert_equal true, response.data[:after_init_called]
|
324
|
-
assert_equal true, response.data[:before_run_called]
|
325
|
-
assert_equal true, response.data[:run_bang_called]
|
326
|
-
assert_equal true, response.data[:after_run_called]
|
234
|
+
after_run{ @first_after_run_call_order = next_call_order }
|
235
|
+
after_run{ @second_after_run_call_order = next_call_order }
|
327
236
|
|
328
|
-
|
237
|
+
def init!
|
238
|
+
@init_call_order = next_call_order
|
329
239
|
end
|
330
240
|
|
331
|
-
|
332
|
-
|
333
|
-
class RenderHandlerTests < UnitTests
|
334
|
-
desc "render helper method"
|
335
|
-
|
336
|
-
should "render template files" do
|
337
|
-
response = test_runner(RenderHandler, 'template_name' => 'test_template').run
|
338
|
-
assert_equal ['test_template', 'RenderHandler', {}], response.data
|
241
|
+
def run!
|
242
|
+
@run_call_order = next_call_order
|
339
243
|
end
|
340
244
|
|
341
|
-
|
342
|
-
assert_raises ArgumentError do
|
343
|
-
test_runner(RenderHandler, 'template_name' => 'test_disallowed_template').run
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
end
|
245
|
+
private
|
348
246
|
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
should "allow easily running another handler" do
|
353
|
-
response = test_runner(RunOtherHandler).run
|
354
|
-
assert_equal 'RunOtherHandler', response.data
|
247
|
+
def next_call_order
|
248
|
+
@order ||= 0
|
249
|
+
@order += 1
|
355
250
|
end
|
356
|
-
|
357
251
|
end
|
358
252
|
|
359
|
-
class
|
360
|
-
|
253
|
+
class FakeRunner
|
254
|
+
attr_accessor :request, :params, :logger, :template_source
|
255
|
+
attr_reader :halt_calls
|
361
256
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
257
|
+
def initialize
|
258
|
+
@request = Factory.string
|
259
|
+
@params = Factory.string
|
260
|
+
@logger = Factory.string
|
261
|
+
@template_source = FakeTemplateSource.new
|
366
262
|
end
|
367
263
|
|
264
|
+
def halt(*args)
|
265
|
+
@halt_calls ||= []
|
266
|
+
@halt_calls << args
|
267
|
+
end
|
368
268
|
end
|
369
269
|
|
370
|
-
class
|
371
|
-
|
372
|
-
|
373
|
-
should "raise a BSON error when run in a test" do
|
374
|
-
assert_raises BSON::InvalidDocument do
|
375
|
-
test_runner(SerializeErrorServiceHandler).run
|
376
|
-
end
|
270
|
+
class FakeTemplateSource
|
271
|
+
def render(path, service_handler, locals)
|
272
|
+
[path.to_s, service_handler.class.to_s, locals]
|
377
273
|
end
|
378
|
-
|
379
274
|
end
|
380
275
|
|
381
276
|
end
|
@@ -50,7 +50,7 @@ class Sanford::TemplateEngine
|
|
50
50
|
class NullTemplateEngineTests < Assert::Context
|
51
51
|
desc "Sanford::NullTemplateEngine"
|
52
52
|
setup do
|
53
|
-
@engine = Sanford::NullTemplateEngine.new('source_path' =>
|
53
|
+
@engine = Sanford::NullTemplateEngine.new('source_path' => ROOT_PATH.to_s)
|
54
54
|
end
|
55
55
|
subject{ @engine }
|
56
56
|
|
@@ -10,7 +10,7 @@ class Sanford::TemplateSource
|
|
10
10
|
subject{ Sanford::TemplateSource }
|
11
11
|
|
12
12
|
should "disallow certain engine extensions" do
|
13
|
-
exp = [ '
|
13
|
+
exp = [ 'rb' ]
|
14
14
|
assert_equal exp, subject::DISALLOWED_ENGINE_EXTS
|
15
15
|
end
|
16
16
|
|
@@ -18,13 +18,13 @@ class Sanford::TemplateSource
|
|
18
18
|
|
19
19
|
class InitTests < Assert::Context
|
20
20
|
setup do
|
21
|
-
@source_path =
|
21
|
+
@source_path = ROOT_PATH.join('test/support').to_s
|
22
22
|
@source = Sanford::TemplateSource.new(@source_path)
|
23
23
|
end
|
24
24
|
subject{ @source }
|
25
25
|
|
26
26
|
should have_readers :path, :engines
|
27
|
-
should have_imeths :engine
|
27
|
+
should have_imeths :engine, :render
|
28
28
|
|
29
29
|
should "know its path" do
|
30
30
|
assert_equal @source_path.to_s, subject.path
|
@@ -35,44 +35,70 @@ class Sanford::TemplateSource
|
|
35
35
|
class EngineRegistrationTests < InitTests
|
36
36
|
desc "when registering an engine"
|
37
37
|
setup do
|
38
|
-
@
|
39
|
-
def render(path, scope); ''; end
|
40
|
-
end
|
38
|
+
@test_engine = TestEngine
|
41
39
|
end
|
42
40
|
|
43
41
|
should "allow registering new engines" do
|
44
|
-
assert_kind_of Sanford::NullTemplateEngine, subject.engines['
|
45
|
-
subject.engine '
|
46
|
-
assert_kind_of @
|
42
|
+
assert_kind_of Sanford::NullTemplateEngine, subject.engines['test']
|
43
|
+
subject.engine 'test', @test_engine
|
44
|
+
assert_kind_of @test_engine, subject.engines['test']
|
47
45
|
end
|
48
46
|
|
49
47
|
should "register with the source path as a default option" do
|
50
|
-
subject.engine '
|
48
|
+
subject.engine 'test', @test_engine
|
51
49
|
exp_opts = { 'source_path' => subject.path }
|
52
|
-
assert_equal exp_opts, subject.engines['
|
50
|
+
assert_equal exp_opts, subject.engines['test'].opts
|
53
51
|
|
54
|
-
subject.engine '
|
52
|
+
subject.engine 'test', @test_engine, 'an' => 'opt'
|
55
53
|
exp_opts = {
|
56
54
|
'source_path' => subject.path,
|
57
55
|
'an' => 'opt'
|
58
56
|
}
|
59
|
-
assert_equal exp_opts, subject.engines['
|
57
|
+
assert_equal exp_opts, subject.engines['test'].opts
|
60
58
|
|
61
|
-
subject.engine '
|
59
|
+
subject.engine 'test', @test_engine, 'source_path' => 'something'
|
62
60
|
exp_opts = { 'source_path' => 'something' }
|
63
|
-
assert_equal exp_opts, subject.engines['
|
61
|
+
assert_equal exp_opts, subject.engines['test'].opts
|
64
62
|
end
|
65
63
|
|
66
64
|
should "complain if registering a disallowed temp" do
|
67
65
|
assert_kind_of Sanford::NullTemplateEngine, subject.engines['rb']
|
68
66
|
assert_raises DisallowedEngineExtError do
|
69
|
-
subject.engine 'rb', @
|
67
|
+
subject.engine 'rb', @test_engine
|
70
68
|
end
|
71
69
|
assert_kind_of Sanford::NullTemplateEngine, subject.engines['rb']
|
72
70
|
end
|
73
71
|
|
74
72
|
end
|
75
73
|
|
74
|
+
class RenderTests < InitTests
|
75
|
+
desc "when rendering a template"
|
76
|
+
setup do
|
77
|
+
@source.engine('test', TestEngine)
|
78
|
+
@source.engine('json', JsonEngine)
|
79
|
+
end
|
80
|
+
|
81
|
+
should "render a matching template using the configured engine" do
|
82
|
+
locals = { :something => Factory.string }
|
83
|
+
result = subject.render('test_template', TestServiceHandler, locals)
|
84
|
+
assert_equal 'test-engine', result
|
85
|
+
end
|
86
|
+
|
87
|
+
should "only try rendering template files its has engines for" do
|
88
|
+
# there should be 2 files called "template" in `test/support` with diff
|
89
|
+
# extensions
|
90
|
+
result = subject.render('template', TestServiceHandler, {})
|
91
|
+
assert_equal 'json-engine', result
|
92
|
+
end
|
93
|
+
|
94
|
+
should "use the null template engine when an engine can't be found" do
|
95
|
+
assert_raises(ArgumentError) do
|
96
|
+
subject.render(Factory.string, TestServiceHandler, {})
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
76
102
|
class NullTemplateSourceTests < Assert::Context
|
77
103
|
desc "Sanford::NullTemplateSource"
|
78
104
|
setup do
|
@@ -90,4 +116,18 @@ class Sanford::TemplateSource
|
|
90
116
|
|
91
117
|
end
|
92
118
|
|
119
|
+
class TestEngine < Sanford::TemplateEngine
|
120
|
+
def render(path, service_handler, locals)
|
121
|
+
'test-engine'
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class JsonEngine < Sanford::TemplateEngine
|
126
|
+
def render(path, service_handler, locals)
|
127
|
+
'json-engine'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
TestServiceHandler = Class.new
|
132
|
+
|
93
133
|
end
|