import pytest from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts from core.sheerka import Sheerka, ExecutionContext from core.concept import Concept from evaluators.PythonEvaluator import PythonEvaluator from parsers.PythonParser import PythonNode, PythonParser def get_context(): sheerka = Sheerka(skip_builtins_in_db=True) sheerka.initialize("mem://") return ExecutionContext("test", "xxx", sheerka) @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(ret_val, expected): context = 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(text, expected): context = get_context() parsed = PythonParser().parse(context, text) evaluated = PythonEvaluator().eval(context, parsed) assert evaluated.status assert evaluated.value == expected def test_i_can_eval_expression_that_references_concepts(): context = get_context() context.sheerka.add_in_cache(Concept("foo")) parsed = PythonParser().parse(context, "foo") evaluated = PythonEvaluator().eval(context, parsed) assert evaluated.status assert evaluated.value == Concept("foo").init_key() def test_i_can_eval_expression_that_references_concepts_with_body(): """ I can test expression with variables :return: """ context = get_context() context.sheerka.add_in_cache(Concept("foo", body="2")) parsed = PythonParser().parse(context, "foo") evaluated = PythonEvaluator().eval(context, parsed) assert evaluated.status assert evaluated.value == 2 def test_i_can_eval_module_with_that_references_concepts(): """ I can test modules with variables :return: """ context = get_context() context.sheerka.add_in_cache(Concept("foo")) parsed = PythonParser().parse(context, "def a(b):\n return b\na(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(): """ I can test modules with variables :return: """ context = 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_with_props(): context = get_context() context.sheerka.add_in_cache(Concept("foo").set_prop("prop", "'a'")) parsed = PythonParser().parse(context, "foo") evaluated = PythonEvaluator().eval(context, parsed) assert evaluated.status assert evaluated.value == Concept("foo").set_prop("prop", "a").init_key() # evaluated version of foo def test_i_cannot_eval_when_body_references_unknown_concept(): context = get_context() context.sheerka.add_in_cache(Concept("foo", body="bar")) parsed = PythonParser().parse(context, "foo") evaluated = PythonEvaluator().eval(context, parsed) assert not evaluated.status assert context.sheerka.isinstance(evaluated.value, BuiltinConcepts.ERROR)