First but not optimized version of AstFormatDict

This commit is contained in:
2020-11-24 13:43:04 +01:00
parent ab30ab3345
commit cac732bd93
21 changed files with 898 additions and 217 deletions
+127 -57
View File
@@ -1,10 +1,12 @@
from dataclasses import dataclass
import pytest
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
from core.concept import Concept
from core.rule import Rule
from core.sheerka.services.SheerkaOut import SheerkaOut
from core.sheerka.services.SheerkaRuleManager import FormatAstRawText, FormatAstVariable, FormatAstSequence, \
FormatAstColor, FormatAstVariableNotFound, FormatAstList
FormatAstColor, FormatAstVariableNotFound, FormatAstList, FormatAstDict
from core.utils import flatten_all_children
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -14,6 +16,12 @@ raw = FormatAstRawText
var = FormatAstVariable
@dataclass
class DummyObj:
prop_1: float
prop_2: str
class TestSheerkaOut(TestUsingMemoryBasedSheerka):
def init_service_with_rules(self, *rules, **kwargs):
@@ -24,20 +32,20 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("rule, expected", [
(("__ret", "hello world"), FormatAstRawText("hello world")),
(("__ret", "{__ret_value}"), FormatAstVariable("__ret_value", None, 1)),
(("__ret", "{status}"), FormatAstVariable("status", None, True)),
(("__ret", "{__ret_value}"), FormatAstVariable("__ret_value", value=1)),
(("__ret", "{status}"), FormatAstVariable("status", value=True)),
(("__ret", "{foo}"), FormatAstVariableNotFound("foo")),
(("__ret", "hello world {__ret_value} !"), seq([FormatAstRawText("hello world "),
FormatAstVariable("__ret_value", None, 1),
FormatAstVariable("__ret_value", value=1),
FormatAstRawText(" !")])),
(("__ret", "red(__ret_value)"), FormatAstColor("red", FormatAstVariable("__ret_value", None, 1))),
(("__ret", "red(__ret_value)"), FormatAstColor("red", FormatAstVariable("__ret_value", value=1))),
(("__ret", "blue('hello world {__ret_value} !')"), FormatAstColor("blue",
seq([FormatAstRawText("hello world "),
FormatAstVariable("__ret_value", None,
1),
FormatAstVariable("__ret_value",
value=1),
FormatAstRawText(" !")]))),
(("__ret", "list(foo)"), FormatAstVariableNotFound("foo")),
(("__ret", "{__ret_value:3}"), FormatAstVariable("__ret_value", "3", " 1"))
(("__ret", "{__ret_value:3}"), FormatAstVariable("__ret_value", "3", value=" 1"))
])
def test_i_can_develop_when_simple_rules(self, rule, expected):
sheerka, context, service, rule = self.init_service_with_rules(rule)
@@ -59,14 +67,14 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
res = service.create_out_tree(context, ret)
assert res == seq([FormatAstRawText("status: "),
FormatAstVariable("status", None, True),
FormatAstVariable("status", value=True),
FormatAstRawText(", value: "),
FormatAstVariable("__ret_value", None, seq([FormatAstVariable("id", None, "1001"),
FormatAstVariable("__ret_value", value=seq([FormatAstVariable("id", value="1001"),
FormatAstRawText("-"),
FormatAstVariable("name", None, "foo"),
FormatAstVariable("name", value="foo"),
FormatAstRawText(":"),
FormatAstVariable("body", None,
"hello world")]))])
FormatAstVariable("body",
value="hello world")]))])
@pytest.mark.parametrize('second_rule', [
("__ret.body", "{id}-{name}:{body}"),
@@ -84,23 +92,32 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
res = service.create_out_tree(context, ret)
assert res == seq([FormatAstRawText("status: "),
FormatAstVariable("status", None, True),
FormatAstVariable("status", value=True),
FormatAstRawText(", value: "),
FormatAstVariable("__ret.body", None, seq([FormatAstVariable("id", None, "1001"),
FormatAstVariable("__ret.body", value=seq([FormatAstVariable("id", value="1001"),
FormatAstRawText("-"),
FormatAstVariable("name", None, "foo"),
FormatAstVariable("name", value="foo"),
FormatAstRawText(":"),
FormatAstVariable("body", None,
"hello world")]))])
FormatAstVariable("body",
value="hello world")]))])
def test_i_can_develop_using_variable_properties(self):
sheerka, context, service, *rules = self.init_service_with_rules(
("isinstance(__obj, Concept)", "{__obj.body}"),
)
obj = Concept("bar", body="value for bar").auto_init()
res = service.create_out_tree(context, obj)
assert res == FormatAstVariable("__obj.body", value="value for bar")
def test_i_can_develop_list(self):
sheerka, context, service, *rules = self.init_service_with_rules(("isinstance(__obj, list)", "list(__obj)"))
lst = list(flatten_all_children(context, lambda item: item.achildren))[:3]
res = service.create_out_tree(context, lst)
assert res == FormatAstList(variable="__obj", items=[FormatAstVariable("__item", None, lst[0], 0),
FormatAstVariable("__item", None, lst[1], 1),
FormatAstVariable("__item", None, lst[2], 2)])
assert res == FormatAstList(variable="__obj", items=[FormatAstVariable("__item", value=lst[0], index=0),
FormatAstVariable("__item", value=lst[1], index=1),
FormatAstVariable("__item", value=lst[2], index=2)])
def test_i_can_develop_list_using_the_default_items_prop(self):
sheerka, context, service, *rules = self.init_service_with_rules(("isinstance(__obj, 'foo')", "list(__obj)"))
@@ -108,9 +125,9 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
foo = Concept("foo", body=lst, key="foo").auto_init()
res = service.create_out_tree(context, foo)
assert res == FormatAstList(variable="__obj", items=[FormatAstVariable("__item", None, lst[0], 0),
FormatAstVariable("__item", None, lst[1], 1),
FormatAstVariable("__item", None, lst[2], 2)])
assert res == FormatAstList(variable="__obj", items=[FormatAstVariable("__item", value=lst[0], index=0),
FormatAstVariable("__item", value=lst[1], index=1),
FormatAstVariable("__item", value=lst[2], index=2)])
def test_i_can_develop_list_using_the_custom_items_prop(self):
sheerka, context, service, *rules = self.init_service_with_rules(
@@ -121,16 +138,16 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
res = service.create_out_tree(context, foo)
assert res == FormatAstList(variable="__obj", items_prop='custom',
items=[FormatAstVariable("__item", None, lst[0], 0),
FormatAstVariable("__item", None, lst[1], 1),
FormatAstVariable("__item", None, lst[2], 2)])
items=[FormatAstVariable("__item", value=lst[0], index=0),
FormatAstVariable("__item", value=lst[1], index=1),
FormatAstVariable("__item", value=lst[2], index=2)])
def test_not_a_list_are_resolved_into_variable_ast(self):
sheerka, context, service, *rules = self.init_service_with_rules(("isinstance(__obj, 'foo')", "list(__obj)"))
foo = Concept("foo", key="foo")
res = service.create_out_tree(context, foo)
assert res == FormatAstVariable("__obj", None, foo)
assert res == FormatAstVariable("__obj", value=foo)
def test_i_can_develop_list_of_return_values(self):
sheerka, context, service, *rules = self.init_service_with_rules(("__rets", "list(__rets)"))
@@ -140,9 +157,9 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
]
res = service.create_out_tree(context, lst)
assert res == FormatAstList(variable="__rets", items=[FormatAstVariable("__item", None, lst[0], 0),
FormatAstVariable("__item", None, lst[1], 1),
FormatAstVariable("__item", None, lst[2], 2)])
assert res == FormatAstList(variable="__rets", items=[FormatAstVariable("__item", value=lst[0], index=0),
FormatAstVariable("__item", value=lst[1], index=1),
FormatAstVariable("__item", value=lst[2], index=2)])
def test_rules_are_correctly_reset_when_list(self):
sheerka, context, service, *rules = self.init_service_with_rules(
@@ -156,8 +173,8 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
res = service.create_out_tree(context, lst)
assert res == FormatAstList(variable="__rets", items=[
FormatAstVariable("__item", None, FormatAstColor("red", FormatAstVariable("__ret", None, lst[0])), 0),
FormatAstVariable("__item", None, FormatAstColor("red", FormatAstVariable("__ret", None, lst[1])), 1),
FormatAstVariable("__item", value=FormatAstColor("red", FormatAstVariable("__ret", value=lst[0])), index=0),
FormatAstVariable("__item", value=FormatAstColor("red", FormatAstVariable("__ret", value=lst[1])), index=1),
])
def test_i_can_develop_list_with_recurse(self):
@@ -174,15 +191,15 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
res = service.create_out_tree(context, lst)
assert res == FormatAstList(variable="__rets", recurse_on='parents', recursion_depth=2, items=[
FormatAstVariable("__item", None, r1, 0),
FormatAstVariable("__item", value=r1, index=0),
FormatAstList(variable="__parents", recurse_on='parents', recursion_depth=1, items=[
FormatAstVariable("__item", None, r11, 0),
FormatAstVariable("__item", value=r11, index=0),
FormatAstList(variable="__parents", recurse_on='parents', recursion_depth=0, items=[
FormatAstVariable("__item", None, r111, 0)])
FormatAstVariable("__item", value=r111, index=0)])
]),
FormatAstVariable("__item", None, r2, 1),
FormatAstVariable("__item", value=r2, index=1),
FormatAstList(variable="__parents", recurse_on='parents', recursion_depth=1, items=[
FormatAstVariable("__item", None, r22, 0)]),
FormatAstVariable("__item", value=r22, index=0)]),
])
def test_i_can_develop_list_with_recurse_using_container_format_instr(self):
@@ -200,29 +217,53 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
res = service.create_out_tree(context, foo)
assert res == FormatAstList(variable="__obj", recurse_on='parents', recursion_depth=2, items=[
FormatAstVariable("__item", None, r1, 0),
FormatAstVariable("__item", value=r1, index=0),
FormatAstList(variable="__parents", recurse_on='parents', recursion_depth=1, items=[
FormatAstVariable("__item", None, r11, 0),
FormatAstVariable("__item", value=r11, index=0),
FormatAstList(variable="__parents", recurse_on='parents', recursion_depth=0, items=[
FormatAstVariable("__item", None, r111, 0)])
FormatAstVariable("__item", value=r111, index=0)])
]),
FormatAstVariable("__item", None, r2, 1),
FormatAstVariable("__item", value=r2, index=1),
FormatAstList(variable="__parents", recurse_on='parents', recursion_depth=1, items=[
FormatAstVariable("__item", None, r22, 0)]),
FormatAstVariable("__item", value=r22, index=0)]),
])
def test_i_can_develop_using_variable_properties(self):
sheerka, context, service, *rules = self.init_service_with_rules(
("isinstance(__obj, Concept)", "{__obj.body}"),
)
lst = Concept("bar", body="value for bar").auto_init()
def test_i_can_develop_dict(self):
sheerka, context, service, *rules = self.init_service_with_rules(("isinstance(__obj, dict)", "dict(__obj)"))
obj = {
"key1": "value1",
"key2": "value2",
}
res = service.create_out_tree(context, lst)
assert res == FormatAstVariable("__obj.body", None, "value for bar")
res = service.create_out_tree(context, obj)
assert res == FormatAstDict(variable="__obj", items=[
(FormatAstVariable("__key", value="key1", index=0),
FormatAstVariable("__value", value="value1", index="key1")),
(FormatAstVariable("__key", value="key2", index=1),
FormatAstVariable("__value", value="value2", index="key2")),
])
def test_i_can_develop_dict_with_list(self):
sheerka, context, service, *rules = self.init_service_with_rules(("isinstance(__obj, dict)", "dict(__obj)"))
obj = {
"key1": "value1",
"key2": ["item1", "item2"],
}
res = service.create_out_tree(context, obj)
assert res == FormatAstDict(variable="__obj", items=[
(FormatAstVariable("__key", value="key1", index=0),
FormatAstVariable("__value", value="value1", index="key1")),
(FormatAstVariable("__key", value="key2", index=1),
FormatAstList(variable='__value', debug=True, prefix='[', suffix=']', items=[
FormatAstVariable("__item", debug=True, value="item1", index=0),
FormatAstVariable("__item", debug=True, value="item2", index=1),
])),
])
@pytest.mark.parametrize("rule, expected", [
(("__ret", "red('hello world')"), FormatAstColor("red", FormatAstRawText("hello world"))),
(("__ret", "blue(__ret_value)"), FormatAstColor("blue", FormatAstVariable("__ret_value", None, 1))),
(("__ret", "blue(__ret_value)"), FormatAstColor("blue", FormatAstVariable("__ret_value", value=1))),
])
def test_i_can_develop_color(self, rule, expected):
sheerka, context, service, rule = self.init_service_with_rules(rule)
@@ -233,12 +274,10 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
assert res == expected
@pytest.mark.parametrize("rule, expected", [
(("__ret", "{__ret}"), FormatAstVariable("__ret", None,
ReturnValueConcept(who="Test", status=True, value=1))),
(("__ret", "{__ret}"), FormatAstVariable("__ret", value=ReturnValueConcept(who="Test", status=True, value=1))),
(("__ret", "magenta(__ret)"),
FormatAstColor("magenta",
FormatAstVariable("__ret", None,
ReturnValueConcept(who="Test", status=True, value=1)))),
FormatAstVariable("__ret", value=ReturnValueConcept(who="Test", status=True, value=1)))),
])
def test_i_can_manage_infinite_recursion(self, rule, expected):
sheerka, context, service, rule = self.init_service_with_rules(rule)
@@ -258,8 +297,8 @@ class TestSheerkaOut(TestUsingMemoryBasedSheerka):
res = service.create_out_tree(context, ret)
assert res == FormatAstColor("white",
FormatAstVariable("__ret", None,
FormatAstVariable("__ret.value", None, "hello world!")))
FormatAstVariable("__ret",
value=FormatAstVariable("__ret.value", value="hello world!")))
def test_i_can_print_out_the_result(self, capsys):
sheerka, context, service, *rules = self.init_service_with_rules(
@@ -379,3 +418,34 @@ ReturnValue(who=Test, status=True, value=r2, message=None)
service.process_return_values(context, ret)
captured = capsys.readouterr()
assert captured.out == "status: \x1b[32mTrue\x1b[0m, \x1b[34mvalue: (1001)foo\x1b[0m\n"
def test_i_can_print_out_dict(self, capsys):
sheerka, context, service, *rules = self.init_service_with_rules(
("isinstance(__obj, dict)", "dict(__obj)"),
("__key=='key1'", "green(__key)")
)
obj = {
"key1": "value1",
"key2": "value2",
}
service.process_return_values(context, obj)
captured = capsys.readouterr()
assert captured.out == """\x1b[32mkey1\x1b[0m: value1
key2: value2
"""
def test_i_can_print_out_dict_in_debug_mode(self, capsys):
sheerka, context, service, *rules = self.init_service_with_rules(
("isinstance(__obj, dict)", "dict(__obj, debug=True)"),
("__key=='key1'", "green(__key)")
)
obj = {
"key1": "value1",
"key2": 1,
"key3": DummyObj(3.15, "a string"),
"key4": ["alpha", 0]
}
service.process_return_values(context, obj)
captured = capsys.readouterr()
assert captured.out == """{'\x1b[32mkey1\x1b[0m': 'value1', 'key2': 1, 'key3': DummyObj(prop_1=3.15, prop_2='a string'), 'key4': ['alpha', 0]}
"""