コード例 #1
0
def test_expression_grammar():
    grammar = expression()
    actual = grammar.parseString("1d6")
    expected = [Dice(rolls=1, sides=6)]

    assert actual is not None
    assert len(actual) == 1
    assert actual[0] == Dice(rolls=1, sides=6)
コード例 #2
0
ファイル: test_dice.py プロジェクト: extesla/dice-python
def test_dice_roll(mocker):
    mock_mt_rand = mocker.patch("dice.tokens.mt_rand")
    mock_mt_rand.return_value = 4

    token = Dice(rolls=1, sides=6)
    actual = token.roll()

    assert len(actual) == 1
    assert actual == [4]
    mock_mt_rand.assert_called_once_with(min=1, max=6)
コード例 #3
0
ファイル: test_dice.py プロジェクト: extesla/dice-python
def test_dice_roll(mocker):
    mock_mt_rand = mocker.patch("dice.tokens.mt_rand")
    mock_mt_rand.return_value = 4

    token = Dice(rolls=1, sides=6)
    actual = token.roll()

    assert len(actual) == 1
    assert actual == [4]
    mock_mt_rand.assert_called_once_with(min=1, max=6)
コード例 #4
0
ファイル: test_dice.py プロジェクト: extesla/dice-python
def test_dice_evaluate(mocker):
    mock_mt_rand = mocker.patch("dice.tokens.mt_rand")
    mock_mt_rand.return_value = 4

    token = Dice(rolls=1, sides=6)
    token.evaluate()

    assert len(token.results) == 1
    assert token.results == [4]
    assert token.total == 4
    mock_mt_rand.assert_called_once_with(min=1, max=6)
コード例 #5
0
ファイル: test_dice.py プロジェクト: extesla/dice-python
def test_dice_evaluate(mocker):
    mock_mt_rand = mocker.patch("dice.tokens.mt_rand")
    mock_mt_rand.return_value = 4

    token = Dice(rolls=1, sides=6)
    token.evaluate()

    assert len(token.results) == 1
    assert token.results == [4]
    assert token.total == 4
    mock_mt_rand.assert_called_once_with(min=1, max=6)
コード例 #6
0
ファイル: test_dice.py プロジェクト: zroot/dice-python
 def test_evaluate(self):
     #: Test that the string representation of the operator is what is
     #: expected.
     #:
     #: Given an instance of the Add operator on operands 5 and 1
     #: When the method __repr__ is called
     #: Then the result should be "Add(5, 1)"
     token = Dice(value="1d6", rolls=1, sides=6)
     with patch("dice.tokens.mt_rand") as mock_random:
         mock_random.return_value = 4
         token.evaluate()
         self.assertEqual(token.result, [4])
         mock_random.assert_called_once_with(min=1, max=6)
コード例 #7
0
ファイル: test_term.py プロジェクト: extesla/dice-python
def test_term_dice_only():
    parse_result = term().parseString("1d6")
    tokens = parse_result.asList()
    assert len(tokens) == 1

    actual = tokens.pop()
    expected = Dice(rolls=1, sides=6)
    assert actual == expected
コード例 #8
0
 def transformer(string, location, tokens):
     logger.debug(
         (
             "Transforming parsed text: `{}` into Dice token with "
             "the following parts: {}"
         ).format(string, str(tokens))
     )
     return Dice(rolls=tokens["dice_rolls"], sides=tokens["dice_sides"])
コード例 #9
0
ファイル: test_term.py プロジェクト: extesla/dice-python
def test_term_dice_with_flag_and_operator():
    parse_result = term().parseString("1d6!keep+5")
    tokens = parse_result.asList()
    assert len(tokens) == 1

    actual = tokens.pop()
    expected = [[Dice(rolls=1, sides=6), "!keep"], "+", 5]
    assert len(actual) == 3
    assert actual == expected
コード例 #10
0
ファイル: test_term.py プロジェクト: extesla/dice-python
def test_term_dice_with_flag():
    parse_result = term().parseString("1d6!keep")
    tokens = parse_result.asList()
    assert len(tokens) == 1

    actual = tokens.pop()
    expected = [Dice(rolls=1, sides=6), "!keep"]
    assert len(actual) == 2
    assert actual == expected
コード例 #11
0
def test_dice_grammar_with_uppercase():
    token = dice()
    actual = token.parseString("1D6")
    expected = Dice(rolls=1, sides=6)

    assert len(actual) == 1
    assert actual[0] == expected
    assert actual[0].rolls == expected.rolls
    assert actual[0].sides == expected.sides
コード例 #12
0
def test_dice_grammar_with_fate_dice():
    token = dice()
    actual = token.parseString("3dfate")
    expected = Dice(rolls=3, sides="fate")

    assert len(actual) == 1
    assert actual[0] == expected
    assert actual[0].rolls == expected.rolls
    assert actual[0].sides == expected.sides
コード例 #13
0
def test_dice_grammaer_with_implicit_rolls():
    token = dice()
    actual = token.parseString("d20")
    expected = Dice(rolls=1, sides=20)

    assert len(actual) == 1
    assert actual[0] == expected
    assert actual[0].rolls == expected.rolls
    assert actual[0].sides == expected.sides
コード例 #14
0
def test_dice_grammar_with_fate_dice_abbr():
    token = dice()
    actual = token.parseString("3dF")
    # ZR: Expected to be lowercase as defined in grammar.py
    expected = Dice(rolls=3, sides="f")

    assert len(actual) == 1
    assert actual[0] == expected
    assert actual[0].rolls == expected.rolls
    assert actual[0].sides == expected.sides
コード例 #15
0
ファイル: test_term.py プロジェクト: extesla/dice-python
def test_term_dice_only_uppercase():
    # NOTE:  this is the same as in test_term_dice_only as CaselessLiteral will
    # return the case of the defined matchString, not the search string passed
    # into parse.   -ZR
    parse_result = term().parseString("1D6")
    tokens = parse_result.asList()
    assert len(tokens) == 1

    actual = tokens.pop()
    expected = Dice(rolls=1, sides=6)
    assert actual == expected
コード例 #16
0
def test_evaluate_advantage_with_dice_token_value(mocker):
    mock_random = mocker.patch("dice.tokens.mt_rand")
    mock_random.side_effect = [5, 17]

    dice_token = Dice(sides=20, rolls=2)
    operator = Advantage(dice_token)
    actual = operator.evaluate()

    assert actual == 17
    assert operator.result == 17
    assert actual == operator.result
コード例 #17
0
ファイル: test_dice.py プロジェクト: extesla/dice-python
def test_dice_str():
    """
    Test that the string representation of the operator is what is
    expected.

    Given an instance of the Dice token with the value "1d6"
    When the method __str__ is called
    Then the results should be "1d6"
    """
    token = Dice(rolls=1, sides=6)
    assert str(token) == "1d6"
コード例 #18
0
def test_evaluate_keep_with_dice_token_values(mocker):
    mock_random = mocker.patch("dice.tokens.mt_rand")
    mock_random.side_effect = [2, 5, 1, 4, 3]

    dice_token = Dice(sides=6, rolls=5)
    operator = Keep(dice_token, 2)

    actual = operator.evaluate()
    assert actual == [4, 5]
    assert operator.result == [4, 5]
    assert actual == operator.result
コード例 #19
0
ファイル: test_dice.py プロジェクト: zroot/dice-python
 def test_roll(self):
     #: Test
     #:
     #: Given
     #: When
     #: Then
     with patch("dice.tokens.mt_rand") as mock_random:
         mock_random.return_value = 4
         actual = Dice.roll(1, 6)
         self.assertEqual(len(actual), 1)
         self.assertEqual(actual, [4])
         mock_random.assert_called_once_with(min=1, max=6)
コード例 #20
0
def test_evaluate_drop_with_dice_token_values(mocker):
    mock_random = mocker.patch("dice.tokens.mt_rand")
    mock_random.side_effect = [2, 5, 1, 4, 3]

    dice_token = Dice(sides=6, rolls=5)
    operator = Drop(dice_token, 2)

    actual = operator.evaluate()
    assert actual == [3, 4, 5]
    assert operator.result == [3, 4, 5]
    assert actual == operator.result
    mock_random.assert_has_calls([
        mocker.call(min=1, max=6),
        mocker.call(min=1, max=6),
        mocker.call(min=1, max=6),
        mocker.call(min=1, max=6),
        mocker.call(min=1, max=6),
    ])
コード例 #21
0
ファイル: test_dice.py プロジェクト: extesla/dice-python
def test_initialize_dice():
    token = Dice(rolls=1, sides=6)
    assert token.rolls == 1
    assert token.sides == 6
    assert token.results is None
    assert token.total == 0