What is the best practice to change export/import list and automatically run tests?

Normally when developing simple code there is no need to modify

  1. module/2,3 public list (think export list).
  2. use_module/2 import list.

When developing more structured code the public list and import list often need modifications. Once a modification is made to either I find that I have to run halt/0 then start a new instance of the top level so that the database is clean. After starting the top level, the typical scenario to check the updated export and/or import list is to consult the source code, which runs check/0. After the load is successful with no errors, I run the test cases which must be done manually, e.g.

Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.4-19-gefb65916d)

?- working_directory(_,'C:/Users/Groot/Documents').
true.

?- [rfc3986].
true.

?- run_tests.
% PL-Unit: rfc3986 ...... done
% PL-Unit: rfc5234 ...... done
% All 12 tests passed
true.

Questions

  1. Is running halt/0 and starting a new top level required to get a clean database? (I recall a similar question being asked but can not find it).

  2. Is there a way to get the test to run automatically after consult? In looking at set_test_options/1 for run it seems such an option for run is needed. As noted, for simple code the make option works nicely, I don’t know all of the scenarios for needing to run the test upon consulting right after starting a top level so can’t think of a name to suggest for the option, but having the option would be nice.

    I also tried adding :- run_tests. separately to the source code file and then the test file, but the sequencing was wrong. It seems the code was not fully loaded before the test were run which resulted in errors.

In most cases simply make/0 should take care of this. Typically a full restart is required if rules for goal/term expansion are modified and do not affect (only) the file they are defined in.

Yes. On POSIX systems it would be fairly trivial to make the process exec itself.

My typical solution is to have a couple of small toplevel files that perform common tasks such as loading for debugging, loading for testing, etc. You probably need to run the tests using initialization/1 (as the standard prescribes to run goals from directives anyway).

Given that a simple double click on test.pl should be enough to load the code and test it. Same for e.g. run.pl and debug.pl.

1 Like

Are these public?
Looks like I need to start writing some of these for myself. Thanks.

Yes that would be optimal, but I use Visual Studio Code on Windows as my primary editor because of all of the different file types and programming languages I bounce between.

See SWI-Prolog -- Project Special Files

I guess you can tell VScode somehow to execute a command easily, no?

1 Like

Yes, but I have not opened that can of worms either.
The only way for me to get things done is to just let certain plates fall.