use common test_framework module for several test suites

This commit is contained in:
Mark Thom
2024-04-10 15:46:52 -06:00
parent 7d6109feca
commit c2657cce37
10 changed files with 105 additions and 251 deletions

View File

@@ -890,7 +890,7 @@ expand_call_goal_(UnexpandedGoals, Module, ExpandedGoals) :-
goal_expansion(UnexpandedGoals1, user, Goals),
( predicate_property(Module:Goals, meta_predicate(MetaSpecs0)),
MetaSpecs0 =.. [_ | MetaSpecs] ->
expand_module_names(Goals, MetaSpecs, Module, ExpandedGoals, HeadVars, TGs)
expand_module_names(Goals, MetaSpecs, Module, ExpandedGoals, [], [])
; ExpandedGoals = Goals
)
; ExpandedGoals = UnexpandedGoals1

View File

@@ -1,5 +1,7 @@
/**/
:- module(dif_tests, []).
:- use_module(library(dcgs)).
:- use_module(library(format)).
:- use_module(library(lists)).
@@ -7,10 +9,30 @@
:- use_module(library(iso_ext)).
:- use_module(library(dif)).
:- use_module(test_framework).
assert_p(A, B) :-
phrase(portray_clause_(A), Portrayed),
phrase((B, ".\n"), Portrayed).
:- meta_predicate call_residual_goals(0, ?).
call_residual_goals(Goal, ResidualGoals) :-
call_residue_vars(Goal, Vars),
variables_residual_goals(Vars, ResidualGoals).
variables_residual_goals(Vars, Goals) :-
phrase(variables_residual_goals(Vars), Goals).
variables_residual_goals([]) --> [].
variables_residual_goals([Var|Vars]) -->
dif:attribute_goals(Var),
variables_residual_goals(Vars).
% Tests from https://www.complang.tuwien.ac.at/ulrich/iso-prolog/dif
test("dif#1",(
call_residual_goals(dif(1,2), Res),
call_residual_goals(dif_tests:dif(1,2), Res),
Res = []
)).
@@ -19,7 +41,7 @@ test("dif#2",(
)).
test("dif#3",(
call_residual_goals((dif(1,Y), Y=2), Res),
call_residual_goals(dif_tests:(dif(1,Y), Y=2), Res),
Y == 2,
Res = []
)).
@@ -47,40 +69,40 @@ test("dif#8",(
% I don't understand exactly what is expected for dif#9 and dif#10
test("dif#11",(
call_residual_goals((X=Y, dif(X-Y,1-2)), Res),
call_residual_goals(dif_tests:(X=Y, dif(X-Y,1-2)), Res),
X == Y,
Res = []
)).
test("dif#12",(
call_residual_goals((dif(X-Y,1-2), X=Y), Res),
call_residual_goals(dif_tests:(dif(X-Y,1-2), X=Y), Res),
X == Y,
Res = []
)).
test("dif#13",(
call_residual_goals((X=Y, Y=1, dif(X-Y,1-2)), Res),
call_residual_goals(dif_tests:(X=Y, Y=1, dif(X-Y,1-2)), Res),
X == 1,
Y == 1,
Res = []
)).
test("dif#14",(
call_residual_goals((dif(X-Y,1-2), X=Y, Y=1), Res),
call_residual_goals(dif_tests:(dif(X-Y,1-2), X=Y, Y=1), Res),
X == 1,
Y == 1,
Res = []
)).
test("dif#15",(
call_residual_goals((dif(X-Y,1-2), X=Y, X=2), Res),
call_residual_goals(dif_tests:(dif(X-Y,1-2), X=Y, X=2), Res),
X == 2,
Y == 2,
Res = []
)).
test("dif#16",(
call_residual_goals((dif(A-C,B-D), C-D=z-z, A-B=1-2), Res),
call_residual_goals(dif_tests:(dif(A-C,B-D), C-D=z-z, A-B=1-2), Res),
A == 1,
B == 2,
C == z,
@@ -89,7 +111,7 @@ test("dif#16",(
)).
test("dif#17",(
call_residual_goals((A-B=1-2, C-D=z-z, dif(A-C,B-D)), Res),
call_residual_goals(dif_tests:(A-B=1-2, C-D=z-z, dif(A-C,B-D)), Res),
A == 1,
B == 2,
C == z,
@@ -98,32 +120,32 @@ test("dif#17",(
)).
test("dif#18",(
call_residual_goals((dif(A,[C|B]), A=[[]|_], A=[B]), Res),
call_residual_goals(dif_tests:(dif(A,[C|B]), A=[[]|_], A=[B]), Res),
A == [[]],
B == [],
Res = [dif:dif([[]], [C])]
)).
test("dif#19",(
call_residual_goals((dif([E],[/]), E=1), Res),
call_residual_goals(dif_tests:(dif([E],[/]), E=1), Res),
E == 1,
Res = []
)).
test("dif#20",(
call_residual_goals((dif([a],B), B=[_|_], B=[b]), Res),
call_residual_goals(dif_tests:(dif([a],B), B=[_|_], B=[b]), Res),
B == [b],
Res = []
)).
test("dif#21",(
call_residual_goals((dif([],A), A = [_]), Res),
call_residual_goals(dif_tests:(dif([],A), A = [_]), Res),
A = [_],
Res = []
)).
test("dif#22",(
call_residual_goals((A = [_], dif([],A)), Res),
call_residual_goals(dif_tests:(A = [_], dif([],A)), Res),
A = [_],
Res = []
)).
@@ -140,7 +162,7 @@ test("dif#t2",(
test("dif#t3",(
set_prolog_flag(occurs_check, false),
call_residual_goals((-X=X, dif(X,1)), Res),
call_residual_goals(dif_tests:(-X=X, dif(X,1)), Res),
X == -X,
Res = []
)).
@@ -172,26 +194,26 @@ test("dif#o1",(
test("dif#o2",(
set_prolog_flag(occurs_check, true),
call_residual_goals((dif(-X,X)), Res),
call_residual_goals(dif_tests:(dif(-X,X)), Res),
Res = []
)).
test("dif#o3",(
set_prolog_flag(occurs_check, true),
call_residual_goals((dif(-X,Y), X=Y), Res),
call_residual_goals(dif_tests:(dif(-X,Y), X=Y), Res),
X == Y,
Res = []
)).
test("dif#12 but with multiple variables in the residuals",(
call_residual_goals((dif(X-Y-_, 1-2-3), X = Y), Res),
call_residual_goals(dif_tests:(dif(X-Y-_, 1-2-3), X = Y), Res),
X == Y,
Res = []
)).
% https://github.com/mthom/scryer-prolog/issues/1956
test("scryer-prolog#1956",(
call_residue_vars((dif(a-a,X-_),X=b), Res),
call_residue_vars(dif_tests:(dif(a-a,X-_),X=b), Res),
X == b,
Res = []
)).
@@ -213,65 +235,3 @@ test("scryer-prolog#2175",(
A=_C*[],
A=[]*D*B,D=[]
)).
main :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests(Tests, Failed),
show_failed(Failed),
halt.
main_quiet :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests_quiet(Tests, Failed),
( Failed = [] ->
format("All tests passed", [])
; format("Some tests failed", [])
),
halt.
portray_failed_([]) --> [].
portray_failed_([F|Fs]) -->
"\"", F, "\"", "\n", portray_failed_(Fs).
portray_failed([]) --> [].
portray_failed([F|Fs]) -->
"\n", "Failed tests:", "\n", portray_failed_([F|Fs]).
show_failed(Failed) :-
phrase(portray_failed(Failed), F),
format("~s", [F]).
run_tests([], []).
run_tests([test(Name, Goal)|Tests], Failed) :-
format("Running test \"~s\"~n", [Name]),
( call(Goal) ->
Failed = Failed1
; format("Failed test \"~s\"~n", [Name]),
Failed = [Name|Failed1]
),
run_tests(Tests, Failed1).
run_tests_quiet([], []).
run_tests_quiet([test(Name, Goal)|Tests], Failed) :-
( call(Goal) ->
Failed = Failed1
; Failed = [Name|Failed1]
),
run_tests_quiet(Tests, Failed1).
assert_p(A, B) :-
phrase(portray_clause_(A), Portrayed),
phrase((B, ".\n"), Portrayed).
call_residual_goals(Goal, ResidualGoals) :-
call_residue_vars(Goal, Vars),
variables_residual_goals(Vars, ResidualGoals).
variables_residual_goals(Vars, Goals) :-
phrase(variables_residual_goals(Vars), Goals).
variables_residual_goals([]) --> [].
variables_residual_goals([Var|Vars]) -->
dif:attribute_goals(Var),
variables_residual_goals(Vars).

View File

@@ -1,11 +1,11 @@
/**/
:- use_module(library(format)).
:- use_module(library(dcgs)).
:- use_module(library(lists)).
:- use_module(library(debug)).
:- module(ground_tests, []).
:- use_module(library(atts)).
:- use_module(test_framework).
:- attribute a/1.
a(Var) :- put_atts(Var, +a(hello)).
@@ -36,48 +36,3 @@ test("ground#2075",(
_=_B*_,_D=_B*_A,_B=_B*_D,\+ ground(_B),
A=[A|B],B=A*B,ground(A)
)).
main :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests(Tests, Failed),
show_failed(Failed),
halt.
main_quiet :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests_quiet(Tests, Failed),
( Failed = [] ->
format("All tests passed", [])
; format("Some tests failed", [])
),
halt.
run_tests([], []).
run_tests([test(Name, Goal)|Tests], Failed) :-
format("Running test \"~s\"~n", [Name]),
( call(Goal) ->
Failed = Failed1
; format("Failed test \"~s\"~n", [Name]),
Failed = [Name|Failed1]
),
run_tests(Tests, Failed1).
run_tests_quiet([], []).
run_tests_quiet([test(Name, Goal)|Tests], Failed) :-
( call(Goal) ->
Failed = Failed1
; Failed = [Name|Failed1]
),
run_tests_quiet(Tests, Failed1).
portray_failed_([]) --> [].
portray_failed_([F|Fs]) -->
"\"", F, "\"", "\n", portray_failed_(Fs).
portray_failed([]) --> [].
portray_failed([F|Fs]) -->
"\n", "Failed tests:", "\n", portray_failed_([F|Fs]).
show_failed(Failed) :-
phrase(portray_failed(Failed), F),
format("~s", [F]).

View File

@@ -1,9 +1,8 @@
/**/
:- use_module(library(format)).
:- use_module(library(dcgs)).
:- use_module(library(lists)).
:- use_module(library(debug)).
:- module(term_variables_tests, []).
:- use_module(test_framework).
test("term_variables#1400", (
term_variables(A+B*C/B-D, Vars),
@@ -70,48 +69,3 @@ termt3(T) :-
T4 = [A|_],
T3 = A,
T0 = A.
main :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests(Tests, Failed),
show_failed(Failed),
halt.
main_quiet :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests_quiet(Tests, Failed),
( Failed = [] ->
format("All tests passed", [])
; format("Some tests failed", [])
),
halt.
run_tests([], []).
run_tests([test(Name, Goal)|Tests], Failed) :-
format("Running test \"~s\"~n", [Name]),
( call(Goal) ->
Failed = Failed1
; format("Failed test \"~s\"~n", [Name]),
Failed = [Name|Failed1]
),
run_tests(Tests, Failed1).
run_tests_quiet([], []).
run_tests_quiet([test(Name, Goal)|Tests], Failed) :-
( call(Goal) ->
Failed = Failed1
; Failed = [Name|Failed1]
),
run_tests_quiet(Tests, Failed1).
portray_failed_([]) --> [].
portray_failed_([F|Fs]) -->
"\"", F, "\"", "\n", portray_failed_(Fs).
portray_failed([]) --> [].
portray_failed([F|Fs]) -->
"\n", "Failed tests:", "\n", portray_failed_([F|Fs]).
show_failed(Failed) :-
phrase(portray_failed(Failed), F),
format("~s", [F]).

View File

@@ -0,0 +1,49 @@
:- module(test_framework, [main/1, main_quiet/1]).
:- use_module(library(dcgs)).
:- use_module(library(format)).
main(TestModule) :-
findall(test(Name, TestModule:Goal), TestModule:test(Name, Goal), Tests),
run_tests(Tests, Failed),
show_failed(Failed),
halt.
main_quiet(TestModule) :-
findall(test(Name, TestModule:Goal), TestModule:test(Name, Goal), Tests),
run_tests_quiet(Tests, Failed),
( Failed = [] ->
format("All tests passed", [])
; format("Some tests failed", [])
),
halt.
portray_failed_([]) --> [].
portray_failed_([F|Fs]) -->
"\"", F, "\"", "\n", portray_failed_(Fs).
portray_failed([]) --> [].
portray_failed([F|Fs]) -->
"\n", "Failed tests:", "\n", portray_failed_([F|Fs]).
show_failed(Failed) :-
phrase(portray_failed(Failed), F),
format("~s", [F]).
run_tests([], []).
run_tests([test(Name, Goal)|Tests], Failed) :-
format("Running test \"~s\"~n", [Name]),
( call(Goal) ->
Failed = Failed1
; format("Failed test \"~s\"~n", [Name]),
Failed = [Name|Failed1]
),
run_tests(Tests, Failed1).
run_tests_quiet([], []).
run_tests_quiet([test(Name, Goal)|Tests], Failed) :-
( call(Goal) ->
Failed = Failed1
; Failed = [Name|Failed1]
),
run_tests_quiet(Tests, Failed1).

View File

@@ -1,13 +1,11 @@
/**/
:- use_module(library(iso_ext)).
:- use_module(library(format)).
:- use_module(library(dcgs)).
:- use_module(library(lists)).
:- use_module(library(debug)).
:- module(when_tests, []).
:- use_module(library(when)).
:- use_module(test_framework).
test("condition true before ground/1",(
A = 1,
when(ground(A), Run = true),
@@ -81,65 +79,3 @@ test("multiple when/2 on same variable",(
B = 1,
Run2 == true
)).
main :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests(Tests, Failed),
show_failed(Failed),
halt.
main_quiet :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests_quiet(Tests, Failed),
( Failed = [] ->
format("All tests passed", [])
; format("Some tests failed", [])
),
halt.
portray_failed_([]) --> [].
portray_failed_([F|Fs]) -->
"\"", F, "\"", "\n", portray_failed_(Fs).
portray_failed([]) --> [].
portray_failed([F|Fs]) -->
"\n", "Failed tests:", "\n", portray_failed_([F|Fs]).
show_failed(Failed) :-
phrase(portray_failed(Failed), F),
format("~s", [F]).
run_tests([], []).
run_tests([test(Name, Goal)|Tests], Failed) :-
format("Running test \"~s\"~n", [Name]),
( call(Goal) ->
Failed = Failed1
; format("Failed test \"~s\"~n", [Name]),
Failed = [Name|Failed1]
),
run_tests(Tests, Failed1).
run_tests_quiet([], []).
run_tests_quiet([test(Name, Goal)|Tests], Failed) :-
( call(Goal) ->
Failed = Failed1
; Failed = [Name|Failed1]
),
run_tests_quiet(Tests, Failed1).
assert_p(A, B) :-
phrase(portray_clause_(A), Portrayed),
phrase((B, ".\n"), Portrayed).
call_residual_goals(Goal, ResidualGoals) :-
call_residue_vars(Goal, Vars),
variables_residual_goals(Vars, ResidualGoals).
variables_residual_goals(Vars, Goals) :-
phrase(variables_residual_goals(Vars), Goals).
variables_residual_goals([]) --> [].
variables_residual_goals([Var|Vars]) -->
dif_:attribute_goals(Var),
variables_residual_goals(Vars).

View File

@@ -1 +1 @@
args = ["-f", "--no-add-history", "src/tests/dif.pl", "-f", "-g", "main_quiet"]
args = ["-f", "--no-add-history", "src/tests/dif.pl", "-f", "-g", "use_module(library(dif_tests)), dif_tests:main_quiet(dif_tests)"]

View File

@@ -1 +1 @@
args = ["-f", "--no-add-history", "src/tests/ground.pl", "-f", "-g", "main_quiet"]
args = ["-f", "--no-add-history", "src/tests/ground.pl", "-f", "-g", "use_module(library(ground_tests)), ground_tests:main_quiet(ground_tests)"]

View File

@@ -1 +1 @@
args = ["-f", "--no-add-history", "src/tests/term_variables.pl", "-f", "-g", "main_quiet"]
args = ["-f", "--no-add-history", "src/tests/term_variables.pl", "-f", "-g", "use_module(library(term_variables_tests)),term_variables_tests:main_quiet(term_variables_tests)"]

View File

@@ -1 +1 @@
args = ["-f", "--no-add-history", "src/tests/when.pl", "-f", "-g", "main_quiet"]
args = ["-f", "--no-add-history", "src/tests/when.pl", "-f", "-g", "use_module(library(when_tests)), when_tests:main_quiet(when_tests)"]