Re: Predicate searching by wildcard or similar

Daniel Diaz <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
Hi Sean,

you can use current_predicate to get the Name/Arity of present
predicates and then test the Name.

For instance, to get a predicate starting with 'test_' you can use
the following (reexecutable by backtracking) :

| ?- current_predicate(Name/_Arity), atom_concat('test_', _,
Name).

Name = test_blabla1 ;

Name = test_blabla2 ;

...

You can then collect them in a list L with:

| ?- findall(Name, (current_predicate(Name/_Arity),
atom_concat('test_', _, Name)), L).

L = [test_blabla1,test_blabla2,...]

To check if the name ends by '_test' simply use atom_concat(_,
'_test', Name).

If you want to only collect predicates without arguments simply
replace _Arity by 0.

Daniel

Le 05/11/2013 22:27, Sean Charles a écrit :

Hi,

I just wrote a *really simple* testing framework for my
project, it looks like this at the test script end:

test_package([it('should
ensure that global values have expected settings',
defaults_correctly_set_test)

,it('should correctly set the quiet flag on "-q"',
respect_quiet('-q'))

,it('should correctly set the quiet flag on "--quiet"',
respect_quiet('--quiet'))

,it('should correctly set the wrap flag on "--wrap"',
respect_wrap)

,it('should correctly set the check flag on "--nocheck"',
respect_check)

,it('should add unhandled options as source filenames',
filename_check)

,it('should throw exceptions on unknown options',
handle_unknown_options)

]).

The test_package predicate is called from the framework by
the script, the script pulls in the file and that has an
initialisation instruction:

:-
initialization(run_tests).

run_tests
:-

test_package(AllTests),

maplist(call,
AllTests),

ink(normal,
'*done*'),

stop.

What would have made it *really* nice was to have been able
to find all predicates starting with test_ or ending with _test
etc. so that I would not have needed to make the test_package
predicate unify the variable with the list of tests to be run.
Some tests mentioned above...

defaults_correctly_set_test
:-

cl_set_defaults,

get_all_globals([],[],user_input,user_output,php,nowrap,check,plain,noisy).

respect_quiet(Flag)
:-

cl_set_defaults,

process_option(Flag),

get_all_globals(_,_,_,_,_,_,_,_,quiet).

As you can see, having to enter the test predicate AND enter
it in the test package isn’t ideal ALTHOUGH it does allow me to
provide a nice label but I could have done that with a really
long predicate name anyway.

So, how would I do that in GNU Prolog…if it is possible. The
listing() predicate is not much help in this instance…

Thanks,

Sean.

--

Ce message a été vérifié par
MailScanner
pour des virus ou des polluriels et rien de
suspect n'a été trouvé.

_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog

--

Ce message a été vérifié par
MailScanner
pour des virus ou des polluriels et rien de
suspect n'a été trouvé.

_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.