summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus/tests/test_parse_pgen.py
blob: 5052f9069c98fe833e7feda067be23a1dfd756f1 (plain)
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
import unittest

import jsparagus.gen
from jsparagus import parse_pgen, parse_pgen_generated


class ParsePgenTestCase(unittest.TestCase):
    def test_self(self):
        import os
        filename = os.path.join(os.path.dirname(parse_pgen.__file__), "..",
                                "pgen.pgen")
        grammar = parse_pgen.load_grammar(filename)
        self.maxDiff = None
        pgen_grammar = parse_pgen.pgen_grammar
        self.assertEqual(pgen_grammar.nonterminals, grammar.nonterminals)
        self.assertEqual(pgen_grammar.variable_terminals,
                         grammar.variable_terminals)
        self.assertEqual(pgen_grammar.goals(), grammar.goals())

        with open(parse_pgen_generated.__file__) as f:
            pre_generated = f.read()

        import io
        out = io.StringIO()
        jsparagus.gen.generate_parser(out, grammar)
        generated_from_file = out.getvalue()

        self.maxDiff = None
        self.assertEqual(pre_generated, generated_from_file)


if __name__ == '__main__':
    unittest.main()