This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
117 lines (82 loc) · 2.61 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
timefuzz
========
```{r echo=FALSE}
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE
)
```
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-check](https://github.com/ropensci/timefuzz/workflows/R-check/badge.svg)](https://github.com/ropensci/timefuzz/actions?query=workflow%3AR-check)
Time travel to test time dependent code - a port of Ruby's [timecop](https://github.com/travisjeffery/timecop)
**BEWARE: VERY ALPHA**
Package API:
```{r echo=FALSE, comment=NA, results="asis"}
cat(paste(" -", paste(sprintf("`%s`", getNamespaceExports("timefuzz")), collapse = "\n - ")))
```
Features supported:
- freeze: freeze time to a specific point
Hope to support soon:
- travel: travel back to a specific point in time, but allow time to continue moving forward from there
- scale: scale time by a given scaling factor that will cause time to move at an accelerated pace
## Installation
```{r eval=FALSE}
remotes::install_github("ropensci/timefuzz")
```
```{r}
library("timefuzz")
```
## freeze
### freeze with a code block
```{r}
library(timefuzz)
library(pendulum)
library(testthat)
```
`book_due()` is a toy function that tells us if a book is due
```{r}
book_due <- function(due_date = Sys.Date() + 10) {
sys_date() > due_date
}
```
Given the due date of `r Sys.Date() + 10` the book is not due
```{r}
expect_false(book_due()) # FALSE
```
Create a `time_fuzz` object
```{r}
x <- time_fuzz$new()
x
```
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
```{r}
x$freeze(Sys.Date() + 450, {
expect_true(book_due())
})
```
`book_due()` results in `TRUE` now, whereas it was `FALSE` above in real time
### freeze without a code block
```{r}
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.
```{r}
sys_time()
```
call `$unfreeze()` to unfreeze
```{r}
x$unfreeze()
```
now we're back in current time
```{r}
sys_time()
```
## Meta
* Please [report any issues or bugs](https://github.com/ropensci/timefuzz/issues).
* License: MIT
* Get citation information for `timefuzz` in R doing `citation(package = 'timefuzz')`
* Please note that this package is released with a [Contributor Code of Conduct](https://ropensci.org/code-of-conduct/). By contributing to this project, you agree to abide by its terms.