174 lines
6.6 KiB
Python
174 lines
6.6 KiB
Python
import pytest
|
|
|
|
from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts
|
|
from core.concept import Concept, DEFINITION_TYPE_DEF
|
|
from core.sheerka.services.SheerkaExecute import ParserInput
|
|
from evaluators.PythonEvaluator import PythonEvaluator
|
|
from parsers.PythonParser import PythonNode, PythonParser
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
def get_concept_name(concept):
|
|
return concept.name
|
|
|
|
|
|
class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
|
|
|
@pytest.mark.parametrize("ret_val, expected", [
|
|
(ReturnValueConcept("some_name", True, ParserResultConcept(value=PythonNode("", None))), True),
|
|
(ReturnValueConcept("some_name", True, ParserResultConcept(value="other thing")), False),
|
|
(ReturnValueConcept("some_name", False, "not relevant"), False),
|
|
(ReturnValueConcept("some_name", True, Concept()), False)
|
|
])
|
|
def test_i_can_match(self, ret_val, expected):
|
|
context = self.get_context()
|
|
assert PythonEvaluator().matches(context, ret_val) == expected
|
|
|
|
@pytest.mark.parametrize("text, expected", [
|
|
("1 + 1", 2),
|
|
("test()", "I have access to Sheerka !"),
|
|
("sheerka.test()", "I have access to Sheerka !"),
|
|
("a=10\na", 10),
|
|
])
|
|
def test_i_can_eval(self, text, expected):
|
|
context = self.get_context()
|
|
parsed = PythonParser().parse(context, ParserInput(text))
|
|
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value == expected
|
|
|
|
def test_i_can_eval_using_context(self):
|
|
context = self.get_context()
|
|
parsed = PythonParser().parse(context, ParserInput("test_using_context('value for param1', 10)"))
|
|
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value.startswith("I have access to Sheerka ! param1='value for param1', param2=10, event=")
|
|
|
|
def test_i_can_eval_using_context_when_self_is_not_sheerka(self):
|
|
sheerka, context = self.init_concepts()
|
|
parsed = PythonParser().parse(context, ParserInput("create_new_concept(Concept('foo'))"))
|
|
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert sheerka.has_key("foo")
|
|
|
|
@pytest.mark.parametrize("concept", [
|
|
Concept("foo"),
|
|
Concept("foo", body="2"),
|
|
Concept("foo").def_var("prop", "'a'"),
|
|
Concept("foo", body="bar")
|
|
])
|
|
def test_i_cannot_eval_simple_concept(self, concept):
|
|
context = self.get_context()
|
|
context.sheerka.add_in_cache(Concept("foo"))
|
|
|
|
parsed = PythonParser().parse(context, ParserInput("foo"))
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert not evaluated.status
|
|
assert context.sheerka.isinstance(evaluated.value, BuiltinConcepts.NOT_FOR_ME)
|
|
|
|
def test_i_can_eval_expression_with_that_references_concepts(self):
|
|
"""
|
|
I can test modules with variables
|
|
:return:
|
|
"""
|
|
context = self.get_context()
|
|
context.sheerka.add_in_cache(Concept("foo", body="1"))
|
|
|
|
parsed = PythonParser().parse(context, ParserInput("foo + 2"))
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value == 3
|
|
|
|
def test_i_can_eval_module_with_that_references_concepts(self):
|
|
"""
|
|
I can test modules with variables
|
|
:return:
|
|
"""
|
|
context = self.get_context()
|
|
context.sheerka.add_in_cache(Concept("foo"))
|
|
|
|
parsed = PythonParser().parse(context, ParserInput("def a(b):\n return b\na(c:foo:)"))
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value == Concept("foo").init_key()
|
|
|
|
def test_i_can_eval_module_with_that_references_concepts_with_body(self):
|
|
"""
|
|
I can test modules with variables
|
|
:return:
|
|
"""
|
|
context = self.get_context()
|
|
context.sheerka.add_in_cache(Concept("foo", body="2"))
|
|
|
|
parsed = PythonParser().parse(context, ParserInput("def a(b):\n return b\na(foo)"))
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value == 2
|
|
|
|
def test_i_can_eval_concept_token(self):
|
|
context = self.get_context()
|
|
context.sheerka.add_in_cache(Concept("foo", body="2"))
|
|
|
|
parsed = PythonParser().parse(context, ParserInput("get_concept_name(c:foo:)"))
|
|
python_evaluator = PythonEvaluator()
|
|
python_evaluator.locals["get_concept_name"] = get_concept_name
|
|
evaluated = python_evaluator.eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value == "foo"
|
|
|
|
# sanity, does not work otherwise
|
|
parsed = PythonParser().parse(context, ParserInput("get_concept_name(foo)"))
|
|
python_evaluator = PythonEvaluator()
|
|
python_evaluator.locals["get_concept_name"] = get_concept_name
|
|
evaluated = python_evaluator.eval(context, parsed)
|
|
|
|
assert not evaluated.status
|
|
assert evaluated.body.body.args[0] == "'int' object has no attribute 'name'"
|
|
|
|
def test_i_can_call_function_with_complex_concepts(self):
|
|
sheerka, context, plus, mult = self.init_concepts(
|
|
self.from_def_concept("plus", "a plus b", ["a", "b"]),
|
|
self.from_def_concept("mult", "a mult b", ["a", "b"]),
|
|
)
|
|
|
|
parsed = PythonParser().parse(context, ParserInput("is_greater_than(BuiltinConcepts.PRECEDENCE, mult, plus)"))
|
|
python_evaluator = PythonEvaluator()
|
|
|
|
evaluated = python_evaluator.eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert sheerka.get_concepts_weights(BuiltinConcepts.PRECEDENCE) == {'1001': 1, '1002': 2}
|
|
|
|
|
|
# @pytest.mark.parametrize("text, concept_key, concept_id, use_concept", [
|
|
# ("__C__key__C__", "key", None, False),
|
|
# ("__C__key__id__C__", "key", "id", False),
|
|
# ("__C__USE_CONCEPT__key__id__C__", "key", "id", True),
|
|
# ("__C__USE_CONCEPT__key__id__C__", "key", "id", True),
|
|
# ])
|
|
# def test_i_can_resolve_name(self, text, concept_key, concept_id, use_concept):
|
|
# context = self.get_context()
|
|
# assert PythonEvaluator().resolve_name(context, text) == (concept_key, concept_id, use_concept)
|
|
#
|
|
# @pytest.mark.parametrize("text", [
|
|
# "__C__",
|
|
# "__C__key",
|
|
# "__C__key____",
|
|
# "__C____",
|
|
# "__C__USE_CONCEPT__",
|
|
# ])
|
|
# def test_i_cannot_resolve_name(self, text):
|
|
# context = self.get_context()
|
|
# assert PythonEvaluator().resolve_name(context, text) is None
|