First but not optimized version of AstFormatDict
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
import pytest
|
||||
from core.sheerka.services.SheerkaRuleManager import FormatAstRawText, FormatAstVariable, FormatAstVariableNotFound, \
|
||||
FormatAstSequence, FormatAstList, FormatAstDict
|
||||
from out.AsStrVisitor import AsStrVisitor
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
class TestAsStrVisitor(TestUsingMemoryBasedSheerka):
|
||||
|
||||
@pytest.mark.parametrize("format_text, expected", [
|
||||
(FormatAstRawText("hello word"), "hello word"),
|
||||
(FormatAstVariable('xxx', value="hello word"), "hello word"),
|
||||
(FormatAstVariable('xxx', value=1), "1"),
|
||||
(FormatAstVariable('xxx', value=1.5), "1.5"),
|
||||
(FormatAstVariable('xxx', value="hello word", debug=True), "'hello word'"),
|
||||
(FormatAstVariable('xxx', value=1, debug=True), "1"),
|
||||
(FormatAstVariable('xxx', value=1.5, debug=True), "1.5"),
|
||||
(FormatAstVariable('xxx', value=False, debug=True), "False"),
|
||||
(FormatAstVariableNotFound('va_name'), "\x1b[31mva_name\x1b[0m"),
|
||||
])
|
||||
def test_i_can_print_simple_ast(self, format_text, expected):
|
||||
visitor = AsStrVisitor()
|
||||
|
||||
res = visitor.visit(format_text)
|
||||
assert res == expected
|
||||
|
||||
def test_i_can_print_sequence(self, capsys):
|
||||
visitor = AsStrVisitor()
|
||||
sequence = FormatAstSequence([
|
||||
FormatAstRawText("hello word"),
|
||||
FormatAstVariable('xxx', value=1),
|
||||
FormatAstVariableNotFound('va_name'),
|
||||
FormatAstVariable('xxx', value="hello word", debug=True)
|
||||
])
|
||||
|
||||
res = visitor.visit(sequence)
|
||||
|
||||
assert res == "hello word1\x1b[31mva_name\x1b[0m'hello word'"
|
||||
|
||||
def test_i_can_print_list(self, capsys):
|
||||
visitor = AsStrVisitor()
|
||||
lst = FormatAstList("xxx", items=[
|
||||
FormatAstVariable('__item', index=0, value="first element"),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
res = visitor.visit(lst)
|
||||
|
||||
assert res == "first element\n'second element'"
|
||||
|
||||
def test_i_can_print_list_with_sub_items(self, capsys):
|
||||
visitor = AsStrVisitor()
|
||||
lst = FormatAstList("xxx", items=[
|
||||
FormatAstVariable('__item', index=0, value="first element"),
|
||||
FormatAstList("__sub", items=[
|
||||
FormatAstVariable('__item', index=0, value="sub item 1"),
|
||||
FormatAstVariable('__item', index=1, value="sub item 2"),
|
||||
]),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
res = visitor.visit(lst)
|
||||
|
||||
assert res == "first element\nsub item 1\nsub item 2\n'second element'"
|
||||
|
||||
def test_i_can_print_list_in_debug_mode(self, capsys):
|
||||
visitor = AsStrVisitor()
|
||||
lst = FormatAstList("xxx", debug=True, prefix="[", suffix="]", items=[
|
||||
FormatAstVariable('__item', index=0, value="first element", debug=True),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
res = visitor.visit(lst)
|
||||
|
||||
assert res == "['first element', 'second element']"
|
||||
|
||||
def test_i_can_print_expanded_list_in_debug_mode(self):
|
||||
visitor = AsStrVisitor(expand=True)
|
||||
lst = FormatAstList("xxx", debug=True, prefix="[", suffix="]", items=[
|
||||
FormatAstVariable('__item', index=0, value="first element", debug=True),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
res = visitor.visit(lst)
|
||||
|
||||
assert res == "['first element',\n'second element']"
|
||||
|
||||
def test_i_can_print_list_with_index(self):
|
||||
visitor = AsStrVisitor()
|
||||
lst = FormatAstList("xxx", show_index=True, items=[
|
||||
FormatAstVariable('__item', index=0, value="first element"),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
res = visitor.visit(lst)
|
||||
|
||||
assert res == "[0] first element\n[1] 'second element'"
|
||||
|
||||
def test_i_can_print_dict(self, capsys):
|
||||
visitor = AsStrVisitor()
|
||||
bag = FormatAstDict("xxx", items=[
|
||||
(FormatAstVariable('__key', index=0, value="key1"),
|
||||
FormatAstVariable('__value', index="key1", value=1)),
|
||||
(FormatAstVariable('__key', index=1, value="long_key2"),
|
||||
FormatAstVariable('__value', index="key2", value="value2")),
|
||||
])
|
||||
|
||||
res = visitor.visit(bag)
|
||||
|
||||
assert res == "key1 : 1\nlong_key2: value2"
|
||||
|
||||
def test_i_can_print_dict_in_debug_mode(self, capsys):
|
||||
visitor = AsStrVisitor()
|
||||
bag = FormatAstDict("xxx", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="key1", debug=True),
|
||||
FormatAstVariable('__value', index="key1", value=1, debug=True)),
|
||||
(FormatAstVariable('__key', index=1, value="long_key2", debug=True),
|
||||
FormatAstVariable('__value', index="key2", value="value2", debug=True)),
|
||||
])
|
||||
|
||||
res = visitor.visit(bag)
|
||||
|
||||
assert res == "{'key1': 1, 'long_key2': 'value2'}"
|
||||
|
||||
def test_i_can_print_expanded_dict_in_debug_mode(self):
|
||||
visitor = AsStrVisitor(expand=True)
|
||||
bag = FormatAstDict("xxx", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="key1", debug=True),
|
||||
FormatAstVariable('__value', index="key1", value=1, debug=True)),
|
||||
(FormatAstVariable('__key', index=1, value="long_key2", debug=True),
|
||||
FormatAstVariable('__value', index="key2", value="value2", debug=True)),
|
||||
])
|
||||
|
||||
res = visitor.visit(bag)
|
||||
|
||||
assert res == "{'key1' : 1,\n'long_key2': 'value2'}"
|
||||
|
||||
def test_i_can_print_sub_level_of_dict_in_expand_mode(self):
|
||||
visitor = AsStrVisitor(expand=True)
|
||||
bag = FormatAstDict("xxx", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="key1", debug=True),
|
||||
FormatAstVariable('__value', index="key1", value=1, debug=True)),
|
||||
|
||||
(FormatAstVariable('__key', index=0, value="key2", debug=True),
|
||||
FormatAstDict("__value", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="sub_key1", debug=True),
|
||||
FormatAstVariable('__value', index="sub_key1", value=1, debug=True)),
|
||||
(FormatAstVariable('__key', index=1, value="sub_long_key2", debug=True),
|
||||
FormatAstDict("__value", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="sub_sub_key1", debug=True),
|
||||
FormatAstVariable('__value', index="sub_sub_key1", value=1, debug=True)),
|
||||
(FormatAstVariable('__key', index=1, value="sub_sub_key2", debug=True),
|
||||
FormatAstVariable('__value', index="sub_sub_key2", value="sub_sub_value", debug=True)),
|
||||
])),
|
||||
])),
|
||||
|
||||
(FormatAstVariable('__key', index=1, value="long_key3", debug=True),
|
||||
FormatAstVariable('__value', index="key2", value="value2", debug=True)),
|
||||
])
|
||||
|
||||
res = visitor.visit(bag)
|
||||
|
||||
assert res == """{'key1' : 1,
|
||||
'key2' : {'sub_key1' : 1,
|
||||
'sub_long_key2': {'sub_sub_key1': 1,
|
||||
'sub_sub_key2': 'sub_sub_value'}},
|
||||
'long_key3': 'value2'}"""
|
||||
@@ -0,0 +1,143 @@
|
||||
import pytest
|
||||
from core.sheerka.services.SheerkaRuleManager import FormatAstRawText, FormatAstVariable, FormatAstVariableNotFound, \
|
||||
FormatAstSequence, FormatAstList, FormatAstDict
|
||||
from out.ConsoleVisistor import ConsoleVisitor
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
class TestConsoleVisitor(TestUsingMemoryBasedSheerka):
|
||||
|
||||
@pytest.mark.parametrize("format_text, expected", [
|
||||
(FormatAstRawText("hello word"), "hello word"),
|
||||
(FormatAstVariable('xxx', value="hello word"), "hello word"),
|
||||
(FormatAstVariable('xxx', value=1), "1"),
|
||||
(FormatAstVariable('xxx', value=1.5), "1.5"),
|
||||
(FormatAstVariable('xxx', value="hello word", debug=True), "'hello word'"),
|
||||
(FormatAstVariable('xxx', value=1, debug=True), "1"),
|
||||
(FormatAstVariable('xxx', value=1.5, debug=True), "1.5"),
|
||||
(FormatAstVariable('xxx', value=False, debug=True), "False"),
|
||||
(FormatAstVariableNotFound('va_name'), "\x1b[31mva_name\x1b[0m"),
|
||||
])
|
||||
def test_i_can_print_simple_ast(self, format_text, expected, capsys):
|
||||
visitor = ConsoleVisitor()
|
||||
|
||||
visitor.visit(format_text)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == expected + "\n"
|
||||
|
||||
def test_i_can_print_sequence(self, capsys):
|
||||
visitor = ConsoleVisitor()
|
||||
sequence = FormatAstSequence([
|
||||
FormatAstRawText("hello word"),
|
||||
FormatAstVariable('xxx', value=1),
|
||||
FormatAstVariableNotFound('va_name'),
|
||||
FormatAstVariable('xxx', value="hello word", debug=True)
|
||||
])
|
||||
|
||||
visitor.visit(sequence)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "hello word1\x1b[31mva_name\x1b[0m'hello word'\n"
|
||||
|
||||
def test_i_can_print_list(self, capsys):
|
||||
visitor = ConsoleVisitor()
|
||||
lst = FormatAstList("xxx", items=[
|
||||
FormatAstVariable('__item', index=0, value="first element"),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
visitor.visit(lst)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "first element\n'second element'\n"
|
||||
|
||||
def test_i_can_print_list_with_sub_items(self, capsys):
|
||||
visitor = ConsoleVisitor()
|
||||
lst = FormatAstList("xxx", items=[
|
||||
FormatAstVariable('__item', index=0, value="first element"),
|
||||
FormatAstList("__sub", items=[
|
||||
FormatAstVariable('__item', index=0, value="sub item 1"),
|
||||
FormatAstVariable('__item', index=1, value="sub item 2"),
|
||||
]),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
visitor.visit(lst)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "first element\nsub item 1\nsub item 2\n'second element'\n"
|
||||
|
||||
def test_i_can_print_list_in_debug_mode(self, capsys):
|
||||
visitor = ConsoleVisitor()
|
||||
lst = FormatAstList("xxx", debug=True, prefix="[", suffix="]", items=[
|
||||
FormatAstVariable('__item', index=0, value="first element", debug=True),
|
||||
FormatAstVariable('__item', index=1, value="second element", debug=True),
|
||||
])
|
||||
|
||||
visitor.visit(lst)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "['first element', 'second element']\n"
|
||||
|
||||
def test_i_can_print_dict(self, capsys):
|
||||
visitor = ConsoleVisitor()
|
||||
bag = FormatAstDict("xxx", items=[
|
||||
(FormatAstVariable('__key', index=0, value="key1"),
|
||||
FormatAstVariable('__value', index="key1", value=1)),
|
||||
(FormatAstVariable('__key', index=1, value="long_key2"),
|
||||
FormatAstVariable('__value', index="key2", value="value2")),
|
||||
])
|
||||
|
||||
visitor.visit(bag)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "key1 : 1\nlong_key2: value2\n"
|
||||
|
||||
def test_i_can_print_dict_in_debug_mode(self, capsys):
|
||||
visitor = ConsoleVisitor()
|
||||
bag = FormatAstDict("xxx", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="key1", debug=True),
|
||||
FormatAstVariable('__value', index="key1", value=1, debug=True)),
|
||||
(FormatAstVariable('__key', index=1, value="long_key2", debug=True),
|
||||
FormatAstVariable('__value', index="key2", value="value2", debug=True)),
|
||||
])
|
||||
|
||||
visitor.visit(bag)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "{'key1': 1, 'long_key2': 'value2'}\n"
|
||||
|
||||
def test_i_can_print_sub_level_of_dict_in_expand_mode(self, capsys):
|
||||
visitor = ConsoleVisitor(expand_mode='always')
|
||||
bag = FormatAstDict("xxx", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="key1", debug=True),
|
||||
FormatAstVariable('__value', index="key1", value=1, debug=True)),
|
||||
|
||||
(FormatAstVariable('__key', index=0, value="key2", debug=True),
|
||||
FormatAstDict("__value", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="sub_key1", debug=True),
|
||||
FormatAstVariable('__value', index="sub_key1", value=1, debug=True)),
|
||||
(FormatAstVariable('__key', index=1, value="sub_long_key2", debug=True),
|
||||
FormatAstDict("__value", debug=True, prefix="{", suffix="}", items=[
|
||||
(FormatAstVariable('__key', index=0, value="sub_sub_key1", debug=True),
|
||||
FormatAstVariable('__value', index="sub_sub_key1", value=1, debug=True)),
|
||||
(FormatAstVariable('__key', index=1, value="sub_sub_key2", debug=True),
|
||||
FormatAstVariable('__value', index="sub_sub_key2", value="sub_sub_value", debug=True)),
|
||||
])),
|
||||
])),
|
||||
|
||||
(FormatAstVariable('__key', index=1, value="long_key3", debug=True),
|
||||
FormatAstVariable('__value', index="key2", value="value2", debug=True)),
|
||||
])
|
||||
|
||||
visitor.visit(bag)
|
||||
captured = capsys.readouterr()
|
||||
|
||||
assert captured.out == """{'key1' : 1,
|
||||
'key2' : {'sub_key1' : 1,
|
||||
'sub_long_key2': {'sub_sub_key1': 1,
|
||||
'sub_sub_key2': 'sub_sub_value'}},
|
||||
'long_key3': 'value2'}
|
||||
"""
|
||||
+127
-57
@@ -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]}
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user