108 lines
5.3 KiB
Python
108 lines
5.3 KiB
Python
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
|
|
assert new_concept.body.parameters == set(item[0] for item in expected)
|
|
|
|
def test_i_can_define_variables_that_are_not_parameters(self, context, evaluator):
|
|
with NewOntology(context, "test_i_can_define_variables_that_are_not_parameters"):
|
|
ret_val_input = get_ret_val_from(context, "def concept color def_var color_name")
|
|
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 == [("color_name", NotInit)]
|
|
assert new_concept.body.parameters == set()
|