Fixed error when desc() returns multiple results

This commit is contained in:
2020-12-04 17:37:06 +01:00
parent 8b86998225
commit d364878ddb
16 changed files with 408 additions and 19 deletions
+3 -2
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, FormatAstDict
FormatRuleSyntaxError, FormatAstList, UnexpectedEof, FormatAstColor, RulePredicate, FormatAstDict, FormatAstMulti
from core.tokenizer import Token, TokenKind
from parsers.BaseNodeParser import SourceCodeWithConceptNode, SourceCodeNode
from parsers.PythonParser import PythonNode
@@ -114,7 +114,8 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
(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="}"))
("dict(var_name, debug=True)", FormatAstDict("var_name", debug=True, prefix="{", suffix="}")),
("multi(var_name)", FormatAstMulti("var_name")),
])
def test_i_can_parse_format_rule(self, text, expected):
assert FormatRuleParser(text).parse() == expected
+50
View File
@@ -0,0 +1,50 @@
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
from core.concept import Concept
from evaluators.BaseEvaluator import BaseEvaluator
from parsers.BaseParser import BaseParser
reduced_requested = ReturnValueConcept("Sheerka", True, Concept(name=BuiltinConcepts.REDUCE_REQUESTED,
key=BuiltinConcepts.REDUCE_REQUESTED))
def ret_val(value="value", who="who", status=True):
return ReturnValueConcept(who, status, value)
def p_ret_val(value="value", parser="parser", status=True):
return ReturnValueConcept(BaseParser.PREFIX + parser, status, value)
def e_ret_val(value="value", evaluator="evaluator", status=True):
return ReturnValueConcept(BaseEvaluator.PREFIX + evaluator, status, value)
def p_ret_val_false(value="value", parser="parser"):
return p_ret_val(value, parser, status=False)
def p_ret_val_true(value="value", parser="parser"):
return p_ret_val(value, parser, status=True)
def e_ret_val_false(value="value", parser="parser"):
return e_ret_val(value, parser, status=False)
def e_ret_val_true(value="value", parser="parser"):
return e_ret_val(value, parser, status=True)
def e_ret_val_new(key, evaluator="evaluator", status=True, **kwargs):
body = new_concept(key, **kwargs)
return e_ret_val(body, evaluator, status)
def new_concept(key, **kwargs):
res = Concept(key=key, name=key, is_builtin=False, is_unique=False)
for k, v in kwargs.items():
to_use = "#" + k + "#" if k in ("body", "pre", "post", "ret") else k
res.set_value(to_use, v)
res.get_metadata().is_evaluated = True
return res
@@ -0,0 +1,55 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from evaluators.MultipleOutEvaluator import MultipleOutEvaluator
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
from tests.evaluators.EvaluatorTestsUtils import reduced_requested, e_ret_val_new, p_ret_val_false, p_ret_val_true, \
e_ret_val_false, e_ret_val
class TestMultipleOutEvaluator(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("return_values, expected", [
([reduced_requested,
e_ret_val_new(BuiltinConcepts.TO_DICT, body={}),
e_ret_val_new(BuiltinConcepts.TO_DICT, body={}),
p_ret_val_false("value"),
e_ret_val_false()], True),
# only one to_dict
([reduced_requested, e_ret_val_new(BuiltinConcepts.TO_DICT, body={})], False),
# reduce request missing
([e_ret_val_new(BuiltinConcepts.TO_DICT, body={}), e_ret_val_new(BuiltinConcepts.TO_DICT, body={})], False),
# parser result not evaluated
([reduced_requested,
e_ret_val_new(BuiltinConcepts.TO_DICT, body={}),
e_ret_val_new(BuiltinConcepts.TO_DICT, body={}),
p_ret_val_true("value")], False),
])
def test_i_can_match(self, return_values, expected):
sheerka, context = self.init_concepts()
evaluator = MultipleOutEvaluator()
assert evaluator.matches(context, return_values) == expected
def test_i_can_eval(self):
sheerka, context = self.init_concepts()
evaluator = MultipleOutEvaluator()
first_out = sheerka.new(BuiltinConcepts.TO_DICT, body={"a": 1})
second_out = sheerka.new(BuiltinConcepts.TO_DICT, body={"b": 1})
return_values = [reduced_requested,
e_ret_val(first_out),
e_ret_val(second_out),
p_ret_val_false("value"),
e_ret_val_false()]
evaluator.matches(context, return_values)
res = evaluator.eval(context, return_values)
assert sheerka.isinstance(res, BuiltinConcepts.RETURN_VALUE)
assert res.body == sheerka.new(BuiltinConcepts.TO_MULTI, body=[first_out, second_out])
assert res.parents == return_values
+38
View File
@@ -14,3 +14,41 @@ class TestSheerkaNonRegDisplay(TestUsingMemoryBasedSheerka):
captured = capsys.readouterr()
assert captured.out == "ReturnValue(who=evaluators.OneSuccess, status=True, value=(1001)one)\n"
def test_i_can_display_multiple_concepts_description(self, capsys):
init = [
"def concept foo as 1",
"def concept foo as 2",
]
sheerka = self.init_scenario(init)
capsys.readouterr()
sheerka.enable_process_return_values = True
sheerka.evaluate_user_input("desc(foo)")
captured = capsys.readouterr()
assert captured.out == """id : 1001
name : foo
key : foo
definition: None
type : None
body : 1
where : None
pre : None
post : None
ret : None
vars : []
props : {}
id : 1002
name : foo
key : foo
definition: None
type : None
body : 2
where : None
pre : None
post : None
ret : None
vars : []
props : {}
"""
+13
View File
@@ -1183,6 +1183,19 @@ as:
assert sheerka.get_attr(res[0].body, sheerka.new("size")) == sheerka.new("little")
assert sheerka.get_attr(res[0].body, sheerka.new("adjective")) == sheerka.new("beautiful")
def test_i_can_display_multiple_concepts_using_desc_command(self):
init = [
"def concept foo as 1",
"def concept foo as 2",
]
sheerka = self.init_scenario(init)
res = sheerka.evaluate_user_input("desc(foo)")
assert len(res) == 1
assert res[0].status
assert sheerka.isinstance(res[0].body, BuiltinConcepts.TO_MULTI)
class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
def test_i_can_def_several_concepts(self):
+46 -7
View File
@@ -1,6 +1,7 @@
from core.builtin_concepts import BuiltinConcepts
from core.sheerka.services.SheerkaDebugManager import NullDebugLogger
from core.sheerka.services.SheerkaOut import SheerkaOut
from core.sheerka.services.SheerkaRuleManager import FormatAstList, FormatAstVariable
from core.sheerka.services.SheerkaRuleManager import FormatAstList, FormatAstVariable, FormatAstDict, FormatAstMulti
from out.DeveloperVisitor import DeveloperVisitor
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -12,10 +13,10 @@ class TestDeveloperVisitor(TestUsingMemoryBasedSheerka):
service_out = sheerka.services[SheerkaOut.NAME]
dev_visitor = DeveloperVisitor(service_out, NullDebugLogger(), set(), 0)
bag = {"a": ["a", "b", "c"]}
bag = {"to_format": ["a", "b", "c"]}
res = dev_visitor.visit(context, FormatAstList("a"), bag)
assert res == FormatAstList(variable="a", items=[
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"),
@@ -26,10 +27,10 @@ class TestDeveloperVisitor(TestUsingMemoryBasedSheerka):
service_out = sheerka.services[SheerkaOut.NAME]
dev_visitor = DeveloperVisitor(service_out, NullDebugLogger(), set(), 0)
bag = {"a": [["a1", "a2"], ["b1"]]}
bag = {"to_format": [["a1", "a2"], ["b1"]]}
res = dev_visitor.visit(context, FormatAstList("a"), bag)
assert res == FormatAstList(variable="a", items=[
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"),
@@ -38,3 +39,41 @@ class TestDeveloperVisitor(TestUsingMemoryBasedSheerka):
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'))])
])