moduleprologswi-prologplunit

plunit test apparently creates a module behind the scenes ... but can that module be unloaded?


I want to use plunit as implemented in SWI Prolog to run a few unit tests in classy style!

So I want to enter them directly on the terminal:

[user].

and then copy paste:

:- begin_tests(exercise).

test(foo) :- bar.

:- end_tests(exercise).

This works perfectly.

... but if I load the test via [user]. again -- there is a Westworld-like experience:

?- [user].
|: :- begin_tests(exercise).

ERROR: user://2:17:
ERROR:    module/2: No permission to redefine module `plunit_exercise' (Already loaded from user://1)
Warning: user://2:17:
Warning:    Goal (directive) failed: user:begin_tests(exercise)

It seems that the implementation creates a Module plunit_X for test X. That makes sense.

But is there a way to unload the test module exercise?

There must be...

Note that you can unload a file with unload_file/1 but not a Module?

Update:

Instead of entering the code using [user]. and using a [file] makes it work. Hmm.


Solution

  • Since I don't know how to reproduce your problem you will have to test this for yourself.

    Use destroy_module/1

    Since it is not exported from the module modules my guess is that modules:destroy_module(X) should work.

    Since it is not an exported predicate and not documented Caveat emptor


    EDIT

    Instead of entering the code using [user]. and using a [file] makes it work. Hmm.

    user is a predefined module, file is not AFAIK.

    ?- current_module(X).
    X = prolog ;
    X = sysetm ;
    X = user ;
    X = predicate_options ;
    X = base32 ;
    X = read_util ;
    X = win_menu ;
    X = shlib ;
    X = qsave ;
    X = prolog_clause ;
    X = prolog_history ;
    X = pce_swi_hooks ;
    X = prolog_stack ;
    X = system ;
    X = ansi_term ;
    X = link_xpce ;
    false.