Commit d77d2999 authored by James Mead's avatar James Mead
Browse files

The beginnings of a more succinct way of testing that a specified sequence of...

The beginnings of a more succinct way of testing that a specified sequence of commands is generated based on a specified sequence of events.

There are loads of things that can be improved about this, but I wanted to spike on the idea of replacing multiple tests with a single test describing the sequence of commands and events. The sequence is tested using the following algorithm :-

* receive each event up until the event just before the next expected command, each time checking that the command has not been sent
* receive the event just before the next expected command and check that the command has been sent
* rinse & repeat until there are no more commands or events

What I'm aiming at is something like the following which I think makes the expected sequence very clear :-

    check_sequence do
      event :offer_presence
      command :say, :text => 'hello, this is connfu'
      acknowledge_command
      event :say_success_presence
      command :say, :text => 'http://www.phono.com/audio/troporocks.mp3'
    end

DSLs rule KO!
parent 3f542e19
......@@ -5,25 +5,19 @@ describe "answering a call" do
testing_dsl do
on :offer do |call|
answer
do_something
finish!
end
end
before :each do
@call_jid = "call-id@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
end
it "should send an answer command" do
incoming :offer_presence, @call_jid, @client_jid
it "should check sequence" do
check_sequence do
@call_jid = "call-id@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
last_command.should == Connfu::Commands::Answer.new(:call_jid => @call_jid, :client_jid => @client_jid)
event :offer_presence, @call_jid, @client_jid
event :result_iq, @call_jid
finished
end
end
it "should continue to execute once the result of the answer is received" do
dsl_instance.should_receive(:do_something)
incoming :offer_presence, @call_jid, @client_jid
incoming :result_iq, @call_jid
end
end
\ No newline at end of file
......@@ -9,29 +9,23 @@ describe "ask a caller for 4 digits" do
end
end
before :each do
@call_jid = "call-id@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
end
it "should send an ask command" do
incoming :offer_presence, @call_jid, @client_jid
last_command.should == Connfu::Commands::Ask.new(
:call_jid => @call_jid,
:client_jid => @client_jid,
:prompt => "please enter a 4 digit pin",
:digits => 4
)
end
it "should continue when ask was successful" do
incoming :offer_presence, @call_jid, @client_jid
incoming :result_iq, @call_jid
incoming :ask_success_presence, @call_jid, "1234"
it "should check sequence" do
check_sequence do
@call_jid = "call-id@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
last_command.should == Connfu::Commands::Say.new(
:text => 'you entered 1234', :call_jid => @call_jid, :client_jid => @client_jid
)
event :offer_presence, @call_jid, @client_jid
command Connfu::Commands::Ask.new(
:call_jid => @call_jid,
:client_jid => @client_jid,
:prompt => "please enter a 4 digit pin",
:digits => 4
)
event :result_iq, @call_jid
event :ask_success_presence, @call_jid, "1234"
command Connfu::Commands::Say.new(
:text => 'you entered 1234', :call_jid => @call_jid, :client_jid => @client_jid
)
end
end
end
\ No newline at end of file
......@@ -9,30 +9,17 @@ describe "say something on a call" do
end
end
before :each do
@call_id = "call-id"
@call_jid = "#{@call_id}@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
end
it "should send first say command" do
incoming :offer_presence, @call_jid, @client_jid
last_command.should == Connfu::Commands::Say.new(:text => 'hello, this is connfu', :call_jid => @call_jid, :client_jid => @client_jid)
end
it "should not send the second say command if the first command's success hasn't been received" do
incoming :offer_presence, @call_jid, @client_jid
incoming :result_iq, @call_jid, @call_id
last_command.should == Connfu::Commands::Say.new(:text => 'hello, this is connfu', :call_jid => @call_jid, :client_jid => @client_jid)
end
it "should send the second say command once the first say command has completed" do
incoming :offer_presence, @call_jid, @client_jid
incoming :result_iq, @call_jid
incoming :say_success_presence, @call_jid
last_command.should == Connfu::Commands::Say.new(:text => 'http://www.phono.com/audio/troporocks.mp3', :call_jid => @call_jid, :client_jid => @client_jid)
it "should check sequence" do
check_sequence do
@call_id = "call-id"
@call_jid = "#{@call_id}@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
event :offer_presence, @call_jid, @client_jid
command Connfu::Commands::Say.new(:text => 'hello, this is connfu', :call_jid => @call_jid, :client_jid => @client_jid)
event :result_iq, @call_jid
event :say_success_presence, @call_jid
command Connfu::Commands::Say.new(:text => 'http://www.phono.com/audio/troporocks.mp3', :call_jid => @call_jid, :client_jid => @client_jid)
end
end
end
\ No newline at end of file
......@@ -48,6 +48,7 @@ def setup_connfu(handler_class, domain='openvoice.org')
end
def incoming(type, *args)
puts "incoming: %p, %p" % [type, args]
stanza = if type.to_s =~ /_iq$/
create_iq(send(type, *args))
else
......@@ -109,6 +110,58 @@ def last_command
Connfu.connection.commands.last
end
class Sequence
include RSpec::Matchers
def initialize
@sequence = []
end
def event(*args)
@sequence << [:incoming, *args]
end
def command(*args)
@sequence << [:command, *args]
end
def finished
@sequence << [:finished]
end
def check
while @sequence.any?
command_index = @sequence.index { |e| e.first == :command } || @sequence.length
events = @sequence.shift(command_index)
command = @sequence.shift
events.each_with_index do |e, index|
if command && (index == 0)
puts "* checking last command is not %p" % command.last
last_command.should_not == command.last
end
if e.first == :incoming
send(*e)
elsif e.first == :finished
puts "* checking script is finished"
Connfu.should be_finished
end
if command && (index == events.length - 1)
puts "* checking last command is %p" % command.last
last_command.should == command.last
end
end
end
end
end
def check_sequence(&block)
sequence = Sequence.new
sequence.instance_eval(&block)
sequence.check
end
def result_iq(call_jid="call-id@#{PRISM_HOST}", id='blather0008')
"<iq type='result' id='#{id}' from='#{call_jid}' to='#{PRISM_JID}/voxeo'/>"
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment