import ast import pytest import os from os import path import shutil from core.concept import Concept, ConceptParts, ReturnValueConcept from core.sheerka import Sheerka, ExecutionContext from parsers.DefaultParser import DefConceptNode, DefaultParser from parsers.PythonParser import PythonParser from sdp.sheerkaDataProvider import SheerkaDataProvider 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 test_root_folder_is_created_after_initialization(): return_value = Sheerka().initialize(root_folder) assert return_value.status, "initialisation should be successful" assert os.path.exists(root_folder), "init folder should be created" def test_lists_of_concepts_is_initialized(): sheerka = Sheerka() sheerka.initialize(root_folder) assert len(sheerka.concepts_cache) > 1 def get_concept(): text = """ def concept a+b where isinstance(a, int) and isinstance(b, int) pre isinstance(a, int) and isinstance(b, int) post isinstance(res, int) as: def func(x,y): return x+y func(a,b) """ parser = DefaultParser(text, PythonParser) return parser.parse() def test_i_can_add_a_concept(): concept = get_concept() sheerka = Sheerka() sheerka.initialize(root_folder) res = sheerka.add_concept(ExecutionContext(sheerka, "xxx"), concept) concept_found = res.value assert res.status assert concept_found == Concept( name="a + b", where="isinstance(a, int) and isinstance(b, int)", pre="isinstance(a, int) and isinstance(b, int)", post="isinstance(res, int)", body="def func(x,y):\n return x+y\nfunc(a,b)") assert isinstance(concept_found.codes[ConceptParts.WHERE], ast.Expression) assert isinstance(concept_found.codes[ConceptParts.PRE], ast.Expression) assert isinstance(concept_found.codes[ConceptParts.POST], ast.Expression) assert isinstance(concept_found.codes[ConceptParts.BODY], ast.Module) all_props = [p.name for p in concept_found.props] assert all_props == ["a", "b"] assert concept_found.key == "__var__0 + __var__1" assert concept_found.id == "1001" assert path.exists(sheerka.sdp.get_obj_path(SheerkaDataProvider.ObjectsFolder, "4f249487410db35d8bcbcf4521acb3dd8354978804cd99bbc4de17a323b2f237")) @pytest.mark.parametrize("text, expected", [ ("1 + 1", 2), ("sheerka.test()", 'I have access to Sheerka !') ]) def test_i_can_eval_simple_python_expressions(text, expected): sheerka = Sheerka(debug=True) sheerka.initialize(root_folder) res = sheerka.eval(text) assert len(res) == 1 assert res[0].status assert res[0].value.body == expected assert sheerka.isinstance(res[0].value, ReturnValueConcept()) def test_i_cannot_add_the_same_concept_twice(): """ Checks that duplicated concepts are managed by sheerka, not by sheerka.sdp :return: """ pass def test_i_can_get_a_concept(): """ Checks that a concept can be found its name even when there are variables in the name (ex 'hello + a' or 'a + b' ) :return: """ pass def test_i_can_instantiate_a_concept(): """ Test the new() functionnality make sure that some Concept are singleton (ex Sheerka, True, False) but some other need a new instance everytime :return: """ pass