139 lines
5.1 KiB
Python
139 lines
5.1 KiB
Python
import pytest
|
|
|
|
from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts
|
|
from core.concept import Concept
|
|
from evaluators.PythonEvaluator import PythonEvaluator
|
|
from parsers.PythonParser import PythonNode, PythonParser
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
def get_context_name(context):
|
|
return context.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),
|
|
("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, text)
|
|
|
|
evaluated = PythonEvaluator().eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value == expected
|
|
|
|
@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, "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, "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, "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, "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, "get_context_name(c:foo:)")
|
|
python_evaluator = PythonEvaluator()
|
|
python_evaluator.locals["get_context_name"] = get_context_name
|
|
evaluated = python_evaluator.eval(context, parsed)
|
|
|
|
assert evaluated.status
|
|
assert evaluated.value == "foo"
|
|
|
|
# sanity, does not work otherwise
|
|
parsed = PythonParser().parse(context, "get_context_name(foo)")
|
|
python_evaluator = PythonEvaluator()
|
|
python_evaluator.locals["get_context_name"] = get_context_name
|
|
evaluated = python_evaluator.eval(context, parsed)
|
|
|
|
assert not evaluated.status
|
|
assert evaluated.body.body.args[0] == "'int' object has no attribute 'name'"
|
|
|
|
# @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
|