From 23113dbcaac93b16128352ad179f1adc18659fdc Mon Sep 17 00:00:00 2001 From: freiheit Date: Sun, 24 Mar 2024 20:05:16 -0700 Subject: [PATCH 1/2] Strip out cheating code. Closes #111 --- mvkroller.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/mvkroller.py b/mvkroller.py index 709dbe2..71d6d2d 100644 --- a/mvkroller.py +++ b/mvkroller.py @@ -74,7 +74,7 @@ def parse_dice(dicestr: str): return dicecounts -def roll_dice(dicecounts, cheat=False): +def roll_dice(dicecounts): """Returns a dictionary of dieSize => rolls[]""" dicerolls = { 20: [], @@ -88,11 +88,7 @@ def roll_dice(dicecounts, cheat=False): try: for size, num in dicecounts.items(): if num > 0: - # pylint: disable=unused-variable - if cheat: - dicerolls[size] = [size for idx in range(0, num)] - else: - dicerolls[size] = [random.randint(1, size) for idx in range(0, num)] + dicerolls[size] = [random.randint(1, size) for idx in range(0, num)] except Exception as exc: raise RollError("Exception while rolling dice.") from exc @@ -201,7 +197,6 @@ def mvkroll(dicestr: str): logger.debug("Roll %s", {dicestr}) answer = "" - cheat = False advantage = False disadvantage = False @@ -210,9 +205,6 @@ def mvkroll(dicestr: str): elif re.search(r"advantage", dicestr, flags=re.IGNORECASE): advantage = True - if re.search(r"cheat", dicestr, flags=re.IGNORECASE): - cheat = True - dicecounts = parse_dice(dicestr) # advantage and disadvantage need _at least_ 2d20 @@ -225,7 +217,7 @@ def mvkroll(dicestr: str): dicecounts[20] = 1 answer += "_No advantage/disadvantage, setting 1d20_\n" - dicerolls = roll_dice(dicecounts, cheat) + dicerolls = roll_dice(dicecounts) # the d20 is called the "Fortune Die" fortunedicerolls = dicerolls[20] @@ -259,9 +251,6 @@ def mvkroll(dicestr: str): answer += calc_impact(fortunedicerolls, characterdicerolls) - if cheat: - answer = "\n# Cheating #\n" + answer + "\n# Cheater #\n" - return answer @@ -271,10 +260,9 @@ def plainroll(dicestr: str): logger.debug("Roll %s", {dicestr}) answer = "" - cheat = False dicecounts = parse_dice(dicestr) - dicerolls = roll_dice(dicecounts, cheat) + dicerolls = roll_dice(dicecounts) answer += print_dice(dicerolls) From 2696f835f6b2acab8c21aed4442e1be559399b80 Mon Sep 17 00:00:00 2001 From: freiheit <19813+freiheit@users.noreply.github.com> Date: Mon, 25 Mar 2024 11:31:47 -0700 Subject: [PATCH 2/2] Pull current test stuff into main branch (#114) * 106 Add a unit test class; add tests for the parser * Add a test that "d10 d10" == "2d10" * Helper script for test.py * Add a github action to run tests Co-authored-by: @slashingweapon --- .github/workflows/test.yml | 23 ++++++++++++++++ README.md | 2 ++ test.py | 55 ++++++++++++++++++++++++++++++++++++++ test.sh | 2 ++ 4 files changed, 82 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 test.py create mode 100755 test.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..c78f190 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: Test + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9", "3.10", "3.11"] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: Run tests + run: | + python test.py diff --git a/README.md b/README.md index ad69a24..a4ac1c9 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,5 @@ https://discord.com/api/oauth2/authorize?client_id=1168083075515826186&permissio 10. Use the OAuth2 URL to authorize the bot to talk to your server. You should be able to run the bot with `./run.sh` + +You can test the dice parsing and rolling code with `python test.py` diff --git a/test.py b/test.py new file mode 100644 index 0000000..f09b5d4 --- /dev/null +++ b/test.py @@ -0,0 +1,55 @@ +#!.venv/bin/python3 +# MvKDiceBot: Discord bot for rolling dice for Mecha Vs Kaiju +# Copyright (C) 2023 Eric Eisenhart +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# https://github.com/freiheit/MvKDiceBot +"""Test module for MvKDiceBot""" + +import unittest +import mvkroller as roller + +class TestRoller(unittest.TestCase): + """Test cases for the mvkroller functions.""" + + def test_parser_good(self): + """Test the parser with valid strings, including some that produce no dice.""" + strings = { + "": {20:0, 12:0, 10:0, 8:0, 6:0, 4:0}, + "there are no matching dice strings here": {20:0, 12:0, 10:0, 8:0, 6:0, 4:0}, + "messyd20what?": {20:1, 12:0, 10:0, 8:0, 6:0, 4:0}, + "d20 3d6 5d10": {20:1, 12:0, 10:5, 8:0, 6:3, 4:0}, + "d20 d6 d6 d6 4d10 d10": {20:1, 12:0, 10:5, 8:0, 6:3, 4:0}, + "4d12 1d10 2d4": {20:0, 12:4, 10:1, 8:0, 6:0, 4:2}, + " d4 d6 d8 d12 ": {20:0, 12:1, 10:0, 8:1, 6:1, 4:1}, + "1024d20, 500d4": {20:1024, 12:0, 10:0, 8:0, 6:0, 4:500}, + } + for (dstring, dspec) in strings.items(): + with self.subTest(dstring=dstring): + self.assertEqual(roller.parse_dice(dstring), dspec) + + def test_parser_bad(self): + """Ensure the parser will throw exceptions when bad dice sizes are given.""" + strings = [ + "50d3nothing", + "50d17", + "1d20 2d7", + ] + for dstring in strings: + with self.subTest(dstring=dstring), self.assertRaises(roller.RollError): + roller.parse_dice(dstring) + +if __name__ == "__main__": + unittest.main() diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..c07ab03 --- /dev/null +++ b/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh +./.venv/bin/python test.py "$@"