<commit_before>import unittest

import sure

from compiler import parse, lex, error, ast, type


class TestParser(unittest.TestCase):
    parsers = {}

    @classmethod
    def setUpClass(cls):
        cls.one = cls._parse("1", "expr")
        cls.two = cls._parse("2", "expr")
        cls.true = cls._parse("true", "expr")

        cls.xfunc = cls._parse("let x = 1", "letdef")
        cls.yfunc = cls._parse("let y = 2", "letdef")

    @classmethod
    def _parse(self, data, start='program'):
        mock = error.LoggerMock()

        lexer = lex.Lexer(logger=mock, optimize=1)

        # memoization
        try:
            parser = TestParser.parsers[start]
        except:
            parser = TestParser.parsers[start] = parse.Parser(
                logger=mock,
                optimize=0,
                start=start,
                debug=0
            )

        tree = parser.parse(
            data=data,
            lexer=lexer
        )

        return tree

    def test_empty_program(self):
        self._parse("").should.be.equal(ast.Program([]))

    def test_letdef(self):
        letdef = ast.LetDef(<commit_msg>Parser test: def_list test<commit_after>import unittest

import sure

from compiler import parse, lex, error, ast, type


class TestParser(unittest.TestCase):
    parsers = {}

    @classmethod
    def setUpClass(cls):
        cls.one = cls._parse("1", "expr")
        cls.two = cls._parse("2", "expr")
        cls.true = cls._parse("true", "expr")

        cls.xfunc = cls._parse("let x = 1", "letdef")
        cls.yfunc = cls._parse("let y = 2", "letdef")

    @classmethod
    def _parse(self, data, start='program'):
        mock = error.LoggerMock()

        lexer = lex.Lexer(logger=mock, optimize=1)

        # memoization
        try:
            parser = TestParser.parsers[start]
        except:
            parser = TestParser.parsers[start] = parse.Parser(
                logger=mock,
                optimize=0,
                start=start,
                debug=0
            )

        tree = parser.parse(
            data=data,
            lexer=lexer
        )

        return tree

    def test_empty_program(self):
        self._parse("").should.be.equal(ast.Program([]))


    def test_def_list(self):
        self._parse("", "def_list").should.be.equal([])
        self._parse("let x = 1", "def_list").should.be.equal([TestParser.xfunc])
        self._parse("let x = 1 let y = 2", "def_list").should.be.equal([TestParser.xfunc, TestParser.yfunc])

    def test_letdef(self):
        letdef = ast.LetDef(
{'fraction_non_alphanumeric': 0.13348946135831383, 'fraction_numerical': 0.00663544106167057, 'mean_word_length': 1.9664351851851851, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '471936', 'n_tokens_mistral': 828, 'n_tokens_neox': 785, 'n_words': 162}