87cab44fb8
Fixed #135: Change services service priorities Fixed #136: ErrorManager: Implement recognize_error Fixed #137: BNFNodeParser : Error when parsing regex with sub parsers Fixed #138: get_last_errors(): real errors sources are lost Fixed #139: OneError return value removes the origin of the error Fixed #140: Concept variables are not correctly handled when parsing sub expression Fixed #143: Implement has_unknown_concepts()
192 lines
8.1 KiB
Python
192 lines
8.1 KiB
Python
from core.concept import Concept, ConceptParts
|
|
from core.global_symbols import NotInit
|
|
from core.rule import ACTION_TYPE_EXEC, Rule
|
|
from core.sheerka.services.SheerkaExecute import ParserInput
|
|
from parsers.BaseNodeParser import RuleNode
|
|
from parsers.BnfNodeParser import BnfNodeParser
|
|
from parsers.FunctionParser import FunctionParser
|
|
from parsers.SyaNodeParser import SyaNodeParser
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
from tests.parsers.parsers_utils import CB, CC, CN, CNC, RN, SCN, get_test_obj
|
|
|
|
|
|
class TestParsersUtils(TestUsingMemoryBasedSheerka):
|
|
|
|
def test_i_can_get_test_obj_when_CNC_from_sya(self):
|
|
sheerka, context, one, two, plus = self.init_concepts(
|
|
"one",
|
|
"two",
|
|
Concept("a plus b").def_var("a").def_var("b")
|
|
)
|
|
|
|
parser = SyaNodeParser().init_from_concepts(context, [one, two, plus])
|
|
cnode = parser.parse(context, ParserInput("one plus two")).body.body[0]
|
|
|
|
# compare all attributes
|
|
cnc_res = get_test_obj(cnode, CNC(concept_key="key", start=0, end=1, source="", exclude_body=False))
|
|
assert isinstance(cnc_res, CNC)
|
|
assert cnc_res == CNC("__var__0 plus __var__1", "one plus two", 0, 4, False, **cnode.concept.get_compiled())
|
|
|
|
# I can discard start, end and source
|
|
cnc_res = get_test_obj(cnode, CNC(concept_key="key"))
|
|
assert isinstance(cnc_res, CNC)
|
|
assert cnc_res == CNC("__var__0 plus __var__1", None, None, None, False, **cnode.concept.get_compiled())
|
|
|
|
def test_i_can_get_test_obj_when_CNC_from_bnf(self):
|
|
sheerka, context, one, two, plus = self.init_concepts(
|
|
"one",
|
|
"two",
|
|
Concept("twenties", definition="'twenty' (one | two)=unit").def_var("unit").def_var("one").def_var("two")
|
|
)
|
|
|
|
parser = BnfNodeParser().init_from_concepts(context, [one, two, plus])
|
|
cnode = parser.parse(context, ParserInput("twenty one")).body.body[0]
|
|
|
|
# compare all attributes
|
|
cnc_res = get_test_obj(cnode, CNC(concept_key="key", start=0, end=1, source="", exclude_body=False))
|
|
assert isinstance(cnc_res, CNC)
|
|
assert cnc_res == CNC("twenties", "twenty one", 0, 2, False, **cnode.concept.get_compiled())
|
|
|
|
# I can exclude body
|
|
cnc_res = get_test_obj(cnode, CNC(concept_key="key", exclude_body=True))
|
|
expected_compiled = {k: v for k, v in cnode.concept.get_compiled().items()}
|
|
del expected_compiled[ConceptParts.BODY]
|
|
assert isinstance(cnc_res, CNC)
|
|
assert cnc_res == CNC("twenties", None, None, None, False, **expected_compiled)
|
|
|
|
def test_i_can_get_test_obj_when_list(self):
|
|
sheerka, context, one, two, plus = self.init_concepts(
|
|
"one",
|
|
"two",
|
|
Concept("a plus b").def_var("a").def_var("b")
|
|
)
|
|
|
|
parser = SyaNodeParser().init_from_concepts(context, [one, two, plus])
|
|
cnode = parser.parse(context, ParserInput("one plus two")).body.body[0]
|
|
|
|
res = get_test_obj([cnode, cnode], [CNC("key1"), CNC("key", 0, 1, "")])
|
|
|
|
assert len(res) == 2
|
|
assert isinstance(res[0], CNC)
|
|
assert res[0] == CNC("__var__0 plus __var__1", None, None, None, False, **cnode.concept.get_compiled())
|
|
assert isinstance(res[1], CNC)
|
|
assert res[1] == CNC("__var__0 plus __var__1", "one plus two", 0, 4, False, **cnode.concept.get_compiled())
|
|
|
|
def test_i_can_get_test_obj_when_dict(self):
|
|
sheerka, context, one, two, plus = self.init_concepts(
|
|
"one",
|
|
"two",
|
|
Concept("a plus b").def_var("a").def_var("b")
|
|
)
|
|
|
|
parser = SyaNodeParser().init_from_concepts(context, [one, two, plus])
|
|
cnode = parser.parse(context, ParserInput("one plus two")).body.body[0]
|
|
|
|
res = get_test_obj({"key1": cnode, "key2": cnode}, {"key1": CNC("key1"), "key2": CNC("key", 0, 1, "")})
|
|
assert len(res) == 2
|
|
assert isinstance(res["key1"], CNC)
|
|
assert res["key1"] == CNC("__var__0 plus __var__1", None, None, None, False, **cnode.concept.get_compiled())
|
|
assert isinstance(res["key2"], CNC)
|
|
assert res["key2"] == CNC("__var__0 plus __var__1", "one plus two", 0, 4, False, **cnode.concept.get_compiled())
|
|
|
|
def test_i_can_get_test_obj_when_CC(self):
|
|
sheerka, context, one, two, plus = self.init_concepts(
|
|
"one",
|
|
"two",
|
|
Concept("twenties", definition="'twenty' (one | two)=unit").def_var("unit").def_var("one").def_var("two")
|
|
)
|
|
|
|
parser = BnfNodeParser().init_from_concepts(context, [one, two, plus])
|
|
cc = parser.parse(context, ParserInput("twenty one")).body.body[0].concept
|
|
|
|
# compare all attributes
|
|
cc_res = get_test_obj(cc, CC(concept="key", source="", exclude_body=False))
|
|
assert isinstance(cc_res, CC)
|
|
assert cc_res == CC("twenties", "twenty one", False, **cc.get_compiled())
|
|
|
|
# I can exclude body
|
|
cnc_res = get_test_obj(cc, CC(concept="key", exclude_body=True))
|
|
expected_compiled = {k: v for k, v in cc.get_compiled().items()}
|
|
del expected_compiled[ConceptParts.BODY]
|
|
assert isinstance(cnc_res, CC)
|
|
assert cnc_res == CC("twenties", "twenty one", True, **expected_compiled)
|
|
|
|
def test_i_can_get_test_obj_when_CN(self):
|
|
sheerka, context, one, two, plus = self.init_concepts(
|
|
"one",
|
|
"two",
|
|
Concept("a plus b").def_var("a").def_var("b")
|
|
)
|
|
|
|
parser = SyaNodeParser().init_from_concepts(context, [one, two, plus])
|
|
cnode = parser.parse(context, ParserInput("one plus two")).body.body[0]
|
|
|
|
cn_res = get_test_obj(cnode, CN(concept="key", start=0, end=1, source=""))
|
|
assert isinstance(cn_res, CN)
|
|
assert cn_res == CN(plus, "one plus two", 0, 4)
|
|
|
|
# I can discard start, end and source
|
|
cnc_res = get_test_obj(cnode, CN(concept="key"))
|
|
assert isinstance(cnc_res, CN)
|
|
assert cnc_res == CN(plus, None, None, None)
|
|
|
|
def test_i_can_get_test_obj_when_SCN(self):
|
|
sheerka, context = self.init_test().unpack()
|
|
|
|
parser = FunctionParser()
|
|
scn = parser.parse(context, ParserInput("test()")).body.body
|
|
|
|
scn_res = get_test_obj(scn, SCN("", start=0, end=1))
|
|
assert isinstance(scn_res, SCN)
|
|
assert scn_res == SCN("test()", start=0, end=2)
|
|
|
|
# I can discard start and end
|
|
scn_res = get_test_obj(scn, SCN(""))
|
|
assert isinstance(scn_res, SCN)
|
|
assert scn_res == SCN("test()", start=None, end=None)
|
|
|
|
def test_i_can_get_test_obj_when_RN(self):
|
|
rule = Rule(ACTION_TYPE_EXEC, "test_rule", "True", "True")
|
|
rn = RuleNode(rule, 1, 1, source="r:|xxx:")
|
|
|
|
rn_res = get_test_obj(rn, RN("", "", 0, 1))
|
|
|
|
assert isinstance(rn_res, RN)
|
|
assert rn_res == RN(rule, rn.source, rn.start, rn.end)
|
|
|
|
# I can discard start and end
|
|
rn_res = get_test_obj(rn, RN("", None, None, None))
|
|
assert isinstance(rn_res, RN)
|
|
assert rn_res == RN(rule, None, None, None)
|
|
|
|
def test_i_can_get_test_obj_when_CB(self):
|
|
a = Concept("a", key="a", body="10").auto_init()
|
|
b = Concept("b", key="b", body=a).auto_init()
|
|
c = Concept("c", key="c")
|
|
other_c = Concept("c", key="c", body="i don't care")
|
|
|
|
# i can test when no body
|
|
res = get_test_obj(c, CB("", ""))
|
|
assert isinstance(res, CB)
|
|
assert res == CB("c", NotInit)
|
|
|
|
# i can test with a concept instead of a key
|
|
res = get_test_obj(c, CB(Concept(), ""))
|
|
assert isinstance(res, CB)
|
|
assert res == CB(other_c, NotInit) # don't care if it's not the real 'c', only test key and body
|
|
|
|
# # i can test when the body is a concept
|
|
res = get_test_obj(b, CB("", ""))
|
|
assert isinstance(res, CB)
|
|
assert res == CB("b", a)
|
|
|
|
# i can go into recursion when body is a concept
|
|
res = get_test_obj(b, CB("", CB("", "")))
|
|
assert isinstance(res, CB)
|
|
assert res == CB("b", CB("a", "10"))
|
|
|
|
# I can try to go into recursion when there nothing to found
|
|
res = get_test_obj(c, CB("", CB("", "")))
|
|
assert isinstance(res, CB)
|
|
assert res == CB("c", NotInit)
|