From 41dcc01a7d67dcd25a55bfbc6bcd1db80b83a04d Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Mon, 1 Feb 2016 17:46:48 +0000 Subject: [PATCH] BUG: Fix coverage calculation for empty files For files with no executable lines, the coverage report should have a nominal 100% coverage rate. This is the behaviour of other coverage packages, such as those for Python. Previously, files without any executable lines would report a coverage of "NaN%", due to the division by 0. --- MOcov/@MOcovMFile/get_coverage_xml.m | 8 +++++++- MOcov/@MOcovMFileCollection/write_cobertura_xml.m | 6 +++++- MOcov/@MOcovMFileCollection/write_html.m | 6 +++++- MOcov/@MOcovMFileCollection/write_html_dir.m | 6 +++++- MOcov/@MOcovMFileCollection/write_xml_file.m | 6 +++++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/MOcov/@MOcovMFile/get_coverage_xml.m b/MOcov/@MOcovMFile/get_coverage_xml.m index 11df51a..f3b8b61 100644 --- a/MOcov/@MOcovMFile/get_coverage_xml.m +++ b/MOcov/@MOcovMFile/get_coverage_xml.m @@ -54,4 +54,10 @@ function r=get_coverage_ratio(obj) executable=get_lines_executable(obj); - r=sum(get_lines_executed(obj) & executable) / sum(executable); \ No newline at end of file + numerator = sum(get_lines_executed(obj) & executable); + denominator = sum(executable); + if denominator==0 + r=1; + else + r=numerator/denominator; + end diff --git a/MOcov/@MOcovMFileCollection/write_cobertura_xml.m b/MOcov/@MOcovMFileCollection/write_cobertura_xml.m index f50bc90..14aaa42 100644 --- a/MOcov/@MOcovMFileCollection/write_cobertura_xml.m +++ b/MOcov/@MOcovMFileCollection/write_cobertura_xml.m @@ -66,6 +66,10 @@ function write_to_file(fn,s) denominator=denominator+sum(able); end - coverage=numerator/denominator; + if denominator==0 + coverage=1; + else + coverage=numerator/denominator; + end diff --git a/MOcov/@MOcovMFileCollection/write_html.m b/MOcov/@MOcovMFileCollection/write_html.m index a758a51..b80bd1e 100644 --- a/MOcov/@MOcovMFileCollection/write_html.m +++ b/MOcov/@MOcovMFileCollection/write_html.m @@ -96,7 +96,11 @@ function write_index_html(output_fn, mfiles, mfile_node_fns) executed(~executable)=false; - coverage=100*sum(executed)/sum(executable); + if sum(executable)==0 + coverage=100; + else + coverage=100*sum(executed)/sum(executable); + end stat=[numel(executable),... sum(executable),... diff --git a/MOcov/@MOcovMFileCollection/write_html_dir.m b/MOcov/@MOcovMFileCollection/write_html_dir.m index 3ce6e8d..9c049e6 100644 --- a/MOcov/@MOcovMFileCollection/write_html_dir.m +++ b/MOcov/@MOcovMFileCollection/write_html_dir.m @@ -111,7 +111,11 @@ function write_index_html(output_fn, mfiles, mfile_node_fns) executed(~executable)=false; - coverage=100*sum(executed)/sum(executable); + if sum(executable)==0 + coverage=100; + else + coverage=100*sum(executed)/sum(executable); + end stat=[numel(executable),... sum(executable),... diff --git a/MOcov/@MOcovMFileCollection/write_xml_file.m b/MOcov/@MOcovMFileCollection/write_xml_file.m index 63445ac..4b38443 100644 --- a/MOcov/@MOcovMFileCollection/write_xml_file.m +++ b/MOcov/@MOcovMFileCollection/write_xml_file.m @@ -83,6 +83,10 @@ function write_to_file(fn,s) denominator=denominator+sum(executable); end - coverage=numerator/denominator; + if denominator==0 + coverage=1; + else + coverage=numerator/denominator; + end