Example #1
0
def test_unexpected_exception_in_grammar(monkeypatch: Any) -> None:
    with pytest.raises(ParseFatalException, match=r"implementation error \(division by zero\)"):
        monkeypatch.setattr(
            grammar,
            "parse_mathematical_expression",
            grammar.fatalexceptions(lambda x, y, z: [1, 1 / 0, 1]),
        )
        grammar.mathematical_expression().parseString("1 + 1")
Example #2
0
def test_mathematical_expression_precedence() -> None:
    assert_equal(
        grammar.mathematical_expression().parseString("A - B * 2**3 - 1")[0],
        Sub(Sub(Variable("A"), Mul(Variable("B"), Pow(Number(2), Number(3)))), Number(1)),
    )
    assert_equal(
        grammar.mathematical_expression().parseString("(A - B) * 2**3 - 1")[0],
        Sub(Mul(Sub(Variable("A"), Variable("B")), Pow(Number(2), Number(3))), Number(1)),
    )
    assert_equal(
        grammar.mathematical_expression().parseString("A - B * 2**(3 - 1)")[0],
        Sub(Variable("A"), Mul(Variable("B"), Pow(Number(2), Sub(Number(3), Number(1))))),
    )
    assert_equal(
        grammar.mathematical_expression().parseString("A - (B * 2)**3 - 1")[0],
        Sub(Sub(Variable("A"), Pow(Mul(Variable("B"), Number(2)), Number(3))), Number(1)),
    )
    assert_equal(
        grammar.mathematical_expression().parseString("A - (B * 2**3 - 1)")[0],
        Sub(Variable("A"), Sub(Mul(Variable("B"), Pow(Number(2), Number(3))), Number(1))),
    )
Example #3
0
def test_mathematical_expression_concatenation() -> None:
    assert_equal(
        grammar.mathematical_expression().parseString('(137) & "PNG" & (13, 10, 26, 10)')[0],
        Aggregate(
            Number(137),
            Number(80),
            Number(78),
            Number(71),
            Number(13),
            Number(10),
            Number(26),
            Number(10),
        ),
    )
Example #4
0
def test_mathematical_expression_string() -> None:
    assert_equal(
        grammar.mathematical_expression().parseString('"PNG"')[0],
        Aggregate(Number(80), Number(78), Number(71)),
    )
Example #5
0
def test_mathematical_expression_aggregate_no_number() -> None:
    with pytest.raises(ParseFatalException, match=r"^Expected Number"):
        grammar.mathematical_expression().parseString("(1, Foo)")
Example #6
0
def test_mathematical_expression_aggregate() -> None:
    assert_equal(
        grammar.mathematical_expression().parseString("(1, 2)")[0], Aggregate(Number(1), Number(2))
    )
    assert_equal(grammar.mathematical_expression().parseString("(1)")[0], Aggregate(Number(1)))