05577012f3
Fixed #35 : Refactor test helper class (CNC, CC, CIO) Fixed #36 : Concept values are not used when declared with variable expression Fixed #37 : Objects in memory lose their values are restart Fixed #38 : func(a=b, c) (which is not allowed) raise an exception
215 lines
9.0 KiB
Python
215 lines
9.0 KiB
Python
from core.concept import Concept, ConceptParts
|
|
from core.global_symbols import NotInit
|
|
from core.rule import Rule, ACTION_TYPE_EXEC
|
|
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 get_test_obj, CNC, CC, CN, SCN, SCWC, UTN, RN, CB
|
|
|
|
|
|
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()", 0, 2)
|
|
|
|
# I can discard start and end
|
|
scn_res = get_test_obj(scn, SCN(""))
|
|
assert isinstance(scn_res, SCN)
|
|
assert scn_res == SCN("test()", None, None)
|
|
|
|
def test_i_can_get_test_obj_when_SCWC(self):
|
|
sheerka, context = self.init_test().unpack()
|
|
|
|
parser = FunctionParser()
|
|
scwc = parser.parse(context, ParserInput("test(param1, test2())")).body.body
|
|
|
|
scwc_res = get_test_obj(scwc, SCWC(UTN(""), UTN(""), UTN(""), UTN(""), SCN("", 0, 0)))
|
|
assert isinstance(scwc_res, SCWC)
|
|
expected = SCWC(UTN("test(", 0, 1),
|
|
UTN(")", 8, 8),
|
|
UTN("param1", 2, 2),
|
|
UTN(", ", 3, 4),
|
|
SCN("test2()", 5, 7))
|
|
expected.start = 0
|
|
expected.end = 8
|
|
assert scwc_res == expected
|
|
|
|
assert isinstance(scwc_res.first, UTN)
|
|
assert isinstance(scwc_res.last, UTN)
|
|
assert isinstance(scwc_res.content[0], UTN)
|
|
assert isinstance(scwc_res.content[1], UTN)
|
|
assert isinstance(scwc_res.content[2], SCN)
|
|
|
|
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)
|