Created
July 17, 2019 15:35
-
-
Save gshotwell/75513ac4d42c38ca2d8880efebe81db9 to your computer and use it in GitHub Desktop.
Function to generate tests based on an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match_vector <- function(vec) { | |
name <- deparse(substitute(vec)) | |
length <- length(vec) | |
if (length < 10) { | |
identity_test <- glue::glue("expect_equal({name}, c(", | |
paste0(vec, collapse = ", "), | |
")") | |
} | |
expectations <- c( | |
glue::glue("expect_is({name}, 'vector')"), | |
glue::glue("expect_equal(length({name}), {length})"), | |
identity_test | |
) | |
to_paste <- paste0(expectations, collapse = "\n") | |
rstudioapi::insertText(text = to_paste) | |
} | |
vec <- function_to_test() | |
match_vector(vec) | |
# RStudio prints | |
expect_is(vec, 'vector') | |
expect_equal(length(vec), 9) | |
expect_equal(vec, c(1, 2, 3, 4, 5, 6, 7, 8, 9) | |
# Then copy into test file | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment