Retrieve and render a component as a mighty_component_test object, for unit tests with code coverage. See mighty_component_test for the workflow.
Requires the callr and covr packages.
Usage
get_test_component(
component,
params = list(),
repos = NULL,
check_coverage = TRUE,
teardown_env = parent.frame()
)Arguments
- component
character(1)Component name. IfreposisNULL, path to a component file.- params
named
listof parameters passed to$render(). See the component's@paramtags (component$params).- repos
Where to look. One of:
NULL(default):componentis a file path.charactervector of repo specs, in priority order. Seemighty_repo().A
mighty_repo_classobject.A
listof repo specs ormighty_repo_classobjects, in priority order.A
mighty_repos()collection.
Character vectors and lists are converted once with
mighty_repos().- check_coverage
logical(1)IfTRUE(default),$check_coverage()runs whenteardown_envends. It raises an error if any line has not run.- teardown_env
Environment that controls when the coverage check runs. Defaults to the calling environment, e.g. the
test_that()block.
Value
A mighty_component_test object.
Examples
path <- system.file("examples", "ady.mustache", package = "mighty.component")
x <- get_test_component(
component = path,
params = list(domain = "adae", variable = "ASTDY", date = "ASTDT"),
check_coverage = FALSE
)
#> → Found "ady.mustache" in "local::/home/runner/work/_temp/Library/mighty.component/examples"
adae <- data.frame(
TRTSDT = as.Date("2024-01-01"),
ASTDT = as.Date(c("2024-01-01", "2024-01-10"))
)
x$assign("adae", adae)$eval()
x$get("adae")
#> TRTSDT ASTDT ASTDY
#> 1 2024-01-01 2024-01-01 1
#> 2 2024-01-01 2024-01-10 10
x$percent_coverage
#> [1] 100
x$close()
