Files
Sheerka/tests/core/test_concept.py
T

105 lines
3.4 KiB
Python

from common.global_symbols import NotFound, NotInit
from core.concept import ConceptDefaultProps
from helpers import GetNextId, get_concept
def test_i_can_retrieve_concept_properties():
foo = get_concept("a plus b", "a + b", variables=("a", "b"), id="1001")
assert foo.name == "a plus b"
assert foo.id == "1001"
assert foo.str_id == "c:#1001:"
assert foo.all_attrs() == ('#where#', '#pre#', '#post#', '#body#', '#ret#', 'a', 'b')
assert foo.get_definition_digest() == "13b61f45934a802b5486a1bdd60e404b32378a801408769cd584e3b3b7518cc2"
# sanity check to make sure that 'get_concept' works as expected
assert foo.get_metadata().variables == (("a", NotInit), ("b", NotInit))
def test_i_can_set_and_get_value():
foo = get_concept("foo", variables=["a"])
foo.set_value("a", "some value")
assert foo.get_value("a") == "some value"
assert foo.a == "some value"
def test_i_can_set_and_get_value_from_bound_attr():
foo = get_concept("foo", variables=["a"], bound_body="a")
foo.set_value("a", "some value")
assert foo.get_value(ConceptDefaultProps.BODY) == "some value"
foo.set_value(ConceptDefaultProps.BODY, "another value")
assert foo.get_value("a") == "another value"
def test_i_can_test_concept_equality():
foo1 = get_concept("foo", "a + b", variables=["a", "b"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a", "b"], id=6)
foo1.set_value("a", 10).set_value("b", 20)
foo2.set_value("a", 10).set_value("b", 20)
assert foo1 == foo2
def test_i_can_detect_when_concepts_are_not_equal():
foo1 = get_concept("foo", "a + b", variables=["a", "b"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a", "b"], id=6)
foo1.set_value("a", 10).set_value("b", 20)
foo2.set_value("a", 10).set_value("b", 25)
assert foo1 != foo2
def test_i_can_test_concept_equality_in_case_of_infinite_recursion():
foo1 = get_concept("foo", "a + b", variables=["a"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a"], id=6)
# foo1 and foo2 are equals
assert foo1 == foo2
foo1.set_value("a", foo1)
foo2.set_value("a", foo2)
assert foo1 == foo2
foo1.set_value("a", foo2)
foo2.set_value("a", foo1)
assert foo1 == foo2
def test_i_can_test_concept_equality_in_case_of_infinite_recursion_with_more_than_two_concepts():
foo1 = get_concept("foo", "a + b", variables=["a"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a"], id=6)
foo3 = get_concept("foo", "a + b", variables=["a"], id=7)
foo1.set_value("a", foo2)
foo2.set_value("a", foo3)
foo3.set_value("a", foo1)
assert foo1 == foo2
foo1.set_value("a", foo2)
foo2.set_value("a", foo3)
foo3.set_value("a", foo3)
assert foo1 == foo2
def test_i_cannot_get_an_attribute_which_is_not_defined():
next_id = GetNextId()
foo = get_concept("add a b", definition="add", variables=["a", "b"], sequence=next_id)
assert foo.get_value("a") is NotInit
assert foo.get_value("b") is NotInit
assert foo.get_value("c") is NotFound
def test_i_can_repr_a_concept():
next_id = GetNextId()
foo = get_concept("foo", sequence=next_id)
assert repr(foo) == "(Concept foo#1001)"
bar = get_concept("bar", pre="is an int", sequence=next_id)
assert repr(bar) == "(Concept bar#1002, #pre=is an int)"
baz = get_concept("baz", definition="add a b", variables=["a", "b"], sequence=next_id)
assert repr(baz) == "(Concept baz#1003, a=**NotInit**, b=**NotInit**)"