from core.builtin_concepts import BuiltinConcepts from core.sheerka.services.SheerkaDebugManager import NullDebugLogger from core.sheerka.services.SheerkaOut import SheerkaOut from parsers.FormatRuleActionParser import FormatAstList, FormatAstVariable, FormatAstDict, FormatAstMulti from out.DeveloperVisitor import DeveloperVisitor from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka class TestDeveloperVisitor(TestUsingMemoryBasedSheerka): def test_i_can_develop_list(self): sheerka, context = self.init_concepts() service_out = sheerka.services[SheerkaOut.NAME] dev_visitor = DeveloperVisitor(service_out, NullDebugLogger(), set(), 0) bag = {"to_format": ["a", "b", "c"]} res = dev_visitor.visit(context, FormatAstList("to_format"), bag) assert res == FormatAstList(variable="to_format", items=[ FormatAstVariable(name="__item", index=0, value="a"), FormatAstVariable(name="__item", index=1, value="b"), FormatAstVariable(name="__item", index=2, value="c"), ]) def test_i_can_develop_list_of_list(self): sheerka, context = self.init_concepts() service_out = sheerka.services[SheerkaOut.NAME] dev_visitor = DeveloperVisitor(service_out, NullDebugLogger(), set(), 0) bag = {"to_format": [["a1", "a2"], ["b1"]]} res = dev_visitor.visit(context, FormatAstList("to_format"), bag) assert res == FormatAstList(variable="to_format", items=[ FormatAstList(variable="__item", index=0, debug=True, prefix='[', suffix=']', items=[ FormatAstVariable(name="__item", index=0, debug=True, value="a1"), FormatAstVariable(name="__item", index=1, debug=True, value="a2"), ]), FormatAstList(variable="__item", index=1, debug=True, prefix='[', suffix=']', items=[ FormatAstVariable(name="__item", index=0, debug=True, value="b1"), ]), ]) def test_i_can_develop_dict(self): sheerka, context = self.init_concepts() service_out = sheerka.services[SheerkaOut.NAME] dev_visitor = DeveloperVisitor(service_out, NullDebugLogger(), set(), 0) bag = {"to_format": {"a": "value1", "b": 3.14}} res = dev_visitor.visit(context, FormatAstDict("to_format"), bag) assert res == FormatAstDict(variable='to_format', items=[ (FormatAstVariable(name='__key', index=0, value='a'), FormatAstVariable(name='__value', index='a', value='value1')), (FormatAstVariable(name='__key', index=1, value='b'), FormatAstVariable(name='__value', index='b', value=3.14))]) def test_i_can_develop_multi(self): sheerka, context, *rules = self.init_format_rules( ("isinstance(to_format, BuiltinConcepts.TO_DICT)", "dict(to_format)")) service_out = sheerka.services[SheerkaOut.NAME] dev_visitor = DeveloperVisitor(service_out, NullDebugLogger(), set(), 0) item1 = sheerka.new(BuiltinConcepts.TO_DICT, body={"a1": "value1"}) item2 = sheerka.new(BuiltinConcepts.TO_DICT, body={"a2": "value2"}) bag = {"to_format": sheerka.new(BuiltinConcepts.TO_MULTI, body=[item1, item2])} res = dev_visitor.visit(context, FormatAstMulti("to_format"), bag) assert res == FormatAstMulti("to_format", items=[ FormatAstDict(variable='to_format', items=[ (FormatAstVariable(name='__key', index=0, value='a1'), FormatAstVariable(name='__value', index='a1', value='value1'))]), FormatAstDict(variable='to_format', items=[ (FormatAstVariable(name='__key', index=0, value='a2'), FormatAstVariable(name='__value', index='a2', value='value2'))]) ])