sanford 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,7 +15,7 @@ module Sanford::Host
15
15
 
16
16
  should have_readers :configuration, :services
17
17
  should have_imeths :name, :ip, :port, :pid_file, :logger, :verbose_logging
18
- should have_imeths :runner, :error, :init, :service_handler_ns, :service
18
+ should have_imeths :error, :init, :service_handler_ns, :service
19
19
 
20
20
  should "know its configuration" do
21
21
  assert_kind_of Configuration, subject.configuration
@@ -54,10 +54,6 @@ module Sanford::Host
54
54
  subject.receives_keep_alive true
55
55
  assert_equal true, subject.receives_keep_alive
56
56
  assert_equal subject.receives_keep_alive, subject.configuration.receives_keep_alive
57
-
58
- subject.runner Sanford::DefaultRunner
59
- assert_equal Sanford::DefaultRunner, subject.runner
60
- assert_equal subject.runner, subject.configuration.runner
61
57
  end
62
58
 
63
59
  should "add error procs to the configuration" do
@@ -123,7 +119,7 @@ module Sanford::Host
123
119
  subject{ @configuration }
124
120
 
125
121
  should have_imeths :name, :ip, :port, :pid_file, :logger, :verbose_logging
126
- should have_imeths :receives_keep_alive, :runner, :error_procs, :init_procs
122
+ should have_imeths :receives_keep_alive, :error_procs, :init_procs
127
123
 
128
124
  should "default name to the class name of the host" do
129
125
  assert_equal @host_class.name, subject.name
@@ -133,10 +129,9 @@ module Sanford::Host
133
129
  assert_equal '0.0.0.0', subject.ip
134
130
  assert_nil subject.port
135
131
  assert_nil subject.pid_file
136
- assert_equal Sanford.config.logger, subject.logger
132
+ assert_equal Sanford.config.logger.class, subject.logger.class
137
133
  assert_true subject.verbose_logging
138
134
  assert_false subject.receives_keep_alive
139
- assert_equal Sanford.config.runner, subject.runner
140
135
  assert_empty subject.error_procs
141
136
  assert_empty subject.init_procs
142
137
  end
@@ -0,0 +1,50 @@
1
+ require 'assert'
2
+ require 'sanford/hosts'
3
+
4
+ require 'sanford/host'
5
+
6
+ class Sanford::Hosts
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Sandford::Hosts"
10
+ setup do
11
+ @hosts = Sanford::Hosts.new
12
+ end
13
+ subject{ @hosts }
14
+
15
+ should have_instance_methods :add, :first, :find
16
+
17
+ end
18
+
19
+ class FindTests < UnitTests
20
+ desc "find method"
21
+ setup do
22
+ @hosts.add ::NotNamedHost
23
+ @hosts.add ::NamedHost
24
+ @hosts.add ::BadlyNamedHost
25
+ end
26
+
27
+ should "allow finding hosts by their class name or configured name" do
28
+ assert_includes ::NotNamedHost, subject
29
+ assert_includes ::NamedHost, subject
30
+
31
+ assert_equal ::NotNamedHost, subject.find('NotNamedHost')
32
+ assert_equal ::NamedHost, subject.find('NamedHost')
33
+ assert_equal ::NamedHost, subject.find('named_host')
34
+ end
35
+
36
+ should "prefer hosts with a matching class name over configured name" do
37
+ assert_includes ::BadlyNamedHost, subject
38
+ assert_equal NotNamedHost, subject.find('NotNamedHost')
39
+ end
40
+
41
+ end
42
+
43
+ # Using this syntax because these classes need to be defined as top-level
44
+ # constants for ease in using their class names in the tests
45
+
46
+ ::NotNamedHost = Class.new{ include Sanford::Host }
47
+ ::NamedHost = Class.new{ include Sanford::Host; name 'named_host' }
48
+ ::BadlyNamedHost = Class.new{ include Sanford::Host; name 'NotNamedHost' }
49
+
50
+ end
@@ -112,7 +112,7 @@ module Sanford::Manager
112
112
 
113
113
  should have_imeths :pid, :to_s, :write, :remove
114
114
 
115
- should "return it's path with #to_s" do
115
+ should "return its path with #to_s" do
116
116
  assert_equal @pid_file_path, subject.to_s
117
117
  end
118
118
 
@@ -1,7 +1,7 @@
1
1
  require 'assert'
2
2
  require 'sanford/runner'
3
3
 
4
- require 'test/support/services'
4
+ require 'test/support/service_handlers'
5
5
 
6
6
  module Sanford::Runner
7
7
 
@@ -9,31 +9,26 @@ module Sanford::Runner
9
9
  desc "Sanford::Runner"
10
10
  setup do
11
11
  request = Sanford::Protocol::Request.new('test', {})
12
- @runner = Sanford::DefaultRunner.new(BasicServiceHandler, request)
12
+ @runner_class = Class.new do
13
+ include Sanford::Runner
14
+ end
15
+ @runner = @runner_class.new(BasicServiceHandler, request)
13
16
  end
14
17
  subject{ @runner }
15
18
 
16
19
  should have_cmeths :run
17
- should have_readers :handler_class, :request, :logger, :run
18
-
19
- should "run the handler and return the response it generates when `run` is called" do
20
- response = subject.run
21
-
22
- assert_instance_of Sanford::Protocol::Response, response
23
- assert_equal 200, response.code
24
- assert_equal 'Joe Test', response.data['name']
25
- assert_equal 'joe.test@example.com', response.data['email']
26
- end
20
+ should have_readers :handler_class, :request, :logger, :handler
21
+ should have_imeths :init, :init!, :run, :run!
22
+ should have_imeths :halt, :catch_halt
27
23
 
28
- should "be able to build a runner with a handler class and params" do
29
- response = nil
30
- assert_nothing_raised do
31
- response = Sanford::DefaultRunner.run(BasicServiceHandler, {})
24
+ should "not implement the run behavior" do
25
+ assert_raises NotImplementedError do
26
+ subject.run
32
27
  end
33
-
34
- assert_equal 200, response.code
35
28
  end
36
29
 
37
30
  end
38
31
 
32
+ # runner behavior tests are handled via system tests
33
+
39
34
  end
@@ -0,0 +1,66 @@
1
+ require 'assert'
2
+ require 'sanford/sanford_runner'
3
+
4
+ require 'sanford/runner'
5
+ require 'test/support/service_handlers'
6
+
7
+ class Sanford::SanfordRunner
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "Sanford::SanfordRunner"
11
+ setup do
12
+ @runner_class = Sanford::SanfordRunner
13
+ end
14
+ subject{ @runner_class }
15
+
16
+ should "be a Runner" do
17
+ assert_includes Sanford::Runner, subject
18
+ end
19
+
20
+ should "be able to build a runner with a handler class and params and run it" do
21
+ response = nil
22
+ assert_nothing_raised do
23
+ response = subject.run(BasicServiceHandler, {})
24
+ end
25
+
26
+ assert_equal 200, response.code
27
+ end
28
+
29
+ end
30
+
31
+ class InitTests < UnitTests
32
+ desc "when init"
33
+ setup do
34
+ @request = Sanford::Protocol::Request.new('test', {})
35
+ @runner = @runner_class.new(BasicServiceHandler, @request)
36
+ end
37
+ subject{ @runner }
38
+
39
+ should "run the handler and return the response it generates when run" do
40
+ response = subject.run
41
+
42
+ assert_instance_of Sanford::Protocol::Response, response
43
+ assert_equal 200, response.code
44
+ assert_equal 'Joe Test', response.data['name']
45
+ assert_equal 'joe.test@example.com', response.data['email']
46
+ end
47
+
48
+ end
49
+
50
+ class CallbackTests < InitTests
51
+ setup do
52
+ @runner = @runner_class.new(FlagServiceHandler, @request)
53
+ end
54
+
55
+ should "call handler `before` and `after` callbacks when run" do
56
+ subject.run
57
+
58
+ assert_true subject.handler.before_called
59
+ assert_true subject.handler.after_called
60
+ end
61
+
62
+ end
63
+
64
+ # live runner behavior tests are handled via system tests
65
+
66
+ end
@@ -1,7 +1,7 @@
1
1
  require 'assert'
2
2
  require 'sanford'
3
3
 
4
- require 'ns-options/proxy'
4
+ require 'sanford/config'
5
5
 
6
6
  module Sanford
7
7
 
@@ -11,60 +11,10 @@ module Sanford
11
11
 
12
12
  should have_imeths :config, :configure, :init, :register, :hosts
13
13
 
14
- end
15
-
16
- class ConfigTests < UnitTests
17
- desc "Config"
18
- subject{ Sanford::Config }
19
-
20
- should have_imeths :services_file, :logger, :runner
21
-
22
- should "be an NsOptions::Proxy" do
23
- assert_includes NsOptions::Proxy, subject
24
- end
25
-
26
- end
27
-
28
- class HostsTests < UnitTests
29
- desc "Hosts"
30
- setup do
31
- @hosts = Sanford::Hosts.new
32
- end
33
- subject{ @hosts }
34
-
35
- should have_instance_methods :add, :first, :find
36
-
37
- end
38
-
39
- class FindTests < HostsTests
40
- desc "find method"
41
- setup do
42
- @hosts.add ::NotNamedHost
43
- @hosts.add ::NamedHost
44
- @hosts.add ::BadlyNamedHost
45
- end
46
-
47
- should "allow finding hosts by their class name or configured name" do
48
- assert_includes ::NotNamedHost, subject
49
- assert_includes ::NamedHost, subject
50
-
51
- assert_equal ::NotNamedHost, subject.find('NotNamedHost')
52
- assert_equal ::NamedHost, subject.find('NamedHost')
53
- assert_equal ::NamedHost, subject.find('named_host')
54
- end
55
-
56
- should "prefer hosts with a matching class name over configured name" do
57
- assert_includes ::BadlyNamedHost, subject
58
- assert_equal NotNamedHost, subject.find('NotNamedHost')
14
+ should "return a `Config` instance with the `config` method" do
15
+ assert_kind_of Sanford::Config, subject.config
59
16
  end
60
17
 
61
18
  end
62
19
 
63
- # Using this syntax because these classes need to be defined as top-level
64
- # constants for ease in using their class names in the tests
65
-
66
- ::NotNamedHost = Class.new{ include Sanford::Host }
67
- ::NamedHost = Class.new{ include Sanford::Host; name 'named_host' }
68
- ::BadlyNamedHost = Class.new{ include Sanford::Host; name 'NotNamedHost' }
69
-
70
20
  end
@@ -1,8 +1,6 @@
1
1
  require 'assert'
2
2
  require 'sanford/server'
3
3
 
4
- require 'test/support/services'
5
-
6
4
  class Sanford::Server
7
5
 
8
6
  class UnitTests < Assert::Context
@@ -19,7 +17,7 @@ class Sanford::Server
19
17
  assert_includes DatTCP::Server, subject.class
20
18
  end
21
19
 
22
- should "save it's host and host options but not initialize a host data yet" do
20
+ should "save its host and host options but not initialize a host data yet" do
23
21
  assert_equal TestHost, subject.sanford_host
24
22
  assert_equal true, subject.sanford_host_options[:receives_keep_alive]
25
23
  assert_nil subject.sanford_host_data
@@ -17,10 +17,13 @@ module Sanford::ServiceHandler
17
17
  subject{ @handler_class }
18
18
 
19
19
  should have_imeths :run
20
+ should have_imeths :before_callbacks, :after_callbacks
20
21
  should have_imeths :before_init_callbacks, :after_init_callbacks
21
22
  should have_imeths :before_run_callbacks, :after_run_callbacks
23
+ should have_imeths :before, :after
22
24
  should have_imeths :before_init, :after_init
23
25
  should have_imeths :before_run, :after_run
26
+ should have_imeths :prepend_before, :prepend_after
24
27
  should have_imeths :prepend_before_init, :prepend_after_init
25
28
  should have_imeths :prepend_before_run, :prepend_after_run
26
29
 
@@ -33,6 +36,14 @@ module Sanford::ServiceHandler
33
36
  assert_equal true, response.data
34
37
  end
35
38
 
39
+ should "return an empty array by default using `before_callbacks`" do
40
+ assert_equal [], subject.before_callbacks
41
+ end
42
+
43
+ should "return an empty array by default using `after_callbacks`" do
44
+ assert_equal [], subject.after_callbacks
45
+ end
46
+
36
47
  should "return an empty array by default using `before_init_callbacks`" do
37
48
  assert_equal [], subject.before_init_callbacks
38
49
  end
@@ -49,6 +60,20 @@ module Sanford::ServiceHandler
49
60
  assert_equal [], subject.after_run_callbacks
50
61
  end
51
62
 
63
+ should "append a block to the before callbacks using `before`" do
64
+ subject.before_callbacks << proc{ }
65
+ block = Proc.new{}
66
+ subject.before(&block)
67
+ assert_equal block, subject.before_callbacks.last
68
+ end
69
+
70
+ should "append a block to the after callbacks using `after`" do
71
+ subject.after_callbacks << proc{ }
72
+ block = Proc.new{}
73
+ subject.after(&block)
74
+ assert_equal block, subject.after_callbacks.last
75
+ end
76
+
52
77
  should "append a block to the before init callbacks using `before_init`" do
53
78
  subject.before_init_callbacks << proc{ }
54
79
  block = Proc.new{}
@@ -77,6 +102,20 @@ module Sanford::ServiceHandler
77
102
  assert_equal block, subject.after_run_callbacks.last
78
103
  end
79
104
 
105
+ should "prepend a block to the before callbacks using `prepend_before`" do
106
+ subject.before_callbacks << proc{ }
107
+ block = Proc.new{}
108
+ subject.prepend_before(&block)
109
+ assert_equal block, subject.before_callbacks.first
110
+ end
111
+
112
+ should "prepend a block to the after callbacks using `prepend_after`" do
113
+ subject.after_callbacks << proc{ }
114
+ block = Proc.new{}
115
+ subject.prepend_after(&block)
116
+ assert_equal block, subject.after_callbacks.first
117
+ end
118
+
80
119
  should "prepend a block to the before init callbacks using `prepend_before_init`" do
81
120
  subject.before_init_callbacks << proc{ }
82
121
  block = Proc.new{}
@@ -121,21 +160,25 @@ module Sanford::ServiceHandler
121
160
  assert_raises(NotImplementedError){ handler.run! }
122
161
  end
123
162
 
124
- should "have called `init!` and it's callbacks" do
163
+ should "not call `before` callbacks when using a test runner" do
164
+ assert_nil subject.before_called
165
+ end
166
+
167
+ should "have called `init!` and its callbacks" do
125
168
  assert_true subject.before_init_called
126
169
  assert_true subject.second_before_init_called
127
170
  assert_true subject.init_bang_called
128
171
  assert_true subject.after_init_called
129
172
  end
130
173
 
131
- should "not have called `run!` or it's callbacks when initialized" do
174
+ should "not have called `run!` or its callbacks when initialized" do
132
175
  assert_nil subject.before_run_called
133
176
  assert_nil subject.run_bang_called
134
177
  assert_nil subject.after_run_called
135
178
  assert_nil subject.second_after_run_called
136
179
  end
137
180
 
138
- should "call `run!` and it's callbacks when it's `run`" do
181
+ should "call `run!` and its callbacks when its run" do
139
182
  subject.run
140
183
 
141
184
  assert_true subject.before_run_called
@@ -144,15 +187,11 @@ module Sanford::ServiceHandler
144
187
  assert_true subject.second_after_run_called
145
188
  end
146
189
 
147
- end
148
-
149
- class RunHandlerTests < UnitTests
150
- desc "run_handler helper"
151
-
152
- should "allow easily running another handler" do
153
- response = test_runner(RunOtherHandler).run
154
- assert_equal 'RunOtherHandler', response.data
190
+ should "not call `after` callbacks when run using a test runner" do
191
+ subject.run
192
+ assert_nil subject.after_called
155
193
  end
194
+
156
195
  end
157
196
 
158
197
  class HaltTests < UnitTests
@@ -203,7 +242,7 @@ module Sanford::ServiceHandler
203
242
  assert_equal 'before_init halting', response.status.message
204
243
  end
205
244
 
206
- should "not call `after_init`, `run!` or it's callbacks when `init!` halts" do
245
+ should "not call `after_init`, `run!` or its callbacks when `init!` halts" do
207
246
  runner = test_runner(HaltingBehaviorServiceHandler, {
208
247
  'when' => 'init!'
209
248
  })
@@ -219,7 +258,7 @@ module Sanford::ServiceHandler
219
258
  assert_equal 'init! halting', response.status.message
220
259
  end
221
260
 
222
- should "not call `run!` or it's callbacks when `after_init` halts" do
261
+ should "not call `run!` or its callbacks when `after_init` halts" do
223
262
  runner = test_runner(HaltingBehaviorServiceHandler, {
224
263
  'when' => 'after_init'
225
264
  })
@@ -285,6 +324,26 @@ module Sanford::ServiceHandler
285
324
 
286
325
  end
287
326
 
327
+ class RenderHandlerTests < UnitTests
328
+ desc "render helper method"
329
+
330
+ should "render template files" do
331
+ response = test_runner(RenderHandler).run
332
+ assert_equal ['test_template', 'RenderHandler', {}], response.data
333
+ end
334
+
335
+ end
336
+
337
+ class RunHandlerTests < UnitTests
338
+ desc "run_handler helper"
339
+
340
+ should "allow easily running another handler" do
341
+ response = test_runner(RunOtherHandler).run
342
+ assert_equal 'RunOtherHandler', response.data
343
+ end
344
+
345
+ end
346
+
288
347
  class InvalidHandlerTests < UnitTests
289
348
  desc "that is invalid"
290
349
 
@@ -0,0 +1,76 @@
1
+ require 'assert'
2
+ require 'sanford/template_engine'
3
+
4
+ require 'pathname'
5
+ require 'test/support/factory'
6
+
7
+ class Sanford::TemplateEngine
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "Sanford::TemplateEngine"
11
+ setup do
12
+ @source_path = Factory.path
13
+ @path = Factory.path
14
+ @service_handler = 'a-service-handler'
15
+ @locals = {}
16
+ @engine = Sanford::TemplateEngine.new('some' => 'opts')
17
+ end
18
+ subject{ @engine }
19
+
20
+ should have_readers :source_path, :opts
21
+ should have_imeths :render
22
+
23
+ should "default its source path" do
24
+ assert_equal Pathname.new(nil.to_s), subject.source_path
25
+ end
26
+
27
+ should "allow custom source paths" do
28
+ engine = Sanford::TemplateEngine.new('source_path' => @source_path)
29
+ assert_equal Pathname.new(@source_path.to_s), engine.source_path
30
+ end
31
+
32
+ should "default the opts if none given" do
33
+ exp_opts = {}
34
+ assert_equal exp_opts, Sanford::TemplateEngine.new.opts
35
+ end
36
+
37
+ should "allow custom opts" do
38
+ exp_opts = {'some' => 'opts'}
39
+ assert_equal exp_opts, subject.opts
40
+ end
41
+
42
+ should "raise NotImplementedError on `render`" do
43
+ assert_raises NotImplementedError do
44
+ subject.render(@path, @service_handler, @locals)
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ class NullTemplateEngineTests < Assert::Context
51
+ desc "Sanford::NullTemplateEngine"
52
+ setup do
53
+ @engine = Sanford::NullTemplateEngine.new('source_path' => ROOT)
54
+ end
55
+ subject{ @engine }
56
+
57
+ should "be a TemplateEngine" do
58
+ assert_kind_of Sanford::TemplateEngine, subject
59
+ end
60
+
61
+ should "read and return the given path in its source path on `render" do
62
+ exists_file = 'test/support/template.json'
63
+ exp = File.read(subject.source_path.join(exists_file).to_s)
64
+ assert_equal exp, subject.render(exists_file, @service_handler, @locals)
65
+ end
66
+
67
+ should "complain if given a path that does not exist in its source path" do
68
+ no_exists_file = '/does/not/exists'
69
+ assert_raises ArgumentError do
70
+ subject.render(no_exists_file, @service_handler, @locals)
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,75 @@
1
+ require 'assert'
2
+ require 'sanford/template_source'
3
+
4
+ require 'sanford/template_engine'
5
+
6
+ class Sanford::TemplateSource
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Sanford::TemplateSource"
10
+ setup do
11
+ @source_path = File.join(ROOT, 'test/support')
12
+ @source = Sanford::TemplateSource.new(@source_path)
13
+ end
14
+ subject{ @source }
15
+
16
+ should have_readers :path, :engines
17
+ should have_imeths :engine
18
+
19
+ should "know its path" do
20
+ assert_equal @source_path.to_s, subject.path
21
+ end
22
+
23
+ end
24
+
25
+ class EngineRegistrationTests < UnitTests
26
+ desc "when registering an engine"
27
+ setup do
28
+ @empty_engine = Class.new(Sanford::TemplateEngine) do
29
+ def render(path, scope); ''; end
30
+ end
31
+ end
32
+
33
+ should "allow registering new engines" do
34
+ assert_kind_of Sanford::NullTemplateEngine, subject.engines['empty']
35
+ subject.engine 'empty', @empty_engine
36
+ assert_kind_of @empty_engine, subject.engines['empty']
37
+ end
38
+
39
+ should "register with the source path as a default option" do
40
+ subject.engine 'empty', @empty_engine
41
+ exp_opts = { 'source_path' => subject.path }
42
+ assert_equal exp_opts, subject.engines['empty'].opts
43
+
44
+ subject.engine 'empty', @empty_engine, 'an' => 'opt'
45
+ exp_opts = {
46
+ 'source_path' => subject.path,
47
+ 'an' => 'opt'
48
+ }
49
+ assert_equal exp_opts, subject.engines['empty'].opts
50
+
51
+ subject.engine 'empty', @empty_engine, 'source_path' => 'something'
52
+ exp_opts = { 'source_path' => 'something' }
53
+ assert_equal exp_opts, subject.engines['empty'].opts
54
+ end
55
+
56
+ end
57
+
58
+ class NullTemplateSourceTests < Assert::Context
59
+ desc "Sanford::NullTemplateSource"
60
+ setup do
61
+ @source = Sanford::NullTemplateSource.new
62
+ end
63
+ subject{ @source }
64
+
65
+ should "be a template source" do
66
+ assert_kind_of Sanford::TemplateSource, subject
67
+ end
68
+
69
+ should "have an empty path string" do
70
+ assert_equal '', subject.path
71
+ end
72
+
73
+ end
74
+
75
+ end