class TestAdd(unittest.TestCase):
    """
    Tests that numeric addition expressions can be parsed.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_add(self):
        """
        Tests that additions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1+2").roll()

        self.assertEqual(3, result)

    def test_add_toNegative(self):
        """
        Tests that additions to a negative value can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("-1+2").roll()

        self.assertEqual(1, result)
class TestAddLong(unittest.TestCase):
    """
    Tests that long numeric addition expressions can be parsed.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_longAdd(self):
        """
        Tests that long additions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1+2+3").roll()

        self.assertEqual(6, result)

    def test_longerAdd(self):
        """
        Tests that longer additions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1+2+3+4+5").roll()

        self.assertEqual(15, result)
class TestSubLong(unittest.TestCase):
    """
    Tests that long numeric subtraction expressions can be parsed.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_longSub(self):
        """
        Tests that long subtractions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1-2-3").roll()

        # TODO: Maybe it should be "-1-2-3"

        self.assertEqual(-4, result)

    def test_longerSub(self):
        """
        Tests that longer subtractions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1-2-3-4-5").roll()

        # TODO: Maybe it should be "-1-2-3-4-5"

        self.assertEqual(-13, result)
class TestRoll(unittest.TestCase):
    """
    Tests that rolling the result from parsing numeric expressions returns the expected value.
    """

    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_number_roll(self):
        """
        Tests that rolling a parsed number returns the expected value.
        """
        result = self.parser.parse("1").roll()

        self.assertEqual(1, result)

    def test_add_roll(self):
        """
        Tests that rolling a parsed numeric addition returns the expected value.
        """
        result = self.parser.parse("1+2").roll()

        self.assertEqual(3, result)

    def test_sub_roll(self):
        """
        Tests that rolling a parsed numeric subtraction returns the expected value.
        """
        result = self.parser.parse("1-2").roll()

        self.assertEqual(-1, result)
class TestNumber(unittest.TestCase):
    """
    Tests that numeric expressions can be parsed.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_positive(self):
        """
        Tests that positive numbers can be parsed.
        """
        result = self.parser.parse("5").roll()

        self.assertEqual(5, result)

    def test_zero(self):
        """
        Tests that the zero value can be parsed.
        """
        result = self.parser.parse("0").roll()

        self.assertEqual(0, result)

    def test_negative(self):
        """
        Tests that negative numbers can be parsed.
        """
        result = self.parser.parse("-5").roll()

        self.assertEqual(-5, result)
class TestIncompleteDice(unittest.TestCase):
    """
    Tests incomplete dice expressions.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_onlySeparator(self):
        """
        Tests that the dice separator can't be parsed.
        """
        result = self.parser.parse("d")

        self.assertIsNone(result)

    def test_noQuantity(self):
        """
        Tests that a dice without quantity can't be parsed.
        """
        # result = self.parser.parse("d6")

        # TODO
        # self.assertIsNone(result)

    def test_noSides(self):
        """
        Tests that a dice with no sides can't be parsed.
        """
        result = self.parser.parse("1d")

        self.assertIsNone(result)
class TestRollMixedBinaryOp(unittest.TestCase):
    """
    Tests that rolling the result from parsing mixed numeric binary operation expressions returns the expected value.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_addAndSub_roll(self):
        """
        Tests that rolling a parsed numeric additions followed by subtractions returns the expected value.
        """
        result = self.parser.parse("1+2-3").roll()

        self.assertEqual(0, result)

    def test_subAndAdd_roll(self):
        """
        Tests that rolling a parsed numeric subtractions followed by additions returns the expected value.
        """
        result = self.parser.parse("3-1+2").roll()

        self.assertEqual(4, result)
Exemple #8
0
class TestNumericBinaryOperationMixed(unittest.TestCase):
    """
    Tests that mixed numeric binary operation expressions can be parsed.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_addAndSub(self):
        """
        Tests that additions followed by subtractions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1+2-3").roll()

        self.assertEqual(0, result)

    def test_subAndAdd(self):
        """
        Tests that subtractions followed by additions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("3-1+2").roll()

        self.assertEqual(4, result)
class TestSub(unittest.TestCase):
    """
    Tests that numeric subtraction expressions can be parsed.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_sub_positive(self):
        """
        Tests that subtractions can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("2-1").roll()

        self.assertEqual(1, result)

    def test_sub_negative(self):
        """
        Tests that subtractions ending in a negative value can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1-2").roll()

        self.assertEqual(-1, result)

    def test_sub_zero(self):
        """
        Tests that subtractions ending in zero can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("1-1").roll()

        self.assertEqual(0, result)

    def test_sub_negatives(self):
        """
        Tests that subtractions of negative values can be parsed, and the result is the expected one.
        """
        result = self.parser.parse("-1-1").roll()

        self.assertEqual(-2, result)
class TestInvalidNumber(unittest.TestCase):
    """
    Tests invalid numeric expressions.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_doubleNegative(self):
        """
        Tests that a number with two negative markers can't be parsed.
        """
        result = self.parser.parse("--15")
class TestInvalidDice(unittest.TestCase):
    """
    Tests invalid expressions.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_negativeQuantity(self):
        """
        Tests that a negative dice can't be parsed.
        """
        result = self.parser.parse("-1d6")

        # TODO
        # self.assertIsNone(result)

    def test_negativeSides(self):
        """
        Tests that a dice with a negative side can't be parsed.
        """
        result = self.parser.parse("1d-6")
class TestSimpleDice(unittest.TestCase):
    """
    Tests that simple dice expressions can be parsed.
    """

    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_minimalDice(self):
        """
        Tests that the minimal dice can be rolled.
        """
        result = self.parser.parse("1d1").roll()

        self.assertEqual(1, result)
 def setUp(self):
     """
     Initializes parser.
     """
     self.parser = DiceParser()
Exemple #14
0
class TestSimpleDice(unittest.TestCase):
    """
    Tests that simple dice expressions can be parsed.
    """
    def setUp(self):
        """
        Initializes parser.
        """
        self.parser = DiceParser()

    def test_simpleDice(self):
        """
        Tests that a simple dice notation can be parsed.
        """
        dice = self.parser.parse("1d6")

        self.assertEqual(1, dice.quantity)
        self.assertEqual(6, dice.sides)

    def test_simpleDice_upperCaseSeparator(self):
        """
        Tests that the upper case dice separator can be parsed.
        """
        dice = self.parser.parse("1D6")

        self.assertEqual(1, dice.quantity)
        self.assertEqual(6, dice.sides)

    def test_onesDice(self):
        """
        Tests that dice notation with a single dice and a single side can be parsed.
        """
        dice = self.parser.parse("1d1")

        self.assertEqual(1, dice.quantity)
        self.assertEqual(1, dice.sides)

    def test_zeroQuantity(self):
        """
        Tests that dice notation with zero dice is parsed.
        """
        dice = self.parser.parse("0d6")

        self.assertEqual(0, dice.quantity)
        self.assertEqual(6, dice.sides)

    def test_zeroSides(self):
        """
        Tests that dice notation with zero sides is parsed.
        """
        dice = self.parser.parse("1d0")

        self.assertEqual(1, dice.quantity)
        self.assertEqual(0, dice.sides)

    def test_zerosDice(self):
        """
        Tests that dice notation with zero dice and zero sides is parsed.
        """
        dice = self.parser.parse("0d0")

        self.assertEqual(0, dice.quantity)
        self.assertEqual(0, dice.sides)

    def test_max(self):
        """
        Tests that dice notation with the maximum integer values dice is parsed.
        """
        dice = self.parser.parse(str(sys.maxsize) + "d" + str(sys.maxsize))

        self.assertEqual(sys.maxsize, dice.quantity)
        self.assertEqual(sys.maxsize, dice.sides)