Skip to content

Commit

Permalink
added expect_*_to_reference
Browse files Browse the repository at this point in the history
  • Loading branch information
markvanderloo committed Feb 21, 2020
1 parent 05038e9 commit 401f608
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Description: Provides a lightweight (zero-dependency) and easy to use
configurable output printing. Compare computed output with output stored
with the package. Run tests in parallel. Extensible by other packages.
Report side effects.
Version: 1.1.0.2
Version: 1.1.0.3
URL: https://github.com/markvanderloo/tinytest
BugReports: https://github.com/markvanderloo/tinytest/issues
Imports: parallel, utils
RoxygenNote: 6.1.1
RoxygenNote: 7.0.2
Encoding: UTF-8
2 changes: 2 additions & 0 deletions pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export(at_home)
export(build_install_test)
export(exit_file)
export(expect_equal)
export(expect_equal_to_reference)
export(expect_equivalent)
export(expect_equivalent_to_reference)
export(expect_error)
export(expect_false)
export(expect_identical)
Expand Down
3 changes: 3 additions & 0 deletions pkg/NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version 1.2.0
- New functions 'expect_equal_to_reference' and 'expect_equivalent_to_reference'
comparing/storing an object with/to an RDS file (Thanks to Jon Clayden for
suggesting).
- Argument 'tol' now renamed 'tolerance'. Also removed internal reliance on
partial argument matching (Thanks to Michel Lang).
- Fix: test_package() would return NULL when called interactively and the package
Expand Down
71 changes: 71 additions & 0 deletions pkg/R/expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,77 @@ expect_message <- function(current, pattern=".*", info=NA_character_){

}

#' Compare object with object stored in a file
#'
#' Compares the current value with a value stored to file with
#' \code{\link{saveRDS}}. If the file does not exist, the current value is
#' stored into file, and the test returns \code{expect_null(NULL)}.
#'
#' @param current \code{[R object or expression]} Outcome or expression under
#' scrutiny.
#' @param file \code{[character]} File where the \code{target} is stored. If
#' \code{file} does not exist, \code{current} will be stored there.
#' @param ... passed to \code{\link{expect_equal}}, respectively \code{\link{expect_equivalent}}.
#'
#' @note
#' Be aware that on CRAN it is not allowed to write data to user space. So make
#' sure that the file is either stored with your tests, or generated with
#' \code{\link{tempfile}}, or the test is skipped on CRAN, using
#' \code{\link{at_home}}.
#'
#' Also note that \code{\link{build_install_test}} clones the package and
#' builds and tests it in a separate R session in the background. This means
#' that if you create a file located at \code{tempfile()} during the run, this
#' file is destroyed when the separate R session is closed.
#'
#'
#' @family test-functions
#'
#'
#' @examples
#' filename <- tempfile()
#' # this gives TRUE: the file does not exist, but is created now.
#' expect_equal_to_reference(1, file=filename)
#' # this gives TRUE: the file now exists, and its contents is equal
#' # to the current value
#' expect_equal_to_reference(1, file=filename)
#' # this gives FALSE: the file exists, but is contents is not equal
#' # to the current value
#' expect_equal_to_reference(2, file=filename)
#'
#' @export
expect_equal_to_reference <- function(current, file, ...){
eetr(current=current, file=file, type="equal", ...)
}

#' @rdname expect_equal_to_reference
#' @export
expect_equivalent_to_reference <- function(current, file, ...){
eetr(current=current, file=file, type="equivalent", ...)
}

eetr <- function (current, file, type=c("equal","equivalent"), ...){

if (file.exists(file)){
out <- if (type=="equal")
tinytest:::expect_equal(current, readRDS(file), ...)
else
tinytest:::expect_equivalent(current, readRDS(file), ...)
if (!out){
diff <- attr(out, "diff")
diff <- paste(
sprintf("current does not match target read from %s\n", file)
, diff)
attr(out,"diff") <- diff
}
out
} else {
tinytest:::expect_null(saveRDS(current, file)
, info=sprintf("Stored value in %s", file))
}
}



#' Report side effects for expressions in test files
#'
Expand Down
1 change: 1 addition & 0 deletions pkg/R/tinytest.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ add_locally_masked_functions <- function(envir, output){
envir$expect_error <- capture(expect_error, output)
envir$expect_identical <- capture(expect_identical, output)
envir$expect_silent <- capture(expect_silent, output)
envir$expect_equal_to_reference <- capture(expect_equal_to_reference, output)
envir$exit_file <- capture_exit(exit_file, output)
envir$ignore <- ignore
envir$at_home <- tinytest::at_home
Expand Down
10 changes: 10 additions & 0 deletions pkg/inst/tinytest/test_tiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ expect_false(ignore(expect_equivalent)(2,c(x=3)))
expect_true(ignore(expect_null)(NULL))
expect_false(ignore(expect_null)(1))

fl <- tempfile()
expect_true(ignore(expect_equal_to_reference)(1, file=fl))
expect_true(ignore(expect_equal_to_reference)(1, file=fl))
expect_false(ignore(expect_equal_to_reference)(2, file=fl))

xx <- c(fu=1)
expect_true(ignore(expect_equivalent_to_reference)(xx, file=fl))
expect_false(ignore(expect_equal_to_reference)(xx, file=fl))




# reading from file
Expand Down

0 comments on commit 401f608

Please sign in to comment.