Title: | Facilitate writing testthat tests and parameter checks for data frames |
---|---|
Description: | Based on a template dataframe, generates R code for `testthat` tests or `stopifnot` checks. Intended to streamline testwriting rather than completely automate it; user will need to review the generated code and decide which comparisons make sense. |
Authors: | Collin Edwards [aut, cre] |
Maintainer: | Collin Edwards <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9000 |
Built: | 2025-01-05 06:34:16 UTC |
Source: | https://github.com/cbedwards-dfw/testmaker |
Helper function to convert string to R code to regenerate that string.
dput_to_string(object)
dput_to_string(object)
object |
R object, expecting a vector |
Character atomic of R code to recreate the object. Plays well with glue
functions and cat()
.
## Not run: temp = dput_to_string(rownames(mtcars)) cat(temp) ## End(Not run)
## Not run: temp = dput_to_string(rownames(mtcars)) cat(temp) ## End(Not run)
Internal helper function.
finish_testmaker(test.text, return.style, silent)
finish_testmaker(test.text, return.style, silent)
test.text |
Character vector of the finished lines of codes to display or provide as appropriate. |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
cli_abort
code for dataframe column classesGenerate cli_abort
code for dataframe column classes
testmaker_df_class_cli( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", for.fun = FALSE )
testmaker_df_class_cli( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", for.fun = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
for.fun |
Is the resulting text meant to be inserted into a helper function?
If |
Either nothing or a character vector of lines of R code for writing a stopifnot test.
testmaker_df_class_cli(cars, return.style = "text")
testmaker_df_class_cli(cars, return.style = "text")
stopifnot
code for dataframe column classesDEPRECATED Generate stopifnot
code for dataframe column classes
testmaker_df_class_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
testmaker_df_class_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
Either nothing or a character vector of lines of R code for writing a stopifnot test.
Generate testthat code for dataframe column classes
testmaker_df_class_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
testmaker_df_class_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
testmaker_df_class_tt(cars, return.style = "text")
testmaker_df_class_tt(cars, return.style = "text")
cli_abort
tests for dataframe inputsGenerates R code to test expectations based on a template database. Intended workflow:
when writing functions to work with a dataframe with a specific expected structure,
load an example of that dataframe into the environment and then call testmaker_df(exampledataframe)
.
This will print if statements and cli::cli_abort(...)
function calls based on the dimensions of the example data frame, the classes
of the columns, and the names of the columns. Note that this is not intended to replace decision-making, but rather
to streamline the process of generating the relevant code. In particular, testing the number of rows is often not appropriate
(e.g., in many use cases the input data frame is expected to have unknown nrow).
testmaker_df_cli( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", col.order.matters = TRUE )
testmaker_df_cli( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", col.order.matters = TRUE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
col.order.matters |
When we generate code to test for column names, should we test for exact equivalence ( |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
## Not run: stopifnotmaker(cars) ## End(Not run)
## Not run: stopifnotmaker(cars) ## End(Not run)
stopifnot
code to check contents of column(s) against templateDEPRECATED Generate stopifnot
code to check contents of column(s) against template
testmaker_df_colcontent_sin( x, cols, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", for.fun = FALSE )
testmaker_df_colcontent_sin( x, cols, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", for.fun = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
cols |
character or character vector of columns in |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
testthat
code to check contents of column(s) against templateUseful for QA/QC when (a) all entries of a column should fall within a contained set (e.g. fishery columns in FRAM model outputs or manipulations
should only contain fishery IDs present in the FRAM database), and/or (b) when all entries of some template column should present in the output template (e.g.,
if processing of a FRAM model table should result in a dataframe with all fishery ids present in the
FRAM database). For example, in the framrsquared
package, we may have functions in which we expect
the fishery_id
column of the function output to contain only fishery_ids present in the FRAM database. Similarly,
we may expect that the output fishery_id
column contains ALL of the fishery_ids present in the FRAM database.
testmaker_df_colcontent_tt( x, cols, return.style = c("clip", "text", "none"), silent = FALSE )
testmaker_df_colcontent_tt( x, cols, return.style = c("clip", "text", "none"), silent = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
cols |
character or character vector of columns in |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
Depending on the number of unique variables in the column(s), this can generate very long code. This function includes comments to distinguish between checking against unexpected entries and checking against missing entries.
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
dat = data.frame(state.x77, state = rownames(state.x77)) dat$category = sample(letters[1:5], size = nrow(dat), replace = TRUE) testmaker_df_colcontent_tt(dat, c("state", "category"), return.style = "none")
dat = data.frame(state.x77, state = rownames(state.x77)) dat$category = sample(letters[1:5], size = nrow(dat), replace = TRUE) testmaker_df_colcontent_tt(dat, c("state", "category"), return.style = "none")
cli_abort
code for dataframe dimensionsGenerate cli_abort
code for dataframe dimensions
testmaker_df_dim_cli( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", for.fun = FALSE )
testmaker_df_dim_cli( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", for.fun = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
for.fun |
Is the resulting text meant to be inserted into a helper function?
If |
Either nothing or a character vector of lines of R code for writing a stopifnot test.
testmaker_df_dim_cli(cars, return.style = "text")
testmaker_df_dim_cli(cars, return.style = "text")
stopifnot
code for dataframe dimensionsDeprecated Generate stopifnot
code for dataframe dimensions
testmaker_df_dim_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
testmaker_df_dim_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
Either nothing or a character vector of lines of R code for writing a stopifnot test.
testthat
code for dataframe dimensionsFrom a provided template data frame, generates testthat code to check that the nrow and ncols of the res
dataframe match
the dimensions of the template data frame. Default behavior loads the code into the clipboard for easy pasting into code.
testmaker_df_dim_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
testmaker_df_dim_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
testmaker_df_dim_tt(cars, return = "none")
testmaker_df_dim_tt(cars, return = "none")
testthat
code for dataframe dimensionsFrom a provided template data frame, generates testthat code to check that the nrow and ncols of the res
dataframe match
the dimensions of the template data frame. Default behavior loads the code into the clipboard for easy pasting into code.
testmaker_df_isit_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
testmaker_df_isit_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
testmaker_df_dim_tt(cars, return = "none")
testmaker_df_dim_tt(cars, return = "none")
stopifnot
code for dataframe column namesGenerate stopifnot
code for dataframe column names
testmaker_df_names_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
testmaker_df_names_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
Either nothing or a character vector of lines of R code for writing a stopifnot test.
testmaker_df_names_sin(cars, return.style = "text")
testmaker_df_names_sin(cars, return.style = "text")
stopifnot
code for dataframe column names disregarding orderDEPRECATED Generate stopifnot
code for dataframe column names disregarding order
testmaker_df_names_sin_orderless( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
testmaker_df_names_sin_orderless( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res" )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
Either nothing or a character vector of lines of R code for writing a stopifnot test.
testmaker_df_names_sin(cars, return.style = "text")
testmaker_df_names_sin(cars, return.style = "text")
testthat
code for dataframe column namesGenerate testthat
code for dataframe column names
testmaker_df_names_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
testmaker_df_names_tt( x, return.style = c("clip", "text", "none"), silent = FALSE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
testmaker_df_names_tt(cars, return.style = "text")
testmaker_df_names_tt(cars, return.style = "text")
stopifnot
tests for dataframe inputsGenerates R code to test expectations based on a template database. Intended workflow:
when writing functions to work with a dataframe with a specific expected structure,
load an example of that dataframe into the environment and then call testmaker_df_sin(exampledataframe)
.
This will print stopifnot(...)
function calls based on the dimensions of the example data frame, the classes
of the columns, and the names of the columns. Note that this is not intended to replace decision-making, but rather
to streamline the process of generating the relevant code. In particular, testing the number of rows is often not appropriate
(e.g., in many use cases the input data frame is expected to have unknown nrow).
testmaker_df_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", col.order.matters = TRUE )
testmaker_df_sin( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "res", col.order.matters = TRUE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
col.order.matters |
When we generate code to test for column names, should we test for exact equivalence ( |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
testthat
tests for dataframe inputsGenerates R code to test expectations based on a template database. Intended workflow:
when writing functions to work with a dataframe with a specific expected structure,
load an example of that dataframe into the environment and then call testmaker_df_tt(exampledataframe)
.
This will print testthat::expect_*()
function calls based on the dimensions of the example data frame, the classes
of the columns, and the names of the columns. Note that this is not intended to replace decision-making, but rather
to streamline the process of generating the relevant code. In particular, testing the number of rows is often not appropriate
(e.g., in many use cases the input data frame is expected to have unknown nrow).
testmaker_df_tt(x, return.style = c("clip", "text", "none"), silent = FALSE)
testmaker_df_tt(x, return.style = c("clip", "text", "none"), silent = FALSE)
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
Either nothing or a character vector of lines of R code for writing a testthat test, depending on return.style
.
## Not run: testmaker_df(cars) ## End(Not run)
## Not run: testmaker_df(cars) ## End(Not run)
cli_abort
frameworkThis function streamlines creating helper functions to check dataframe inputs, writing code based on the characteristics of a template dataframe. Intended use: when writing a function that takes a dataframe as an input, and you want to include an input check to confirm the input has the same # of columns, column names, and column classes as the dataframe you're using to develop your code. Providing informative error messages requires several lines of code, so it can be cleaner to write this input check as a separate function.
testmaker_df_validator( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "x", col.order.matters = TRUE )
testmaker_df_validator( x, return.style = c("clip", "text", "none"), silent = FALSE, object.name = "x", col.order.matters = TRUE )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Designation for what to return. "clip" returns nothing, but loads the text into the clipboard. "text" returns the text as a character vector, "none" returns nothing |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument |
col.order.matters |
When we generate code to test for column names, should we test for exact equivalence ( |
By default, this function copies the appropriate text to the system clipboard; all that is needed after running the function is to paste into the appropriate R file and provide a function name.
Note that testmaker_df_cli generates all the likely/common attribute checks, not all of which may be relevant for any specific use case(e.g., the generated code includes a check that the input dataframe contains the same number of rows as the template data frame; this is often not the expectation). Modify the resulting function template to match your specific needs. checking that the input has the ).
Either nothing (when return.style is "clip"
or "none"
), or character string of R code.
testmaker_df_validator(cars, return.style = "none") ## Not run: #Workflow version, which loads text into clipboard for easy pasting: testmaker_df_validator(cars) ## End(Not run)
testmaker_df_validator(cars, return.style = "none") ## Not run: #Workflow version, which loads text into clipboard for easy pasting: testmaker_df_validator(cars) ## End(Not run)
Internal helper function to streamline validation
validate_testmaker( x, return.style, silent, object.name = NULL, call = rlang::caller_env() )
validate_testmaker( x, return.style, silent, object.name = NULL, call = rlang::caller_env() )
x |
Template dataframe of target object (e.g. we expect our test object to share properties with this) |
return.style |
Defines what is returned by testmaker functions. SHOULD be "clip", "text", or "none" |
silent |
Suppress printing text to console? logical; default of false. |
object.name |
Name of the object to apply the stopifnot to; presumably the name of the dataframe argument in the function the test is being written for. Defaults to NULL; provide when validating a stopifnot_* function. |
call |
Identifies the function calling this helper function, allowing more informative error messages |