require "spec_helper" include Rmpd shared_examples_for "a command with a song pos" do it "should pass along the song pos" do @socket.stub!(:readline).and_return(*@responses) @socket.stub!(:puts).and_return(@socket.puts) @socket.stub!(:eof?).and_return(false) @command.call end end describe Rmpd::Commands do before(:each) do @socket = mock_socket @config = mock_config Socket.stub!(:new).and_return(@socket) @conn = Connection.new end def set_password(password="foobar") @config.stub!(:password).and_return(password) end it "should handle a closed connection gracefully" do @socket.stub!(:puts).and_raise(EOFError) @socket.stub!(:readline).and_return(*(connect_response + ok)) @socket.should_receive(:close).exactly(5).times lambda do @conn.connect @conn.ping end.should raise_error(Rmpd::MpdDisconnectedError) end it "should abort after a limited number of tries against a closed connection" do pending do @socket.stub!(:puts).and_raise(EOFError) @socket.stub!(:readline).and_return(*(connect_response + ok)) @socket.should_receive(:close).exactly(5).times lambda do @conn.connect @conn.ping end.should raise_error(MpdError, "Retry count exceeded") end end describe "close" do it "should close the socket" do @socket.stub!(:readline).and_return(*connect_response) @socket.should_receive(:close).once @conn.connect @conn.ping @conn.close end end describe "volume" do it "should be deprecated" do set_password @socket.stub!(:readline).and_return(*connect_and_auth_responses) $stderr.should_receive(:puts).with(/deprecated/i) @conn.connect @conn.volume(0) end end describe "kill" do it "should kill the daemon" do set_password @socket.should_receive(:puts).with("kill") @socket.stub!(:readline).and_return(*connect_and_auth_responses) @socket.should_receive(:close) @conn.connect @conn.kill end end describe "playlistinfo" do before(:each) do set_password @song_id = 1 @cmd = "playlistinfo \"#{@song_id}\"" @responses = connect_and_auth_responses + ["file: blah\n", "boobies: yay!\n"] + ok @command = Proc.new {@conn.connect; @conn.playlistinfo(@song_id)} end it_should_behave_like "a command with a song pos" end describe "play" do before(:each) do set_password @song_id = 12 @cmd = "play \"#{@song_id}\"" @responses = connect_and_auth_responses + ok @command = Proc.new {@conn.connect; @conn.play(@song_id)} end it_should_behave_like "a command with a song pos" end describe "delete" do before(:each) do set_password @song_id = 23 @cmd = "delete \"#{@song_id}\"" @responses = connect_and_auth_responses + ok @command = Proc.new {@conn.connect; @conn.delete(@song_id)} end it_should_behave_like "a command with a song pos" end describe "command_list" do it "shouldn't wait for a response from the first command" do @responses = connect_response + ok @socket.stub!(:readline).and_return(*@responses) @socket.stub!(:puts).and_return(@socket.puts) @socket.stub!(:eof?).and_return(false) @conn.connect @conn.command_list do |c| c.status c.stats end end it "should handle multi responses" do @responses = connect_and_auth_responses + playlist_id_response + ok @socket.stub!(:readline).and_return(*@responses) @socket.stub!(:puts).and_return(@socket.puts) @socket.stub!(:eof?).and_return(false) @conn.connect @conn.command_list do |c| c.addid("foo") end end end describe "noidle" do it "returns nil" do @responses = connect_response @socket.stub!(:readline).and_return(*@responses) @conn.connect @conn.noidle.should be_nil end it "consumes no response, since none is tendered" do @responses = connect_response + status_response + ok @socket.stub!(:readline).and_return(*@responses) @conn.connect response = @conn.status response.should have(2).items, response.pretty_inspect response.should include("volume") response.should include("state") end end describe "command_list_ok" do it "should return a list of multi responses" do @responses = connect_response + command_list_ok_responses + ok @socket.stub!(:readline).and_return(*@responses) @socket.stub!(:puts).and_return(@socket.puts) @socket.stub!(:eof?).and_return(false) @conn.connect results = @conn.command_list_ok do |c| c.list("album") c.list("artist") end results.should have(2).items, results.pretty_inspect end end end