spec/adhearsion/call_controller/output_spec.rb in adhearsion-2.5.4 vs spec/adhearsion/call_controller/output_spec.rb in adhearsion-2.6.0

- old
+ new

@@ -14,23 +14,33 @@ def expect_async_ssml_output(ssml, options = {}) expect_message_waiting_for_response Punchblock::Component::Output.new(options.merge(:ssml => ssml)) end + def expect_url_output(url, options = {}) + component = Punchblock::Component::Output.new(options.merge(render_document: {value: url, content_type: "application/ssml+xml"})) + expect_component_execution component + end + + def expect_async_url_output(url, options = {}) + component = Punchblock::Component::Output.new(options.merge(render_document: {value: url, content_type: "application/ssml+xml"})) + expect_message_waiting_for_response component + end + describe "#player" do it "should return a Player component targetted at the current controller" do player = controller.player - player.should be_a Output::Player - player.controller.should be controller + expect(player).to be_a Output::Player + expect(player.controller).to be controller end end describe "#async_player" do it "should return an AsyncPlayer component targetted at the current controller" do player = controller.async_player - player.should be_a Output::AsyncPlayer - player.controller.should be controller + expect(player).to be_a Output::AsyncPlayer + expect(player.controller).to be controller end end describe "#play_audio" do let(:audio_file) { "/sounds/boo.wav" } @@ -40,11 +50,11 @@ RubySpeech::SSML.draw { audio :src => file } end it 'plays the correct ssml' do expect_ssml_output ssml - subject.play_audio(audio_file).should be true + expect(subject.play_audio(audio_file)).to be true end context "with a fallback" do let(:fallback) { "text for tts" } @@ -56,19 +66,19 @@ end end it 'places the fallback in the SSML doc' do expect_ssml_output ssml - subject.play_audio(audio_file, :fallback => fallback).should be true + expect(subject.play_audio(audio_file, :fallback => fallback)).to be true end end context "with a media engine" do let(:media_engine) { :native } it "should use the specified media engine in the component" do expect_ssml_output ssml, renderer: media_engine - subject.play_audio(audio_file, renderer: media_engine).should be true + expect(subject.play_audio(audio_file, renderer: media_engine)).to be true end end end describe "#play_audio!" do @@ -79,11 +89,11 @@ RubySpeech::SSML.draw { audio :src => file } end it 'plays the correct ssml' do expect_async_ssml_output ssml - subject.play_audio!(audio_file).should be_a Punchblock::Component::Output + expect(subject.play_audio!(audio_file)).to be_a Punchblock::Component::Output end context "with a fallback" do let(:fallback) { "text for tts" } @@ -95,19 +105,19 @@ end end it 'places the fallback in the SSML doc' do expect_async_ssml_output ssml - subject.play_audio!(audio_file, :fallback => fallback).should be_a Punchblock::Component::Output + expect(subject.play_audio!(audio_file, :fallback => fallback)).to be_a Punchblock::Component::Output end end context "with a media engine" do let(:media_engine) { :native } it "should use the specified media engine in the SSML" do expect_async_ssml_output ssml, renderer: media_engine - subject.play_audio!(audio_file, renderer: media_engine).should be_a Punchblock::Component::Output + expect(subject.play_audio!(audio_file, renderer: media_engine)).to be_a Punchblock::Component::Output end end end describe "#play_numeric" do @@ -120,38 +130,38 @@ describe "with a number" do let(:input) { 123 } it 'plays the correct ssml' do expect_ssml_output ssml - subject.play_numeric(input).should be true + expect(subject.play_numeric(input)).to be true end end describe "with a string representation of a number" do let(:input) { "123" } it 'plays the correct ssml' do expect_ssml_output ssml - subject.play_numeric(input).should be true + expect(subject.play_numeric(input)).to be true end end describe "with something that's not a number" do let(:input) { 'foo' } it 'raises ArgumentError' do - lambda { subject.play_numeric input }.should raise_error(ArgumentError) + expect { subject.play_numeric input }.to raise_error(ArgumentError) end end context "with a renderer" do let(:input) { 123 } let(:renderer) { :native } it "should use the specified renderer in the SSML" do expect_ssml_output ssml, renderer: renderer - subject.play_numeric(input, renderer: renderer).should be true + expect(subject.play_numeric(input, renderer: renderer)).to be true end end end describe "#play_numeric!" do @@ -164,42 +174,80 @@ describe "with a number" do let(:input) { 123 } it 'plays the correct ssml' do expect_async_ssml_output ssml - subject.play_numeric!(input).should be_a Punchblock::Component::Output + expect(subject.play_numeric!(input)).to be_a Punchblock::Component::Output end end describe "with a string representation of a number" do let(:input) { "123" } it 'plays the correct ssml' do expect_async_ssml_output ssml - subject.play_numeric!(input).should be_a Punchblock::Component::Output + expect(subject.play_numeric!(input)).to be_a Punchblock::Component::Output end end describe "with something that's not a number" do let(:input) { 'foo' } it 'raises ArgumentError' do - lambda { subject.play_numeric! input }.should raise_error(ArgumentError) + expect { subject.play_numeric! input }.to raise_error(ArgumentError) end end context "with a renderer" do let(:input) { 123 } let(:renderer) { :native } it "should use the specified renderer in the SSML" do expect_async_ssml_output ssml, renderer: renderer - subject.play_numeric!(input, renderer: renderer).should be_a Punchblock::Component::Output + expect(subject.play_numeric!(input, renderer: renderer)).to be_a Punchblock::Component::Output end end end + describe "#play_document" do + describe "with a URL" do + let(:input) { 'http://example.com/ex.ssml' } + + it 'plays the url' do + expect_url_output input + expect(subject.play_document(input)).to be true + end + end + + describe "with something that's not a URL" do + let(:input) { 'ceci n\'est pas une url' } + + it 'raises ArgumentError' do + expect { subject.play_document input }.to raise_error(ArgumentError) + end + end + end + + describe "#play_document!" do + describe "with a URL" do + let(:input) { 'http://example.com/ex.ssml' } + + it 'plays the url' do + expect_async_url_output input + expect(subject.play_document!(input)).to be_a Punchblock::Component::Output + end + end + + describe "with something that's not a URL" do + let(:input) { 'ceci n\'est pas une url' } + + it 'raises ArgumentError' do + expect { subject.play_document! input }.to raise_error(ArgumentError) + end + end + end + describe "#play_time" do let :ssml do content = input.to_s opts = expected_say_as_options RubySpeech::SSML.draw do @@ -211,32 +259,32 @@ let(:input) { Time.parse("12/5/2000") } let(:expected_say_as_options) { {:interpret_as => 'time'} } it 'plays the correct SSML' do expect_ssml_output ssml - subject.play_time(input).should be true + expect(subject.play_time(input)).to be true end end describe "with a date" do let(:input) { Date.parse('2011-01-23') } let(:expected_say_as_options) { {:interpret_as => 'date'} } it 'plays the correct SSML' do expect_ssml_output ssml - subject.play_time(input).should be true + expect(subject.play_time(input)).to be true end end describe "with a date and a say_as format" do let(:input) { Date.parse('2011-01-23') } let(:format) { "d-m-y" } let(:expected_say_as_options) { {:interpret_as => 'date', :format => format} } it 'plays the correct SSML' do expect_ssml_output ssml - subject.play_time(input, :format => format).should be true + expect(subject.play_time(input, :format => format)).to be true end end describe "with a date and a strftime option" do let(:strftime) { "%d-%m-%Y" } @@ -244,11 +292,11 @@ let(:input) { base_input.strftime(strftime) } let(:expected_say_as_options) { {:interpret_as => 'date'} } it 'plays the correct SSML' do expect_ssml_output ssml - subject.play_time(base_input, :strftime => strftime).should be true + expect(subject.play_time(base_input, :strftime => strftime)).to be true end end describe "with a date, a format option and a strftime option" do let(:strftime) { "%d-%m-%Y" } @@ -257,11 +305,11 @@ let(:input) { base_input.strftime(strftime) } let(:expected_say_as_options) { {:interpret_as => 'date', :format => format} } it 'plays the correct SSML' do expect_ssml_output ssml - subject.play_time(base_input, :format => format, :strftime => strftime).should be true + expect(subject.play_time(base_input, :format => format, :strftime => strftime)).to be true end end context "with a renderer" do let(:renderer) { :native } @@ -269,19 +317,19 @@ let(:format) { "d-m-y" } let(:expected_say_as_options) { {:interpret_as => 'date', :format => format} } it "should use the specified renderer in the SSML" do expect_ssml_output ssml, renderer: renderer - subject.play_time(input, format: format, renderer: renderer).should be true + expect(subject.play_time(input, format: format, renderer: renderer)).to be true end end describe "with an object other than Time, DateTime, or Date" do let(:input) { "foo" } it 'raises ArgumentError' do - lambda { subject.play_time input }.should raise_error(ArgumentError) + expect { subject.play_time input }.to raise_error(ArgumentError) end end end describe "#play_time!" do @@ -297,32 +345,32 @@ let(:input) { Time.parse("12/5/2000") } let(:expected_say_as_options) { {:interpret_as => 'time'} } it 'plays the correct SSML' do expect_async_ssml_output ssml - subject.play_time!(input).should be_a Punchblock::Component::Output + expect(subject.play_time!(input)).to be_a Punchblock::Component::Output end end describe "with a date" do let(:input) { Date.parse('2011-01-23') } let(:expected_say_as_options) { {:interpret_as => 'date'} } it 'plays the correct SSML' do expect_async_ssml_output ssml - subject.play_time!(input).should be_a Punchblock::Component::Output + expect(subject.play_time!(input)).to be_a Punchblock::Component::Output end end describe "with a date and a say_as format" do let(:input) { Date.parse('2011-01-23') } let(:format) { "d-m-y" } let(:expected_say_as_options) { {:interpret_as => 'date', :format => format} } it 'plays the correct SSML' do expect_async_ssml_output ssml - subject.play_time!(input, :format => format).should be_a Punchblock::Component::Output + expect(subject.play_time!(input, :format => format)).to be_a Punchblock::Component::Output end end describe "with a date and a strftime option" do let(:strftime) { "%d-%m-%Y" } @@ -330,11 +378,11 @@ let(:input) { base_input.strftime(strftime) } let(:expected_say_as_options) { {:interpret_as => 'date'} } it 'plays the correct SSML' do expect_async_ssml_output ssml - subject.play_time!(base_input, :strftime => strftime).should be_a Punchblock::Component::Output + expect(subject.play_time!(base_input, :strftime => strftime)).to be_a Punchblock::Component::Output end end describe "with a date, a format option and a strftime option" do let(:strftime) { "%d-%m-%Y" } @@ -343,11 +391,11 @@ let(:input) { base_input.strftime(strftime) } let(:expected_say_as_options) { {:interpret_as => 'date', :format => format} } it 'plays the correct SSML' do expect_async_ssml_output ssml - subject.play_time!(base_input, :format => format, :strftime => strftime).should be_a Punchblock::Component::Output + expect(subject.play_time!(base_input, :format => format, :strftime => strftime)).to be_a Punchblock::Component::Output end end context "with a renderer" do let(:renderer) { :native } @@ -355,19 +403,19 @@ let(:format) { "d-m-y" } let(:expected_say_as_options) { {:interpret_as => 'date', :format => format} } it "should use the specified renderer in the SSML" do expect_async_ssml_output ssml, renderer: renderer - subject.play_time!(input, format: format, renderer: renderer).should be_a Punchblock::Component::Output + expect(subject.play_time!(input, format: format, renderer: renderer)).to be_a Punchblock::Component::Output end end describe "with an object other than Time, DateTime, or Date" do let(:input) { "foo" } it 'raises ArgumentError' do - lambda { subject.play_time! input }.should raise_error(ArgumentError) + expect { subject.play_time! input }.to raise_error(ArgumentError) end end end describe '#play' do @@ -388,16 +436,16 @@ RubySpeech::SSML.draw { audio :src => file } end it 'plays the audio file' do expect_ssml_output ssml - subject.play(audio_file).should be true + expect(subject.play(audio_file)).to be true end it 'plays the audio file with the specified extra options if present' do expect_ssml_output ssml, extra_options - subject.play(audio_file, extra_options).should be true + expect(subject.play(audio_file, extra_options)).to be true end end describe "with multiple arguments" do let(:args) { ["/foo/bar.wav", 1, Time.now, "123#"] } @@ -414,17 +462,17 @@ end end it 'plays all arguments in one document' do expect_ssml_output ssml - subject.play(*args).should be true + expect(subject.play(*args)).to be true end it 'plays all arguments in one document with the extra options if present' do expect_ssml_output ssml, extra_options args << extra_options - subject.play(*args).should be true + expect(subject.play(*args)).to be true end end describe "with a collection of arguments" do let(:args) { ["/foo/bar.wav", 1, Time.now] } @@ -439,11 +487,11 @@ end end it 'plays all arguments in one document' do expect_ssml_output ssml - subject.play(args).should be true + expect(subject.play(args)).to be true end context "that is empty" do it "is a noop" do subject.play [] @@ -461,11 +509,11 @@ end end it 'plays the number' do expect_ssml_output ssml - subject.play(argument).should be true + expect(subject.play(argument)).to be true end end describe "with a string representation of a number" do let(:argument) { '123' } @@ -477,11 +525,11 @@ end end it 'plays the number' do expect_ssml_output ssml - subject.play(argument).should be true + expect(subject.play(argument)).to be true end end describe "with a time" do let(:time) { Time.parse "12/5/2000" } @@ -493,11 +541,11 @@ end end it 'plays the time' do expect_ssml_output ssml - subject.play(time).should be true + expect(subject.play(time)).to be true end end describe "with a date" do let(:date) { Date.parse '2011-01-23' } @@ -508,11 +556,11 @@ end end it 'plays the time' do expect_ssml_output ssml - subject.play(date).should be true + expect(subject.play(date)).to be true end end describe "with an hash containing a Date/DateTime/Time object and format options" do let(:date) { Date.parse '2011-01-23' } @@ -527,20 +575,20 @@ end end it 'plays the time with the specified format and strftime' do expect_ssml_output ssml - subject.play(:value => date, :format => format, :strftime => strftime).should be true + expect(subject.play(:value => date, :format => format, :strftime => strftime)).to be true end end describe "with an SSML document" do let(:ssml) { RubySpeech::SSML.draw { string "Hello world" } } it "plays the SSML without generating" do expect_ssml_output ssml - subject.play(ssml).should be true + expect(subject.play(ssml)).to be true end end end describe '#play!' do @@ -561,11 +609,11 @@ RubySpeech::SSML.draw { audio :src => file } end it 'plays the audio file' do expect_async_ssml_output ssml - subject.play!(audio_file).should be_a Punchblock::Component::Output + expect(subject.play!(audio_file)).to be_a Punchblock::Component::Output end it 'plays the audio file with the specified extra options if present' do expect_async_ssml_output ssml, extra_options subject.play!(audio_file, extra_options) @@ -585,11 +633,11 @@ end end it 'plays all arguments in one document' do expect_async_ssml_output ssml - subject.play!(*args).should be_a Punchblock::Component::Output + expect(subject.play!(*args)).to be_a Punchblock::Component::Output end it 'plays all arguments in one document with the extra options if present' do expect_async_ssml_output ssml, extra_options args << extra_options @@ -607,11 +655,11 @@ end end it 'plays the number' do expect_async_ssml_output ssml - subject.play!(argument).should be_a Punchblock::Component::Output + expect(subject.play!(argument)).to be_a Punchblock::Component::Output end end describe "with a string representation of a number" do let(:argument) { '123' } @@ -623,11 +671,11 @@ end end it 'plays the number' do expect_async_ssml_output ssml - subject.play!(argument).should be_a Punchblock::Component::Output + expect(subject.play!(argument)).to be_a Punchblock::Component::Output end end describe "with a time" do let(:time) { Time.parse "12/5/2000" } @@ -639,11 +687,11 @@ end end it 'plays the time' do expect_async_ssml_output ssml - subject.play!(time).should be_a Punchblock::Component::Output + expect(subject.play!(time)).to be_a Punchblock::Component::Output end end describe "with a date" do let(:date) { Date.parse '2011-01-23' } @@ -654,11 +702,11 @@ end end it 'plays the time' do expect_async_ssml_output ssml - subject.play!(date).should be_a Punchblock::Component::Output + expect(subject.play!(date)).to be_a Punchblock::Component::Output end end describe "with an array containing a Date/DateTime/Time object and a hash" do let(:date) { Date.parse '2011-01-23' } @@ -673,20 +721,20 @@ end end it 'plays the time with the specified format and strftime' do expect_async_ssml_output ssml - subject.play!(:value => date, :format => format, :strftime => strftime).should be_a Punchblock::Component::Output + expect(subject.play!(:value => date, :format => format, :strftime => strftime)).to be_a Punchblock::Component::Output end end describe "with an SSML document" do let(:ssml) { RubySpeech::SSML.draw { string "Hello world" } } it "plays the SSML without generating" do expect_async_ssml_output ssml - subject.play!(ssml).should be_a Punchblock::Component::Output + expect(subject.play!(ssml)).to be_a Punchblock::Component::Output end end end describe "#interruptible_play" do @@ -694,30 +742,30 @@ let(:output2) { "three four" } let(:non_existing) { "http://adhearsion.com/nonexistingfile.mp3" } let(:extra_options) { {renderer: :native } } it "plays two outputs in succession" do - subject.should_receive(:stream_file).twice + expect(subject).to receive(:stream_file).twice digit = subject.interruptible_play output1, output2 - digit.should be_nil + expect(digit).to be_nil end it "stops at the first play when input is received" do - subject.should_receive(:stream_file).once.and_return(2) + expect(subject).to receive(:stream_file).once.and_return(2) digit = subject.interruptible_play output1, output2 - digit.should be == 2 + expect(digit).to eq(2) end it "passes options on to #stream_file" do - subject.should_receive(:stream_file).once.with(output1, '0123456789#*', extra_options) - subject.should_receive(:stream_file).once.with(output2, '0123456789#*', extra_options) + expect(subject).to receive(:stream_file).once.with(output1, '0123456789#*', extra_options) + expect(subject).to receive(:stream_file).once.with(output2, '0123456789#*', extra_options) digit = subject.interruptible_play output1, output2, extra_options - digit.should be_nil + expect(digit).to be_nil end it 'raises an exception when output is unsuccessful' do - subject.should_receive(:stream_file).once.and_raise Output::PlaybackError, "Output failed" + expect(subject).to receive(:stream_file).once.and_raise Output::PlaybackError, "Output failed" expect { subject.interruptible_play non_existing }.to raise_error(Output::PlaybackError) end end describe "#stream_file" do @@ -753,35 +801,35 @@ expect_input_component_complete_event 'dtmf-5' end #test does pass and method works, but not sure if the empty method is a good idea it "plays the correct output" do - controller.stub(:write_and_await_response) + allow(controller).to receive(:write_and_await_response) expect_component_complete_event expect_component_execution Punchblock::Component::Output.new(:ssml => ssml) subject.stream_file prompt, allowed_digits end it "returns a single digit amongst the allowed when pressed" do - controller.should_receive(:write_and_await_response).with(kind_of(Punchblock::Component::Input)) do |input_component| + expect(controller).to receive(:write_and_await_response).with(kind_of(Punchblock::Component::Input)) do |input_component| input_component.trigger_event_handler Punchblock::Event::Complete.new end - controller.should_receive(:write_and_await_response).once.with(kind_of(Punchblock::Component::Output)) + expect(controller).to receive(:write_and_await_response).once.with(kind_of(Punchblock::Component::Output)) - Punchblock::Component::Output.any_instance.should_receive(:stop!) - Punchblock::Component::Output.any_instance.should_receive(:complete_event).and_return double('complete', reason: double('Reason')) + expect_any_instance_of(Punchblock::Component::Output).to receive(:stop!) + expect_any_instance_of(Punchblock::Component::Output).to receive(:complete_event).and_return double('complete', reason: double('Reason')) expect_input_component_complete_event 'dtmf-5' - subject.stream_file(prompt, allowed_digits).should be == '5' + expect(subject.stream_file(prompt, allowed_digits)).to eq('5') end context "with output options passed in" do let(:extra_options) { {renderer: :native } } it "plays the correct output with options" do - controller.stub(:write_and_await_response) + allow(controller).to receive(:write_and_await_response) expect_component_complete_event expect_component_execution Punchblock::Component::Output.new({:ssml => ssml}.merge(extra_options)) subject.stream_file prompt, allowed_digits, extra_options end @@ -797,20 +845,20 @@ describe "with a RubySpeech document" do it 'plays the correct SSML' do ssml = RubySpeech::SSML.draw { string "Hello world" } expect_ssml_output ssml - subject.say(ssml).should be_a Punchblock::Component::Output + expect(subject.say(ssml)).to be_a Punchblock::Component::Output end end describe "with a string" do it 'outputs the correct text' do str = "Hello world" ssml = RubySpeech::SSML.draw { string str } expect_ssml_output ssml - subject.say(str).should be_a Punchblock::Component::Output + expect(subject.say(str)).to be_a Punchblock::Component::Output end end describe "with a default voice set in PB config" do before { Adhearsion.config.punchblock.default_voice = 'foo' } @@ -879,18 +927,18 @@ describe "converts the argument to a string" do it 'calls output with a string' do argument = 123 ssml = RubySpeech::SSML.draw { string '123' } expect_ssml_output ssml - subject.say(argument).should be_a Punchblock::Component::Output + expect(subject.say(argument)).to be_a Punchblock::Component::Output end end end describe "#speak" do it "should be an alias for #say" do - subject.method(:speak).should be == subject.method(:say) + expect(subject.method(:speak)).to eq(subject.method(:say)) end end describe "#say!" do describe "with a nil argument" do @@ -901,20 +949,20 @@ describe "with a RubySpeech document" do it 'plays the correct SSML' do ssml = RubySpeech::SSML.draw { string "Hello world" } expect_async_ssml_output ssml - subject.say!(ssml).should be_a Punchblock::Component::Output + expect(subject.say!(ssml)).to be_a Punchblock::Component::Output end end describe "with a string" do it 'outputs the correct text' do str = "Hello world" ssml = RubySpeech::SSML.draw { string str } expect_async_ssml_output ssml - subject.say!(str).should be_a Punchblock::Component::Output + expect(subject.say!(str)).to be_a Punchblock::Component::Output end end describe "with a default voice set in PB config" do before { Adhearsion.config.punchblock.default_voice = 'foo' } @@ -983,18 +1031,18 @@ describe "converts the argument to a string" do it 'calls output with a string' do argument = 123 ssml = RubySpeech::SSML.draw { string '123' } expect_async_ssml_output ssml - subject.say!(argument).should be_a Punchblock::Component::Output + expect(subject.say!(argument)).to be_a Punchblock::Component::Output end end end describe "#speak!" do it "should be an alias for #say!" do - subject.method(:speak!).should be == subject.method(:say!) + expect(subject.method(:speak!)).to eq(subject.method(:say!)) end end describe "#say_characters" do context "with a string" do @@ -1004,11 +1052,11 @@ end end it 'plays the correct ssml' do expect_ssml_output ssml - subject.say_characters('1234#abc').should be true + expect(subject.say_characters('1234#abc')).to be true end end context "with a numeric" do let :ssml do @@ -1017,11 +1065,11 @@ end end it 'plays the correct ssml' do expect_ssml_output ssml - subject.say_characters(1234).should be true + expect(subject.say_characters(1234)).to be true end end end describe "#say_characters!" do @@ -1032,11 +1080,11 @@ end end it 'plays the correct ssml' do expect_async_ssml_output ssml - subject.say_characters!('1234#abc').should be_a Punchblock::Component::Output + expect(subject.say_characters!('1234#abc')).to be_a Punchblock::Component::Output end end context "with a numeric" do let :ssml do @@ -1045,10 +1093,10 @@ end end it 'plays the correct ssml' do expect_async_ssml_output ssml - subject.say_characters!(1234).should be_a Punchblock::Component::Output + expect(subject.say_characters!(1234)).to be_a Punchblock::Component::Output end end end end end