Fixed #9 : I can parse 'def concept'

This commit is contained in:
2023-06-11 09:45:44 +02:00
parent 62391f786e
commit ba397b0b72
22 changed files with 3043 additions and 93 deletions
@@ -0,0 +1,94 @@
import pytest
from base import BaseTest
from common.global_symbols import NotInit
from conftest import NewOntology
from core.BuiltinConcepts import BuiltinConcepts
from evaluators.DefConceptEvaluator import DefConceptEvaluator
from evaluators.RecognizeDefConcept import RecognizeDefConcept
from helpers import _rv, _rvf, get_concepts
from parsers.ConceptDefinitionParser import ConceptDefinition
from parsers.ParserInput import ParserInput
from parsers.parser_utils import UnexpectedEof
def get_ret_val_from(context, command):
pi = ParserInput(command)
pi.init()
parser_start = _rv(context.sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=pi))
ret = RecognizeDefConcept().eval(context, None, parser_start)
return ret.new[0]
class TestDefConceptEvaluator(BaseTest):
@pytest.fixture()
def evaluator(self, sheerka):
return sheerka.evaluators[DefConceptEvaluator.NAME]
def test_i_can_match(self, sheerka, context, evaluator):
ret_val = _rv(ConceptDefinition(name="foo"))
assert evaluator.matches(context, ret_val).status is True
ret_val = _rv("Not a ConceptDefinition class")
assert evaluator.matches(context, ret_val).status is False
ret_val = _rvf(ConceptDefinition(name="foo")) # status is false
assert evaluator.matches(context, ret_val).status is False
def test_i_can_add_a_new_concept(self, context, evaluator):
ret_val_input = get_ret_val_from(context, "def concept foo")
res = evaluator.eval(context, None, ret_val_input)
assert len(res.new) == 1
assert res.new[0].status
assert context.sheerka.isinstance(res.new[0].value, BuiltinConcepts.NEW_CONCEPT)
assert res.eaten == [ret_val_input]
def test_i_cannot_add_when_definition_validation_fails(self, context, evaluator):
ret_val_input = get_ret_val_from(context, "def concept foo from bnf a |") # only one '|' is required
res = evaluator.eval(context, None, ret_val_input)
assert len(res.new) == 1
assert not res.new[0].status
assert isinstance(res.new[0].value.value, UnexpectedEof)
assert res.eaten == []
@pytest.mark.parametrize("concept_def, expected", [
(ConceptDefinition(name="inc a", ret="a"), [("a", NotInit)]),
(ConceptDefinition(name="inc a", where="isinstance(a, int)"), [("a", NotInit)]),
(ConceptDefinition(name="inc a", def_var=[("a", 10)]), [("a", 10)]),
(ConceptDefinition(name="inc a", def_var=["a"]), [("a", NotInit)]),
(ConceptDefinition(name="a + b", where="a is an int", ret="b"), [("a", NotInit), ("b", NotInit)]),
(ConceptDefinition(name="b + a", where="a is an int", ret="b"), [("b", NotInit), ("a", NotInit)]),
])
def test_i_can_get_variables(self, context, evaluator, concept_def, expected):
assert evaluator._get_variables(context, concept_def) == expected
def test_concept_name_is_not_considered_as_variable(self, context, evaluator):
with NewOntology(context, "test_concept_name_is_not_considered_as_variable"):
get_concepts(context, "one", use_sheerka=True)
concept_def = ConceptDefinition(name="add one + a", where="one is an int")
assert evaluator._get_variables(context, concept_def) == []
@pytest.mark.parametrize("concept_def, expected", [
("def concept add a b where a and b", [("a", NotInit), ("b", NotInit)]),
("def concept add a b where a ret b", [("a", NotInit), ("b", NotInit)]),
("def concept add a b where xxx a and yyy b", [("a", NotInit), ("b", NotInit)]),
("def concept add b a where xxx a and yyy b", [("b", NotInit), ("a", NotInit)]),
("def concept add a b def_var a,b", [("a", NotInit), ("b", NotInit)]),
("def concept add a b def_var a b", [("a", NotInit), ("b", NotInit)]),
("def concept add a b def_var a def_var b", [("a", NotInit), ("b", NotInit)]),
("def concept add a b def_var a=10 def_var b", [("a", 10), ("b", NotInit)]),
("def concept add a b def_var a='hello' def_var b", [("a", "'hello'"), ("b", NotInit)]),
])
def test_i_can_add_a_new_concept_with_variables(self, context, evaluator, concept_def, expected):
with NewOntology(context, "test_i_can_add_a_new_concept_with_variables"):
ret_val_input = get_ret_val_from(context, concept_def)
res = evaluator.eval(context, None, ret_val_input)
assert len(res.new) == 1
assert res.new[0].status
new_concept = res.new[0].value
assert context.sheerka.isinstance(new_concept, BuiltinConcepts.NEW_CONCEPT)
assert new_concept.body.variables == expected
+5 -12
View File
@@ -1,6 +1,6 @@
import pytest
from base import BaseTest
from base import BaseParserTest
from core.BuiltinConcepts import BuiltinConcepts
from core.error import ErrorContext
from evaluators.PythonParser import PythonParser
@@ -8,7 +8,7 @@ from helpers import _rv, _rvf
from parsers.ParserInput import ParserInput
class TestPythonParser(BaseTest):
class TestPythonParser(BaseParserTest):
@pytest.fixture()
def evaluator(self, sheerka):
return sheerka.evaluators[PythonParser.NAME]
@@ -28,9 +28,7 @@ class TestPythonParser(BaseTest):
"a = 20"
])
def test_i_can_parse_python(self, sheerka, context, evaluator, text):
pi = ParserInput(text)
pi.init()
start = _rv(sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=pi))
start = self.get_parser_input(context, text)
res = evaluator.eval(context, None, start)
@@ -43,9 +41,7 @@ class TestPythonParser(BaseTest):
def test_invalid_python_are_rejected(self, sheerka, context, evaluator):
text = "1 + "
pi = ParserInput(text)
pi.init()
start = _rv(sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=pi))
start = self.get_parser_input(context, text)
res = evaluator.eval(context, None, start)
@@ -57,9 +53,7 @@ class TestPythonParser(BaseTest):
assert ret_val.parents == [start]
def test_i_can_detect_concepts(self, sheerka, context, evaluator):
pi = ParserInput("c:one: + c:two:")
pi.init()
start = _rv(sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=pi))
start = self.get_parser_input(context, "c:one: + c:two:")
res = evaluator.eval(context, None, start)
@@ -72,4 +66,3 @@ class TestPythonParser(BaseTest):
assert len(ret_val.value.pf.namespace) == 2
assert ret_val.value.pf.namespace["__C__KEY_one__ID_00None00__C__"].value == ("one", None)
assert ret_val.value.pf.namespace["__C__KEY_two__ID_00None00__C__"].value == ("two", None)
@@ -0,0 +1,51 @@
import pytest
from base import BaseParserTest
from core.BuiltinConcepts import BuiltinConcepts
from core.error import ErrorContext
from evaluators.RecognizeDefConcept import RecognizeDefConcept
from helpers import _rv, _rvf
from parsers.ConceptDefinitionParser import ConceptDefinition
from parsers.ParserInput import ParserInput
class TestRecognizeDefConcept(BaseParserTest):
@pytest.fixture()
def evaluator(self, sheerka):
return sheerka.evaluators[RecognizeDefConcept.NAME]
def test_i_can_match(self, sheerka, context, evaluator):
ret_val = _rv(sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=ParserInput("def concept")))
assert evaluator.matches(context, ret_val).status is True
ret_val = _rv(sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=ParserInput("def")))
assert evaluator.matches(context, ret_val).status is False
ret_val = _rv(sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=ParserInput("other text")))
assert evaluator.matches(context, ret_val).status is False
ret_val = _rv(sheerka.newn(BuiltinConcepts.UNKNOWN_CONCEPT)) # it responds to USER_INPUT only
assert evaluator.matches(context, ret_val).status is False
ret_val = _rvf(sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=ParserInput("def concept"))) # status is false
assert evaluator.matches(context, ret_val).status is False
def test_i_can_recognize_a_def_concept(self, context, evaluator):
ret_val_input = self.get_parser_input(context, "def concept one as 1")
res = evaluator.eval(context, None, ret_val_input)
assert len(res.new) == 1
assert res.new[0].status
assert isinstance(res.new[0].value, ConceptDefinition)
assert res.eaten == [ret_val_input]
def test_i_can_manage_when_def_concept_fails(self, context, evaluator):
ret_val_input = self.get_parser_input(context, "def concept")
res = evaluator.eval(context, None, ret_val_input)
assert len(res.new) == 1
assert not res.new[0].status
assert isinstance(res.new[0].value, ErrorContext)
assert res.eaten == []