Implemented some enhancement requests

This commit is contained in:
2020-12-14 10:30:10 +01:00
parent 657c7536f7
commit e3c2adb533
46 changed files with 352 additions and 1286 deletions
+1 -2
View File
@@ -851,7 +851,7 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
res = sheerka.inspect(context, 0)
assert res.body == {'#type#': 'NotFound',
'id': '70',
'id': sheerka.concepts_ids[BuiltinConcepts.NOT_FOUND],
'key': '__NOT_FOUND',
'name': '__NOT_FOUND',
'body': 'no digest'}
@@ -873,7 +873,6 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
'#type#': 'ReturnValueConcept',
'id': '43',
'key': '__RETURN_VALUE',
'message': None,
'name': '__RETURN_VALUE',
'parents': [concept_debug_obj],
'status': True,
-233
View File
@@ -1,233 +0,0 @@
from dataclasses import dataclass
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.sheerka.services.SheerkaFilter import Pipe, SheerkaFilter
from printer.FormatInstructions import FormatInstructions, FormatDetailDesc, FormatDetailType
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@dataclass
class Obj:
prop1: str
prop2: str
@dataclass
class ObjWithAsBag:
prop1: str
prop2: object
def as_bag(self):
return {
"first_prop": self.prop1,
"second_prop": self.prop2,
}
class TestSheerkaFilter(TestUsingMemoryBasedSheerka):
def test_i_can_pipe_using_decorator(self):
@Pipe
def is_ok_with_decorator(iterable):
for item in iterable:
yield item + " ok"
def exclamation(iterable):
for item in iterable:
yield item + "!"
res = ["one", "two", "three"] | is_ok_with_decorator | Pipe(exclamation)
assert list(res) == ["one ok!", "two ok!", "three ok!"]
def test_i_can_pipe_function_with_context_as_first_parameter(self):
def func_with_context(context, iterable, var_name):
for item in iterable:
yield f"{context.desc}: {var_name}={item}"
sheerka, context = self.init_concepts()
context.desc = "desc"
pipeable = Pipe(func_with_context, context)
assert pipeable.need_context
assert list(["one", "two", "three"] | pipeable("var")) == ['desc: var=one', 'desc: var=two', 'desc: var=three']
def test_i_can_pipe_function_with_context_as_only_parameter(self):
# This time, func_with_context does not have other parameter than context and iterable
def func_with_context(context, iterable):
for item in iterable:
yield f"{context.desc}: var={item}"
sheerka, context = self.init_concepts()
context.desc = "desc"
pipeable = Pipe(func_with_context, context)
assert pipeable.need_context
assert list(["one", "two", "three"] | pipeable) == ['desc: var=one', 'desc: var=two', 'desc: var=three']
def test_i_can_pipe_explanation_concept(self):
sheerka, context = self.init_concepts()
execution_contexts = [context.push(BuiltinConcepts.NOP, None, desc=f"desc_{i}") for i in range(4)]
explanation_node = sheerka.new(BuiltinConcepts.EXPLANATION, body=execution_contexts)
@Pipe
def get_desc(iterable):
for item in iterable:
yield item.desc
res = explanation_node | get_desc
assert sheerka.isinstance(res, BuiltinConcepts.EXPLANATION)
assert list(res.body) == ["desc_0", "desc_1", "desc_2", "desc_3"] # body is modified
@pytest.mark.parametrize("predicate, expected", [
("True", ["one", "two", "three"]),
("self == 'two'", ["two"])
])
def test_i_can_filter(self, predicate, expected):
filter_service = SheerkaFilter(None)
res = ["one", "two", "three"] | Pipe(filter_service.pipe_filter)(predicate)
assert list(res) == expected
def test_i_can_filter_obj(self):
filter_service = SheerkaFilter(None)
lst = [Obj("a", "b"), Obj("c", "d")]
predicate = "prop2 == 'd'"
res = lst | Pipe(filter_service.pipe_filter)(predicate)
assert list(res) == [Obj("c", "d")]
def test_i_can_filter_obj_implementing_as_bag(self):
filter_service = SheerkaFilter(None)
lst = [ObjWithAsBag("a", "b"), ObjWithAsBag("c", "d")]
predicate = "second_prop == 'd'"
res = lst | Pipe(filter_service.pipe_filter)(predicate)
assert list(res) == [ObjWithAsBag("c", "d")]
def test_i_can_manage_name_error(self):
filter_service = SheerkaFilter(None)
lst = [Obj("a", "b"), Obj("c", "d"), ObjWithAsBag("a", "b"), ObjWithAsBag("c", "d")]
predicate = "second_prop == 'd'" # 'second_prop' does not exist in Obj
res = lst | Pipe(filter_service.pipe_filter)(predicate)
assert list(res) == [ObjWithAsBag("c", "d")]
def test_i_cannot_filter_if_the_predicate_is_incorrect(self):
filter_service = SheerkaFilter(None)
lst = [Obj("a", "b"), Obj("c", "d")]
predicate = "prop2 =="
with pytest.raises(SyntaxError):
res = lst | Pipe(filter_service.pipe_filter)(predicate)
list(res)
def test_i_can_format_l(self):
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
lst = [foo, bar]
res = lst | Pipe(SheerkaFilter.pipe_format_l)("my_format")
res = list(res)
assert len(res) == 2
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.format_l[f"c:{foo.id}:"] == "my_format"
format_instructions = res[1].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.format_l[f"c:{bar.id}:"] == "my_format"
def test_i_can_format_d(self):
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
lst = [foo, bar]
res = lst | Pipe(SheerkaFilter.pipe_format_d)("id", "name", "body", id="%red%{id}%reset%")
res = list(res)
expected_props = {
"id": "%red%{id}%reset%",
"name": "{name}",
"body": "{body}"
}
assert len(res) == 2
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.format_d[f"c:{foo.id}:"] == FormatDetailDesc(FormatDetailType.Props_In_Line, expected_props)
format_instructions = res[1].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.format_d[f"c:{bar.id}:"] == FormatDetailDesc(FormatDetailType.Props_In_Line, expected_props)
def test_i_can_format_d_all_properties(self):
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
lst = [foo, bar]
res = lst | Pipe(SheerkaFilter.pipe_format_d)()
res = list(res)
expected_props = {
'id': '{id}',
'name': '{name}',
'key': '{key}',
'body': '{body}',
'self': '{self}'
}
assert len(res) == 2
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.format_d[f"c:{foo.id}:"] == FormatDetailDesc(FormatDetailType.Props_In_Line, expected_props)
def test_i_can_set_recurse(self):
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
lst = [foo, bar]
res = lst | Pipe(SheerkaFilter.pipe_recurse)(10)
res = list(res)
assert len(res) == 2
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.recursive_props["_children"] == 10
format_instructions = res[1].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.recursive_props["_children"] == 10
res = lst | Pipe(SheerkaFilter.pipe_recurse)(15, "other_prop")
res = list(res)
assert len(res) == 2
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
assert isinstance(format_instructions, FormatInstructions)
assert format_instructions.recursive_props["_children"] == 10
assert format_instructions.recursive_props["other_prop"] == 15
def test_i_can_inspect_obj(self):
filter_service = SheerkaFilter(None)
lst = [Obj("a", "b"), Obj("c", "d")]
res = lst | Pipe(filter_service.pipe_inspect)("prop2")
assert list(res) == ["b", "d"]
def test_i_can_inspect_obj_with_bag(self):
filter_service = SheerkaFilter(None)
lst = [ObjWithAsBag("a", "b"), ObjWithAsBag("c", "d")]
res = lst | Pipe(filter_service.pipe_inspect)("second_prop")
assert list(res) == ["b", "d"]
lst = [ObjWithAsBag("a", ObjWithAsBag("b", ObjWithAsBag("c", "d")))]
res = lst | Pipe(filter_service.pipe_inspect)("second_prop.second_prop.second_prop")
assert list(res) == ["d"]
+1 -2
View File
@@ -86,14 +86,13 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
def test_i_can_instantiate_a_builtin_concept_when_it_has_its_own_class(self):
sheerka = self.get_sheerka()
ret = sheerka.new(BuiltinConcepts.RETURN_VALUE, who="who", status="status", value="value", message="message")
ret = sheerka.new(BuiltinConcepts.RETURN_VALUE, who="who", status="status", value="value")
assert isinstance(ret, ReturnValueConcept)
assert ret.key == str(BuiltinConcepts.RETURN_VALUE)
assert ret.who == "who"
assert ret.status == "status"
assert ret.value == "value"
assert ret.message == "message"
# check the others
for key, concept_class in sheerka.get_builtins_classes_as_dict().items():
-11
View File
@@ -3,7 +3,6 @@ from dataclasses import dataclass
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, ConceptParts
from core.sheerka.services.SheerkaFilter import Pipe, SheerkaFilter
from printer.Formatter import Formatter, BraceToken
from printer.SheerkaPrinter import FormatInstructions
@@ -446,16 +445,6 @@ bar: *name 'bar' is not defined*
captured = capsys.readouterr()
assert captured.out == expected
def test_i_can_manage_exception_when_printing(self, capsys):
sheerka = self.get_sheerka()
filter_service = SheerkaFilter(sheerka)
predicate = "self='two'" # it should be self=='two'
items = ["one", "two", "three"] | Pipe(filter_service.pipe_filter)(predicate)
sheerka.print(items)
captured = capsys.readouterr()
assert captured.out == "\x1b[31mSyntaxError: invalid syntax\nself='two'\n ^\x1b[0m\n"
@pytest.mark.parametrize("template, expected", [
(None, []),
("", []),