Refactored to use cached_asts in Concepts, rather than setting up a value directly
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
import core.utils
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from core.concept import Concept, ConceptParts, DoNotResolve
|
||||
from core.sheerka import Sheerka, ExecutionContext
|
||||
from core.tokenizer import Tokenizer, TokenKind, Token
|
||||
from parsers.ConceptLexerParser import ConceptLexerParser, ConceptNode, Sequence, StrMatch, OrderedChoice, Optional, \
|
||||
@@ -58,7 +58,20 @@ def get_context():
|
||||
|
||||
|
||||
def get_expected(concept, text=None):
|
||||
return Concept(name=concept.name, body=text or concept.name).init_key()
|
||||
c = Concept(name=concept.name)
|
||||
c.cached_asts[ConceptParts.BODY] = DoNotResolve(text or concept.name)
|
||||
c.init_key()
|
||||
return c
|
||||
|
||||
|
||||
def cbody(concept):
|
||||
"""cbody stands for compiled body"""
|
||||
return concept.cached_asts[ConceptParts.BODY]
|
||||
|
||||
|
||||
def cprop(concept, prop_name):
|
||||
"""cbody stands for compiled property"""
|
||||
return concept.cached_asts[prop_name]
|
||||
|
||||
|
||||
def init(concepts, grammar):
|
||||
@@ -359,20 +372,15 @@ def test_i_can_use_reference():
|
||||
assert context.sheerka.isinstance(res[0].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[0].value.body == [("foo", 0, 2, "one two")]
|
||||
concept_found_1 = res[0].value.body[0].concept
|
||||
assert concept_found_1.metadata.is_evaluated
|
||||
assert concept_found_1.body == "one two"
|
||||
assert cbody(concept_found_1) == DoNotResolve("one two")
|
||||
|
||||
assert res[1].status
|
||||
assert context.sheerka.isinstance(res[1].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[1].value.body == [("bar", 0, 2, "one two")]
|
||||
concept_found_2 = res[1].value.body[0].concept
|
||||
assert concept_found_2.metadata.is_evaluated
|
||||
# the body and the prop['foo'] are the same concept 'foo'
|
||||
assert isinstance(concept_found_2.body, Concept)
|
||||
assert concept_found_2.body.key == "foo"
|
||||
assert concept_found_2.body.metadata.is_evaluated
|
||||
assert concept_found_2.body.body == "one two"
|
||||
assert id(concept_found_2.props["foo"].value) == id(concept_found_2.body)
|
||||
assert cbody(concept_found_2) == get_expected(foo, "one two")
|
||||
assert id(cprop(concept_found_2, "foo")) == id(cbody(concept_found_2))
|
||||
|
||||
|
||||
def test_i_can_use_a_reference_with_a_body():
|
||||
@@ -394,20 +402,15 @@ def test_i_can_use_a_reference_with_a_body():
|
||||
assert context.sheerka.isinstance(res[0].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[0].value.body == [("foo", 0, 2, "one two")]
|
||||
concept_found_1 = res[0].value.body[0].concept
|
||||
assert not concept_found_1.metadata.is_evaluated
|
||||
assert concept_found_1.body == "'foo'"
|
||||
|
||||
assert res[1].status
|
||||
assert context.sheerka.isinstance(res[1].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[1].value.body == [("bar", 0, 2, "one two")]
|
||||
concept_found_2 = res[1].value.body[0].concept
|
||||
assert concept_found_2.metadata.is_evaluated
|
||||
# the body and the prop['foo'] are the same concept 'foo'
|
||||
assert isinstance(concept_found_2.body, Concept)
|
||||
assert concept_found_2.body.key == "foo"
|
||||
assert not concept_found_2.body.metadata.is_evaluated
|
||||
assert concept_found_2.body.body == "'foo'"
|
||||
assert id(concept_found_2.props["foo"].value) == id(concept_found_2.body)
|
||||
assert cbody(concept_found_2) == foo
|
||||
assert id(cprop(concept_found_2, "foo")) == id(cbody(concept_found_2))
|
||||
|
||||
|
||||
def test_i_can_use_context_reference_with_multiple_levels():
|
||||
@@ -429,23 +432,23 @@ def test_i_can_use_context_reference_with_multiple_levels():
|
||||
assert context.sheerka.isinstance(res[0].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[0].value.body == [("foo", 0, 2, "one two")]
|
||||
concept_found_1 = res[0].value.body[0].concept
|
||||
assert concept_found_1.body == "one two"
|
||||
assert concept_found_1.metadata.is_evaluated
|
||||
assert cbody(concept_found_1) == DoNotResolve("one two")
|
||||
|
||||
assert res[1].status
|
||||
assert context.sheerka.isinstance(res[1].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[1].value.body == [("bar", 0, 2, "one two")]
|
||||
concept_found_2 = res[1].value.body[0].concept
|
||||
assert concept_found_2.body == get_expected(foo, "one two")
|
||||
assert id(concept_found_2.props["foo"].value) == id(concept_found_2.body)
|
||||
assert cbody(concept_found_2) == get_expected(foo, "one two")
|
||||
assert id(cprop(concept_found_2, "foo")) == id(cbody(concept_found_2))
|
||||
|
||||
assert res[2].status
|
||||
assert context.sheerka.isinstance(res[2].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[2].value.body == [("baz", 0, 2, "one two")]
|
||||
concept_found_3 = res[2].value.body[0].concept
|
||||
expected_foo = get_expected(foo, "one two")
|
||||
assert concept_found_3.body == get_expected(bar, expected_foo).set_prop("foo", expected_foo)
|
||||
assert id(concept_found_3.props["bar"].value) == id(concept_found_3.body)
|
||||
assert cbody(concept_found_3) == get_expected(bar, expected_foo)
|
||||
assert cprop(concept_found_3, "foo") == expected_foo
|
||||
assert id(cprop(concept_found_3, "bar")) == id(cbody(concept_found_3))
|
||||
|
||||
|
||||
def test_order_is_not_important_when_using_references():
|
||||
@@ -476,26 +479,21 @@ def test_i_can_parse_when_reference():
|
||||
assert res.status
|
||||
assert res.value.body == [("bar", 0, 2, "twenty two")]
|
||||
concept_found = res.value.body[0].concept
|
||||
assert concept_found.body == "twenty two"
|
||||
assert concept_found.metadata.is_evaluated
|
||||
assert concept_found.get_prop("foo") == get_expected(foo, "twenty")
|
||||
assert concept_found.get_prop("foo").metadata.is_evaluated
|
||||
assert cbody(concept_found) == DoNotResolve("twenty two")
|
||||
assert cprop(concept_found, "foo") == get_expected(foo, "twenty")
|
||||
|
||||
res = parser.parse(context, "thirty one")
|
||||
assert res.status
|
||||
assert res.value.body == [("bar", 0, 2, "thirty one")]
|
||||
concept_found = res.value.body[0].concept
|
||||
assert concept_found.body == "thirty one"
|
||||
assert concept_found.metadata.is_evaluated
|
||||
assert concept_found.get_prop("foo") == get_expected(foo, "thirty")
|
||||
assert concept_found.get_prop("foo").metadata.is_evaluated
|
||||
assert cbody(concept_found) == DoNotResolve("thirty one")
|
||||
assert cprop(concept_found, "foo") == get_expected(foo, "thirty")
|
||||
|
||||
res = parser.parse(context, "twenty")
|
||||
assert res.status
|
||||
assert res.value.body == [("foo", 0, 0, "twenty")]
|
||||
concept_found = res.value.body[0].concept
|
||||
assert concept_found.body == "twenty"
|
||||
assert concept_found.metadata.is_evaluated
|
||||
assert cbody(concept_found) == DoNotResolve("twenty")
|
||||
|
||||
|
||||
def test_i_can_parse_when_reference_has_a_body():
|
||||
@@ -508,17 +506,14 @@ def test_i_can_parse_when_reference_has_a_body():
|
||||
assert res.status
|
||||
assert res.value.body == [("bar", 0, 2, "twenty two")]
|
||||
concept_found = res.value.body[0].concept
|
||||
assert concept_found.body == "twenty two"
|
||||
assert concept_found.metadata.is_evaluated
|
||||
assert concept_found.get_prop("foo") == get_expected(foo, "'one'")
|
||||
assert not concept_found.get_prop("foo").metadata.is_evaluated
|
||||
assert cbody(concept_found) == DoNotResolve("twenty two")
|
||||
assert cprop(concept_found, "foo") == foo
|
||||
|
||||
res = parser.parse(context, "twenty")
|
||||
assert res.status
|
||||
assert res.value.body == [("foo", 0, 0, "twenty")]
|
||||
concept_found = res.value.body[0].concept
|
||||
assert concept_found.body == "'one'"
|
||||
assert not concept_found.metadata.is_evaluated
|
||||
|
||||
|
||||
def test_i_can_parse_multiple_results():
|
||||
@@ -536,16 +531,14 @@ def test_i_can_parse_multiple_results():
|
||||
assert context.sheerka.isinstance(res[0].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[0].value.body == [("bar", 0, 2, "one two")]
|
||||
concept_found_0 = res[0].value.body[0].concept
|
||||
assert concept_found_0.body == "one two"
|
||||
assert concept_found_0.metadata.is_evaluated
|
||||
assert cbody(concept_found_0) == DoNotResolve("one two")
|
||||
assert len(concept_found_0.props) == 0
|
||||
|
||||
assert res[1].status
|
||||
assert context.sheerka.isinstance(res[1].value, BuiltinConcepts.PARSER_RESULT)
|
||||
assert res[1].value.body == [("foo", 0, 2, "one two")]
|
||||
concept_found_1 = res[1].value.body[0].concept
|
||||
assert concept_found_1.body == "one two"
|
||||
assert concept_found_1.metadata.is_evaluated
|
||||
assert cbody(concept_found_1) == DoNotResolve("one two")
|
||||
assert len(concept_found_1.props) == 0
|
||||
|
||||
|
||||
@@ -617,10 +610,8 @@ def test_i_can_parse_concept_reference_that_is_not_in_grammar():
|
||||
assert res.status
|
||||
assert res.value.body == [("foo", 0, 2, "twenty two")]
|
||||
concept_found = res.value.body[0].concept
|
||||
assert concept_found.body == "twenty two"
|
||||
assert concept_found.metadata.is_evaluated
|
||||
assert concept_found.get_prop("two") == get_expected(two, "two")
|
||||
assert concept_found.get_prop("two").metadata.is_evaluated
|
||||
assert cbody(concept_found) == DoNotResolve("twenty two")
|
||||
assert cprop(concept_found, "two") == get_expected(two, "two")
|
||||
|
||||
res = parser.parse(context, "twenty one")
|
||||
assert res.status
|
||||
@@ -638,8 +629,7 @@ def test_i_can_parse_zero_or_more():
|
||||
assert return_value[0].underlying == u(grammar[foo], 0, 2, [u("one", 0, 0), u("one", 2, 2)])
|
||||
|
||||
concept_found = return_value[0].concept
|
||||
assert concept_found.body == "one one"
|
||||
assert concept_found.metadata.is_evaluated
|
||||
assert cbody(concept_found) == DoNotResolve("one one")
|
||||
|
||||
|
||||
def test_i_can_parse_sequence_and_zero_or_more():
|
||||
@@ -1064,8 +1054,8 @@ def test_i_can_get_the_inner_concept_when_possible():
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert return_value == [("foo", 0, 0, "one")]
|
||||
concept_found = return_value[0].concept
|
||||
assert concept_found.body == get_expected(one, "one")
|
||||
assert concept_found.get_prop("one") == concept_found.body
|
||||
assert cbody(concept_found) == get_expected(one, "one")
|
||||
assert id(cprop(concept_found, "one")) == id(cbody(concept_found))
|
||||
|
||||
|
||||
def test_i_can_get_the_inner_concept_when_possible_with_rule_name():
|
||||
@@ -1081,11 +1071,11 @@ def test_i_can_get_the_inner_concept_when_possible_with_rule_name():
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert return_value == [("foo", 0, 0, "one")]
|
||||
concept_found = return_value[0].concept
|
||||
assert concept_found.body == get_expected(one, "one")
|
||||
assert id(concept_found.get_prop("one")) == id(concept_found.body)
|
||||
assert id(concept_found.get_prop("zero")) == id(concept_found.body)
|
||||
assert id(concept_found.get_prop("opt")) == id(concept_found.body)
|
||||
assert id(concept_found.get_prop("seq")) == id(concept_found.body)
|
||||
assert cbody(concept_found) == get_expected(one, "one")
|
||||
assert id(cprop(concept_found, "one")) == id(cbody(concept_found))
|
||||
assert id(cprop(concept_found, "zero")) == id(cbody(concept_found))
|
||||
assert id(cprop(concept_found, "opt")) == id(cbody(concept_found))
|
||||
assert id(cprop(concept_found, "seq")) == id(cbody(concept_found))
|
||||
|
||||
|
||||
def test_i_get_multiple_props_when_zero_or_more():
|
||||
@@ -1098,15 +1088,14 @@ def test_i_get_multiple_props_when_zero_or_more():
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert return_value == [("foo", 0, 4, "one one one")]
|
||||
concept_found = return_value[0].concept
|
||||
assert concept_found.body == "one one one"
|
||||
assert len(concept_found.props) == 1
|
||||
assert len(concept_found.get_prop("one")) == 3
|
||||
assert concept_found.get_prop("one")[0] == get_expected(one)
|
||||
assert concept_found.get_prop("one")[1] == get_expected(one)
|
||||
assert concept_found.get_prop("one")[2] == get_expected(one)
|
||||
assert id(concept_found.get_prop("one")[0]) != id(concept_found.get_prop("one")[1])
|
||||
assert id(concept_found.get_prop("one")[1]) != id(concept_found.get_prop("one")[2])
|
||||
assert id(concept_found.get_prop("one")[2]) != id(concept_found.get_prop("one")[0])
|
||||
assert cbody(concept_found) == DoNotResolve("one one one")
|
||||
assert len(concept_found.cached_asts["one"]) == 3
|
||||
assert cprop(concept_found, "one")[0] == get_expected(one)
|
||||
assert cprop(concept_found, "one")[1] == get_expected(one)
|
||||
assert cprop(concept_found, "one")[2] == get_expected(one)
|
||||
assert id(cprop(concept_found, "one")[0]) != id(cprop(concept_found, "one")[1])
|
||||
assert id(cprop(concept_found, "one")[1]) != id(cprop(concept_found, "one")[2])
|
||||
assert id(cprop(concept_found, "one")[2]) != id(cprop(concept_found, "one")[0])
|
||||
|
||||
|
||||
def test_i_get_multiple_props_when_zero_or_more_and_different_values():
|
||||
@@ -1119,13 +1108,12 @@ def test_i_get_multiple_props_when_zero_or_more_and_different_values():
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert return_value == [("foo", "one ok un ok uno ok")]
|
||||
concept_found = return_value[0].concept
|
||||
assert concept_found.get_prop("one")[0] == get_expected(one, "one")
|
||||
assert concept_found.get_prop("one")[1] == get_expected(one, "un")
|
||||
assert concept_found.get_prop("one")[2] == get_expected(one, "uno")
|
||||
assert concept_found.get_prop("seq")[0] == "one ok"
|
||||
assert concept_found.get_prop("seq")[1] == "un ok"
|
||||
assert concept_found.get_prop("seq")[2] == "uno ok"
|
||||
|
||||
assert cprop(concept_found, "one")[0] == get_expected(one, "one")
|
||||
assert cprop(concept_found, "one")[1] == get_expected(one, "un")
|
||||
assert cprop(concept_found, "one")[2] == get_expected(one, "uno")
|
||||
assert cprop(concept_found, "seq")[0] == DoNotResolve("one ok")
|
||||
assert cprop(concept_found, "seq")[1] == DoNotResolve("un ok")
|
||||
assert cprop(concept_found, "seq")[2] == DoNotResolve("uno ok")
|
||||
|
||||
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user