Debian Codegraph @

R/stub.R

raw
1
2 #' Replace a function with a stub.
3 #'
4 #' The result of calling \code{stub} is that, when \code{where}
5 #' is invoked and when it internally makes a call to \code{what},
6 #' \code{how} is going to be called instead.
7 #'
8 #' This is much more limited in scope in comparison to
9 #' `testthat::with_mock()` which effectively replaces
10 #' \code{what} everywhere. In other words, when using \code{with_mock}
11 #' and regardless of the number of intermediate calls, \code{how} is
12 #' always called instead of \code{what}. However, using this API,
13 #' the replacement takes place only for a single function \code{where}
14 #' and only for calls originating in that function.
15 #'
16 #'
17 #' @name stub
18 #' @rdname stub
19 NULL
20
21 # \code{remote_stub} reverses the effect of \code{stub}.
22
23
24 #' @param where Function to be called that will in turn call
25 #' \code{what}.
26 #' @param what Name of the function you want to stub out (a
27 #' \code{character} string).
28 #' @param how Replacement function (also a \code{\link{mock}} function)
29 #' or a return value for which a function will be created
30 #' automatically.
31 #' @param depth Specifies the depth to which the function should be stubbed
32 #'
33 #' @export
34 #' @rdname stub
35 #'
36 #' @examples
37 #' f <- function() TRUE
38 #' g <- function() f()
39 #' stub(g, 'f', FALSE)
40 #'
41 #' # now g() returns FALSE because f() has been stubbed out
42 #' g()
43 #'
44 #' # you can stub multiple functions by calling stub() multiple times
45 #' f <- function() TRUE
46 #' g <- function() TRUE
47 #' h <- function() any(f(), g())
48 #' stub(h, 'f', FALSE)
49 #' stub(h, 'g', FALSE)
50 #'
51 #' # now h() returns FALSE because both f() and g() have been stubbed out
52 #' h()
53 #'
54 `stub` <- function (where, what, how, depth=1)
55 {
56 # `where` needs to be a function
57 where_name <- deparse(substitute(where))
58
59 # `what` needs to be a character value
60 stopifnot(is.character(what), length(what) == 1)
61
62 test_env <- parent.frame()
63 tree <- build_function_tree(test_env, where, where_name, depth)
64
65 mock_through_tree(tree, what, how)
66 }
67
68 mock_through_tree <- function(tree, what, how) {
69 for (d in tree) {
70 for (parent in d) {
71 parent_env = parent[['parent_env']]
72 func_dict = parent[['funcs']]
73 for (func_name in ls(func_dict, all.names=TRUE)) {
74 func = func_dict[[func_name]]
75 func_env = new.env(parent = environment(func))
76
77 what <- override_seperators(what, func_env)
78 where_name <- override_seperators(func_name, parent_env)
79
80 if (!is.function(how)) {
81 assign(what, function(...) how, func_env)
82 } else {
83 assign(what, how, func_env)
84 }
85
86 environment(func) <- func_env
87 locked <- exists(where_name, parent_env, inherits = FALSE) && bindingIsLocked(where_name, parent_env)
88 if (locked) {
89 baseenv()$unlockBinding(where_name, parent_env)
90 }
91 assign(where_name, func, parent_env)
92 if (locked) {
93 lockBinding(where_name, parent_env)
94 }
95 }
96 }
97 }
98 }
99
100 override_seperators = function(name, env) {
101 for (sep in c('::', "$")) {
102 if (grepl(sep, name, fixed = TRUE)) {
103 elements <- strsplit(name, sep, fixed = TRUE)
104 mangled_name <- paste(elements[[1L]][1L], elements[[1L]][2L], sep='XXX')
105
106 stub_list <- c(mangled_name)
107 if ("stub_list" %in% names(attributes(get(sep, env)))) {
108 stub_list <- c(stub_list, attributes(get(sep, env))[['stub_list']])
109 }
110
111 create_new_name <- create_create_new_name_function(stub_list, env, sep)
112 assign(sep, create_new_name, env)
113 }
114 }
115 return(if (exists('mangled_name')) mangled_name else name)
116 }
117
118 backtick <- function(x) {
119 encodeString(x, quote = "`", na.encode = FALSE)
120 }
121
122 create_create_new_name_function <- function(stub_list, env, sep)
123 {
124 force(stub_list)
125 force(env)
126 force(sep)
127
128 create_new_name <- function(pkg, func)
129 {
130 pkg_name <- deparse(substitute(pkg))
131 func_name <- deparse(substitute(func))
132 for(stub in stub_list) {
133 if (paste(pkg_name, func_name, sep='XXX') == stub) {
134 return(eval(parse(text = backtick(stub)), env))
135 }
136 }
137
138 # used to avoid recursively calling the replacement function
139 eval_env = new.env(parent=parent.frame())
140 assign(sep, eval(parse(text=paste0('`', sep, '`'))), eval_env)
141
142 code = paste(pkg_name, backtick(func_name), sep=sep)
143 return(eval(parse(text=code), eval_env))
144 }
145 attributes(create_new_name) <- list(stub_list=stub_list)
146 return(create_new_name)
147 }
148
149 build_function_tree <- function(test_env, where, where_name, depth)
150 {
151 func_dict = new.env()
152 func_dict[[where_name]] = where
153 tree = list(
154 # one depth
155 list(
156 # one parent
157 list(parent_env=test_env, funcs=func_dict)
158 )
159 )
160
161 if (depth > 1) {
162 for (d in 2:depth) {
163 num_parents = 0
164 new_depth = list()
165 for (funcs in tree[[d - 1]]) {
166 parent_dict = funcs[['funcs']]
167 for (parent_name in ls(parent_dict, all.names=TRUE)) {
168 func_dict = new.env()
169 parent_env = environment(get(parent_name, parent_dict))
170 for (func_name in ls(parent_env, all.names=TRUE)) {
171 func = get(func_name, parent_env)
172 if (is.function(func)) {
173 func_dict[[func_name]] = func
174 }
175 }
176
177 new_parent = list(parent_env=parent_env, funcs=func_dict)
178 num_parents = num_parents + 1
179 new_depth[[num_parents]] = new_parent
180 }
181 }
182 tree[[d]] = new_depth
183 }
184 }
185 return(tree)
186 }