How can a predicate parent/2
be called if it is created in a test, but the predicate that calls parent/2, (ancestor/2) is outside of the test and the call to ancestor/2
is initiated by a test?
Test module Main module Test module
test -> ancestor/2 -> parent/2
Here is a complete minimal example.
The main module shows a set of predicates (contains/3
and ancestor/3
) that work if I pass the module as an argument.
The main module also shows a set of predicates that I would like to use (contains/2
and ancestor/2
) which don’t pass the module in an argument.
There are also two separate tests modules that tests these predicates.
assembly_a
test the predicates in main that pass the module as an argument and succeed.
assembly_b
test the predicates in main that do not pass the module as an argument and these fail when I would like them to succeed.
Click triangle to see minimal example
:- module(main, []).
contains(Module,Assembly,Part_type) :-
ancestor(Module,Assembly,Part_type), !.
ancestor(Module,A,B) :-
Module:parent(A,B).
ancestor(Module,A,C) :-
Module:parent(A,B),
ancestor(Module,B,C).
contains(Assembly,Part_type) :-
ancestor(Assembly,Part_type), !.
ancestor(A,B) :-
parent(A,B).
ancestor(A,C) :-
parent(A,B),
ancestor(B,C).
% ---------------------------------------------------------------------------------------------------------------------
:- begin_tests(assembly_a).
parent(bike,frame).
parent(bike,drivechain).
parent(bike,wheel).
parent(wheel,spokes).
parent(wheel,rim).
parent(wheel,hub).
parent(drivechain,crank).
parent(drivechain,pedal).
parent(drivechain,chain).
% Test: parent/2 - module verification
test(01) :-
predicate_property(parent(_,_), implementation_module(Module)),
assertion( Module == plunit_assembly_a ).
% Test: contains/3 - no error - 1 level
test(02) :-
contains(plunit_assembly_a,bike,frame).
% Test: contains/3 - no error - 2 levels
test(03) :-
contains(plunit_assembly_a,bike,crank).
% Test: contains/3 - fail
test(04,[fail]) :-
contains(plunit_assembly_a,bike,a).
:- end_tests(assembly_a).
% -----------------------------------------------------------------------------
:- begin_tests(assembly_b).
:- meta_predicate parent(2,?).
parent(bike,frame).
parent(bike,drivechain).
parent(bike,wheel).
parent(wheel,spokes).
parent(wheel,rim).
parent(wheel,hub).
parent(drivechain,crank).
parent(drivechain,pedal).
parent(drivechain,chain).
% Test: parent/2 - module verification
test(01) :-
predicate_property(parent(_,_), implementation_module(Module)),
assertion( Module == plunit_assembly_b ).
% Test: contains/3 - no error - 1 level
test(02) :-
contains(bike,frame).
% Test: contains/3 - no error - 2 levels
test(03) :-
contains(bike,crank).
% Test: contains/3 - fail
test(04,[fail]) :-
contains(bike,a).
:- end_tests(assembly_b).
I am thinking meta_predicate/1 should work as given in assembly_b
,
:- meta_predicate parent(2,?).
but it did not.
Previous related post: How can the module for a term be identified?