Forall unit test generation: examples?

Can’t find any examples of this being used in the head as the options list. throws is working fine but I am stuck… actually I don’t think I have ever used forall as a generator at all ever anyway.

Just need one good example, my test, which I want to refactor, looks like this:

% parses a good definition
%
%  when I come back!
%     forall generator, have a tk s1 and s2 variations
%
% [tk(10,`ERIC`), s1(10,`ERIC`), s2(10,`ERIC`)]
test(good_definition_token) :-
    phrase(
        ast:s_defvar(Out),
        [op(0), defvar(1), tk(2,`name`), tk(10,`ERIC`), cp(20)]),
    assertion(Out == defvar(`name`,tk(10,`ERIC`)))
    ,
    phrase(
        ast:s_defvar(Out),
        [op(0), defvar(1), tk(2,`name`), s1(10,`ERIC`), cp(20)]),
    assertion(Out == defvar(`name`,s1(10,`ERIC`)))
    ,
    phrase(
        ast:s_defvar(Out),
        [op(0), defvar(1), tk(2,`name`), s2(10,`ERIC`), cp(20)]),
    assertion(Out == defvar(`name`,s2(10,`ERIC`))).

I think I write one of those every day.

See: UTF-8 encode and decode

Also see: Unit testing and using assertion/1


If you search here for begin_tests you will find many example test cases and often mine will use forall.

@EricGT thank you very much…I have now managed to get a solution from your code and I continue to read, learn and be amazed.

Thanks,
Sean

For the record,

test(good_def_all_rhs_types,
     [forall(member(RHS, [tk(10,`ERIC`), s1(10,`ERIC`), s2(10,`ERIC`)]))
     ]) :-
    phrase(
        ast:s_defvar(Out),
        [op(0), defvar(1), tk(2,`name`), RHS, cp(20)]),
    assertion(Out == defvar(`name`, RHS)).

…as neat and tody and concise as I hoped. Thanks again @EricGT

Actually, I can reduce that to just the functor! Love Prolog! :smiley:

test(good_def_all_rhs_types,
     [forall(member(RHS, [tk(10,`ERIC`), s1(10,`ERIC`), s2(10,`ERIC`)]))
     ])

I would have done it like

good_def_all_rhs_types_test_case(tk(10,`ERIC`)).
good_def_all_rhs_types_test_case(s1(10,`ERIC`)).
good_def_all_rhs_types_test_case(s2(10,`ERIC`)).

test(good_def_all_rhs_types,
     [forall(good_def_all_rhs_types_test_case(RHS))]) :-

where each test case is a separate fact and forall pulls out one of the facts for each test.

Note: I did not run this but typed it in here so it may have a typo.

I considered that based on your code but personally I prefer it wrapped up… should it ever be needed for more than this test than that is the road I shall take.
Here’s what I ended up with

test(good_def_all_rhs_types, [forall(member(Type, [tk, s1, s2]))]) :-
    Term =.. [Type, 10, `ERIC`],
    phrase(
        ast:s_defvar(Out),
        [op(0), defvar(1), tk(2,`name`), Term, cp(20)]),
    assertion(Out == defvar(`name`, Term)).

Thanks again.

1 Like