Files
Sheerka-Old/tests/evaluators/test_PrepareEvalQuestionEvaluator.py
T
kodjo 89e1f20975 Fixed #131 : Implement ExprToConditions
Fixed #130 : ArithmeticOperatorParser
Fixed #129 : python_wrapper : create_namespace
Fixed #128 : ExpressionParser: Cannot parse func(x) infixed concept 'xxx'
2021-10-13 16:06:57 +02:00

76 lines
3.8 KiB
Python

import pytest
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept, UserInputConcept
from core.concept import Concept, ConceptParts
from evaluators.PrepareEvalQuestionEvaluator import PrepareEvalQuestionEvaluator
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
r = ReturnValueConcept
class TestPrepareEvalQuestionEvaluator(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("ret_val, expected", [
(r("name", True, UserInputConcept("question(1 + 1)")), True),
(r("name", True, UserInputConcept(" question(1 + 1) ")), True),
(r("name", True, UserInputConcept("question()")), False),
(r("name", True, UserInputConcept("1+1")), False),
(r("name", True, UserInputConcept("")), False),
(r("name", True, UserInputConcept("question(")), False),
(r("name", True, UserInputConcept(")")), False),
(r("name", True, UserInputConcept([])), False),
(r("name", True, Concept("foo")), False),
(r("name", True, "not a concept"), False),
(r("name", False, UserInputConcept("question(1 + 1)")), False),
])
def test_i_can_match(self, ret_val, expected):
context = self.get_context()
assert PrepareEvalQuestionEvaluator().matches(context, ret_val) == expected
@pytest.mark.parametrize("ret_val, expected", [
(r("name", True, UserInputConcept("question(1 + 1)")), "1 + 1"),
(r("name", True, UserInputConcept(" question( 1 + 1 ) ")), "1 + 1"),
])
def test_i_can_eval(self, ret_val, expected):
context = self.get_context()
sheerka = context.sheerka
prepare_evaluator = PrepareEvalQuestionEvaluator()
prepare_evaluator.matches(context, ret_val)
res = prepare_evaluator.eval(context, ret_val)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.USER_INPUT)
assert res.body.body == expected
assert BuiltinConcepts.EVAL_QUESTION_REQUESTED in context.protected_hints
assert BuiltinConcepts.EVAL_UNTIL_SUCCESS_REQUESTED in context.protected_hints
assert BuiltinConcepts.EVAL_BODY_REQUESTED in context.protected_hints
assert BuiltinConcepts.RETURN_BODY_REQUESTED in context.protected_hints
def test_i_can_eval_when_parsing_asts(self):
sheerka, context, foo = self.init_concepts(Concept("foo", body="question(q)").def_var("q"))
parsing_prop_ctx = context.push(BuiltinConcepts.PARSE_CODE, {"concept": foo, "prop": ConceptParts.BODY})
level1_ctx = parsing_prop_ctx.push(BuiltinConcepts.TESTING, "some stuff")
level2_ctx = level1_ctx.push(BuiltinConcepts.TESTING, "some stuff") # another level for the fun
ret_val = r("name", True, UserInputConcept("question(q)"))
prepare_evaluator = PrepareEvalQuestionEvaluator()
prepare_evaluator.matches(level2_ctx, ret_val)
res = prepare_evaluator.eval(level2_ctx, ret_val)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.USER_INPUT)
assert res.body.body == "q"
assert BuiltinConcepts.EVAL_QUESTION_REQUESTED not in context.protected_hints
assert BuiltinConcepts.EVAL_UNTIL_SUCCESS_REQUESTED not in context.protected_hints
assert BuiltinConcepts.EVAL_BODY_REQUESTED not in context.protected_hints
assert BuiltinConcepts.RETURN_BODY_REQUESTED not in context.protected_hints
assert foo.get_compiled_context_hints() == {"q": [BuiltinConcepts.EVAL_QUESTION_REQUESTED,
BuiltinConcepts.EVAL_UNTIL_SUCCESS_REQUESTED,
BuiltinConcepts.EVAL_BODY_REQUESTED,
BuiltinConcepts.RETURN_BODY_REQUESTED]}