79 lines
3.0 KiB
Python
79 lines
3.0 KiB
Python
import pytest
|
|
|
|
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
|
|
from core.concept import Concept
|
|
from evaluators.EvalEvaluator import EvalEvaluator
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
def retval(obj, who="who", status=True):
|
|
"""ret_val"""
|
|
return ReturnValueConcept(who, status, obj)
|
|
|
|
|
|
class TestEvalEvaluator(TestUsingMemoryBasedSheerka):
|
|
def test_i_can_match_and_eval(self):
|
|
context = self.get_context()
|
|
context.global_hints.add(BuiltinConcepts.CONCEPT_VALUE_REQUESTED)
|
|
|
|
to_eval1 = ReturnValueConcept("some_name", True, Concept(name="2", body="to eval").auto_init())
|
|
to_eval2 = ReturnValueConcept("some_name", True, Concept(name="3", body="also to eval").auto_init())
|
|
|
|
return_values = [
|
|
ReturnValueConcept("some_name", True, "not to eval"),
|
|
ReturnValueConcept("some_name", True, Concept(name="not to eval")),
|
|
ReturnValueConcept("some_name", False, Concept(name="1", body="'not to eval'")),
|
|
to_eval1,
|
|
to_eval2,
|
|
]
|
|
|
|
evaluator = EvalEvaluator()
|
|
assert evaluator.matches(context, return_values)
|
|
|
|
evaluated = evaluator.eval(context, return_values)
|
|
assert len(evaluated) == 2
|
|
assert evaluated[0].value == to_eval1.body.body
|
|
assert evaluated[0].parents == [to_eval1]
|
|
|
|
assert evaluated[1].value == to_eval2.body.body
|
|
assert evaluated[1].parents == [to_eval2]
|
|
|
|
@pytest.mark.parametrize("return_value", [
|
|
retval(Concept("foo", body="bar")),
|
|
retval(Concept("status is false", body="bar"), status=False),
|
|
retval("string_value"),
|
|
retval(Concept("no body")),
|
|
])
|
|
def test_i_cannot_match_if_eval_request_is_not_present(self, return_value):
|
|
context = self.get_context()
|
|
assert not EvalEvaluator().matches(context, [return_value])
|
|
|
|
context.global_hints.add(BuiltinConcepts.CONCEPT_VALUE_REQUESTED)
|
|
assert EvalEvaluator().matches(context, [return_value])
|
|
|
|
def test_concept_eval_requested_is_reduced_when_nothing_to_reduce(self):
|
|
context = self.get_context()
|
|
|
|
return_values = [
|
|
ReturnValueConcept("some_name", True, "not to eval"), # not a concept
|
|
ReturnValueConcept("some_name", True, Concept(name="not to eval")), # no body
|
|
ReturnValueConcept("some_name", False, Concept(name="1", body="not to eval")), # no evaluated body
|
|
]
|
|
|
|
evaluated = EvalEvaluator().eval(context, return_values)
|
|
assert evaluated is None
|
|
|
|
def test_i_can_evaluate_sets(self):
|
|
sheerka, context, foo, bar, baz, number = self.init_concepts(
|
|
Concept("foo"),
|
|
Concept("bar"),
|
|
Concept("baz"),
|
|
Concept("number"))
|
|
sheerka.sets_handler.add_concepts_to_set(context, [foo, bar, baz], number)
|
|
|
|
evaluated = EvalEvaluator().eval(context, [retval(number)])
|
|
|
|
assert len(evaluated) == 1
|
|
assert evaluated[0].status
|
|
assert set(evaluated[0].body) == {foo, bar, baz}
|