Exemplo n.º 1
0
 def test_check_response(self):
     # Setup common testing objects
     serv1 = ServerMock(None)
     serv1.name = "test_serv1"
     chan1 = serv1.get_channel_by_name("test_chan1")
     user1 = serv1.get_user_by_name("test_user1")
     # Test basic response works
     rm1 = ReplyMessage("test")
     rm1.add_response("response")
     assert rm1.check_response("just a test", user1, chan1) == "response"
     # Test regex pattern match
     rm2 = ReplyMessage("\\btest[0-9]+\\b")
     rm2.add_response("response")
     assert rm2.check_response("random testing", user1, chan1) is None
     assert rm2.check_response("random test here", user1, chan1) is None
     assert rm2.check_response("this is test3 I think", user1, chan1) == "response"
     assert rm2.check_response("this is test4", user1, chan1) == "response"
     # Test multi-response works
     rm3 = ReplyMessage("test")
     rm3.add_response("response1")
     rm3.add_response("response2")
     rm3.add_response("response3")
     found_responses = set()
     for _ in range(50):
         response = rm3.check_response("another test", user1, chan1)
         found_responses.add(response)
         assert response in ["response1", "response2", "response3"]
     assert len(found_responses) > 1
     # Test replacements
     rm4 = ReplyMessage("test")
     rm4.add_response("response {USER} {CHANNEL} {SERVER}")
     assert rm4.check_response("test", user1, chan1) == "response test_user1 test_chan1 test_serv1"
Exemplo n.º 2
0
 def test_xml(self):
     rm1_regex = "\\btest[0-9]+\\b"
     rm1_resp1 = "response1"
     rm1_resp2 = "response2 {USER} {CHANNEL} {SERVER}"
     rm1_resp3 = "<response>"
     rm1_serv1 = "serv1"
     rm1_serv2 = "serv2"
     rm1_serv3 = "serv3"
     rm1_chan1 = "chan1"
     rm1_chan2 = "chan2"
     rm1_chan3 = "chan3"
     rm1 = ReplyMessage(rm1_regex)
     rm1.add_response(rm1_resp1)
     rm1.add_response(rm1_resp2)
     rm1.add_response(rm1_resp3)
     rm1.add_whitelist(rm1_serv1, rm1_chan1)
     rm1.add_blacklist(rm1_serv2, rm1_chan2)
     rm1.add_blacklist(rm1_serv3, rm1_chan3)
     rm1_xml = rm1.to_xml()
     rm1_obj = ReplyMessage.from_xml(rm1_xml)
     assert rm1_obj.prompt.pattern == rm1.prompt.pattern
     assert len(rm1_obj.response_list) == len(rm1.response_list)
     for resp in rm1_obj.response_list:
         assert resp in rm1.response_list
     assert len(rm1_obj.whitelist) == len(rm1.whitelist)
     for white_serv in rm1_obj.whitelist:
         assert white_serv in rm1.whitelist
         for white_chan in rm1_obj.whitelist[white_serv]:
             assert white_chan in rm1.whitelist[white_serv]
     assert len(rm1_obj.blacklist) == len(rm1.blacklist)
     for black_serv in rm1_obj.blacklist:
         assert black_serv in rm1.blacklist
         for black_chan in rm1_obj.blacklist[black_serv]:
             assert black_chan in rm1.blacklist[black_serv]
Exemplo n.º 3
0
 def test_add_response(self):
     # Create reply message
     rm = ReplyMessage("test")
     # Add response
     rm.add_response("reply2")
     # Check
     assert len(rm.response_list) == 1
     assert rm.response_list[0] == "reply2"
     # Add another
     rm.add_response("reply3")
     assert len(rm.response_list) == 2
     assert rm.response_list[1] == "reply3"
Exemplo n.º 4
0
 def test_get_response(self):
     # Setup common testing objects
     serv1 = ServerMock(None)
     serv1.name = "test_serv1"
     chan1 = serv1.get_channel_by_name("test_chan1")
     user1 = serv1.get_user_by_name("test_user1")
     # Basic ReplyMessageList get_response() test
     rml1 = ReplyMessageList()
     rm1 = ReplyMessage("test1")
     rm1.add_response("response1")
     rm2 = ReplyMessage("test2")
     rm2.add_response("response2")
     rml1.add_reply_message(rm1)
     rml1.add_reply_message(rm2)
     # Check response 1 works
     assert rml1.get_response("test1", user1, chan1) == "response1"
     assert rml1.get_response("test2", user1, chan1) == "response2"
     assert rml1.get_response("test3", user1, chan1) is None
     # Check blacklists
     rml2 = ReplyMessageList()
     rm1 = ReplyMessage("test")
     rm1.add_response("response1")
     rm1.add_blacklist(serv1.name, chan1.name)
     rm2 = ReplyMessage("test")
     rm2.add_response("response2")
     rml2.add_reply_message(rm1)
     rml2.add_reply_message(rm2)
     assert rml2.get_response("test", user1, chan1) == "response2"
     # Check whitelists
     rml3 = ReplyMessageList()
     rm1 = ReplyMessage("test")
     rm1.add_response("response1")
     rm1.add_whitelist(serv1.name, "not_chan_1")
     rm2 = ReplyMessage("test")
     rm2.add_response("response2")
     rml3.add_reply_message(rm1)
     rml3.add_reply_message(rm2)
     assert rml3.get_response("test", user1, chan1) == "response2"
     # Check replacements
     rml4 = ReplyMessageList()
     rm1 = ReplyMessage("test")
     rm1.add_response("response {USER} {CHANNEL} {SERVER}")
     rml4.add_reply_message(rm1)
     assert rml4.get_response("test", user1, chan1) == "response test_user1 test_chan1 test_serv1"