Skip to content

Instantly share code, notes, and snippets.

@kamahen
Created July 12, 2021 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamahen/1e72ba14c996aa2eacac886b88f2bdf5 to your computer and use it in GitHub Desktop.
Save kamahen/1e72ba14c996aa2eacac886b88f2bdf5 to your computer and use it in GitHub Desktop.
Hack on top of swipl library(test_cover) for more granular coverage
% -*- mode: Prolog -*-
/*
TODO: generalize this code to work in any file (currently, file name is hard-coded)
This code depends on a modification to ~/src/swipl-devel/packages/plunit/test_cover.pl:
$ git diff
diff --git a/test_cover.pl b/test_cover.pl
index 5290499..24d8874 100644
--- a/test_cover.pl
+++ b/test_cover.pl
@@ -170,9 +170,11 @@ assert_exited(Cl) :-
% Restore state and collect failed and succeeded clauses.
covered(Succeeded, Failed) :-
+ % findall(Cl, (entered(Cl), \+exited(Cl)), Failed0),
+ % findall(Cl, retract(exited(Cl)), Succeeded0),
+ % retractall(entered(Cl)),
findall(Cl, (entered(Cl), \+exited(Cl)), Failed0),
- findall(Cl, retract(exited(Cl)), Succeeded0),
- retractall(entered(Cl)),
+ findall(Cl, exited(Cl), Succeeded0),
sort(Failed0, Failed),
sort(Succeeded0, Succeeded).
*/
/* To run this:
[test_read].
['../test_protobufs'].
show_coverage(run_tests).
my_coverage.
setof(Line, not_covered(Line), Lines), maplist(writeln, Lines).
*/
:- use_module(library(prolog_code), [pi_head/2]).
my_coverage :-
retractall(covered(_Module, _File, _Pred, _Line)),
my_coverage(Srcs),
maplist(assertmy_coverage, Srcs).
my_coverage(Srcs) :-
prolog_cover:covered(Succeeded,_),
% length(Succeeded, Len),
maplist(src, Succeeded, Srcs0),
sort(Srcs0, Srcs).
src(Clause, covered(Module,File,Pred,Line)) :-
clause_property(Clause, file(File)),
clause_property(Clause, module(Module)),
clause_property(Clause, predicate(Pred0)),
pi_head(Pred0, Pred),
clause_property(Clause, line_count(Line)).
lines(Lines) :-
File = '/home/peter/src/swipl-devel/build/home/library/protobufs.pl',
setof(Line-Pred, covered(protobufs, File, Pred, Line), Lines0),
keysort(Lines0, Lines).
not_covered(Line-P) :-
% TODO: current_predicate(_, protobufs:P) - doesn't need pi_head
current_predicate(protobufs:P0),
pi_head(P0, P),
predicate_property(protobufs:P, file('/home/peter/src/swipl-devel/build/home/library/protobufs.pl')),
predicate_property(protobufs:P, line_count(Line)),
\+ covered(protobufs, _, protobufs:P, Line).
print_lines :-
lines(Lines),
length(Lines, Len),
writeln(length:Len),
maplist(writeln, Lines).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment