Learn R Programming

mighty.component (version 0.2.0)

mighty_component_test: Test mighty component class

Description

R6 class for unit testing a component with code coverage tracking. The code runs in a separate R process (the test session), and the lines executed are counted.

Arguments

Super classes

mighty_component -> mighty_component_rendered -> mighty_component_test

Active bindings

percent_coverage

numeric(1) Percentage of lines covered (0-100).

line_coverage

data.frame with columns line and value (number of times the line has run).

Methods

Inherited methods

mighty_component_test$new()

Create a test component from a rendered template. Starts a new R session. Requires the callr and covr packages.

Usage

mighty_component_test$new(template, id)

Arguments

template

character Rendered template, one element per line.

id

character(1) Component ID.


mighty_component_test$print()

Print the test component with its code coverage.

Usage

mighty_component_test$print()

Returns

(invisible) self


mighty_component_test$assign()

Assign a variable in the global environment of the test session. Your R session is not modified.

Usage

mighty_component_test$assign(x, value)

Arguments

x

character(1) Variable name.

value

Value to assign. It is copied to the test session.

Returns

(invisible) self


mighty_component_test$get()

Get a variable from the global environment of the test session.

Usage

mighty_component_test$get(x)

Arguments

x

character(1) Variable name.

Returns

A copy of the variable's value.


mighty_component_test$ls()

List the variables in the global environment of the test session.

Usage

mighty_component_test$ls()

Returns

character Variable names.


mighty_component_test$eval()

Run the code in the global environment of the test session and update the coverage.

Usage

mighty_component_test$eval()

Returns

(invisible) self


mighty_component_test$check_coverage()

Check that every line of the code has run at least once. Raises an error otherwise.

Usage

mighty_component_test$check_coverage()

Returns

(invisible) self


mighty_component_test$close()

Close the test session. Called automatically when the object is garbage collected.

Usage

mighty_component_test$close()

Returns

(invisible) self


mighty_component_test$clone()

The objects of this class are cloneable with this method.

Usage

mighty_component_test$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Details

Test session

get_test_component() starts a new R process with 'callr', the test session. Input data, the component code and its results all live in the global environment of the test session. Your own R session and its global environment are never modified.

The test session is closed with $close(), or when the object is garbage collected. After that, $assign(), $get(), $ls() and $eval() raise an error.

Workflow

  1. Create the test component with get_test_component().

  2. Assign input data in the test session with $assign().

  3. Run the code in the test session and update coverage with $eval().

  4. Retrieve results from the test session with $get().

  5. Test the results with testthat expect_*() functions.

  6. Close the test session with $close().

By default, get_test_component() checks coverage with $check_coverage() when the test ends. Coverage is kept in your R session, so it can still be checked after $close().

See Also

get_test_component()

Examples

Run this code
if (FALSE) { # rlang::is_installed(c("admiral", "callr", "covr", "dplyr"))
path <- system.file("examples", "ady.mustache", package = "mighty.component")
x <- get_test_component(
  component = path,
  params = list(domain = "adae", variable = "ADY", date = "ADT"),
  check_coverage = FALSE
)
x$percent_coverage

x$assign(
  "adae",
  data.frame(TRTSDT = as.Date("2024-01-01"), ADT = as.Date("2024-01-05"))
)
x$eval()
x$ls()
x$get("adae")$ADY
x$percent_coverage
x$check_coverage()
x$close()
}

Run the code above in your browser using DataLab