Example #1
0
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"
Example #2
0
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."
Example #3
0
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"
Example #4
0
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"
Example #5
0
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")
    ))
Example #6
0
async def test_remove_alias():
    """Remove an alias."""
    assert (await
            alias("remove",
                  "test",
                  packet=MessagePacket("!alias remove test",
                                       role=5))) == "Alias !test removed."
Example #7
0
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!"
Example #8
0
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."
Example #9
0
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!"
Example #10
0
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
Example #11
0
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"
                                               })
Example #12
0
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
    ))
Example #13
0
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³ · 😃³")
Example #14
0
def _split(text, *args, **kwargs):
    return [
        component.text
        for component in MessagePacket(text).split(*args, **kwargs)
    ]
Example #15
0
async def test_on_message():
    """Test the message event."""

    assert (await response_handler.on_message(
        MessagePacket("!testing", user="******"))) == StopIteration
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."
Example #17
0
async def test_on_message():
    assert (await
            command_handler.on_message(MessagePacket("!cactus")
                                       )).text == "Ohai! I'm CactusBot! 🌵"
Example #18
0
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
Example #19
0
async def test_list_alias():
    """Lis aliases."""
    assert (await
            alias("list",
                  packet=MessagePacket("!alias list",
                                       role=5))) == "Aliases: test (testing)."
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"