Skip to content

Commit

Permalink
provide the environment to find_globals()
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Apr 29, 2024
1 parent d65c1b2 commit 15d5447
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tests/test-ci/test-cache.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
library(testit)

assert('find_globals() identifies global variables', {
env = list2env(
list(qwer = 1, g = 2, y = 3, d = 4, z = 5), parent = emptyenv()
)
# nothing from outside environment
(find_globals('x=1') %==% character(0))
(find_globals('x=1', env) %==% character(0))
# qwer must be created outside somewhere
(find_globals('a=1; b=a; d=qwer') %==% 'qwer')
(find_globals('a=function(){f=2;g}') %==% 'g')
(find_globals('a=1; b=a; d=qwer', env) %==% 'qwer')
(find_globals('a=function(){f=2;g}', env) %==% 'g')
# y was assigned locally in z, but there is another y outside from nowhere
(find_globals('z=function(){y=1};y') %==% 'y')
(find_globals('z=function(){y=1};y', env) %==% 'y')
# more complicated cases: operators, subscripts, ...
(find_globals(c('a=1%*%1%o%2 %in% d', 'b=d%%10+3%/%2-z[1:3]')) %==% c('d', 'z'))
(find_globals(c('a=1%*%1%o%2 %in% d', 'b=d%%10+3%/%2-z[1:3]'), env) %==% c('d', 'z'))
})

0 comments on commit 15d5447

Please sign in to comment.