Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

test refactoring

  • Loading branch information...
commit 7a18a0268d8fe813bc540e115ccf214666b67d93 1 parent 0055510
@vangberg authored
View
48 spec/helper.rb
@@ -3,20 +3,17 @@
require 'bacon'
require 'librevox'
-class Librevox::Listener::Outbound
-end
-
-module Librevox::Matchers
- module Listener
+module Librevox::Test
+ module Matchers
def send_command command
lambda {|obj|
obj.outgoing_data.shift.should == "#{command}\n\n"
}
end
- end
- module Outbound
- include Listener
+ def send_nothing
+ lambda {|obj| obj.outgoing_data.shift.should == nil}
+ end
def send_application app, args=nil
lambda {|obj|
@@ -43,9 +40,40 @@ def update_session session_id=nil
}
end
end
+ end
- def send_nothing
- lambda {|obj| obj.outgoing_data.shift.should == nil}
+ module ListenerHelpers
+ def command_reply args={}
+ args["Content-Type"] = "command/reply"
+ response args
+ end
+
+ def api_response args={}
+ args["Content-Type"] = "api/response"
+ response args
+ end
+
+ def response args={}
+ body = args.delete :body
+ headers = args
+
+ if body.is_a? Hash
+ body = body.map {|k,v| "#{k}: #{v}"}.join "\n"
+ end
+
+ headers["Content-Length"] = body.size if body
+ msg = headers.map {|k, v| "#{k}: #{v}"}.join "\n"
+
+ msg << "\n\n" + body if body
+
+ @listener.receive_data msg + "\n\n"
+ end
+
+ def event name
+ body = "Event-Name: #{name}"
+ headers = "Content-Length: #{body.size}"
+
+ @listener.receive_data "#{headers}\n\n#{body}\n\n"
end
end
end
View
43 spec/librevox/listener.rb
@@ -1,7 +1,8 @@
require 'spec/helper'
-
require 'librevox/listener/base'
+include Librevox::Test::ListenerHelpers
+
class Librevox::Listener::Base
attr_accessor :outgoing_data
@@ -19,6 +20,8 @@ def read_data
end
end
+# These tests are a bit fragile, as they depend on event hooks being
+# executed before on_event.
shared "events" do
before do
@class = @listener.class
@@ -43,33 +46,37 @@ def @listener.on_event(e)
end
should "execute callback for event" do
- @listener.receive_data("Content-Length: 23\n\nEvent-Name: OTHER_EVENT\n\n")
+ event "OTHER_EVENT"
@listener.read_data.should == "something else"
- @listener.receive_data("Content-Length: 22\n\nEvent-Name: SOME_EVENT\n\n")
+ event "SOME_EVENT"
@listener.read_data.should == "something"
end
should "pass response duplicate as arg to hook block" do
- @listener.receive_data("Content-Length: 25\n\nEvent-Name: HOOK_WITH_ARG\n\n")
+ event "HOOK_WITH_ARG"
+
reply = @listener.read_data
reply.should =~ /^got event arg: /
reply.should.not =~ /^got event arg: #{@listener.response.object_id}$/
end
should "expose response as event" do
- @listener.receive_data("Content-Length: 23\n\nEvent-Name: OTHER_EVENT\n\n")
+ event "OTHER_EVENT"
+
@listener.event.class.should == Librevox::Response
@listener.event.content[:event_name].should == "OTHER_EVENT"
end
should "call on_event" do
- @listener.receive_data("Content-Length: 23\n\nEvent-Name: THIRD_EVENT\n\n")
+ event "THIRD_EVENT"
+
@listener.read_data.should =~ /^from on_event/
end
should "call on_event with response duplicate as argument" do
- @listener.receive_data("Content-Length: 23\n\nEvent-Name: THIRD_EVENT\n\n")
+ event "THIRD_EVENT"
+
@listener.read_data.should.not =~ /^from on_event: #{@listener.response.object_id}$/
end
@@ -81,7 +88,7 @@ def @listener.on_event e
end
@class.event(:channel_data) {send_data "event hook: CHANNEL_DATA test"}
- @listener.receive_data("Content-Length: 24\n\nEvent-Name: CHANNEL_DATA\n\n")
+ event "CHANNEL_DATA"
@listener.outgoing_data.should.include "on_event: CHANNEL_DATA test"
@listener.outgoing_data.should.include "event hook: CHANNEL_DATA test"
@@ -99,10 +106,12 @@ def sample_cmd cmd, args=""
@class = @listener.class
# Establish session
- @listener.receive_data("Content-Type: command/reply\nTest: Testing\n\n")
+ command_reply "Test" => "Testing"
end
describe "multiple api commands" do
+ extend Librevox::Test::Matchers
+
before do
@listener.outgoing_data.clear
@@ -116,16 +125,16 @@ def @listener.on_event(e) end # Don't send anything, kthx.
end
should "only send one command at a time, and return response for commands" do
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 22\n\nEvent-Name: API_TEST\n\n")
- @listener.read_data.should == "api foo\n\n"
- @listener.read_data.should == nil
+ command_reply :body => {"Event-Name" => "API_TEST"}
+ @listener.should send_command "api foo"
+ @listener.should send_nothing
- @listener.receive_data("Content-Type: api/response\nReply-Text: +OK\n\n")
- @listener.read_data.should == "api foo bar baz\n\n"
- @listener.read_data.should == nil
+ api_response "Reply-Text" => "+OK"
+ @listener.should send_command "api foo bar baz"
+ @listener.should send_nothing
- @listener.receive_data("Content-Type: api/response\nContent-Length: 4\n\n+YAY\n\n")
- @listener.read_data.should == "response +YAY\n\n"
+ api_response :body => "+YAY"
+ @listener.should send_command "response +YAY"
end
end
end
View
38 spec/librevox/listener/spec_outbound.rb
@@ -20,34 +20,8 @@ def event_and_linger_replies
command_reply "Reply-Text" => "+OK will linger"
end
-def command_reply args={}
- args["Content-Type"] = "command/reply"
- response args
-end
-
-def api_response args={}
- args["Content-Type"] = "api/response"
- response args
-end
-
-def response args={}
- body = args.delete :body
- headers = args
-
- if body.is_a? Hash
- body = body.map {|k,v| "#{k}: #{v}"}.join "\n"
- end
-
- headers["Content-Length"] = body.size if body
- msg = headers.map {|k, v| "#{k}: #{v}"}.join "\n"
-
- msg << "\n\n" + body if body
-
- @listener.receive_data msg + "\n\n"
-end
-
describe "Outbound listener" do
- extend Librevox::Matchers::Outbound
+ extend Librevox::Test::Matchers
before do
@listener = OutboundTestListener.new(nil)
@@ -92,7 +66,7 @@ def session_initiated
end
describe "Outbound listener with apps" do
- extend Librevox::Matchers::Outbound
+ extend Librevox::Test::Matchers
before do
@listener = OutboundListenerWithNestedApps.new(nil)
@@ -155,7 +129,7 @@ def session_initiated
end
describe "Outbound listener with app reading data" do
- extend Librevox::Matchers::Outbound
+ extend Librevox::Test::Matchers
before do
@listener = OutboundListenerWithReader.new(nil)
@@ -212,7 +186,7 @@ def session_initiated
end
describe "Outbound listener with non-nested apps" do
- extend Librevox::Matchers::Outbound
+ extend Librevox::Test::Matchers
before do
@listener = OutboundListenerWithNonNestedApps.new(nil)
@@ -255,7 +229,7 @@ def session_initiated
end
describe "Outbound listener with both apps and api calls" do
- extend Librevox::Matchers::Outbound
+ extend Librevox::Test::Matchers
before do
@listener = OutboundListenerWithAppsAndApi.new(nil)
@@ -285,7 +259,7 @@ def session_initiated
end
describe "Outbound listener with update session callback" do
- extend Librevox::Matchers::Outbound
+ extend Librevox::Test::Matchers
before do
@listener = OutboundListenerWithUpdateSessionCallback.new(nil)
Please sign in to comment.
Something went wrong with that request. Please try again.