README.md

timefuzz

Project Status: Active – The project has reached a stable, usable state and is being actively developed. R-check

Time travel to test time dependent code - a port of Ruby's timecop

BEWARE: VERY ALPHA

Package API:

Features supported:

Hope to support soon:

Installation

remotes::install_github("ropensci/timefuzz")
library("timefuzz")

freeze

freeze with a code block

library(timefuzz)
library(pendulum)
library(testthat)

book_due() is a toy function that tells us if a book is due

book_due <- function(due_date = Sys.Date() + 10) {
  sys_date() > due_date
}

Given the due date of 2021-01-29 the book is not due

expect_false(book_due()) # FALSE

Create a time_fuzz object

x <- time_fuzz$new()
x
#> <time_fuzz> 
#>   date:

Call freez(), passing the date you want to freeze time to, and then a code block to run in the context of that frozen time. Here we'll freeze time to today + 450 days

x$freeze(Sys.Date() + 450, {
  expect_true(book_due())
})
#> Error: book_due() is not TRUE
#> 
#> `actual`:   FALSE
#> `expected`: TRUE

book_due() results in TRUE now, whereas it was FALSE above in real time

freeze without a code block

x <- time_fuzz$new()
## set to today + 450 days
x$freeze(Sys.Date() + 450)

We're in the freezed date. So any time based actions using the [pendulum][] package are now using your frozen time context.

sys_time()
#> [1] "2021-01-19 10:29:39 PST"

call $unfreeze() to unfreeze

x$unfreeze()

now we're back in current time

sys_time()
#> [1] "2021-01-19 10:29:39 PST"

Meta



ropenscilabs/timefuzz documentation built on Jan. 21, 2021, 1:11 p.m.