|
| 1 | +"""Tests for Sopel's ``find`` plugin""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from sopel.formatting import bold |
| 7 | +from sopel.tests import rawlist |
| 8 | + |
| 9 | + |
| 10 | +TMP_CONFIG = """ |
| 11 | +[core] |
| 12 | +owner = Admin |
| 13 | +nick = Sopel |
| 14 | +enable = |
| 15 | + find |
| 16 | +host = irc.libera.chat |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def bot(botfactory, configfactory): |
| 22 | + settings = configfactory('default.ini', TMP_CONFIG) |
| 23 | + return botfactory.preloaded(settings, ['find']) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def irc(bot, ircfactory): |
| 28 | + return ircfactory(bot) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def user(userfactory): |
| 33 | + return userfactory('User') |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def other_user(userfactory): |
| 38 | + return userfactory('other_user') |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def channel(): |
| 43 | + return '#testing' |
| 44 | + |
| 45 | + |
| 46 | +REPLACES_THAT_WORK = ( |
| 47 | + ("A simple line.", r"s/line/message/", f"A simple {bold('message')}."), |
| 48 | + ("An escaped / line.", r"s/\//slash/", f"An escaped {bold('slash')} line."), |
| 49 | + ("A piped line.", r"s|line|replacement|", f"A piped {bold('replacement')}."), |
| 50 | + ("An escaped | line.", r"s|\||pipe|", f"An escaped {bold('pipe')} line."), |
| 51 | + ("An escaped \\ line.", r"s/\\/backslash/", f"An escaped {bold('backslash')} line."), |
| 52 | + ("abABab", r"s/b/c/g", "abABab".replace('b', bold('c'))), # g (global) flag |
| 53 | + ("ABabAB", r"s/b/c/i", f"A{bold('c')}abAB"), # i (case-insensitive) flag |
| 54 | + ("ABabAB", r"s/b/c/ig", f"A{bold('c')}a{bold('c')}A{bold('c')}"), # both flags |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize('original, command, result', REPLACES_THAT_WORK) |
| 59 | +def test_valid_replacements(bot, irc, user, channel, original, command, result): |
| 60 | + """Verify that basic replacement functionality works.""" |
| 61 | + irc.channel_joined(channel, [user.nick]) |
| 62 | + |
| 63 | + irc.say(user, channel, original) |
| 64 | + irc.say(user, channel, command) |
| 65 | + |
| 66 | + assert len(bot.backend.message_sent) == 1, ( |
| 67 | + "The bot should respond with exactly one line.") |
| 68 | + assert bot.backend.message_sent == rawlist( |
| 69 | + "PRIVMSG %s :%s meant to say: %s" % (channel, user.nick, result), |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def test_multiple_users(bot, irc, user, other_user, channel): |
| 74 | + """Verify that correcting another user's line works.""" |
| 75 | + irc.channel_joined(channel, [user.nick, other_user.nick]) |
| 76 | + |
| 77 | + irc.say(other_user, channel, 'Some weather we got yesterday') |
| 78 | + irc.say(user, channel, '%s: s/yester/to/' % other_user.nick) |
| 79 | + |
| 80 | + assert len(bot.backend.message_sent) == 1, ( |
| 81 | + "The bot should respond with exactly one line.") |
| 82 | + assert bot.backend.message_sent == rawlist( |
| 83 | + "PRIVMSG %s :%s thinks %s meant to say: %s" % ( |
| 84 | + channel, user.nick, other_user.nick, |
| 85 | + f"Some weather we got {bold('to')}day", |
| 86 | + ), |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def test_replace_the_replacement(bot, irc, user, channel): |
| 91 | + """Verify replacing text that was already replaced.""" |
| 92 | + irc.channel_joined(channel, [user.nick]) |
| 93 | + |
| 94 | + irc.say(user, channel, 'spam') |
| 95 | + irc.say(user, channel, 's/spam/eggs/') |
| 96 | + irc.say(user, channel, 's/eggs/bacon/') |
| 97 | + |
| 98 | + assert len(bot.backend.message_sent) == 2, ( |
| 99 | + "The bot should respond twice.") |
| 100 | + assert bot.backend.message_sent == rawlist( |
| 101 | + "PRIVMSG %s :%s meant to say: %s" % ( |
| 102 | + channel, user.nick, bold('eggs'), |
| 103 | + ), |
| 104 | + "PRIVMSG %s :%s meant to say: %s" % ( |
| 105 | + channel, user.nick, bold('bacon'), |
| 106 | + ), |
| 107 | + ) |
0 commit comments