Last active
April 11, 2016 15:55
-
-
Save dholstius/d196450853950503f8cf184b2e24a090 to your computer and use it in GitHub Desktop.
Adventures with `ensurer`
This file contains hidden or 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
library(ensurer) # awesome | |
# Example 1 --- OK | |
groceries <- c("eggs", "bread", "milk") | |
check_that(groceries, "bread" %in% .) | |
# Example 2 --- :-( | |
# I'd really like this to work! It's quite readable. | |
# Doesn't work b/c `ensurer` (v1.1) doesn't first evaluate calls. | |
contains <- function (what) function (x) { what %in% x } | |
check_that(groceries, contains("bread")) # :-( | |
# Example 3 --- ?? | |
# On the other hand, this will wwork. `contains_bread` is not a call. | |
contains_bread <- contains("bread") | |
check_that(groceries, contains_bread) | |
# Example 4 --- what I want for production: have upstream data changed? | |
has_md5 <- function (expected) { | |
function (obj) { | |
identical(expected, digest::digest(obj, "md5")) | |
} | |
} | |
check_that(groceries, has_md5("ad2a7a75b9443b916224dc7cf347e8ac")) | |
# ... perform analysis of groceries | |
# Example 6 --- thanks to @stefanbache | |
ensure_md5 <- function (object, expected) { | |
ensure_that(object, identical(md5(.), expected)) | |
} | |
ensure_md5(groceries, "ad2a7a75b9443b916224dc7cf347e8ac") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment