Skip to content

Commit

Permalink
test: remove MockSopel from get_example_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Exirel committed Dec 9, 2019
1 parent 7115801 commit e1eae44
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 62 deletions.
14 changes: 7 additions & 7 deletions sopel/modules/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@


@commands('c', 'calc')
@example('.c 5 + 3', '8')
@example('.c 0.9*10', '9')
@example('.c 10*0.9', '9')
@example('.c 2*(1+2)*3', '18')
@example('.c 2**10', '1024')
@example('.c 5 // 2', '2')
@example('.c 5 / 2', '2.5')
@example('.c 5 + 3', '$nickname: 8')
@example('.c 0.9*10', '$nickname: 9')
@example('.c 10*0.9', '$nickname: 9')
@example('.c 2*(1+2)*3', '$nickname: 18')
@example('.c 2**10', '$nickname: 1024')
@example('.c 5 // 2', '$nickname: 2')
@example('.c 5 / 2', '$nickname: 2.5')
def c(bot, trigger):
"""Evaluate some calculation."""
if not trigger.group(2):
Expand Down
14 changes: 7 additions & 7 deletions sopel/modules/choose.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
@module.commands("ch")
@module.commands("choose")
@module.priority("medium")
@module.example(".choose a, b, c", r'Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a | b | c", r'Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a,b,c", r'Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a|b|c", r'Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a b c", r'Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a, b, c", r'$nickname: Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a | b | c", r'$nickname: Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a,b,c", r'$nickname: Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a|b|c", r'$nickname: Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a b c", r'$nickname: Your options: a, b, c. My choice: (a|b|c)', re=True)
@module.example(".choose a, b | just a",
r'Your options: "a, b", just a. My choice: ((a, b)|(just a))',
r'$nickname: Your options: "a, b", just a. My choice: ((a, b)|(just a))',
re=True)
@module.example(".choose a", 'Your options: a. My choice: a')
@module.example(".choose a", '$nickname: Your options: a. My choice: a')
def choose(bot, trigger):
""".choice option1|option2|option3 - Makes a difficult choice easy."""
if not trigger.group(2):
Expand Down
4 changes: 2 additions & 2 deletions sopel/modules/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ def exchange(bot, match):

@commands('cur', 'currency', 'exchange')
@example('.cur 100 usd in btc cad eur',
r'100\.0 USD is [\d\.]+ BTC, [\d\.]+ CAD, [\d\.]+ EUR',
r'$nickname: 100\.0 USD is [\d\.]+ BTC, [\d\.]+ CAD, [\d\.]+ EUR',
re=True)
@example('.cur 100 usd in btc cad eur can aux',
r'100\.0 USD is [\d\.]+ BTC, [\d\.]+ CAD, [\d\.]+ EUR, \(unsupported: CAN, AUX\)',
r'$nickname: 100\.0 USD is [\d\.]+ BTC, [\d\.]+ CAD, [\d\.]+ EUR, \(unsupported: CAN, AUX\)',
re=True)
def exchange_cmd(bot, trigger):
if not trigger.group(2):
Expand Down
16 changes: 8 additions & 8 deletions sopel/modules/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ def _roll_dice(bot, dice_expression):
@sopel.module.commands("dice")
@sopel.module.commands("d")
@sopel.module.priority("medium")
@sopel.module.example(".roll 3d1+1", 'You roll 3d1+1: (1+1+1)+1 = 4')
@sopel.module.example(".roll 3d1v2+1", 'You roll 3d1v2+1: (1[+1+1])+1 = 2')
@sopel.module.example(".roll 2d4", r'You roll 2d4: \(\d\+\d\) = \d', re=True, user_help=True)
@sopel.module.example(".roll 100d1", r'[^:]*: \(100x1\) = 100', re=True)
@sopel.module.example(".roll 1001d1", 'I only have 1000 dice. =(')
@sopel.module.example(".roll 1d1 + 1d1", 'You roll 1d1 + 1d1: (1) + (1) = 2')
@sopel.module.example(".roll 1d1+1d1", 'You roll 1d1+1d1: (1)+(1) = 2')
@sopel.module.example(".roll 1d6 # initiative", r'You roll 1d6: \(\d\) = \d', re=True)
@sopel.module.example(".roll 3d1+1", '$nickname: You roll 3d1+1: (1+1+1)+1 = 4')
@sopel.module.example(".roll 3d1v2+1", '$nickname: You roll 3d1v2+1: (1[+1+1])+1 = 2')
@sopel.module.example(".roll 2d4", r'$nickname: You roll 2d4: \(\d\+\d\) = \d', re=True, user_help=True)
@sopel.module.example(".roll 100d1", r'$nickname: [^:]*: \(100x1\) = 100', re=True)
@sopel.module.example(".roll 1001d1", '$nickname: I only have 1000 dice. =(')
@sopel.module.example(".roll 1d1 + 1d1", '$nickname: You roll 1d1 + 1d1: (1) + (1) = 2')
@sopel.module.example(".roll 1d1+1d1", '$nickname: You roll 1d1+1d1: (1)+(1) = 2')
@sopel.module.example(".roll 1d6 # initiative", r'$nickname: You roll 1d6: \(\d\) = \d', re=True)
def roll(bot, trigger):
""".dice XdY[vZ][+N][#COMMENT], rolls dice and reports the result.
Expand Down
10 changes: 5 additions & 5 deletions sopel/modules/rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@


@commands('rand')
@example('.rand 2', r'random\(0, 2\) = (0|1|2)', re=True, repeat=10)
@example('.rand -1 -1', 'random(-1, -1) = -1')
@example('.rand', r'random\(0, \d+\) = \d+', re=True)
@example('.rand 99 10', r'random\(10, 99\) = \d\d', re=True, repeat=10)
@example('.rand 10 99', r'random\(10, 99\) = \d\d', re=True, repeat=10)
@example('.rand 2', r'$nickname: random\(0, 2\) = (0|1|2)', re=True, repeat=10)
@example('.rand -1 -1', '$nickname: random(-1, -1) = -1')
@example('.rand', r'$nickname: random\(0, \d+\) = \d+', re=True)
@example('.rand 99 10', r'$nickname: random\(10, 99\) = \d\d', re=True, repeat=10)
@example('.rand 10 99', r'$nickname: random\(10, 99\) = \d\d', re=True, repeat=10)
def rand(bot, trigger):
"""Replies with a random number between first and second argument."""
arg1 = trigger.group(3)
Expand Down
8 changes: 4 additions & 4 deletions sopel/modules/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ def duck_api(query):
# test for bad Unicode handling in py2
@example(
'.duck grandorder.wiki chulainn alter',
r'https://grandorder.wiki/C%C3%BA_Chulainn_(Alter)',
r'$nickname: https://grandorder.wiki/C%C3%BA_Chulainn_(Alter)',
online=True)
# the last example (in source line order) is what .help displays
@example(
'.duck sopel.chat irc bot',
r'https?:\/\/sopel\.chat\/?',
r'$nickname: https?:\/\/sopel\.chat\/?',
re=True,
online=True)
def duck(bot, trigger):
Expand Down Expand Up @@ -165,8 +165,8 @@ def search(bot, trigger):

@commands('suggest')
@example('.suggest wikip', 'wikipedia', online=True)
@example('.suggest ', 'No query term.', online=True)
@example('.suggest lkashdfiauwgeaef', 'Sorry, no result.', online=True)
@example('.suggest ', '$nickname: No query term.', online=True)
@example('.suggest lkashdfiauwgeaef', '$nickname: Sorry, no result.', online=True)
def suggest(bot, trigger):
"""Suggests terms starting with given input"""
if not trigger.group(2):
Expand Down
8 changes: 5 additions & 3 deletions sopel/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ def tr(bot, trigger):

@commands('translate', 'tr')
@example('.tr :en :fr my dog',
'"mon chien" (en to fr, translate.google.com)',
'$nickname: "mon chien" (en to fr, translate.google.com)',
online=True)
@example('.tr היי',
'$nickname: "Hi" (iw to en, translate.google.com)',
online=True)
@example('.tr היי', '"Hi" (iw to en, translate.google.com)', online=True)
@example('.tr mon chien',
'"my dog" (fr to en, translate.google.com)',
'$nickname: "my dog" (fr to en, translate.google.com)',
online=True)
def tr2(bot, trigger):
"""Translates a phrase, with an optional language hint."""
Expand Down
26 changes: 13 additions & 13 deletions sopel/modules/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def k_to_c(temp):


@commands('temp')
@example('.temp 100F', '37.78°C = 100.00°F = 310.93K')
@example('.temp 100C', '100.00°C = 212.00°F = 373.15K')
@example('.temp 100K', '-173.15°C = -279.67°F = 100.00K')
@example('.temp 100F', '$nickname: 37.78°C = 100.00°F = 310.93K')
@example('.temp 100C', '$nickname: 100.00°C = 212.00°F = 373.15K')
@example('.temp 100K', '$nickname: -173.15°C = -279.67°F = 100.00K')
def temperature(bot, trigger):
"""Convert temperatures"""
try:
Expand Down Expand Up @@ -66,16 +66,16 @@ def temperature(bot, trigger):


@commands('length', 'distance')
@example('.distance 3m', '3.00m = 9 feet, 10.11 inches')
@example('.distance 3km', '3.00km = 1.86 miles')
@example('.distance 3 miles', '4.83km = 3.00 miles')
@example('.distance 3 inch', '7.62cm = 3.00 inches')
@example('.distance 3 feet', '91.44cm = 3 feet, 0.00 inches')
@example('.distance 3 yards', '2.74m = 9 feet, 0.00 inches')
@example('.distance 155cm', '1.55m = 5 feet, 1.02 inches')
@example('.length 3 ly', '28382191417742.40km = 17635876112814.77 miles')
@example('.length 3 au', '448793612.10km = 278867421.71 miles')
@example('.length 3 parsec', '92570329129020.20km = 57520535754731.61 miles')
@example('.distance 3m', '$nickname: 3.00m = 9 feet, 10.11 inches')
@example('.distance 3km', '$nickname: 3.00km = 1.86 miles')
@example('.distance 3 miles', '$nickname: 4.83km = 3.00 miles')
@example('.distance 3 inch', '$nickname: 7.62cm = 3.00 inches')
@example('.distance 3 feet', '$nickname: 91.44cm = 3 feet, 0.00 inches')
@example('.distance 3 yards', '$nickname: 2.74m = 9 feet, 0.00 inches')
@example('.distance 155cm', '$nickname: 1.55m = 5 feet, 1.02 inches')
@example('.length 3 ly', '$nickname: 28382191417742.40km = 17635876112814.77 miles')
@example('.length 3 au', '$nickname: 448793612.10km = 278867421.71 miles')
@example('.length 3 parsec', '$nickname: 92570329129020.20km = 57520535754731.61 miles')
def distance(bot, trigger):
"""Convert distances"""
try:
Expand Down
2 changes: 1 addition & 1 deletion sopel/modules/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def shutdown(bot):
@module.commands('title')
@module.example(
'.title https://www.google.com',
'[ Google ] - www.google.com',
'$nickname: [ Google ] - www.google.com',
online=True)
def title_command(bot, trigger):
"""
Expand Down
52 changes: 40 additions & 12 deletions sopel/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ class MockSopelWrapper(SopelWrapper):
pass


TEST_CONFIG = """
[core]
nick = {name}
owner = {owner}
admin = {admin}
"""


def get_example_test(tested_func, msg, results, privmsg, admin,
owner, repeat, use_regexp, ignore=[]):
"""Get a function that calls ``tested_func`` with fake wrapper and trigger.
Expand All @@ -138,8 +146,16 @@ def get_example_test(tested_func, msg, results, privmsg, admin,
:return: a test function for ``tested_func``
:rtype: ``callable``
"""
def test():
bot = MockSopel("NickName", admin=admin, owner=owner)
def test(configfactory, botfactory, ircfactory):
test_config = TEST_CONFIG.format(
name='NickName',
admin=admin,
owner=owner,
)
settings = configfactory('default.cfg', test_config)
bot = botfactory(settings)
server = ircfactory(bot)
server.channel_joined('#Sopel')

match = None
if hasattr(tested_func, "commands"):
Expand All @@ -156,7 +172,7 @@ def test():
full_message = ':{} PRIVMSG {} :{}'.format(hostmask, sender, msg)

pretrigger = sopel.trigger.PreTrigger(bot.nick, full_message)
trigger = sopel.trigger.Trigger(bot.config, pretrigger, match)
trigger = sopel.trigger.Trigger(bot.settings, pretrigger, match)

module = sys.modules[tested_func.__module__]
if hasattr(module, 'setup'):
Expand All @@ -172,18 +188,30 @@ def isnt_ignored(value):
expected_output_count = 0
for _i in range(repeat):
expected_output_count += len(results)
wrapper = MockSopelWrapper(bot, trigger)
wrapper = SopelWrapper(bot, trigger)
tested_func(wrapper, trigger)
wrapper.output = list(filter(isnt_ignored, wrapper.output))
assert len(wrapper.output) == expected_output_count
for result, output in zip(results, wrapper.output):
if type(output) is bytes:
output = output.decode('utf-8')

triggers = (
sopel.trigger.PreTrigger(bot.nick, output.decode('utf-8'))
for output in wrapper.backend.message_sent
)
outputs = [
output.args[-1]
for output in triggers
if isnt_ignored(output.args[-1])
]

# output lenght
assert len(outputs) == expected_output_count

# output content
for result, output in zip(results, outputs):
expected = result.replace('$nickname', bot.nick)
if use_regexp:
if not re.match(result, output):
assert result == output
if not re.match(expected, output):
assert expected == output
else:
assert result == output
assert expected == output

return test

Expand Down

0 comments on commit e1eae44

Please sign in to comment.