skates 0.5.0 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- describe Skates::Generator::ApplicationGenerator do
4
- # Hum, how do we spec Templater?
5
- end
6
-
7
-
8
- describe Skates::Generator::ControllerGenerator do
9
- # Hum, how do we spec Templater?
10
- end
@@ -1,46 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
-
3
- describe Skates::Router::DSL do
4
- before(:each) do
5
- Skates.router = Skates::StanzaRouter.new
6
- Skates.router.purge_routes!
7
- class ControllerController; end
8
- end
9
-
10
- it "raises an exception if the route lacks a controller" do
11
- lambda { Skates.router.draw do
12
- xpath("/test").to(:action => "foo")
13
- end }.should raise_error(/controller/)
14
- end
15
-
16
- it "raises an exception if the route lacks an action" do
17
- lambda { Skates.router.draw do
18
- xpath("/test").to(:controller => "foo")
19
- end }.should raise_error(/action/)
20
- end
21
-
22
- it "raises an exception if the route has no destination" do
23
- lambda { Skates.router.draw do
24
- xpath("//test")
25
- end }.should raise_error(/destination/)
26
- end
27
-
28
- it "creates a route with the specified xpath, controller, action and priority" do
29
- Skates.router.draw do
30
- xpath("//test"
31
- ).to(:controller => "controller", :action => "action").priority(5)
32
- end
33
- routes = Skates.router.instance_variable_get("@routes")
34
- routes.length.should == 1
35
- end
36
-
37
- it "should create routes with the right namespace" do
38
- Skates.router.draw do
39
- xpath("//ns:test", {"ns" => "http://my.namespace.uri"}
40
- ).to(:controller => "controller", :action => "action").priority(5)
41
- end
42
- route = Skates.router.instance_variable_get("@routes").first
43
- route.xpath.should == ["//ns:test", {"ns"=>"http://my.namespace.uri"}]
44
- end
45
-
46
- end
@@ -1,252 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- describe Skates::Route do
4
- before(:each) do
5
- @controller = "bar"
6
- @action = "bar"
7
- @xpath = "//message"
8
- Kernel.stub!(:const_get).with("#{@controller.capitalize}Controller")
9
- end
10
-
11
- describe ".initialize" do
12
- it "should raise an exception if no controller is specified" do
13
- lambda { Skates::Route.new("action" => @action, "xpath" => @xpath) }.should raise_error(/controller/)
14
- end
15
- it "should raise an exception if no action is specified" do
16
- lambda { Skates::Route.new("controller" => @controller, "xpath" => @xpath) }.should raise_error(/action/)
17
- end
18
- it "should raise an exception if no xpath is specified" do
19
- lambda { Skates::Route.new("action" => @action, "controller" => @controller) }.should raise_error(/xpath/)
20
- end
21
- end
22
-
23
- describe ".accepts?" do
24
- it "should check the stanza with Xpath" do
25
- mock_stanza = mock(Object)
26
- route = Skates::Route.new("controller" => "bar", "action" => "bar", "xpath" => ["//message", {}])
27
- mock_stanza.should_receive(:xpath).with("//message", {}).and_return([])
28
- route.accepts?(mock_stanza)
29
- end
30
- end
31
- end
32
-
33
-
34
- describe Skates::StanzaRouter do
35
-
36
- before(:each) do
37
- @router = Skates::StanzaRouter.new
38
- end
39
-
40
- describe "initialize" do
41
- it "should have an empty array as routes" do
42
- @router.routes.should == []
43
- end
44
- end
45
-
46
- describe "connected" do
47
- it "should set the connection" do
48
- connection = mock(Object)
49
- @router.connected(connection)
50
- @router.connection.should == connection
51
- end
52
- end
53
-
54
- describe "route" do
55
- before(:each) do
56
- @xml = mock(Nokogiri::XML::Node)
57
- 3.times do |t|
58
- @router.routes << mock(Skates::Route, :accepts? => false)
59
- end
60
- end
61
-
62
- context "when the before_route callback is defined" do
63
- before(:each) do
64
- @proc = Proc.new { |stanza|
65
- }
66
- @router.before_route(&@proc)
67
- end
68
-
69
- it "should call the callback" do
70
- @proc.should_receive(:call).with(@xml)
71
- @router.route(@xml)
72
- end
73
-
74
- context "when the callback returns true" do
75
- before(:each) do
76
- @proc = Proc.new { |stanza|
77
- true
78
- }
79
- @router.before_route(&@proc)
80
- end
81
-
82
- it "should not even check if a route accepts this stanza" do
83
- @router.routes.each do |r|
84
- r.should_not_receive(:accepts?).with(@xml)
85
- end
86
- @router.route(@xml)
87
- end
88
- end
89
-
90
- context "when the callback returns false" do
91
- before(:each) do
92
- @proc = Proc.new { |stanza|
93
- false
94
- }
95
- @router.before_route(&@proc)
96
- end
97
-
98
- it "should check if a route accepts this stanza" do
99
- @router.routes.each do |r|
100
- r.should_receive(:accepts?).with(@xml)
101
- end
102
- @router.route(@xml)
103
- end
104
- end
105
-
106
- context "when the callback raises an error" do
107
- before(:each) do
108
- @proc = Proc.new { |stanza|
109
- raise
110
- }
111
- @router.before_route(&@proc)
112
- end
113
-
114
- it "should check if a route accepts this stanza" do
115
- @router.routes.each do |r|
116
- r.should_receive(:accepts?).with(@xml)
117
- end
118
- @router.route(@xml)
119
- end
120
- end
121
-
122
- end
123
-
124
- context "when the before_route callback is not defined" do
125
- it "should check each routes to see if they match the stanza and take the first of the matching" do
126
- @router.routes.each do |r|
127
- r.should_receive(:accepts?).with(@xml)
128
- end
129
- @router.route(@xml)
130
- end
131
-
132
- context "if one route is found" do
133
- before(:each) do
134
- @accepting_route = mock(Skates::Route, :accepts? => true, :action => "action", :controller => "controller", :xpath => "xpath")
135
- @router.routes << @accepting_route
136
- end
137
-
138
- it "should call execute_route" do
139
- @router.should_receive(:execute_route).with(@accepting_route.controller, @accepting_route.action, @xml)
140
- @router.route(@xml)
141
- end
142
- end
143
-
144
- context "if no route matches the stanza" do
145
- it "should return false" do
146
- @router.route(@xml).should be_false
147
- end
148
- end
149
- end
150
- end
151
-
152
- describe "execute_route" do
153
- before(:each) do
154
- @action = "action"
155
- @controller = Skates::Base::Controller
156
- @xml = mock(Nokogiri::XML::Node)
157
- @mock_stanza = mock(Skates::Base::Stanza)
158
- @mock_controller = mock(Skates::Base::Controller, {:new => true, :evaluate => "hello world"})
159
- Kernel.stub!(:const_get).with(@action.capitalize).and_return(Skates::Base::Stanza)
160
- Skates::Base::Stanza.stub!(:new).with(@xml).and_return(@mock_stanza)
161
- @connection = mock(Skates::XmppConnection, :send_xml => true)
162
- @router.stub!(:connection).and_return(@connection)
163
- @controller.stub!(:new).and_return(@mock_controller)
164
- @mock_controller.stub!(:perform).with(@action)
165
- end
166
-
167
- describe "when the Stanza class exists" do
168
- it "should instantiate the route's stanza " do
169
- Kernel.should_receive(:const_get).with(@action.capitalize).and_return(Skates::Base::Stanza)
170
- Skates::Base::Stanza.should_receive(:new).with(@xml).and_return(@mock_stanza)
171
- @router.execute_route(@controller, @action, @xml)
172
- end
173
-
174
- it "should instantiate the route's controller" do
175
- @controller.should_receive(:new).with(@mock_stanza).and_return(@mock_controller)
176
- @router.execute_route(@controller, @action, @xml)
177
- end
178
- end
179
-
180
- describe "when the stanza class doesn't exist" do
181
- it "should instantiate the route's controller with the xml" do
182
- Kernel.should_receive(:const_get).with(@action.capitalize).and_raise(NameError)
183
- @controller.should_receive(:new).with(@xml).and_return(@mock_controller)
184
- @router.execute_route(@controller, @action, @xml)
185
- end
186
- end
187
-
188
- it "should call perform on the controller with the action's name" do
189
- @mock_controller.should_receive(:perform).with(@action)
190
- @router.execute_route(@controller, @action, @xml)
191
- end
192
-
193
- it "should send the controller's response to the connection" do
194
- @connection.should_receive(:send_xml).with(@mock_controller.evaluate)
195
- @router.execute_route(@controller, @action, @xml)
196
- end
197
- end
198
-
199
- describe "purge_routes!" do
200
- it "should delete all routes" do
201
- @router.instance_variable_set("@routes", [mock(Skates::Route), mock(Skates::Route)])
202
- @router.purge_routes!
203
- @router.routes.should == []
204
- end
205
- end
206
-
207
- describe "draw" do
208
- before(:each) do
209
- @dsl = Skates::Router::DSL.new
210
- Skates::Router::DSL.stub!(:new).and_return(@dsl)
211
- @routes = [mock(Skates::Route, :is_a? => true), mock(Skates::Route, :is_a? => true), mock(Skates::Route, :is_a? => true)]
212
- @router.stub!(:sort)
213
- @dsl.stub!(:routes).and_return(@routes)
214
- end
215
-
216
- it "shoudl instantiate a new DSL" do
217
- Skates::Router::DSL.should_receive(:new).and_return(@dsl)
218
- @router.draw {}
219
- end
220
-
221
- it "should instance_eval the block" do
222
- block = Proc.new {}
223
- @dsl.should_receive(:instance_eval).with(&block)
224
- @router.draw &block
225
- end
226
-
227
- it "should check that each route is a Route" do
228
- @dsl.should_receive(:routes).twice.and_return(@routes)
229
- @router.draw {}
230
- end
231
-
232
- it "should raise an error if one of the routes is not valid" do
233
- @dsl.should_receive(:routes).and_return([mock(Skates::Route, :is_a? => false)])
234
- lambda {
235
- @router.draw {}
236
- }.should raise_error()
237
- end
238
-
239
- it "should assign the dsl routes as @routes" do
240
- @dsl.should_receive(:routes).twice.and_return(@routes)
241
- @router.draw {}
242
- @router.routes.should == @routes
243
- end
244
-
245
- it "should sort the routes" do
246
- @router.should_receive(:sort)
247
- @router.draw {}
248
- end
249
-
250
- end
251
-
252
- end
@@ -1,233 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
- require File.dirname(__FILE__) + '/../../em_mock'
3
-
4
- describe Skates::Runner do
5
- before(:all) do
6
- FileUtils.chdir("#{FileUtils.pwd}/templates/skates") unless ("#{FileUtils.pwd}" =~ /\/templates\/skates/ )
7
- end
8
-
9
- describe ".prepare" do
10
- before(:each) do
11
- @config = {"production"=>{"port"=>5278, "auto-reconnect"=>true, "jid"=>"component.server.com", "host"=>"localhost", "password"=>"password"}, "development"=>{"auto-reconnect"=>true, "jid"=>"user@server.com", "application_type"=>"client", "password"=>"password"}, "test"=>{"port"=>5278, "auto-reconnect"=>true, "jid"=>"component.server.com", "host"=>"localhost", "password"=>"password"}}
12
- Skates.config_file = "config/config.yaml"
13
- YAML.stub(:load_file).and_return(@config)
14
- Skates::Runner.stub!(:require_directory).and_return(true)
15
- end
16
-
17
- it "should add the environment log file as an outputter to skates's default log" do
18
- Skates.should_receive(:reopen_logs)
19
- Skates::Runner.prepare("test")
20
- end
21
-
22
- it "should require all models" do
23
- Skates::Runner.should_receive(:require_directory).with('app/models/*.rb').and_return(true)
24
- Skates::Runner.prepare("test")
25
- end
26
-
27
- it "should require all stanzas" do
28
- Skates::Runner.should_receive(:require_directory).with('app/stanzas/*.rb').and_return(true)
29
- Skates::Runner.prepare("test")
30
- end
31
-
32
- it "should require all controllers" do
33
- Skates::Runner.should_receive(:require_directory).with('app/controllers/*_controller.rb').and_return(true)
34
- Skates::Runner.prepare("test")
35
- end
36
-
37
- it "should create a router" do
38
- router = Skates::StanzaRouter.new
39
- Skates::StanzaRouter.should_receive(:new).and_return(router)
40
- Skates.should_receive(:router=).with(router)
41
- Skates::Runner.prepare("test")
42
- end
43
-
44
- it "should load the routes" do
45
- Skates::Runner.should_receive(:require).with('config/routes.rb')
46
- Skates::Runner.prepare("test")
47
- end
48
-
49
- it "should load the configuration file" do
50
- YAML.should_receive(:load_file).with('config/config.yaml').and_return(@config)
51
- Skates::Runner.prepare("test")
52
- end
53
-
54
- it "should assign the configuration" do
55
- Skates::Runner.prepare("test")
56
- Skates.config.should == {"port"=>5278, "jid"=>"component.server.com", "auto-reconnect"=>true, "host"=>"localhost", "password"=>"password"}
57
- end
58
-
59
- it "should cache the views" do
60
- Skates.should_receive(:cache_views)
61
- Skates::Runner.prepare("test")
62
- end
63
- end
64
-
65
- describe "require_directory" do
66
- before(:each) do
67
- @dir = "/my/dir"
68
- @files = ["hello.rb", "byebye.rb"]
69
- Dir.stub!(:glob).with(@dir).and_return(@files)
70
- @files.each do |f|
71
- Skates::Runner.stub!(:require).with(f).and_return(true)
72
- end
73
- end
74
- it "should list all files in the directory" do
75
- Dir.should_receive(:glob).with(@dir).and_return(@files)
76
- Skates::Runner.require_directory(@dir)
77
- end
78
- it "should require each of the files" do
79
- @files.each do |f|
80
- Skates::Runner.should_receive(:require).with(f).and_return(true)
81
- end
82
- Skates::Runner.require_directory(@dir)
83
- end
84
- end
85
-
86
- describe ".run" do
87
- before(:each) do
88
- Skates::ClientConnection.stub!(:connect).and_return(true)
89
- Skates::ComponentConnection.stub!(:connect).and_return(true)
90
- EventMachine.stub!(:run).and_yield
91
- end
92
-
93
- it "should set the environment" do
94
- Skates::Runner.run("test")
95
- Skates.environment.should == "test"
96
- end
97
-
98
- it "should epoll the EventMachine" do
99
- EventMachine.should_receive(:epoll)
100
- Skates::Runner.run("test")
101
- end
102
-
103
- it "should run the EventMachine" do
104
- EventMachine.should_receive(:run)
105
- Skates::Runner.run("test")
106
- end
107
-
108
- it "should call prepare" do
109
- Skates::Runner.should_receive(:prepare).with("test")
110
- Skates::Runner.run("test")
111
- end
112
-
113
- it "should connect the client connection if specified by the config" do
114
- Skates.stub!(:config).and_return({"application_type" => "client"})
115
- Skates::ClientConnection.should_receive(:connect).with(Skates.config, Skates::Runner)
116
- Skates::Runner.run("test")
117
- end
118
-
119
- it "should connect the component connection if no application_type specified by the config" do
120
- Skates.stub!(:config).and_return({})
121
- Skates::ComponentConnection.should_receive(:connect).with(Skates.config, Skates::Runner)
122
- Skates::Runner.run("test")
123
- end
124
-
125
- end
126
-
127
- describe ".connection_observers" do
128
- it "should return an array" do
129
- Skates::Runner.connection_observers.should be_an_instance_of(Array)
130
- end
131
- end
132
-
133
- describe ".add_connection_observer" do
134
- before(:each) do
135
- class MyController < Skates::Base::Controller; end
136
- end
137
-
138
- it "should not accept non-Skates::Base::Controller subclasses" do
139
- Skates::Runner.add_connection_observer(Object).should be_false
140
- end
141
-
142
- it "should accept" do
143
- Skates::Runner.add_connection_observer(MyController).should be_true
144
- end
145
-
146
- it "should add it to the list of observers" do
147
- observers = Skates::Runner.connection_observers
148
- Skates::Runner.add_connection_observer(MyController)
149
- observers.include?(MyController).should be_true
150
- end
151
-
152
- it "should not add it twice" do
153
- observers = Skates::Runner.connection_observers
154
- Skates::Runner.add_connection_observer(MyController)
155
- Skates::Runner.add_connection_observer(MyController)
156
- observers.should == [MyController]
157
- end
158
-
159
- end
160
-
161
- describe ".on_connected" do
162
- before(:each) do
163
- @connection = mock(Object)
164
- Skates.router = Skates::StanzaRouter.new
165
- Skates.router.stub!(:connected).with(@connection)
166
- Skates.router.stub!(:execute_route).with(MyController, "on_connected")
167
- end
168
-
169
- it "should call connected on StanzaRouter" do
170
- Skates.router.should_receive(:connected).with(@connection)
171
- Skates::Runner.on_connected(@connection)
172
- end
173
-
174
- it "should call on_connected on the various observers and send the corresponding response" do
175
- Skates::Runner.add_connection_observer(MyController)
176
- Skates.router.should_receive(:execute_route).with(MyController, "on_connected")
177
- Skates::Runner.on_connected(@connection)
178
- end
179
- end
180
-
181
- describe ".on_disconnected" do
182
- it "should call on_disconnected on the various observers" do
183
- class MyObserver < Skates::Base::Controller; def on_disconnected; end; end
184
- my_observer = MyObserver.new
185
- Skates::Runner.add_connection_observer(MyObserver)
186
- MyObserver.should_receive(:new).and_return(my_observer)
187
- my_observer.should_receive(:on_disconnected)
188
- Skates::Runner.on_disconnected
189
- end
190
-
191
- context "when the application should auto-reconnect" do
192
- before(:each) do
193
- Skates.config["auto-reconnect"] = true
194
- EventMachine.stub!(:reactor_running?).and_return(false)
195
- EventMachine.stub!(:add_timer).and_yield()
196
- @delay = 15
197
- Skates::Runner.stub!(:fib).and_return(@delay)
198
- end
199
-
200
- it "should determine when is the best time to reconnect with fibonacci" do
201
- Skates::Runner.should_receive(:fib).and_return(@delay)
202
- Skates::Runner.on_disconnected()
203
- end
204
-
205
- it "should try to reconnect at the determined time" do
206
- EventMachine.stub!(:reactor_running?).and_return(true)
207
- Skates::Runner.should_receive(:reconnect)
208
- EventMachine.should_receive(:add_timer).with(@delay).and_yield()
209
- Skates::Runner.on_disconnected()
210
- end
211
- end
212
-
213
- context "when the application should not auto-reconnect" do
214
- before(:each) do
215
- Skates.config["auto-reconnect"] = false
216
- end
217
-
218
- it "should stop the event loop" do
219
- connection = mock(Object)
220
- EventMachine.should_receive(:stop_event_loop)
221
- Skates::Runner.on_disconnected()
222
- end
223
- end
224
- end
225
-
226
- describe ".on_stanza" do
227
- it "should call route on StanzaRouter" do
228
- stanza = mock(Object)
229
- Skates.router.should_receive(:route).with(stanza)
230
- Skates::Runner.on_stanza(stanza)
231
- end
232
- end
233
- end