diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2021-03-25 12:42:01 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2021-03-25 12:42:01 +0100 |
| commit | 3199c11a7b92e2ce2739fe6a167a09fb401e62aa (patch) | |
| tree | 44b0e41d131d54514d886563985f342cf90451cd | |
| parent | e9f15a794b62fa8d18e926dfd2fe52fcef180c56 (diff) | |
| download | geldschieberbot-3199c11a7b92e2ce2739fe6a167a09fb401e62aa.tar.gz geldschieberbot-3199c11a7b92e2ce2739fe6a167a09fb401e62aa.zip | |
support !split person amount
| -rw-r--r-- | geldschieberbot.py | 21 | ||||
| -rwxr-xr-x | test.py | 14 |
2 files changed, 25 insertions, 10 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py index cc0b0cc..135fa33 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -269,21 +269,30 @@ def split(sender, args, msg): try: amount = to_cent(args[1]) + persons = args[2:] except: - return None, "amount must be a positive number" + # support !split name amount + if len(args) == 3: + try: + amount = to_cent(args[2]) + persons = [args[1]] + except: + return None, "amount must be a positive number" + else: + return None, "amount must be a positive number" - # len(args) - cmd - amount + sender - persons = len(args) - 2 + 1 - amount_per_person = int(amount/persons) + # persons + sender + npersons = len(persons) + 1 + amount_per_person = int(amount/npersons) if sender in num2name: recipient = num2name[sender] else: return None, "you must register first" - output = f"Split {to_euro(amount)} between {persons} -> {to_euro(amount_per_person)} each\n" + output = f"Split {to_euro(amount)} between {npersons} -> {to_euro(amount_per_person)} each\n" change = [args] - for p in args[2:]: + for p in persons: if p in name2num: if p == recipient: output += f"{p}, you will be charged multiple times. This may not be what you want\n" @@ -299,10 +299,6 @@ class TestSplitCmd(unittest.TestCase): res = run_bot(self, num[alice], "!split 10") self.assertEqual(res.stdout, 'ERROR: not in form "!split amount [name]+"') - res = run_bot(self, num[alice], "!split foo 10") - self.assertEqual(res.stdout, 'ERROR: amount must be a positive number') - - def test_split_one_unknown_user(self): res = run_bot(self, num[alice], "!split 30 " + "foo" + " " + charlie) msg = \ @@ -346,6 +342,16 @@ alice: \tBalance: 20.00""" self.assertEqual(res.stdout, msg) + def test_split_single_user_amount_swap(self): + res = run_bot(self, num[alice], f"!split {bob} 10") + msg = \ +"""Split 10.00 between 2 -> 5.00 each +New Balance: +alice: +\t<- bob 5.00 +\tBalance: 5.00""" + self.assertEqual(res.stdout, msg) + def test_split_whitespace(self): res = run_bot(self, num[alice], "!split 30 " + bob + " " + charlie + " ") msg = \ |
