import pytest from core.builtin_concepts import BuiltinConcepts from core.sheerka.services.SheerkaExecute import ParserInput from parsers.RuleParser import RuleParser, RuleNotFoundError from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka my_rules = {("__rets", "list(__rets)")} class TestRuleParser(TestUsingMemoryBasedSheerka): sheerka = None @classmethod def setup_class(cls): t = cls() cls.sheerka, context, _ = t.init_parser(my_rules) def init_parser(self, rules=None): if rules is not None: sheerka, context, *concepts = self.init_format_rules(*rules) else: sheerka = TestRuleParser.sheerka context = self.get_context(sheerka) parser = RuleParser() return sheerka, context, parser def test_i_cannot_parse_is_empty(self): sheerka, context, parser = self.init_parser() res = parser.parse(context, ParserInput("")) assert not res.status assert sheerka.isinstance(res.body, BuiltinConcepts.IS_EMPTY) def test_i_cannot_parse_when_not_for_me(self): sheerka, context, parser = self.init_parser() res = parser.parse(context, ParserInput("hello world!")) assert not res.status assert sheerka.isinstance(res.body, BuiltinConcepts.NOT_FOR_ME) def test_i_cannot_parse_a_rule_by_name(self): sheerka, context, parser = self.init_parser() res = parser.parse(context, ParserInput("r:1:")) assert not res.status assert sheerka.isinstance(res.body, BuiltinConcepts.NOT_IMPLEMENTED) def test_i_cannot_parse_an_unknown_rule(self): sheerka, context, parser = self.init_parser() res = parser.parse(context, ParserInput("r:|999999:")) error = res.body errors_causes = res.body.body assert not res.status assert sheerka.isinstance(error, BuiltinConcepts.ERROR) assert errors_causes == [RuleNotFoundError("999999")] def test_i_can_parse_rule(self): sheerka, context, parser = self.init_parser() res = parser.parse(context, ParserInput("r:|1:")) parser_result = res.body rules = res.body.body assert res.status assert sheerka.isinstance(parser_result, BuiltinConcepts.PARSER_RESULT) assert len(rules) == 1 assert rules[0].id == "1" def test_rule_recognition_is_deferred_when_id_is_a_string(self): sheerka, context, parser = self.init_parser() res = parser.parse(context, ParserInput("r:|xxx:")) parser_result = res.body rules = res.body.body assert res.status assert sheerka.isinstance(parser_result, BuiltinConcepts.PARSER_RESULT) assert len(rules) == 1 assert rules[0].id == "xxx" assert rules[0].metadata.action_type == "deferred" @pytest.mark.parametrize("text", [ "r:|1:xxx", "xxxr:|1:", "r:|1: + 1" ]) def test_i_can_only_parse_simple_rule(self, text): sheerka, context, parser = self.init_parser() res = parser.parse(context, ParserInput(text)) not_for_me = res.body assert not res.status assert sheerka.isinstance(not_for_me, BuiltinConcepts.NOT_FOR_ME)