gxs_selection {xpectr} | R Documentation |
Generate testhat expectations from selection
Description
Based on the selection (string of code), a set of testthat
expect_*
statements are generated.
Example: If the selected code is the name of a data.frame
object,
it will create an expect_equal
test for each column, along with a test of the column names,
types and classes, dimensions, grouping keys, etc.
See supported objects in details
.
When testing a function that alters non-local variables, consider enabling `copy_env`
.
Feel free to suggest useful tests etc. in a GitHub issue!
Addin: insertExpectationsAddin()
Usage
gxs_selection(
selection,
indentation = 0,
tolerance = "1e-4",
round_to_tolerance = TRUE,
strip = TRUE,
sample_n = 30,
envir = NULL,
copy_env = FALSE,
assign_output = TRUE,
seed = 42,
test_id = NULL,
add_wrapper_comments = TRUE,
add_test_comments = TRUE,
start_with_newline = TRUE,
end_with_newline = TRUE,
out = "insert"
)
Arguments
selection |
String of code. (Character) E.g. |
indentation |
Indentation of the selection. (Numeric) |
tolerance |
The tolerance for numeric tests as a string, like |
round_to_tolerance |
Whether to round numeric elements to the specified tolerance. (Logical) This is currently applied to numeric columns and vectors (excluding some lists). |
strip |
Whether to insert
Sometimes testthat tests have differences in punctuation and newlines on different systems. By stripping both the error message and the expected message of non-alphanumeric symbols, we can avoid such failed tests. |
sample_n |
The number of elements/rows to sample. Set to Inserts The order of the elements/rows is kept intact. No replacement is used, why no oversampling will take place. When testing a big |
envir |
Environment to evaluate in. Defaults to
|
copy_env |
Whether to work in a deep copy of the environment. (Logical) Side effects will be captured in copies of the copy, why two copies of the environment will exist at the same time. Disabled by default to save memory but is often preferable to enable, e.g. when the function changes non-local variables. |
assign_output |
Whether to assign the output of a function call or long selection to a variable. This will avoid recalling the function and decrease cluttering. (Logical) Heuristic: when the The tests themselves can be more difficult to interpret, as you will have to look at the assignment to see the object that is being tested. |
seed |
|
test_id |
Number to append to assignment names. (Whole number) For instance used to create the |
add_wrapper_comments |
Whether to add intro and outro comments. (Logical) |
add_test_comments |
Whether to add comments for each test. (Logical) |
start_with_newline , end_with_newline |
Whether to have a newline in the beginning/end. (Logical) |
out |
Either "insert" (Default)Inserts the expectations via
"return"Returns the expectations in a These can be prepared for insertion with
|
Details
The following "types" are currently supported or intended to be supported in the future. Please suggest more types and tests in a GitHub issue!
Note: A set of fallback tests will be generated for unsupported objects.
Type | Supported | Notes |
Side effects | Yes | Errors, warnings, and messages. |
Vector | Yes | Lists are treated differently, depending on their structure. |
Factor | Yes | |
Data Frame | Yes | List columns (like nested tibbles) are currently skipped. |
Matrix | Yes | Supported but could be improved. |
Formula | Yes | |
Function | Yes | |
NULL | Yes | |
Array | No | |
Dates | No | Base and lubridate . |
ggplot2 | No | This may be a challenge, but would be cool! |
Value
Either NULL
or the unprepared expectations as a character vector
.
Author(s)
Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk
See Also
Other expectation generators:
gxs_function()
,
initializeGXSFunctionAddin()
,
insertExpectationsAddin()
Examples
# Attach packages
library(xpectr)
## Not run:
df <- data.frame('a' = c(1, 2, 3), 'b' = c('t', 'y', 'u'),
stringsAsFactors = FALSE)
gxs_selection("stop('This gives an expect_error test!')")
gxs_selection("warning('This gives a set of side effect tests!')")
gxs_selection("message('This also gives a set of side effect tests!')")
gxs_selection("stop('This: tests the -> punctuation!')", strip = FALSE)
gxs_selection("sum(1, 2, 3, 4)")
gxs_selection("df")
tests <- gxs_selection("df", out = "return")
for_insertion <- prepare_insertion(tests)
rstudioapi::insertText(for_insertion)
## End(Not run)