Debian Codegraph @

tests/testthat/test-namespace.R

raw
1 test_that("with_package works", {
2
3 # tools package not attached to the search path
4 expect_false("package:tools" %in% search())
5
6 with_package("tools",
7
8 # SIGINT is an exported object in tools
9 expect_equal(SIGINT, 2))
10
11 # tools package still not attached to the search path
12 expect_false("package:tools" %in% search())
13 })
14
15 test_that("local_package works", {
16
17 # tools package not attached to the search path
18 expect_false("package:tools" %in% search())
19
20 f <- function() {
21 local_package("tools")
22
23 # SIGINT is an exported object in tools
24 expect_equal(SIGINT, 2)
25 }
26
27 f()
28
29 # tools package still not attached to the search path
30 expect_false("package:tools" %in% search())
31 })
32
33 test_that("with_namespace works", {
34
35 # tools package not attached to the search path
36 expect_false("<environment: namespace:tools>" %in% search())
37
38 with_namespace("tools", {
39
40 expect_true("<environment: namespace:tools>" %in% search())
41
42 # .check_packages is a non-exported object in tools
43 expect_true(is.function(.check_packages))
44 })
45
46 # tools namespace still not attached to the search path
47 expect_false("<environment: namespace:tools>" %in% search())
48 })
49
50 test_that("local_namespace works", {
51
52 # tools package not attached to the search path
53 expect_false("<environment: namespace:tools>" %in% search())
54
55 f <- function() {
56 local_namespace("tools")
57
58 expect_true("<environment: namespace:tools>" %in% search())
59
60 # .check_packages is a non-exported object in tools
61 expect_true(is.function(.check_packages))
62 }
63
64 f()
65
66 # tools namespace still not attached to the search path
67 expect_false("<environment: namespace:tools>" %in% search())
68 })
69
70 test_that("with_environment works", {
71
72 e <- new.env()
73 e$a <- 1
74
75 # environment not attached to the search path
76 expect_false(format(e) %in% search())
77
78 with_environment(e, {
79
80 # environment attached to the search path
81 expect_true(format(e) %in% search())
82 expect_equal(a, 1)
83 })
84
85 # environment not attached to the search path
86 expect_false(format(e) %in% search())
87 })
88
89 test_that("local_environment works", {
90
91 e <- new.env()
92 e$a <- 1
93
94 # environment not attached to the search path
95 expect_false(format(e) %in% search())
96
97 f <- function() {
98 local_environment(e)
99
100 # environment attached to the search path
101 expect_true(format(e) %in% search())
102 expect_equal(a, 1)
103 }
104
105 f()
106
107 # environment not attached to the search path
108 expect_false(format(e) %in% search())
109 })