61 lines
3.0 KiB
Python
61 lines
3.0 KiB
Python
import pytest
|
|
|
|
|
|
from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts
|
|
from core.rule import Rule, RuleMetadata
|
|
from core.sheerka.services.SheerkaExecute import ParserInput
|
|
from core.tokenizer import Tokenizer
|
|
from evaluators.DefRuleEvaluator import DefRuleEvaluator
|
|
from parsers.DefRuleParser import DefFormatRuleNode, DefRuleParser, DefExecRuleNode
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
class TestDefRuleEvaluator(TestUsingMemoryBasedSheerka):
|
|
|
|
@staticmethod
|
|
def get_ret_val_from_rule(rule_def):
|
|
tokens = {k: list(Tokenizer(k.value + " " + v, yield_eof=False)) for k, v in rule_def.items()}
|
|
for v in tokens.values():
|
|
del v[1]
|
|
|
|
node = DefFormatRuleNode(tokens)
|
|
return ReturnValueConcept("parsers.FormatRule", True, ParserResultConcept(value=node))
|
|
|
|
@pytest.mark.parametrize("ret_val, expected", [
|
|
(ReturnValueConcept("name", True, ParserResultConcept(value=DefFormatRuleNode({}))), True),
|
|
(ReturnValueConcept("name", True, ParserResultConcept(value=DefExecRuleNode({}))), True),
|
|
(ReturnValueConcept("name", True, ParserResultConcept(value="other object")), False),
|
|
(ReturnValueConcept("name", False, ParserResultConcept(value=DefFormatRuleNode({}))), False),
|
|
(ReturnValueConcept("name", False, DefFormatRuleNode({})), False),
|
|
])
|
|
def test_i_can_match(self, ret_val, expected):
|
|
context = self.get_context()
|
|
assert DefRuleEvaluator().matches(context, ret_val) == expected
|
|
|
|
@pytest.mark.parametrize("text, expected_action_type, expected_name", [
|
|
("when True print 'hello world'", "print", None),
|
|
("when True then 'hello world'", "exec", None),
|
|
("def rule rule name as when True print 'hello world'", "print", "rule name"),
|
|
("def rule rule name as when True then 'hello world'", "exec", "rule name"),
|
|
])
|
|
def test_i_can_eval(self, text, expected_action_type, expected_name):
|
|
sheerka, context = self.init_concepts()
|
|
ret_val = DefRuleParser().parse(context, ParserInput(text))
|
|
|
|
res = DefRuleEvaluator().eval(context, ret_val)
|
|
|
|
assert res.status
|
|
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_RULE)
|
|
assert isinstance(res.body.body, Rule)
|
|
assert res.body.body.metadata == RuleMetadata(expected_action_type,
|
|
expected_name,
|
|
"True",
|
|
"'hello world'",
|
|
id=res.body.body.metadata.id, # no need to compare the id
|
|
is_compiled=True,
|
|
is_enabled=True)
|
|
rule = res.body.body
|
|
assert rule.compiled_predicates is not None
|
|
assert rule.compiled_action is not None
|
|
assert rule.rete_disjunctions is not None
|