#!/usr/bin/ruby
def from_this_file_path *args
File.join( File.dirname(__FILE__), *args )
end
require from_this_file_path('..', 'lib', 'violet', 'response.rb')
require 'test/unit'
class ResponseTest < Test::Unit::TestCase
def test_bad_protocol
assert_raise(Response::ProtocolExcepion) { Response.parse 'a comment without messages' }
assert_raise(Response::ProtocolExcepion) { Response.parse '' }
end
def test_invalid_xml
assert_raise(REXML::ParseException) { Response.parse '' }
end
def test_simple_case
rsp = Response.parse 'YES'
assert_kind_of Response::Base::ServerRsp, rsp
end
def test_get_all
rsp = Response.parse 'YES'
assert_equal 'YES', rsp.get_all(:rabbitSleep) { |e| e.text }.first
rsp = Response.parse 'LINKPREVIEWa comment'
assert_equal 'LINKPREVIEW', rsp.get_all(:message) { |e| e.text }.first
assert_equal 'a comment', rsp.get_all(:comment) { |e| e.text }.first
rsp = Response.parse ''
assert_equal({:nb => '2'}, rsp.get_all(:listfriend) { |e| e.attributes.to_hash }.first)
assert_equal [{:name => 'toto'},{:name => 'tata'}], rsp.get_all(:friend) { |e| e.attributes.to_hash }
end
def test_undefined_element
rsp = Response.parse 'YES'
assert_raise(NameError) { rsp.comment }
assert_raise(NameError) { rsp.message }
end
def test_has_and_has_many
rsp = Response.parse 'LINKPREVIEWa comment'
assert rsp.has_message?
assert !rsp.has_messages?
assert !rsp.has_many_messages?
assert rsp.has_comment?
assert !rsp.has_comments?
assert !rsp.has_many_comments?
rsp = Response.parse ''
assert rsp.has_listfriend?
assert !rsp.has_listfriends?
assert !rsp.has_many_listfriends?
assert rsp.has_friend?
assert !rsp.has_friends?
assert rsp.has_many_friends?
end
def test_accessors_message_and_comment
rsp = Response.parse 'NABCASTNOTSENDYour idmessage is private'
assert_equal 'NABCASTNOTSEND', rsp.message
assert_equal 'Your idmessage is private', rsp.comment
end
def test_accessors_with_hash
rsp = Response.parse ''
assert_equal({:nb => '3'}, rsp.listfriend)
assert_equal({:name => 'toto'}, rsp.friend)
assert_equal [{:name => 'toto'}, {:name => 'tata'}, {:name => 'titi'}], rsp.friends
assert_equal rsp.listfriend[:nb], rsp.friends.size.to_s
rsp = Response.parse ''
assert_equal rsp.listreceivedmsg, {:nb => '1'}
assert_equal({:from => 'toto', :title => 'my message', :date => 'today 11:59', :url => 'broad/001/948.mp3'}, rsp.msg)
assert_equal rsp.listreceivedmsg[:nb].to_i, rsp.msgs.size
end
def test_good
rsp = Response::Base::GoodServerRsp.new(String.new)
assert rsp.good?
assert !rsp.bad?
end
def test_bad
rsp = Response::Base::BadServerRsp.new(String.new)
assert rsp.bad?
assert !rsp.good?
end
def test_EmptyServerRsp
rsp = Response.parse ''
rsp2 = Response.parse %{ \n \n }
assert_instance_of Response::EmptyServerRsp, rsp
assert_instance_of Response::EmptyServerRsp, rsp2
assert !rsp.good?
assert !rsp.bad?
end
def test_LinkPreview
rsp = Response.parse 'LINKPREVIEWa comment'
assert_instance_of Response::LinkPreview, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_ListFriend
rsp = Response.parse ''
assert_instance_of Response::ListFriend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_ListReceivedMsg
rsp = Response.parse ''
assert_instance_of Response::ListReceivedMsg, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_Timezone
rsp = Response.parse '(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London'
assert_instance_of Response::Timezone, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_Signature
rsp = Response.parse 'my pretty signature ! '
assert_instance_of Response::Signature, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_Blacklist
rsp = Response.parse ''
assert_instance_of Response::Blacklist, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_RabbitSleep
rsp = Response.parse 'YES'
assert_instance_of Response::RabbitSleep, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_RabbitVersion
rsp = Response.parse 'V2'
assert_instance_of Response::RabbitVersion, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_VoiceListTTS
rsp = Response.parse ''
assert_instance_of Response::VoiceListTts, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_RabbitName
rsp = Response.parse 'Theo'
assert_instance_of Response::RabbitName, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_LangListUser
rsp = Response.parse ''
assert_instance_of Response::LangListUser, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_CommandSend
rsp = Response.parse 'COMMANDSENDYou rabbit will change status'
assert_instance_of Response::CommandSend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_AbuseSending
rsp = Response.parse 'ABUSESENDINGToo much message sending,try later'
assert_instance_of Response::AbuseSending, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_NoGoodTokenOrSerial
rsp = Response.parse 'NOGOODTOKENORSERIALYour token or serial number are not correct !'
assert_instance_of Response::NoGoodTokenOrSerial, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_MessageNotSend
rsp = Response.parse 'MESSAGENOTSENDYour idmessage is not correct or is private'
assert_instance_of Response::MessageNotSend, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_NabCastNotSend
rsp = Response.parse 'NABCASTNOTSENDYour idmessage is private'
assert_instance_of Response::NabCastNotSend, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_NabCastSend
rsp = Response.parse 'NABCASTSENDYour nabcast has been sent'
assert_instance_of Response::NabCastSend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_MessageSend
rsp = Response.parse 'MESSAGESENDYour message has been sent'
assert_instance_of Response::MessageSend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_TtsNotSend
rsp = Response.parse 'TTSNOTSENDYour text could not be sent'
assert_instance_of Response::TtsNotSend, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_TtsSend
rsp = Response.parse 'TTSSENDYour text has been sent'
assert_instance_of Response::TtsSend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_ChorSend
rsp = Response.parse 'CHORSENDYour chor has been sent'
assert_instance_of Response::ChorSend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_ChorNotSend
rsp = Response.parse 'CHORNOTSENDYour chor could not be sent (bad chor)'
assert_instance_of Response::ChorNotSend, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_EarPositionSend
rsp = Response.parse 'EARPOSITIONSENDYour ears command has been sent'
assert_instance_of Response::EarPositionSend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_EarPositionNotSend
rsp = Response.parse 'EARPOSITIONNOTSENDYour ears command could not be sent'
assert_instance_of Response::EarPositionNotSend, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_PositionEar
rsp = Response.parse 'POSITIONEAR810'
assert_instance_of Response::PositionEar, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_WebRadioSend
rsp = Response.parse 'WEBRADIOSENDYour webradio has been sent'
assert_instance_of Response::WebRadioSend, rsp
assert_kind_of Response::Base::GoodServerRsp, rsp
end
def test_WebRadioNotSend
rsp = Response.parse 'WEBRADIONOTSENDYour webradio could not be sent'
assert_instance_of Response::WebRadioNotSend, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_NoCorrectParameters
rsp = Response.parse ' NOCORRECTPARAMETERSPlease check urlList parameter !'
assert_instance_of Response::NoCorrectParameters, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
def test_NotV2Rabbit
rsp = Response.parse 'NOTV2RABBITV2 rabbit can use this action'
assert_instance_of Response::NotV2Rabbit, rsp
assert_kind_of Response::Base::BadServerRsp, rsp
end
end