Skip to content

Commit ab1f96b

Browse files
Added ability to calculate maximum achievable slicing coverage
1 parent ab4dd97 commit ab1f96b

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export(close_connection)
44
export(configure)
55
export(file_coverage)
66
export(get_option)
7+
export(maximum_coverage)
78
export(package_coverage)
89
export(with_options)

R/config.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ option_env$slicing_points <- FALSE
77
option_env$log_level <- "INFO"
88
option_env$unknown_locations <- FALSE
99
option_env$return_srcrefs <- FALSE
10+
option_env$return_annotated_cov <- FALSE
1011

1112
#' Configures various options for this package. If no value is given for any
1213
#' option, it's default is used.
@@ -27,6 +28,8 @@ option_env$return_srcrefs <- FALSE
2728
#' know about
2829
#' \item return_srcrefs Whether to return all srcrefs, the ones that were covered and the
2930
#' ones that were in the slice.
31+
#' \item return_annotated_cov Whether the returned object should be annotated with
32+
#' slicing information.
3033
#' }
3134
#' @return A list of all options and their values before the configuration.
3235
#'

R/coverage.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,23 @@ package_coverage <- function(path = ".") {
3939

4040
return(give_me_covr_and_i_do_the_rest(covr_measure, sources$files, tests$files))
4141
}
42+
43+
#' Calculate the maximum possible slicing coverage with the current assertions.
44+
#' Considering this value can indicate whether the slicing coverage score is limited by
45+
#' the number of assertions or the amount of covered code.
46+
#'
47+
#' @param f Function to calculate slicing coverage
48+
#' @param ... Arguments to pass to the function
49+
#'
50+
#' @export
51+
maximum_coverage <- function(f, ...) {
52+
cov <- with_options(list(return_annotated_cov = TRUE), f(...))
53+
max_cov <- recalculate_values(cov, new_value = function(row) {
54+
if (is.null(row$in_slice) || row$in_slice) {
55+
max(row$value, 1)
56+
} else {
57+
0
58+
}
59+
}) |> remove_slc_from_coverage()
60+
return(max_cov)
61+
}

R/utils.R

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ build_return_value <- function(covr, covr_time,
8888
slicing_coverage, slicing_points, ana_time, slicing_time, query_time,
8989
unknown_locations,
9090
srcrefs) {
91+
if (!get_option("return_annotated_cov")) {
92+
slicing_coverage <- remove_slc_from_coverage(slicing_coverage)
93+
}
9194
if (get_option("measure_time") || get_option("return_covr_result") || get_option("slicing_points")) {
9295
res <- list(coverage = slicing_coverage)
9396
if (get_option("measure_time")) {
@@ -209,19 +212,13 @@ remove_slc_from_coverage <- function(coverage) {
209212
return(coverage)
210213
}
211214

212-
recalculate_values <- function(coverage) {
215+
recalculate_values <- function(
216+
coverage,
217+
new_value = function(row) if (is.null(row$in_slice) || row$in_slice) row$value else 0) {
213218
logger::log_trace("Adjusting coverage values", namespace = "slicingCoverage")
214219
for (i in seq_along(coverage)) {
215220
elem <- coverage[[i]]
216-
in_slice <- elem$in_slice
217-
if (is.null(in_slice)) { # This should not happen (see add_ids_to_coverage)
218-
next
219-
}
220-
if (in_slice) { # No need to change anything as element is in the slice
221-
next
222-
}
223-
224-
elem$value <- 0
221+
elem$value <- new_value(elem)
225222
coverage[[i]] <- elem
226223
}
227224
return(coverage)
@@ -270,7 +267,7 @@ give_me_covr_and_i_do_the_rest <- function(covr_measure, sources, tests) { # nol
270267

271268
srcrefs <- get_coverered_and_sliced_srcrefs(coverage_with_slc)
272269

273-
slicing_coverage <- recalculate_values(coverage_with_slc) |> remove_slc_from_coverage()
270+
slicing_coverage <- recalculate_values(coverage_with_slc)
274271

275272
return(build_return_value(
276273
covr, covr_time,

man/configure.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/maximum_coverage.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)