import pytest import shutil from os import path import os from core.builtin_concepts import ReturnValueConcept, ParserResultConcept from core.sheerka import Sheerka, ExecutionContext from core.concept import Concept from evaluators.PythonEvaluator import PythonEvaluator from parsers.PythonParser import PythonNode, PythonParser tests_root = path.abspath("../build/tests") root_folder = "init_folder" @pytest.fixture(autouse=True) def init_test(): if path.exists(tests_root): shutil.rmtree(tests_root) if not path.exists(tests_root): os.makedirs(tests_root) current_pwd = os.getcwd() os.chdir(tests_root) yield None os.chdir(current_pwd) def get_context(): sheerka = Sheerka() sheerka.initialize(root_folder) 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_with_variables(): """ I can test expression with variables :return: """ context = get_context() context.sheerka.add_in_cache(Concept("foo", body="2")) parsed = PythonParser().parse(context, "foo + 2") evaluated = PythonEvaluator().eval(context, parsed) assert evaluated.status assert evaluated.value == 4 def test_i_can_eval_module_with_variables(): """ 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