File: constants_test.py

package info (click to toggle)
python-tatsu 5.13.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 892 kB
  • sloc: python: 10,202; makefile: 54
file content (53 lines) | stat: -rw-r--r-- 1,266 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import annotations

from tatsu.tool import parse


def test_constant_interpolation():
    input = '42 69'
    grammar = r"""
            start = a:number b: number i:`"seen: {a}, {b}"` $ ;
            number = /\d+/ ;
    """
    assert parse(grammar, input) == {'a': '42', 'b': '69', 'i': 'seen: 42, 69'}


def test_constant_interpolation_free():
    input = '42 69'
    grammar = r"""
            start = a:number b: number i:`seen: {a}, {b}` $ ;
            number = /\d+/ ;
    """
    assert parse(grammar, input) == {'a': '42', 'b': '69', 'i': 'seen: 42, 69'}


def test_constant_interpolation_multiline():
    input = '42 69'
    grammar = r"""
            start = a:number b: number
            i:```
            seen:
            {a}
            {b}
            ``` $ ;
            number = /\d+/ ;
    """

    result = parse(grammar, input)
    print(result)
    assert result == {'a': '42', 'b': '69', 'i': 'seen:\n42\n69\n'}


def test_evaluate_constant():
    grammar = """
        @@grammar :: constants
        start = int bool str null 'a' $;

        int = `42` ;
        bool = `True` ;
        str = `Something` ;
        null = `None` ;
    """

    ast = parse(grammar, 'a')
    assert ast == (42, True, 'Something', None, 'a')