Enhanced PythonEvaluator to accept concepts
This commit is contained in:
@@ -91,6 +91,10 @@ def test_body_is_evaluated_when_concept_body():
|
||||
|
||||
|
||||
def test_body_is_evaluated_when_concept_body_with_a_body():
|
||||
"""
|
||||
The concept refers to another concept which has a body
|
||||
:return:
|
||||
"""
|
||||
context = get_context()
|
||||
concept_one = Concept(name="one", body="1").init_key()
|
||||
context.sheerka.add_in_cache(concept_one)
|
||||
|
||||
@@ -100,7 +100,7 @@ def get_concept_part(part):
|
||||
if isinstance(part, str):
|
||||
node = PythonNode(part, ast.parse(part, mode="eval"))
|
||||
return ReturnValueConcept(
|
||||
who="Parsers:PythonParser",
|
||||
who="Parsers:DefaultParser",
|
||||
status=True,
|
||||
value=ParserResultConcept(
|
||||
source=part,
|
||||
@@ -109,7 +109,7 @@ def get_concept_part(part):
|
||||
|
||||
if isinstance(part, PythonNode):
|
||||
return ReturnValueConcept(
|
||||
who="Parsers:PythonParser",
|
||||
who="Parsers:DefaultParser",
|
||||
status=True,
|
||||
value=ParserResultConcept(
|
||||
source=part.source,
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
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
|
||||
@@ -0,0 +1,154 @@
|
||||
import os
|
||||
import shutil
|
||||
from os import path
|
||||
|
||||
import pytest
|
||||
import ast
|
||||
|
||||
from core.ast.nodes import NodeParent, GenericNodeConcept
|
||||
import core.ast.nodes
|
||||
from core.ast.visitors import ConceptNodeVisitor, UnreferencedNamesVisitor
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.sheerka import Sheerka
|
||||
|
||||
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_sheerka():
|
||||
sheerka = Sheerka()
|
||||
sheerka.initialize(root_folder)
|
||||
|
||||
return sheerka
|
||||
|
||||
|
||||
class TestNameVisitor(ConceptNodeVisitor):
|
||||
"""
|
||||
Test class for a basic Visitor test
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.names = []
|
||||
|
||||
def visit_Name(self, node):
|
||||
self.names.append(node)
|
||||
|
||||
|
||||
def test_i_can_transform_simple_ast_using_generic_node():
|
||||
source = """
|
||||
def my_function(a,b):
|
||||
for i in range(b):
|
||||
a = a+b
|
||||
return a
|
||||
"""
|
||||
tree = ast.parse(source)
|
||||
tree_as_concept = core.ast.nodes.transform(tree)
|
||||
sheerka = get_sheerka()
|
||||
|
||||
assert tree_as_concept.node_type == "Module"
|
||||
assert sheerka.isinstance(tree_as_concept.get_prop("body"), BuiltinConcepts.LIST)
|
||||
|
||||
def_func = tree_as_concept.get_prop("body")[0]
|
||||
assert sheerka.isinstance(def_func, BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func.node_type == "FunctionDef"
|
||||
assert def_func.parent == NodeParent(tree_as_concept, "body")
|
||||
assert def_func.get_prop("name") == "my_function"
|
||||
|
||||
def_func_args = def_func.get_prop("args")
|
||||
assert sheerka.isinstance(def_func_args, BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func_args.node_type == "arguments"
|
||||
|
||||
def_func_args_real_args = def_func_args.get_prop("args")
|
||||
assert sheerka.isinstance(def_func_args_real_args, BuiltinConcepts.LIST)
|
||||
assert len(def_func_args_real_args) == 2
|
||||
|
||||
assert sheerka.isinstance(def_func_args_real_args[0], BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func_args_real_args[0].node_type == "arg"
|
||||
assert def_func_args_real_args[0].parent == NodeParent(def_func_args, "args")
|
||||
assert def_func_args_real_args[0].get_prop("arg") == "a"
|
||||
assert sheerka.isinstance(def_func_args_real_args[1], BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func_args_real_args[1].node_type == "arg"
|
||||
assert def_func_args_real_args[1].parent == NodeParent(def_func_args, "args")
|
||||
assert def_func_args_real_args[1].get_prop("arg") == "b"
|
||||
|
||||
def_fun_body = def_func.get_prop("body")
|
||||
assert sheerka.isinstance(def_fun_body, BuiltinConcepts.LIST)
|
||||
assert len(def_fun_body) == 2
|
||||
|
||||
def_fun_body_for = def_fun_body[0]
|
||||
assert sheerka.isinstance(def_fun_body_for, BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_fun_body_for.node_type == "For"
|
||||
assert def_fun_body_for.parent == NodeParent(def_func, "body")
|
||||
|
||||
def_fun_body_return = def_fun_body[1]
|
||||
assert sheerka.isinstance(def_fun_body_return, BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_fun_body_return.node_type == "Return"
|
||||
assert def_fun_body_return.parent == NodeParent(def_func, "body")
|
||||
|
||||
|
||||
def test_i_can_visit_concept_node():
|
||||
source = """
|
||||
def my_function(a,b):
|
||||
for i in range(b):
|
||||
a = a+b
|
||||
return a
|
||||
"""
|
||||
|
||||
node = ast.parse(source)
|
||||
concept_node = core.ast.nodes.transform(node)
|
||||
|
||||
visitor = TestNameVisitor()
|
||||
visitor.visit(concept_node)
|
||||
|
||||
sheerka = get_sheerka()
|
||||
assert sheerka.value(visitor.names[0]) == "i"
|
||||
assert sheerka.value(visitor.names[1]) == "range"
|
||||
assert sheerka.value(visitor.names[2]) == "b"
|
||||
assert sheerka.value(visitor.names[3]) == "a"
|
||||
assert sheerka.value(visitor.names[4]) == "a"
|
||||
assert sheerka.value(visitor.names[5]) == "b"
|
||||
assert sheerka.value(visitor.names[6]) == "a"
|
||||
|
||||
|
||||
def test_i_can_get_non_referenced_variables():
|
||||
source = """
|
||||
def my_function(a,b):
|
||||
for i in range(b):
|
||||
a = a+b
|
||||
return a
|
||||
|
||||
my_function(x,y)
|
||||
"""
|
||||
|
||||
sheerka = get_sheerka()
|
||||
|
||||
node = ast.parse(source)
|
||||
concept_node = core.ast.nodes.transform(node)
|
||||
|
||||
visitor = UnreferencedNamesVisitor(sheerka)
|
||||
visitor.visit(concept_node)
|
||||
values = visitor.names
|
||||
|
||||
assert len(visitor.names) == 2
|
||||
assert "x" in values
|
||||
assert "y" in values
|
||||
|
||||
|
||||
def test_i_can_compare_NodeParent_with_tuple():
|
||||
node_parent = NodeParent(GenericNodeConcept("For", None), "target")
|
||||
assert node_parent == ("For", "target")
|
||||
@@ -0,0 +1,145 @@
|
||||
import shutil
|
||||
from os import path
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
|
||||
from core.sheerka import Sheerka, ExecutionContext
|
||||
import core.builtin_helpers
|
||||
|
||||
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_i_can_use_expect_one_when_empty():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), [])
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.IS_EMPTY)
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_too_many_success():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", True, "value1"),
|
||||
ReturnValueConcept("who", True, "value2"),
|
||||
]
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_SUCCESS)
|
||||
assert res.value.obj == items
|
||||
assert res.parents == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_same_success():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", True, "value"),
|
||||
ReturnValueConcept("who", True, "value"),
|
||||
]
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
||||
assert res.status
|
||||
assert res.value == items[0].value
|
||||
assert res.parents == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_when_only_errors_1():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", False, None),
|
||||
]
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS)
|
||||
assert res.value.obj == items
|
||||
assert res.parents == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_when_only_errors_2():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", False, None),
|
||||
ReturnValueConcept("who", False, None),
|
||||
]
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS)
|
||||
assert res.value.obj == items
|
||||
assert res.parents == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_one_success_1():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", True, None),
|
||||
]
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
||||
assert res.status
|
||||
assert res.body == items[0].body
|
||||
assert res.parents == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_one_success_2():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", False, None),
|
||||
ReturnValueConcept("who", True, None),
|
||||
ReturnValueConcept("who", False, None),
|
||||
]
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
||||
assert res.status
|
||||
assert res.body == items[1].body
|
||||
assert res.parents == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_not_a_list_true():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
item = ReturnValueConcept("who", True, None)
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), item)
|
||||
assert res.status
|
||||
assert res == item
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_not_a_list_false():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
item = ReturnValueConcept("who", False, None)
|
||||
res = core.builtin_helpers.expect_one(get_context(sheerka), item)
|
||||
|
||||
assert not res.status
|
||||
assert res == item
|
||||
|
||||
|
||||
def get_sheerka():
|
||||
sheerka = Sheerka()
|
||||
sheerka.initialize(root_folder)
|
||||
|
||||
return sheerka
|
||||
|
||||
|
||||
def get_context(sheerka):
|
||||
return ExecutionContext("test", "xxx", sheerka)
|
||||
+24
-79
@@ -33,6 +33,11 @@ def init_test():
|
||||
os.chdir(current_pwd)
|
||||
|
||||
|
||||
class ConceptWithGetValue(Concept):
|
||||
def get_value(self):
|
||||
return self.get_prop("my_prop")
|
||||
|
||||
|
||||
def test_root_folder_is_created_after_initialization():
|
||||
return_value = Sheerka().initialize(root_folder)
|
||||
assert return_value.status, "initialisation should be successful"
|
||||
@@ -288,93 +293,33 @@ def test_i_cannot_instantiate_when_properties_are_not_recognized():
|
||||
assert sheerka.isinstance(new.concept, concept)
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_empty():
|
||||
@pytest.mark.parametrize("concept, allow_non_body, expected", [
|
||||
(None, False, None),
|
||||
(3.14, False, 3.14),
|
||||
(Concept("name", body="foo"), False, "foo"),
|
||||
(Concept("name"), True, Concept("name")),
|
||||
(ConceptWithGetValue("name").set_prop("my_prop", "my_value"), True, "my_value"),
|
||||
])
|
||||
def test_i_can_get_value(concept, allow_non_body, expected):
|
||||
sheerka = get_sheerka()
|
||||
|
||||
res = sheerka.expect_one(get_context(sheerka), [])
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.IS_EMPTY)
|
||||
assert sheerka.value(concept, allow_non_body) == expected
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_too_many_success():
|
||||
def test_i_cannot_get_value_when_no_body_and_allow_none_body_is_false():
|
||||
sheerka = get_sheerka()
|
||||
concept = Concept("name")
|
||||
allow_none_body = False
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", True, None),
|
||||
ReturnValueConcept("who", True, None),
|
||||
]
|
||||
res = sheerka.expect_one(get_context(sheerka), items)
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_SUCCESS)
|
||||
assert res.value.obj == items
|
||||
assert sheerka.value(concept, allow_none_body) == sheerka.new(BuiltinConcepts.CANNOT_RESOLVE_VALUE_ERROR,
|
||||
body=concept)
|
||||
|
||||
|
||||
def test_i_can_use_expect_when_only_errors_1():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", False, None),
|
||||
]
|
||||
res = sheerka.expect_one(get_context(sheerka), items)
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS)
|
||||
assert res.value.obj == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_when_only_errors_2():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", False, None),
|
||||
ReturnValueConcept("who", False, None),
|
||||
]
|
||||
res = sheerka.expect_one(get_context(sheerka), items)
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS)
|
||||
assert res.value.obj == items
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_one_success_1():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", True, None),
|
||||
]
|
||||
res = sheerka.expect_one(get_context(sheerka), items)
|
||||
assert res.status
|
||||
assert res == items[0]
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_one_success_2():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
items = [
|
||||
ReturnValueConcept("who", False, None),
|
||||
ReturnValueConcept("who", True, None),
|
||||
ReturnValueConcept("who", False, None),
|
||||
]
|
||||
res = sheerka.expect_one(get_context(sheerka), items)
|
||||
assert res.status
|
||||
assert res == items[1]
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_not_a_list_true():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
res = sheerka.expect_one(get_context(sheerka), ReturnValueConcept("who", True, None))
|
||||
assert res.status
|
||||
assert res == ReturnValueConcept("who", True, None)
|
||||
|
||||
|
||||
def test_i_can_use_expect_one_when_not_a_list_false():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
res = sheerka.expect_one(get_context(sheerka), ReturnValueConcept("who", False, None))
|
||||
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS)
|
||||
assert res.value.obj == [ReturnValueConcept("who", False, None)]
|
||||
|
||||
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
#
|
||||
# E V A L U A T I O N S
|
||||
#
|
||||
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
@pytest.mark.parametrize("text, expected", [
|
||||
("1 + 1", 2),
|
||||
|
||||
Reference in New Issue
Block a user