From 2474b081506a4080c7ddf9217c87e70e408762e1 Mon Sep 17 00:00:00 2001 From: Kodjo Sossouvi Date: Sat, 21 Dec 2019 16:15:54 +0100 Subject: [PATCH] Updated sheerka.value() and added unit tests --- core/builtin_concepts.py | 23 ----------------------- core/sheerka.py | 9 ++++++--- tests/test_sheerka.py | 9 ++++++++- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/core/builtin_concepts.py b/core/builtin_concepts.py index ee4d434..1abe77c 100644 --- a/core/builtin_concepts.py +++ b/core/builtin_concepts.py @@ -45,7 +45,6 @@ class BuiltinConcepts(Enum): ENUMERATION = "enum" # represents a list or a set LIST = "list" # represents a list CANNOT_RESOLVE_VALUE_ERROR = "value cannot be resolved" # don't know how to find concept value - CONCEPT_NOT_INITIALIZED = "concept not evaluated" # Try to work on a concept that is not evaluated NODE = "node" GENERIC_NODE = "generic node" @@ -69,7 +68,6 @@ BuiltinErrors = [str(e) for e in { BuiltinConcepts.CONCEPT_ALREADY_DEFINED, BuiltinConcepts.CONCEPT_EVAL_ERROR, BuiltinConcepts.CANNOT_RESOLVE_VALUE_ERROR, - BuiltinConcepts.CONCEPT_NOT_INITIALIZED }] """ @@ -293,27 +291,6 @@ class ConceptEvalError(Concept): return self.props["property_name"].value -class ConceptNotInitialized(Concept): - def __init__(self, error=None, concept=None, property_name=None): - super().__init__(BuiltinConcepts.CONCEPT_NOT_INITIALIZED, - True, - False, - BuiltinConcepts.CONCEPT_NOT_INITIALIZED, - error) - self.set_prop("concept", concept) - - def __repr__(self): - return f"ConceptNotInitialized(error={self.error}, concept={self.concept})" - - @property - def error(self): - return self.body - - @property - def concept(self): - return self.props["concept"].value - - class EnumerationConcept(Concept): def __init__(self, iteration=None): super().__init__(BuiltinConcepts.ENUMERATION, True, False, BuiltinConcepts.ENUMERATION, iteration) diff --git a/core/sheerka.py b/core/sheerka.py index 20f9381..572d53e 100644 --- a/core/sheerka.py +++ b/core/sheerka.py @@ -546,7 +546,7 @@ class Sheerka(Concept): return from_db # else return new Unknown concept - # Note that I don't call the new() method, as it use get() -> cyclic call + # Note that I don't call the new() method to prevent cyclic call unknown_concept = Concept() template = self.concepts_cache[str(BuiltinConcepts.UNKNOWN_CONCEPT)] unknown_concept.update_from(template) @@ -623,7 +623,7 @@ class Sheerka(Concept): if self.isinstance(obj, BuiltinConcepts.RETURN_VALUE) and \ obj.status and \ self.isinstance(obj.value, BuiltinConcepts.USER_INPUT): - return obj.value.text + return obj.value.body if not isinstance(obj, Concept): return obj @@ -632,7 +632,10 @@ class Sheerka(Concept): return obj.get_value() if obj.body is not None: - return obj.body + if (isinstance(obj.body, list) or isinstance(obj.body, set)) and len(obj.body) == 1: + return obj.body[0] + else: + return obj.body return obj if allow_none_body else self.new(BuiltinConcepts.CANNOT_RESOLVE_VALUE_ERROR, body=obj) diff --git a/tests/test_sheerka.py b/tests/test_sheerka.py index f8e9528..b6b8807 100644 --- a/tests/test_sheerka.py +++ b/tests/test_sheerka.py @@ -3,7 +3,7 @@ import os from os import path import shutil -from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept +from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept, UserInputConcept from core.concept import Concept, PROPERTIES_TO_SERIALIZE, Property from core.sheerka import Sheerka, ExecutionContext from evaluators.MutipleSameSuccessEvaluator import MultipleSameSuccessEvaluator @@ -320,9 +320,16 @@ def test_i_cannot_instantiate_when_properties_are_not_recognized(): @pytest.mark.parametrize("concept, allow_non_body, expected", [ (None, False, None), (3.14, False, 3.14), + ("foo", False, "foo"), + (True, False, True), (Concept("name", body="foo"), False, "foo"), (Concept("name"), True, Concept("name")), (ConceptWithGetValue("name").set_prop("my_prop", "my_value"), True, "my_value"), + (ReturnValueConcept(value="return_value"), False, "return_value"), + (ReturnValueConcept(value=Concept(key=BuiltinConcepts.USER_INPUT, body="text"), status=True), False, "text"), + (ReturnValueConcept(value=UserInputConcept("text"), status=True), False, "text"), + (Concept("name", body=["foo", "bar"]), False, ["foo", "bar"]), + (Concept("name", body=["foo"]), False, "foo"), ]) def test_i_can_get_value(concept, allow_non_body, expected): sheerka = get_sheerka()