Skip to content

Instantly share code, notes, and snippets.

@gshotwell
Created July 17, 2019 15:35
Show Gist options
  • Save gshotwell/75513ac4d42c38ca2d8880efebe81db9 to your computer and use it in GitHub Desktop.
Save gshotwell/75513ac4d42c38ca2d8880efebe81db9 to your computer and use it in GitHub Desktop.
Function to generate tests based on an object
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