Skip to content

Commit 5996c52

Browse files
authored
Add examples to docs
Fixes #473
1 parent 00c6de0 commit 5996c52

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

R/covr.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ as_coverage <- function(counters = NULL, ...) {
133133
#' @param code expressions to run.
134134
#' @param env environment the function is defined in.
135135
#' @param enc the enclosing environment which to run the expressions.
136+
#' @examples
137+
#' add <- function(x, y) { x + y }
138+
#' function_coverage(fun = add, code = NULL) # 0% coverage
139+
#' function_coverage(fun = add, code = add(1, 2) == 3) # 100% coverage
136140
#' @export
137141
function_coverage <- function(fun, code = NULL, env = NULL, enc = parent.frame()) {
138142
if (is.function(fun)) {
@@ -175,6 +179,12 @@ function_coverage <- function(fun, code = NULL, env = NULL, enc = parent.frame()
175179
#' functions
176180
#' @param parent_env The parent environment to use when sourcing the files.
177181
#' @inheritParams package_coverage
182+
#' @examples
183+
#' # For the purpose of this example, save code containing code and tests to files
184+
#' cat("add <- function(x, y) { x + y }", file="add.R")
185+
#' cat("add(1, 2) == 3", file="add_test.R")
186+
#' # Use file_coverage() to calculate test coverage
187+
#' file_coverage(source_files = "add.R", test_files = "add_test.R")
178188
#' @export
179189
file_coverage <- function(
180190
source_files,
@@ -218,6 +228,10 @@ file_coverage <- function(
218228
#' @param test_code A character vector of test code
219229
#' @inheritParams file_coverage
220230
#' @param ... Additional arguments passed to [file_coverage()]
231+
#' @examples
232+
#' source <- "add <- function(x, y) { x + y }"
233+
#' test <- "add(1, 2) == 3"
234+
#' code_coverage(source, test)
221235
#' @export
222236
code_coverage <- function(
223237
source_code,

man/code_coverage.Rd

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

man/file_coverage.Rd

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

man/function_coverage.Rd

Lines changed: 5 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)