Skip to content

Commit

Permalink
Make test more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich committed Mar 10, 2023
1 parent 3cf3234 commit de31e74
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions tests/testthat/test-memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,28 @@ test_that("omit_args respected", {

expect_true(identical(res1, res2))

# Also works for default args
f <- function(n, x = rnorm(1)) {
rnorm(n)
# Also works for default arguments
a <- 0
f <- function(x = a) {
a <<- a + 1
a
}
mem_f <- memoise(f)
expect_false(identical(res1, res2))
res1 <- mem_f(1)
res2 <- mem_f(1)

# everytime `f()` is called its value increases by 1
expect_equal(f(), 1)
expect_equal(f(), 2)

# it still increases by one when memoised as the argument `x` changes
a <- 0
mem_f <- memoise::memoise(f)
expect_equal(mem_f(), 1)
expect_equal(mem_f(), 2)

# but `x` can be ignored via `omit_args`
a <- 0
mem_f2 <- memoise(f, omit_args = "x")
res1 <- mem_f2(1)
res2 <- mem_f2(1)
expect_true(identical(res1, res2))
expect_equal(mem_f2(), 1)
expect_equal(mem_f2(), 1)
})

context("has_cache")
Expand Down

0 comments on commit de31e74

Please sign in to comment.