File: test_utils.py

package info (click to toggle)
python-baron 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,080 kB
  • sloc: python: 26,926; makefile: 126; sh: 27
file content (38 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download
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
import sys

from baron.grammator import generate_parse
from baron.dumper import dumps
from baron.baron import parse as baron_parse
from baron.utils import python_version

if python_version == 3:
    from itertools import zip_longest
else:
    from itertools import izip_longest
    zip_longest = izip_longest

parse = generate_parse(False)


def parse_simple(tokens, result):
    if not tokens or tokens[-1][0] != "ENDL":
        tokens += [('ENDL', '\n')]
    assert parse(tokens + [('ENDMARKER', ''), None]) == (result + [{"type": "endl", "value": "\n", "formatting": [], "indent": ""}])


def parse_multi(tokens, result):
    assert parse(tokens + [('ENDMARKER', ''), None]) == result


def check_dumps(source_code):
    try:
        open("/tmp/c", "w").write(source_code)
        open("/tmp/d", "w").write(dumps(baron_parse(source_code)))
    except Exception as e:
        import json
        import traceback
        traceback.print_exc(file=sys.stdout)
        sys.stdout.write("Warning: couldn't write dumps output to debug file, exception: %s\n\n" % e)
        sys.stdout.write("Tree: %s" % json.dumps(baron_parse(source_code), indent=4) + "\n")

    assert dumps(baron_parse(source_code), strict=True) == source_code