Skip to contents

This function reads R code with at least one function definition (a file, a URL or a character string) and generates testthat tests for it.

Usage

write_test(file, ..., overwrite = FALSE)

Arguments

file

A file path, URL or character string with the R function(s) to test (see read_text()).

...

Additional arguments to pass to the chat_completion() function.

overwrite

Logical. If FALSE (default), the function stops before calling the API when the test file already exists; if TRUE, it replaces it. Only used for file input.

Value

For literal text or a URL: the generated test code as a character string, returned visibly; nothing is written. For a file: the path of the test file written (see Details), returned invisibly; in an interactive session the file is also opened with utils::file.edit(). Input that is empty (error class openair_empty_input) or holds no R function stops before any API call. For file input, test code that does not parse as R is not written: the function stops with an error of class openair_invalid_output whose output field holds the code. Literal or URL input returns such code all the same, with a message that it may be invalid.

Details

For a file, the tests go where testthat finds them. If the file sits in the R/ directory of a package (a DESCRIPTION file one level up), they are written to tests/testthat/test-<name>.R of that package (the directory is created if needed); otherwise to test-<name>.R next to the input file.

Examples

if (FALSE) { # \dontrun{
# Write tests for a function of a package: creates
# path/to/pkg/tests/testthat/test-file.R
write_test("path/to/pkg/R/file.R")
} # }