Enhanced complex concepts handling
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from core.builtin_concepts import ReturnValueConcept, ParserResultConcept
|
||||
from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from core.sheerka import Sheerka, ExecutionContext
|
||||
from evaluators.ConceptNodeEvaluator import ConceptNodeEvaluator
|
||||
@@ -15,7 +15,7 @@ def get_context():
|
||||
return ExecutionContext("test", Event(), sheerka)
|
||||
|
||||
|
||||
def get_return_value(context, grammar, expression):
|
||||
def from_parsing(context, grammar, expression):
|
||||
parser = ConceptLexerParser()
|
||||
parser.initialize(context, grammar)
|
||||
|
||||
@@ -31,7 +31,7 @@ def init(concept, grammar, text):
|
||||
context.sheerka.add_in_cache(c)
|
||||
else:
|
||||
context.sheerka.add_in_cache(concept)
|
||||
ret_val = get_return_value(context, grammar, text)
|
||||
ret_val = from_parsing(context, grammar, text)
|
||||
node = ret_val.value.value[0]
|
||||
|
||||
return context, node
|
||||
@@ -56,172 +56,24 @@ def test_i_can_match(ret_val, expected):
|
||||
assert ConceptNodeEvaluator().matches(context, ret_val) == expected
|
||||
|
||||
|
||||
def test_parser_result_of_concept_is_returned_when_list_of_one_concept_node():
|
||||
def test_concept_is_returned_when_only_one_in_the_list():
|
||||
foo = Concept("foo")
|
||||
context = get_context()
|
||||
context.sheerka.add_in_cache(foo)
|
||||
|
||||
evaluator = ConceptNodeEvaluator()
|
||||
ret_val = get_return_value(context, {foo: StrMatch("foo")}, "foo")
|
||||
ret_val = from_parsing(context, {foo: StrMatch("foo")}, "foo")
|
||||
|
||||
result = evaluator.eval(context, ret_val)
|
||||
wrapper = result.body
|
||||
return_value = result.body.body
|
||||
|
||||
assert result.who == evaluator.name
|
||||
assert result.status
|
||||
assert result.value == ParserResultConcept(
|
||||
evaluator,
|
||||
"foo",
|
||||
Concept("foo", body="'foo'").init_key(),
|
||||
None)
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert wrapper.parser == evaluator
|
||||
assert wrapper.source == "foo"
|
||||
assert return_value == Concept("foo", body="foo").init_key()
|
||||
assert return_value.metadata.is_evaluated
|
||||
assert result.parents == [ret_val]
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_for_str_match():
|
||||
foo = Concept("foo")
|
||||
grammar = {foo: StrMatch("foo", rule_name="variable")}
|
||||
context, node = init(foo, grammar, "foo")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(context.sheerka, node.concept, node.underlying)
|
||||
|
||||
assert "variable" in updated.props
|
||||
assert updated.props["variable"].value == "'foo'"
|
||||
assert updated.body == "'foo'"
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_for_sequence():
|
||||
foo = Concept("foo")
|
||||
grammar = {foo: Sequence("one", "two", rule_name="variable")}
|
||||
context, node = init(foo, grammar, "one two")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying)
|
||||
|
||||
assert "variable" in updated.props
|
||||
assert updated.props["variable"].value == "'one two'"
|
||||
assert updated.body == "'one two'"
|
||||
|
||||
|
||||
def test_concept_property_is_updated_for_str_in_sequence():
|
||||
foo = Concept("foo")
|
||||
grammar = {foo: Sequence(StrMatch("one", rule_name="s1"), StrMatch("two", rule_name="s2"), rule_name="variable")}
|
||||
context, node = init(foo, grammar, "one two")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying)
|
||||
|
||||
assert updated.props["variable"].value == "'one two'"
|
||||
assert updated.props["s1"].value == "'one'"
|
||||
assert updated.props["s2"].value == "'two'"
|
||||
assert updated.body == "'one two'"
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_for_optional():
|
||||
foo = Concept("foo")
|
||||
grammar = {foo: Sequence("one", Optional("two", rule_name="o"), rule_name="variable")}
|
||||
context, node = init(foo, grammar, "one two")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying)
|
||||
|
||||
assert "variable" in updated.props
|
||||
assert updated.props["variable"].value == "'one two'"
|
||||
assert updated.props["o"].value == "'two'"
|
||||
assert updated.body == "'one two'"
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_for_zero_or_more():
|
||||
foo = Concept("foo")
|
||||
grammar = {foo: ZeroOrMore("one", rule_name="variable")}
|
||||
context, node = init(foo, grammar, "one one one")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying)
|
||||
|
||||
assert "variable" in updated.props
|
||||
assert updated.props["variable"].value == "'one one one'"
|
||||
assert updated.body == "'one one one'"
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_when_list_of_properties():
|
||||
foo = Concept("foo")
|
||||
grammar = {foo: Sequence(StrMatch("one", rule_name="s"), StrMatch("two", rule_name="s"), rule_name="variable")}
|
||||
context, node = init(foo, grammar, "one two")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying)
|
||||
|
||||
assert updated.props["variable"].value == "'one two'"
|
||||
assert updated.props["s"].value == ["'one'", "'two'"]
|
||||
assert updated.body == "'one two'"
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_when_another_concept():
|
||||
foo = Concept("foo")
|
||||
bar = Concept("bar")
|
||||
grammar = {
|
||||
foo: Sequence("one", "two", rule_name="var"),
|
||||
bar: Sequence(foo, "three", "four", rule_name="var")}
|
||||
context, node = init([foo, bar], grammar, "one two three four")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying)
|
||||
|
||||
assert updated.body == "foo 'three four'"
|
||||
assert updated.props["var"].value == "foo 'three four'"
|
||||
assert updated.props["foo"].value == Concept("foo", body="'one two'").set_prop("var", "'one two'").init_key()
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_when_concept_recursion_using_optional():
|
||||
number = Concept("number")
|
||||
add = Concept("add")
|
||||
grammar = {
|
||||
number: OrderedChoice("one", "two"),
|
||||
add: Sequence(number, Optional(Sequence(OrderedChoice("plus", "minus", rule_name="op"), add)))
|
||||
}
|
||||
context, node = init([number, add], grammar, "one plus two")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying)
|
||||
|
||||
assert updated.props["number"].value == Concept("number", body="'one'").init_key()
|
||||
assert updated.props["op"].value == "'plus'"
|
||||
expected_add = Concept("add", body="number"). \
|
||||
set_prop("number", Concept("number", body="'two'").init_key()). \
|
||||
init_key()
|
||||
assert updated.props["add"].value == expected_add
|
||||
|
||||
|
||||
def test_concept_property_is_correctly_updated_when_concept_recursion_using_zero_or_more():
|
||||
number = Concept("number")
|
||||
add = Concept("add")
|
||||
grammar = {
|
||||
number: OrderedChoice("one", "two", 'three'),
|
||||
add: Sequence(number, ZeroOrMore(Sequence(OrderedChoice("plus", "minus", rule_name="op"), number)))
|
||||
}
|
||||
context, node = init([number, add], grammar, "one plus two minus three")
|
||||
|
||||
updated = ConceptNodeEvaluator().finalize_concept(
|
||||
context.sheerka,
|
||||
context.sheerka.new(node.concept.key),
|
||||
node.underlying,
|
||||
init_empty_body=True)
|
||||
|
||||
assert updated.props["number"].value == [Concept("number", body="'one'").init_key(),
|
||||
Concept("number", body="'two'").init_key(),
|
||||
Concept("number", body="'three'").init_key()]
|
||||
assert updated.props["op"].value == ["'plus'", "'minus'"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user