Last active
August 19, 2019 09:02
-
-
Save moodymudskipper/cf750c7c080f821dc0319f00b4c03a71 to your computer and use it in GitHub Desktop.
custom testthat expectation to test if 2 syntaxes are equivalent
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
expect_same_behavior <- function(object, expected, ...){ | |
object_chr <- deparse(substitute(object)) | |
expected_chr <- deparse(substitute(expected)) | |
quiet <- purrr::safely(purrr::quietly(identity)) | |
object <- eval(substitute(quiet(object))) | |
expected <- eval(substitute(quiet(expected))) | |
testthat::expect( | |
identical(object$result$result, expected$result$result, ...), | |
sprintf("`%s` and `%s` don't return the same result.", object_chr, expected_chr)) | |
testthat::expect( | |
identical(object$result$output, expected$result$output, ...), | |
sprintf("`%s` and `%s` don't print the same output", object_chr, expected_chr)) | |
testthat::expect( | |
identical(object$result$messages, expected$result$messages, ...), | |
sprintf("`%s` and `%s` don't print the same messages", object_chr, expected_chr)) | |
testthat::expect( | |
identical(object$result$warnings, expected$result$warnings, ...), | |
sprintf("`%s` and `%s` don't trigger the same warnings", object_chr, expected_chr)) | |
testthat::expect( | |
identical(object$error$message, expected$error$message, ...), | |
sprintf("`%s` and `%s` don't trigger the same error", object_chr, expected_chr)) | |
invisible(NULL) | |
} | |
expect_same_behavior(1, 1) | |
expect_same_behavior(print(1),print(1)) | |
expect_same_behavior(message(1),message(1)) | |
expect_same_behavior(stop(1), stop(1)) | |
expect_same_behavior(1, 2) | |
#> Error: `1` and `2` don't return the same result. | |
expect_same_behavior(print(1),1) | |
#> Error: `print(1)` and `1` don't print the same output | |
expect_same_behavior(message(1),message(2)) | |
#> Error: `message(1)` and `message(2)` don't print the same messages | |
expect_same_behavior(stop(1), stop(2)) | |
#> Error: `stop(1)` and `stop(2)` don't trigger the same error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment