52 lines
2.5 KiB
Python
52 lines
2.5 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.FormatRuleEvaluator import FormatRuleEvaluator
|
|
from parsers.FormatRuleParser import FormatRuleNode, FormatRuleParser
|
|
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
class TestFormatRuleEvaluator(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 = FormatRuleNode(tokens)
|
|
return ReturnValueConcept("parsers.FormatRule", True, ParserResultConcept(value=node))
|
|
|
|
@pytest.mark.parametrize("ret_val, expected", [
|
|
(ReturnValueConcept("name", True, ParserResultConcept(value=FormatRuleNode({}))), True),
|
|
(ReturnValueConcept("name", True, ParserResultConcept(value="other object")), False),
|
|
(ReturnValueConcept("name", False, ParserResultConcept(value=FormatRuleNode({}))), False),
|
|
(ReturnValueConcept("name", False, FormatRuleNode({})), False),
|
|
])
|
|
def test_i_can_match(self, ret_val, expected):
|
|
context = self.get_context()
|
|
assert FormatRuleEvaluator().matches(context, ret_val) == expected
|
|
|
|
def test_i_can_eval(self):
|
|
sheerka, context = self.init_concepts()
|
|
text = "when isinstance(value, __EXPLANATION) print list(value)"
|
|
ret_val = FormatRuleParser().parse(context, ParserInput(text))
|
|
|
|
res = FormatRuleEvaluator().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("print",
|
|
None,
|
|
"isinstance(value, __EXPLANATION)",
|
|
"list(value)",
|
|
id=res.body.body.metadata.id, # no need to compare the id
|
|
is_compiled=True,
|
|
is_enabled=True)
|
|
assert res.body.body.compiled_predicate is not None
|
|
assert res.body.body.compiled_action is not None
|