def test_replace(): assert MessagePacket("a b c").replace(a='x', b='y').text == "x y c" assert MessagePacket("a b c").replace().text == "a b c" assert MessagePacket("a b c").replace(b='').text == "a c"
def test_sub(): """Test regex substitution.""" assert MessagePacket("%USER% is great!").sub( "%USER%", "Stanley").text == "Stanley is great!" assert MessagePacket("I would like 3 ", ("emoji", "😃"), "s.").sub( r'\d+', "<number>").text == "I would like <number> 😃s."
def test_check_urls(): assert not spam_handler.contains_urls(MessagePacket( "This message contains no URLs." )) assert not spam_handler.contains_urls(MessagePacket( "google.com was not parsed as a URL, and is therefore 'fine'." )) assert spam_handler.contains_urls(MessagePacket( "You should go check out ", ("url", "cactusbot.rtfd.org", "https://cactusbot.rtfd.org") ))
async def test_remove_alias(): """Remove an alias.""" assert (await alias("remove", "test", packet=MessagePacket("!alias remove test", role=5))) == "Alias !test removed."
async def test_create_alias(): """Create an alias.""" assert (await alias("add", "test", "testing", packet=MessagePacket( "!alias add test testing", role=5))) == "Alias !test for command !testing updated."
async def test_on_message(): assert (await spam_handler.on_message( MessagePacket("THIS CONTAINS EXCESSIVE CAPITAL LETTERS.") ))[0].text == "Please do not spam capital letters." assert (await spam_handler.on_message(MessagePacket( "This is what one hundred emoji looks like!", *(("emoji", "😮"),) * 100 )))[0].text == "Please do not spam emoji." assert (await spam_handler.on_message(MessagePacket( "Check out my amazing Twitter!", ("url", "twitter.com/CactusDevTeam", "https://twitter.com/CactusDevTeam") )))[0].text == "Please do not post URLs." assert await spam_handler.on_message( MessagePacket("PLEASE STOP SPAMMING CAPITAL LETTERS.", role=50) ) is None
def test_synthesize(): assert BeamParser.synthesize( MessagePacket( "Hey, ", ("tag", "Stanley"), "! ", ("emoji", "🌵"), " Check out ", ("url", "https://cactusbot.rtfd.org", "cactusbot.rtfd.org"), "!")) == (( "Hey, @Stanley! :cactus Check out cactusbot.rtfd.org!", ), {}) assert BeamParser.synthesize(MessagePacket( "waves", action=True)) == (("/me waves", ), {}) assert BeamParser.synthesize(MessagePacket("Hello!", target="Stanley")) == (( "Stanley", "Hello!", ), { "method": "whisper" })
def test_copy(): initial = MessagePacket("Test message.", user="******") copy = initial.copy() assert copy.text == "Test message." assert copy.user == "TestUser" assert initial.copy() is not initial new_copy = initial.copy("New message!") assert new_copy.text == "New message!" assert new_copy.user == "TestUser"
def test_join(): """Test joining message packets.""" assert MessagePacket.join( MessagePacket(("text", "I like "), ("emoji", "😃")), MessagePacket(" kittens!")).text == "I like 😃 kittens!" assert MessagePacket.join( MessagePacket("Testing"), MessagePacket(" Stuff!")).text == "Testing Stuff!" assert MessagePacket.join().text == "" assert MessagePacket.join(MessagePacket("Hello"), MessagePacket("world!"), separator="... ").text == "Hello... world!"
def test_check_emoji(): assert not spam_handler.check_emoji(MessagePacket( "This message contains no emoji." )) assert not spam_handler.check_emoji(MessagePacket( "Wow, that was great!", ("emoji", "😄"))) assert not spam_handler.check_emoji(MessagePacket( *(("emoji", "🌵"),) * 6 )) assert not spam_handler.check_emoji(MessagePacket( ("emoji", "😃"), ("emoji", "😛"), ("emoji", "🌵"), ("emoji", "🐹"), ("emoji", "🥔"), ("emoji", "💚") )) assert spam_handler.check_emoji(MessagePacket( *(("emoji", "🌵"),) * 7 )) assert spam_handler.check_emoji(MessagePacket( ("emoji", "😃"), ("emoji", "😛"), ("emoji", "🌵"), ("emoji", "🐹"), ("emoji", "🥔"), ("emoji", "💚"), ("emoji", "😎") )) assert spam_handler.check_emoji(MessagePacket( *(("emoji", "😄"),) * 100 ))
async def test_cube(): await verify_cube(MessagePacket("!cube", user="******"), "Username³") await verify_cube(MessagePacket("!cube 2"), "8. Whoa, that's 2Cubed!") await verify_cube( MessagePacket("!cube a b c d e f g h"), ' · '.join([n + '³' for n in "a b c d e f g h".split()])) await verify_cube(MessagePacket("!cube a b c d e f g h i"), "Whoa, that's 2 many cubes!") await verify_cube( MessagePacket("!cube 3 eggs and 4 slices of toast"), "27 · eggs³ · and³ · 64 · slices³ · of³ · toast³") await verify_cube( MessagePacket("!cube lots of taco salad ", ("emoji", "😃")), "lots³ · of³ · taco³ · salad³ · 😃³")
async def test_on_message(): """Test the message event.""" assert (await response_handler.on_message( MessagePacket("!testing", user="******"))) == StopIteration
async def test_command_remove(): """Remove a command.""" packet = MessagePacket("!command remove testing", role=5) assert (await command("remove", "testing", packet=packet)) == "Removed command !testing."
async def test_command_list(): """List commands.""" packet = MessagePacket("!command list", role=5) assert (await command("list", packet=packet)) == "Commands: testing"
async def test_on_message(): assert (await command_handler.on_message(MessagePacket("!cactus") )).text == "Ohai! I'm CactusBot! 🌵"
def verify(message, expected, *args, **kwargs): """Verify target substitutions.""" actual = command_handler._inject( MessagePacket(*message if isinstance(message, list) else (message, )), *args, **kwargs).text assert actual == expected
async def test_list_alias(): """Lis aliases.""" assert (await alias("list", packet=MessagePacket("!alias list", role=5))) == "Aliases: test (testing)."
def _split(text, *args, **kwargs): return [ component.text for component in MessagePacket(text).split(*args, **kwargs) ]
async def test_command_add(): """Add a command.""" packet = MessagePacket(("text", "lol"), ("emoji", "😃"), role=5) assert (await command("add", "testing", packet, packet=packet)) == "Updated command !testing."