Files
Sheerka-Old/tests/out/test_DeveloperVisitor.py
kodjo 7dcaa9c111 Fixed #29: Parsers: Implement parsing memoization
Fixed #77 : Parser: ShortTermMemoryParser should be called separately
Fixed #78 : Remove VariableNode usage
Fixed #79 : ConceptManager: Implement compile caching
Fixed #80 : SheerkaExecute : parsers_key is not correctly computed
Fixed #81 : ValidateConceptEvaluator : Validate concept's where and pre clauses right after the parsing
Fixed #82 : SheerkaIsAManager: isa() failed when the set as a body
Fixed #83 : ValidateConceptEvaluator : Support BNF and SYA Concepts
Fixed #84 : ExpressionParser: Implement the parser as a standard parser
Fixed #85 : Services: Give order to services
Fixed #86 : cannot manage smart_get_attr(the short, color)
2021-06-07 21:14:03 +02:00

80 lines
3.7 KiB
Python

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'))])
])