Skip to content

Commit

Permalink
inline duplicate function, avoid using it in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Müller committed Sep 22, 2014
1 parent 51e17af commit 3208491
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
8 changes: 2 additions & 6 deletions R/mock.r
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ extract_mocks <- function(new_values, .env) {
)
}

#' @useDynLib testthat C_duplicate
mock <- function(name, env, new) {
target_value <- get(name, env, mode = "function")
structure(list(
env = env, name = name,
orig_value = duplicate(target_value), target_value = target_value,
orig_value = .Call(C_duplicate, target_value), target_value = target_value,
new_value = new), class = "mock")
}

Expand All @@ -103,8 +104,3 @@ set_mock <- function(mock) {
reset_mock <- function(mock) {
.Call(reassign_function, as.name(mock$name), mock$env, mock$target_value, mock$orig_value)
}

#' @useDynLib testthat C_duplicate
duplicate <- function(x) {
.Call(C_duplicate, x)
}
16 changes: 8 additions & 8 deletions tests/testthat/test-mock.r
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ test_that("can access variables defined in function", {
})

test_that("can mock both qualified and unqualified functions", {
expect_identical(with_mock(`stats::acf` = identity, duplicate(stats::acf)), identity)
expect_identical(with_mock(`stats::acf` = identity, duplicate(acf)), identity)
expect_identical(with_mock(acf = identity, duplicate(stats::acf), .env = "stats"), identity)
expect_identical(with_mock(acf = identity, duplicate(acf), .env = "stats"), identity)
with_mock(`stats::acf` = identity, expect_identical(stats::acf, identity))
with_mock(`stats::acf` = identity, expect_identical(acf, identity))
with_mock(acf = identity, expect_identical(stats::acf, identity), .env = "stats")
with_mock(acf = identity, expect_identical(acf, identity), .env = "stats")
})

test_that("can mock hidden functions", {
expect_identical(with_mock(`stats:::add1.default` = identity, duplicate(stats:::add1.default)), identity)
with_mock(`stats:::add1.default` = identity, expect_identical(stats:::add1.default, identity))
})

test_that("can mock if package is not loaded", {
expect_false("package:devtools" %in% search())
expect_identical(with_mock(`devtools::add_path` = identity, duplicate(devtools::add_path)), identity)
with_mock(`devtools::add_path` = identity, expect_identical(devtools::add_path, identity))
})

test_that("changes to variables are preserved between calls and visible outside", {
Expand All @@ -132,8 +132,8 @@ test_that("changes to variables are preserved between calls and visible outside"

test_that("can mock function imported from other package", {
expect_true("setRefClass" %in% getNamespaceImports("testthat")[["methods"]])
expect_identical(with_mock(`testthat::setRefClass` = identity, duplicate(setRefClass)), identity)
expect_identical(with_mock(`methods::setRefClass` = identity, duplicate(setRefClass)), identity)
with_mock(`testthat::setRefClass` = identity, expect_identical(setRefClass, identity))
with_mock(`methods::setRefClass` = identity, expect_identical(setRefClass, identity))
})

test_that("mock extraction", {
Expand Down

0 comments on commit 3208491

Please sign in to comment.