-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-static-analysis.R
343 lines (297 loc) · 10.2 KB
/
test-static-analysis.R
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
context("Static analysis")
## Collection ###########################################################
test_that("we can collect the symbols in a simple expression", {
expr <- quote(42)
res <- collect_assigned_symbols_in_expression(expr, env = environment())
expect_equal(res, list())
expr <- quote(x <- 42)
res <- collect_assigned_symbols_in_expression(expr, env = environment())
expect_equal(res, list(locals = "x"))
})
test_that("we can collect the symbols in a simple function", {
f <- function() {
42
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, character(0))
f <- function() {
x <- 42
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, "x")
f <- function() {
x <- 42
y <- 24
x + y
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, c("x", "y"))
f <- function(x = 42) {
y <- 24
x + y
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, "y")
# we shouldn't include formal arguments
f <- function(x = 42) {
x <- 42
y <- 24
x + y
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, "y")
# we shouldn't include duplications
f <- function(x = 42) {
y <- x
x <- 42
y <- x + 2
x + y
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, "y")
})
test_that("we can collect the symbols when there are for-loops", {
f <- function(n) {
for (i in 1:n) i
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, "i")
f <- function(n) {
x <- 0
for (i in 1:n) {
x <- x + i
}
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, c("x", "i"))
})
test_that("we don't collect symbols we know are in a different scope", {
f <- function(df) {
with(df, z <- x + y)
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, character())
f <- function(x) {
y <- 2 * x
function(x) {
z <- 2 * x
}
}
res <- collect_assigned_symbols_in_function(f)
expect_equal(res, c("y"))
})
## Annotation ###########################################################
test_that("we can annotate with the symbols in a simple function", {
f <- function() {
42
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), character(0))
expect_equal(attr(body(res), "bound"), character(0))
f <- function() {
x <- 42
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), "x")
expect_equal(attr(body(res), "bound"), "x")
f <- function() {
x <- 42
y <- 24
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("x", "y"))
expect_equal(attr(body(res), "bound"), c("x", "y"))
# when there is a formal parameter, that is also a bound variable
# although it is not an assigned symbol.
f <- function(x = 42) {
y <- 24
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), "y")
expect_equal(attr(body(res), "bound"), c("x", "y"))
# If we analyse the full function, we might not want to consider
# formal parameters as local variables, but inside the *body*
# of this function, we do assign to `x`, so we include it in
# the annotation. It defintely belongs in the bound variables.
f <- function(x = 42) {
x <- 42
y <- 24
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("x", "y"))
expect_equal(attr(body(res), "bound"), c("x", "y"))
# we shouldn't include duplications
f <- function(x = 42) {
y <- x
x <- 42
y <- x + 2
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("y", "x"))
# the bound variables see the formal parameter first, so x before y
expect_equal(attr(body(res), "bound"), c("x", "y"))
})
test_that("we hande `=` assignments as well", {
f <- function() {
x <- 42
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), "x")
expect_equal(attr(body(res), "bound"), "x")
f <- function() {
x <- 42
y <- 24
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("x", "y"))
expect_equal(attr(body(res), "bound"), c("x", "y"))
# when there is a formal parameter, that is also a bound variable
# although it is not an assigned symbol.
f <- function(x = 42) {
y <- 24
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), "y")
expect_equal(attr(body(res), "bound"), c("x", "y"))
# If we analyse the full function, we might not want to consider
# formal parameters as local variables, but inside the *body*
# of this function, we do assign to `x`, so we include it in
# the annotation. It defintely belongs in the bound variables.
f <- function(x = 42) {
x <- 42
y <- 24
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("x", "y"))
expect_equal(attr(body(res), "bound"), c("x", "y"))
# we shouldn't include duplications
f <- function(x = 42) {
y <- x
x <- 42
y <- x + 2
x + y
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("y", "x"))
# the bound variables see the formal parameter first, so x before y
expect_equal(attr(body(res), "bound"), c("x", "y"))
})
test_that("we can annotate with symbols when there are for-loops", {
f <- function(n) {
for (i in 1:n) i
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), "i")
expect_equal(attr(body(res), "bound"), c("n", "i"))
f <- function(n) {
x <- 0
for (i in 1:n) {
x <- x + i
}
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("x", "i"))
expect_equal(attr(body(res), "bound"), c("n", "x", "i"))
})
test_that("we don't annotate with symbols we know are in a different scope", {
f <- function(df) {
with(df, z <- x + y)
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), character())
expect_equal(attr(body(res), "bound"), "df")
f <- function(x) {
y <- 2 * x
function(x) {
z <- 2 * x
}
}
res <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(res), "assigned_symbols"), c("y"))
expect_equal(attr(body(res), "bound"), c("x", "y"))
nested_body <- body(res)[[3]][[3]]
expect_equal(attr(nested_body, "assigned_symbols"), c("z"))
# since the inner function is a cosure, it cna see "y". It can also
# see x -- we have propagated both the inner and the outper, but ok
# it doesn't matter... there should just be an x in scope.
expect_equal(attr(nested_body, "bound"), c("x", "y", "z"))
})
test_that("we can handle local functions", {
f <- function(x) {
y <- 2 * x
g <- function(x) {
z <- 2 * x
}
g(y)
}
f_an <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(f_an), "assigned_symbols"), c("y", "g"))
expect_equal(attr(body(f_an), "bound"), c("x", "y", "g"))
# Don't confuse named parameters with assignments
f <- function(x) {
y <- 2 * x
g <- function(v = x) {
z <- 2 * v
}
g(y)
}
f_an <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(f_an), "assigned_symbols"), c("y", "g"))
expect_equal(attr(body(f_an), "bound"), c("x", "y", "g"))
nested_fun <- body(f_an)[[3]][[3]]
expect_equal(attr(nested_fun, "assigned_symbols"), character(0))
expect_equal(attr(nested_fun, "bound"), c("v", "x", "y", "g"))
nested_body <- nested_fun[[3]]
expect_equal(attr(nested_body, "assigned_symbols"), "z")
expect_equal(attr(nested_body, "bound"), c("v", "x", "y", "g", "z"))
f <- function(x) {
y <- 2 * x
g <- function(v = x) {
z <- 2 * v
}
g(v = y)
}
f_an <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(f_an), "assigned_symbols"), c("y", "g"))
expect_equal(attr(body(f_an), "bound"), c("x", "y", "g"))
nested_fun <- body(f_an)[[3]][[3]]
expect_equal(attr(nested_fun, "assigned_symbols"), character(0))
expect_equal(attr(nested_fun, "bound"), c("v", "x", "y", "g"))
nested_body <- nested_fun[[3]]
expect_equal(attr(nested_body, "assigned_symbols"), "z")
expect_equal(attr(nested_body, "bound"), c("v", "x", "y", "g", "z"))
# with <- in function call, we *should* consider it an assignment
f <- function(x) {
y <- 2 * x
g <- function(v = x) {
z <- 2 * v
}
g(v <- y)
}
f_an <- annotate_bound_symbols_in_function(f)
expect_equal(attr(body(f_an), "assigned_symbols"), c("y", "g", "v"))
expect_equal(attr(body(f_an), "bound"), c("x", "y", "g", "v"))
nested_fun <- body(f_an)[[3]][[3]]
expect_equal(attr(nested_fun, "assigned_symbols"), character(0))
expect_equal(attr(nested_fun, "bound"), c("v", "x", "y", "g"))
nested_body <- nested_fun[[3]]
expect_equal(attr(nested_body, "assigned_symbols"), "z")
expect_equal(attr(nested_body, "bound"), c("v", "x", "y", "g", "z"))
# We shouldn't confuse a local function with
# one from the tranformation-scope.
g <- function() stop("outer")
cb <- rewrite_callbacks() %>%
add_call_callback(g, function(expr, ...) stop("don't call this"))
expect_warning(
res <- depth_first_rewrite_function(f_an, cb),
"The function g is not processed .*"
)
expect_equal(body(f_an), body(res))
})