| 
1 |  | -test_that("deprecated in 3rd edition", {  | 
2 |  | -  expect_warning(local_mock(), "deprecated")  | 
3 |  | -  expect_warning(with_mock(is_testing = function() FALSE), "deprecated")  | 
4 |  | -})  | 
5 |  | - | 
6 |  | -test_that("can change value of internal function", {  | 
7 |  | -  local_edition(2)  | 
8 |  | - | 
9 |  | -  with_mock(  | 
10 |  | -    test_mock_internal2 = function() 5,  | 
11 |  | -    expect_equal(test_mock_internal(), 5)  | 
12 |  | -  )  | 
13 |  | - | 
14 |  | -  # and value is restored on error  | 
15 |  | -  expect_error(  | 
16 |  | -    with_mock(  | 
17 |  | -      test_mock_internal2 = function() 5,  | 
18 |  | -      stop("!")  | 
19 |  | -    )  | 
20 |  | -  )  | 
21 |  | -  expect_equal(test_mock_internal(), "y")  | 
22 |  | -})  | 
23 |  | - | 
24 |  | - | 
25 |  | -test_that("mocks can access local variables", {  | 
26 |  | -  local_edition(2)  | 
27 |  | -  x <- 5  | 
28 |  | - | 
29 |  | -  with_mock(  | 
30 |  | -    test_mock_internal2 = function() x,  | 
31 |  | -    expect_equal(test_mock_internal(), 5)  | 
32 |  | -  )  | 
33 |  | -})  | 
34 |  | - | 
35 |  | -test_that("non-empty mock with return value", {  | 
36 |  | -  local_edition(2)  | 
37 |  | -  expect_true(with_mock(  | 
38 |  | -    compare = function(x, y, ...) list(equal = TRUE, message = "TRUE"),  | 
39 |  | -    TRUE  | 
40 |  | -  ))  | 
41 |  | -})  | 
42 |  | - | 
43 |  | -test_that("nested mock", {  | 
44 |  | -  local_edition(2)  | 
45 |  | -  with_mock(  | 
46 |  | -    all.equal = function(x, y, ...) TRUE,  | 
47 |  | -    {  | 
48 |  | -      with_mock(  | 
49 |  | -        expect_warning = expect_error,  | 
50 |  | -        {  | 
51 |  | -          expect_warning(stopifnot(!compare(3, "a")$equal))  | 
52 |  | -        }  | 
53 |  | -      )  | 
54 |  | -    },  | 
55 |  | -    .env = asNamespace("base")  | 
56 |  | -  )  | 
57 |  | -  expect_false(isTRUE(all.equal(3, 5)))  | 
58 |  | -  expect_warning(warning("test"))  | 
59 |  | -})  | 
60 |  | - | 
61 |  | -test_that("can't mock non-existing", {  | 
62 |  | -  local_edition(2)  | 
63 |  | -  expect_error(with_mock(..bogus.. = identity, TRUE), "Function [.][.]bogus[.][.] not found in environment testthat")  | 
64 |  | -})  | 
65 |  | - | 
66 |  | -test_that("can't mock non-function", {  | 
67 |  | -  local_edition(2)  | 
68 |  | -  expect_error(with_mock(pkg_and_name_rx = FALSE, TRUE), "Function pkg_and_name_rx not found in environment testthat")  | 
69 |  | -})  | 
70 |  | - | 
71 |  | -test_that("empty or no-op mock", {  | 
72 |  | -  local_edition(2)  | 
73 |  | -  expect_warning(  | 
74 |  | -    expect_null(with_mock()),  | 
75 |  | -    "Not mocking anything. Please use named parameters to specify the functions you want to mock.",  | 
76 |  | -    fixed = TRUE  | 
77 |  | -  )  | 
78 |  | -  expect_warning(  | 
79 |  | -    expect_true(with_mock(TRUE)),  | 
80 |  | -    "Not mocking anything. Please use named parameters to specify the functions you want to mock.",  | 
81 |  | -    fixed = TRUE  | 
82 |  | -  )  | 
83 |  | -})  | 
84 |  | - | 
85 |  | -test_that("visibility", {  | 
86 |  | -  local_edition(2)  | 
87 |  | -  expect_warning(expect_false(withVisible(with_mock())$visible))  | 
88 |  | -  expect_true(withVisible(with_mock(compare = function() {}, TRUE))$visible)  | 
89 |  | -  expect_false(withVisible(with_mock(compare = function() {}, invisible(5)))$visible)  | 
90 |  | -})  | 
91 |  | - | 
92 |  | -test_that("multiple return values", {  | 
93 |  | -  local_edition(2)  | 
94 |  | -  expect_true(with_mock(FALSE, TRUE, compare = function() {}))  | 
95 |  | -  expect_equal(with_mock(3, compare = function() {}, 5), 5)  | 
96 |  | -})  | 
97 |  | - | 
98 |  | -test_that("can access variables defined in function", {  | 
99 |  | -  local_edition(2)  | 
100 |  | -  x <- 5  | 
101 |  | -  expect_equal(with_mock(x, compare = function() {}), 5)  | 
102 |  | -})  | 
103 |  | - | 
104 |  | -test_that("can mock if package is not loaded", {  | 
105 |  | -  local_edition(2)  | 
106 |  | -  if ("package:curl" %in% search()) {  | 
107 |  | -    skip("curl is loaded")  | 
108 |  | -  }  | 
109 |  | -  skip_if_not_installed("curl")  | 
110 |  | -  with_mock(`curl::curl` = identity, expect_identical(curl::curl, identity))  | 
111 |  | -})  | 
112 |  | - | 
113 |  | -test_that("changes to variables are preserved between calls and visible outside", {  | 
114 |  | -  local_edition(2)  | 
115 |  | -  x <- 1  | 
116 |  | -  with_mock(  | 
117 |  | -    show_menu = function() {},  | 
118 |  | -    x <- 3,  | 
119 |  | -    expect_equal(x, 3)  | 
120 |  | -  )  | 
121 |  | -  expect_equal(x, 3)  | 
122 |  | -})  | 
123 |  | - | 
124 |  | -test_that("mock extraction", {  | 
125 |  | -  local_edition(2)  | 
126 |  | -  expect_identical(  | 
127 |  | -    extract_mocks(list(compare = compare), .env = asNamespace("testthat"))$compare$name,  | 
128 |  | -    as.name("compare")  | 
129 |  | -  )  | 
130 |  | -  expect_error(  | 
131 |  | -    extract_mocks(list(..bogus.. = identity), "testthat"),  | 
132 |  | -    "Function [.][.]bogus[.][.] not found in environment testthat"  | 
133 |  | -  )  | 
134 |  | -  expect_equal(  | 
135 |  | -    length(extract_mocks(list(not = identity, show_menu = identity), "testthat")),  | 
136 |  | -    2  | 
137 |  | -  )  | 
138 |  | -})  | 
139 |  | -# local_mock --------------------------------------------------------------  | 
140 |  | - | 
141 |  | -test_that("local_mock operates locally", {  | 
142 |  | -  local_edition(2)  | 
143 |  | -  f <- function() {  | 
144 |  | -    local_mock(compare = function(x, y) FALSE)  | 
145 |  | -    compare(1, 1)  | 
146 |  | -  }  | 
147 |  | - | 
148 |  | -  expect_false(f())  | 
149 |  | -  expect_equal(compare(1, 1), no_difference())  | 
 | 1 | +test_that("deprecated", {  | 
 | 2 | +  expect_snapshot(local_mock())  | 
 | 3 | +  expect_snapshot(with_mock(is_testing = function() FALSE))  | 
150 | 4 | })  | 
0 commit comments