From c66731f623e190aa9198dca7cc99a96151ff4d4e Mon Sep 17 00:00:00 2001 From: dgw Date: Thu, 26 Oct 2023 06:40:40 -0500 Subject: [PATCH 1/3] calc: cover missing lines with `plugin.example` tests Oops! All Error Cases! ...were not covered by the test suite. That was the easiest 100% ever. --- sopel/builtins/calc.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sopel/builtins/calc.py b/sopel/builtins/calc.py index f591a3ab0..71486726d 100644 --- a/sopel/builtins/calc.py +++ b/sopel/builtins/calc.py @@ -12,6 +12,10 @@ @plugin.command('c', 'calc') +@plugin.example('.c', 'Nothing to calculate.') +@plugin.example('.c foo * bar', "Can't process expression: Node type 'Name' is not supported.") +@plugin.example('.c 10 / 0', 'Division by zero is not supported in this universe.') +@plugin.example('.c 10\\2', 'Invalid syntax') @plugin.example('.c 5 + 3', '8') @plugin.example('.c 0.9*10', '9') @plugin.example('.c 10*0.9', '9') From 0f7e91038c0557cf9b55f650430f797ba0c454f3 Mon Sep 17 00:00:00 2001 From: dgw Date: Thu, 26 Oct 2023 06:41:44 -0500 Subject: [PATCH 2/3] calc: more help examples Sopel displays only one example command if none of the examples is defined as `user_help=True`, and I believe these four happen to show off the most variety of usage in a suitably succinct way. --- sopel/builtins/calc.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sopel/builtins/calc.py b/sopel/builtins/calc.py index 71486726d..89d714f0d 100644 --- a/sopel/builtins/calc.py +++ b/sopel/builtins/calc.py @@ -19,10 +19,10 @@ @plugin.example('.c 5 + 3', '8') @plugin.example('.c 0.9*10', '9') @plugin.example('.c 10*0.9', '9') -@plugin.example('.c 2*(1+2)*3', '18') -@plugin.example('.c 2**10', '1024') -@plugin.example('.c 5 // 2', '2') -@plugin.example('.c 5 / 2', '2.5') +@plugin.example('.c 2*(1+2)*3', '18', user_help=True) +@plugin.example('.c 2**10', '1024', user_help=True) +@plugin.example('.c 5 // 2', '2', user_help=True) +@plugin.example('.c 5 / 2', '2.5', user_help=True) @plugin.output_prefix('[calc] ') def c(bot, trigger): """Evaluate some calculation.""" From 5854e8ad9c31ee4f0c7f61d4a80f968db85957da Mon Sep 17 00:00:00 2001 From: dgw Date: Sat, 28 Oct 2023 09:48:32 -0500 Subject: [PATCH 3/3] calc: test even more edge cases These additional cases (and a new error handler) cover as much of the `tools.calculation` submodule as possible using the example decorator. Further coverage of `tools.calculation` would require real unit tests of the sort one finds in this project's `test/` directory. --- sopel/builtins/calc.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sopel/builtins/calc.py b/sopel/builtins/calc.py index 89d714f0d..50816e657 100644 --- a/sopel/builtins/calc.py +++ b/sopel/builtins/calc.py @@ -16,9 +16,19 @@ @plugin.example('.c foo * bar', "Can't process expression: Node type 'Name' is not supported.") @plugin.example('.c 10 / 0', 'Division by zero is not supported in this universe.') @plugin.example('.c 10\\2', 'Invalid syntax') +@plugin.example('.c (10**1000000)**2', + "Error running calculation: " + "ValueError('Pow expression too complex to calculate.')") +@plugin.example('.c (10**100000)**2 * (10**100000)**2', + "Error running calculation: " + "ValueError('Value is too large to be handled in limited time and memory.')") @plugin.example('.c 5 + 3', '8') @plugin.example('.c 0.9*10', '9') @plugin.example('.c 10*0.9', '9') +@plugin.example('.c 0.5**2', '0.25') +@plugin.example('.c 3**0', '1') +@plugin.example('.c 0 * 5', '0') +@plugin.example('.c 5**5', '3125') @plugin.example('.c 2*(1+2)*3', '18', user_help=True) @plugin.example('.c 2**10', '1024', user_help=True) @plugin.example('.c 5 // 2', '2', user_help=True) @@ -43,5 +53,8 @@ def c(bot, trigger): except SyntaxError: bot.reply('Invalid syntax') return + except ValueError as err: + bot.reply("Error running calculation: %r" % err) + return bot.say(result)