def test__eq__(): assert Variable("A") == Variable("A") assert Variable("A") != Variable("B") with pytest.raises(TypeError) as error: Variable("A") == "A" assert str(error.value) == "A must be a Variable"
def test_02(): """ P02 (*): Find the last but one element of a list last_but_one(X,L) :- X is the last but one element of the list L (element,list) (?,?) last_but_one(X,[X,_]). last_but_one(X,[_,Y|Ys]) :- last_but_one(X,[Y|Ys]). """ Ys = Variable("Ys") db = [ dict(last_but_one=X, list=(X, _W)), Rule( dict(last_but_one=X, list=(_W, Y, *Ys)), dict(last_but_one=X, list=(Y, *Ys)), ), ] query = dict(last_but_one=Q, list=["a", "b", "c"]) assert next(search(db, query)) == {Q: "b"}
def test__init__error(): with pytest.raises(ValueError) as error: Variable(1) assert str(error.value) == "name must be a string"
def test_new_frame(): assert Variable("A", 3).new_frame(4) == Variable("A", 4)
def test__repr__(name, frame, message): assert repr(Variable(name, frame)) == message
def test_factory(): assert Variable.factory("A", "B") == [Variable("A"), Variable("B")]
import pytest from inference_logic import Rule, Variable from inference_logic.algorithms import search from inference_logic.data_structures import Assert, Assign, PrologList X, Y, Z = Variable.factory("X", "Y", "Z") L, _W = Variable.factory("L", "W") Q = Variable("Q") def test_01(): """ P01 (*): Find the last element of a list my_last(X,L) :- X is the last element of the list L (element,list) (?,?) Note: last(?Elem, ?List) is predefined my_last(X,[X]). my_last(X,[_|L]) :- my_last(X,L). """ db = [ dict(last=X, list=[X]), Rule(dict(last=X, list=[_W, *L]), dict(last=X, list=L)), ] query = dict(last=Q, list=["a", "b", "c"]) assert list(search(db, query)) == [{Q: "c"}]
import pytest from inference_logic import Rule, Variable from inference_logic.data_structures import construct, new_frame A, B = Variable.factory("A", "B") A_1 = Variable("A", 1) B_1 = Variable("B", 1) @pytest.mark.parametrize( "initial, final", [ (True, True), (A, Variable("A", 1)), ((A, True), (A_1, True)), (dict(a=A), dict(a=A_1)), ], ) def test_new_frame_function(initial, final): assert new_frame(construct(initial), 1) == construct(final) @pytest.mark.parametrize( "initial, final", [(Rule(dict(a=A), dict(b=B)), Rule(dict(a=A_1), dict(b=B_1)))], ) def test_new_frame_method(initial, final): assert construct(initial).new_frame(1) == construct(final)