Term expansion within plunit tests

In working on a patch for ffi, I am running into the following problem using term_expansion/2 within plunit tests:

:- module(a,[]).


system:term_expansion(T, (H :- R=4) ) :-
   nonvar(T),
   % Simply expand terms of the form texp(R)
   T = (H  :- texp(R)).

t(R) :-
    texp(R).

:- begin_tests(t).

test(expansion,R=4) :-
    texp(R).

test(noexpansion,R=5) :-
   R=5.

:- end_tests(t).

Query:

1 ?- run_tests.
% PL-Unit: t . done
% test passed
true.

2 ?- listing(plunit_t:_).

'unit body'('noexpansion@line 18', vars(R)) :-
    !,
    R=5.

test(expansion, R=4) :-
    R=4.

'unit test'(noexpansion, 18, [true(A=5)], plunit_t:'unit body'('noexpansion@line 18', vars(A))).
true.

As you can see only test(noexpansion,R=5) is run. The test with the term expansion is skipped. Any workarounds or am I missing something?

Note that plunit also depends on expansion, so to make it work properly you really have to load the module. Now things get a little awkward as the first test is both expanded by your rule and plunit, so it depends on who comes first. If you do term expansion to simplify defining tests, keep them local (do not use system:). Then your rules come first and you can also generate test test rules (the XSB tabling tests do that a lot).

As far as I can tell, all works as it is supposed to.

Actually the system:term_expansion/2 is done by the ffi pack, the example above was just a reduced version to show the problem.

I found a work around by writing a do_test_expansion/1 predicate in the module and calling that within the pl_unit tests. Something like this:

:- module(a,[]).


system:term_expansion(T, (H :- R=4) ) :-
   nonvar(T),
   % Simply expand terms of the form texp(R)
   T = (H  :- texp(R)).

do_expansion_test(R) :-
    texp(R).

:- begin_tests(t).

test(expansion,R=4) :-
   do_expansion_test(R).

test(noexpansion,R=5) :-
   R=5.

:- end_tests(t).