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
+1 -1
View File
@@ -318,7 +318,7 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
def test_i_can_evaluate_when_compiled_is_set_up_with_return_value(self):
sheerka = self.get_sheerka()
python_node = PythonNode("1 +1 ")
python_node = PythonNode("1 +1 ").init_ast()
parser_result = ParserResultConcept(parser="who", value=python_node)
concept = Concept("to_eval").def_var("prop")
+9 -5
View File
@@ -7,7 +7,7 @@ from core.global_symbols import RULE_COMPARISON_CONTEXT
from core.rule import Rule
from core.sheerka.services.SheerkaRuleManager import SheerkaRuleManager, FormatRuleParser, \
FormatAstRawText, FormatAstVariable, FormatAstSequence, FormatAstFunction, \
FormatRuleSyntaxError, FormatAstList, UnexpectedEof, FormatAstColor, RulePredicate
FormatRuleSyntaxError, FormatAstList, UnexpectedEof, FormatAstColor, RulePredicate, FormatAstDict
from core.tokenizer import Token, TokenKind
from parsers.BaseNodeParser import SourceCodeWithConceptNode, SourceCodeNode
from parsers.PythonParser import PythonNode
@@ -102,8 +102,8 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
('green("")', FormatAstColor("green", raw(""))),
("list(var_name, 2, 'children')", FormatAstList("var_name", recurse_on="children", recursion_depth=2)),
("list(var_name, recursion_depth=2, recurse_on='children')", FormatAstList("var_name",
recurse_on="children",
recursion_depth=2)),
recurse_on="children",
recursion_depth=2)),
("list(var_name, recursion_depth=2, 'children')", FormatAstList("var_name", recursion_depth=2)),
("list(var_name, 'children', recursion_depth=2)", FormatAstList("var_name", recursion_depth=2)),
("list(var_name)", FormatAstList("var_name")),
@@ -112,6 +112,9 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
("{variable:format}", FormatAstVariable("variable", "format")),
("{variable:3}", FormatAstVariable("variable", "3")),
(r"\not_a_function(a={var})", seq([raw("not_a_function(a="), var("var"), raw(")")])),
("dict(var_name)", FormatAstDict("var_name")),
("dict(var_name, items_prop='props')", FormatAstDict("var_name", items_prop='props')),
("dict(var_name, debug=True)", FormatAstDict("var_name", debug=True, prefix="{", suffix="}"))
])
def test_i_can_parse_format_rule(self, text, expected):
assert FormatRuleParser(text).parse() == expected
@@ -129,10 +132,11 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
("red(xy {v})", FormatRuleSyntaxError("Invalid identifier", None)),
("list()", FormatRuleSyntaxError("variable name not found", None)),
("list(recursion_depth=2)", FormatRuleSyntaxError("variable name not found", None)),
("list(a,b,c,d)", FormatRuleSyntaxError("too many positional arguments",
Token(TokenKind.IDENTIFIER, "d", 11, 1, 12))),
("list(a,b,c,d,e)", FormatRuleSyntaxError("too many positional arguments",
Token(TokenKind.IDENTIFIER, "e", 13, 1, 14))),
("list(a, recursion_depth=hello)", FormatRuleSyntaxError("'hello' is not numeric", None)),
("list(a, recursion_depth='hello')", FormatRuleSyntaxError("'recursion_depth' must be an integer", None)),
("dict()", FormatRuleSyntaxError("variable name not found", None)),
])
def test_i_cannot_parse_invalid_format(self, text, expected_error):
parser = FormatRuleParser(text)
+168
View File
@@ -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'}"""
+143
View File
@@ -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
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]}
"""