-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtest-FunctionReporter-class.R
282 lines (237 loc) · 9.49 KB
/
test-FunctionReporter-class.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
##### TESTS #####
## PUBLIC INTERFACE ##
test_that('FunctionReporter public interface is as expected', {
publicInterfaceExpected <- c(
# R6 Special Methods
".__enclos_env__"
, "clone"
# Graph Reporter fields and active bindings
, "pkg_name"
, "nodes"
, "edges"
, "pkg_graph"
, "network_measures"
, "graph_viz"
, "layout_type"
# Graph Reporter methods
, "set_package"
, "calculate_default_measures"
, "get_summary_view"
, "report_markdown_path"
# FunctionReporter-specific
# none
)
reporter <- pkgnet::FunctionReporter$new()
expect_setequal(object = names(reporter)
, expected = publicInterfaceExpected)
})
### USAGE OF PUBLIC AND PRIVATE METHODS AND FIELDS
test_that('FunctionReporter works end-to-end for typical use', {
testObj <- FunctionReporter$new()
# inherited set_package works, with pkg_path
expect_silent({
testObj$set_package(
pkg_name = "baseballstats"
# Covr only works on source code. find.package path will not work
# covr also requires an absolute path, which is provided by system.file
, pkg_path = system.file("baseballstats"
, package = "pkgnet"
, lib.loc = Sys.getenv('PKGNET_TEST_LIB')
)
)
})
# pkg_name works
expect_equal(object = testObj$pkg_name
, expected = "baseballstats"
, info = "$pkg_name did not return expected package name")
## Node and Edge extraction work ##
expect_silent({
testObj$nodes
testObj$edges
})
expect_true(data.table::is.data.table(testObj$nodes))
expect_true(object = is.element("node", names(testObj$nodes))
, info = "Node column created")
expect_true(data.table::is.data.table(testObj$edges))
expect_true(object = all(c("TARGET", "SOURCE") %in% names(testObj$edges))
, info = "TARGET and SOURCE fields in edge table at minimum")
## pkg_graph works ##
expect_silent({testObj$pkg_graph})
expect_true({"AbstractGraph" %in% class(testObj$pkg_graph)})
expect_true({"DirectedGraph" %in% class(testObj$pkg_graph)})
expect_true({igraph::is_igraph(testObj$pkg_graph$igraph)})
expect_setequal(
object = igraph::vertex_attr(testObj$pkg_graph$igraph)[['name']]
, expected = testObj$nodes[, node]
)
expect_setequal(
object = igraph::as_edgelist(testObj$pkg_graph$igraph)[,1]
, expected = testObj$edges[, SOURCE]
)
expect_setequal(
object = igraph::as_edgelist(testObj$pkg_graph$igraph)[,2]
, expected = testObj$edges[, TARGET]
)
## calculate_default_measures works ##
expect_true({
testObj$calculate_default_measures()
TRUE
})
# Default node measures were generated
expect_true({
all(testObj$pkg_graph$default_node_measures %in% names(testObj$nodes))
})
# Default graph measures were generated
expect_true({
all(testObj$pkg_graph$default_graph_measures %in% names(testObj$network_measures))
})
# Coverage measures were generated
expect_true(object = all( c("coverageRatio"
, "meanCoveragePerLine"
, "totalLines"
, "coveredLines"
, "filename")
%in% names(testObj$nodes))
, info = "Not all expected function coverage measures are in nodes table"
)
## graph_viz works ##
expect_silent({testObj$graph_viz})
expect_true(object = is.element("visNetwork", attributes(testObj$graph_viz)))
expect_equivalent(
object = as.data.table(testObj$graph_viz$x$nodes)[, .(id)]
, expected = testObj$nodes[, .(id = node)]
, ignore.col.order = TRUE
, ignore.row.order = TRUE
)
expect_equivalent(
object = as.data.table(testObj$graph_viz$x$edges)[, .(from, to)]
, expected = testObj$edges[, .(from = SOURCE, to = TARGET)]
, ignore.col.order = TRUE
, ignore.row.order = TRUE
)
})
test_that('FunctionReporter can directly generate pkg_graph', {
testObj <- FunctionReporter$new()$set_package("baseballstats")
expect_silent(testObj$pkg_graph)
expect_true("AbstractGraph" %in% class(testObj$pkg_graph))
expect_true(object = igraph::is_igraph(testObj$pkg_graph$igraph)
, info = "Package graph did not successfuly generate igraph object")
})
test_that('FunctionReporter can directly generate graph_viz', {
testObj <- FunctionReporter$new()$set_package("baseballstats")
expect_silent({testObj$graph_viz})
expect_true(object = is.element("visNetwork", attributes(testObj$graph_viz)))
})
test_that("FunctionReporter does not let you set_package twice", {
expect_error({
x <- FunctionReporter$new()
x$set_package("baseballstats")
x$set_package("baseballstats")
}, regexp = "A package has already been set for this reporter")
})
test_that("FunctionReporter rejects bad packages with an informative error", {
expect_error({
testObj <- FunctionReporter$new()
testObj$set_package(
pkg_name = "w0uldNEverB33aPackageName"
)
}, regexp = "pkgnet could not find an installed package named 'w0uldNEverB33aPackageName'. Please install the package first.")
})
test_that("FunctionReporter rejects bad pkg_path with an informative error", {
expect_error({
x <- FunctionReporter$new()
x$set_package(pkg_name = "baseballstats", pkg_path = "hopefully/not/a/real/path")
}, regexp = "Package directory does not exist: hopefully/not/a/real/path")
})
test_that("set_package works with relative pkg_path",{
# set_package works
expect_silent({
testObj <- FunctionReporter$new()
# testing set_package with a pkg_path that is relative to the current directory
entry_wd <- getwd()
baseball_dir <- system.file(
'baseballstats'
, package='pkgnet'
, lib.loc = Sys.getenv('PKGNET_TEST_LIB')
)
parent_dir <- dirname(baseball_dir)
setwd(parent_dir)
testObj$set_package(
pkg_name = "baseballstats"
, pkg_path = 'baseballstats'
)
setwd(entry_wd)
})
# Sometimes with R CMD CHECK the temp dir begins /private/vars. Other times, just /vars.
# Also, sometimes there are double slashes.
fmtPath <- function(path){
out <- gsub('^/private', '', path)
out <- gsub('//', '/', out)
out <- tools::file_path_as_absolute(out)
return(out)
}
# Correct path
expect_identical(
object = fmtPath(testObj$.__enclos_env__$private$pkg_path)
, expected = fmtPath(baseball_dir)
, info = "set_package did not use the absolute path of the directory"
)
})
test_that('FunctionReporter$report_markdown_path returns path to real file', {
reporter <- FunctionReporter$new()
expect_true(is.character(reporter$report_markdown_path))
expect_true(file.exists(reporter$report_markdown_path))
})
### NETWORK EXTRACTION HELPER FUNCTIONS ###
test_that(".parse_function correctly parses expressions for symbols", {
# Correctly parses body of function and finds all function symbols
expect_true({
myfunc <- function() {
x <- innerfunc1()
y <- innerfunc2()
z <- innerfunc3(innerfunc4())
2+2
}
result <- pkgnet:::.parse_function(body(myfunc))
all(c("innerfunc1", "innerfunc2", "innerfunc3", "innerfunc4") %in% result)
})
})
test_that(".parse_function correctly ignores right side of list extraction", {
# Correctly keeps left side of $ but drops right side of $
expect_true({
result <- pkgnet:::.parse_function(quote(myfunc()$listitem))
"myfunc" %in% result & !("listitem" %in% result)
})
})
test_that(".parse_R6_expression correctly parses expressions for symbols", {
# Correctly parses body of function and finds all function symbols
expect_true({
myr6method <- function() {
x <- regularfunc1()
z <- regularfunc2(regularfunc3())
self$public_method()
self$active_binding <- "new_value"
private$private_method
2+2
}
result <- pkgnet:::.parse_R6_expression(body(myr6method))
all(c("regularfunc1", "regularfunc2", "regularfunc3", "self$public_method"
, "self$active_binding", "private$private_method"
) %in% result)
})
})
test_that(".parse_R6_expression correctly ignores right side of list extraction", {
# Correctly keeps left side of $ but drops right side of $ for non-keywords
expect_true({
result <- pkgnet:::.parse_function(quote(myfunc()$listitem))
"myfunc" %in% result & !("listitem" %in% result)
})
})
test_that("FunctionReporter R6 edge extraction handles case where all methods have the same number of dependencies", {
testObj <- FunctionReporter$new()$set_package('silverstein')
expect_equal(testObj$edges,
data.table::data.table(SOURCE = c("Carrots$private_methods$finalize",
"Carrots$public_methods$initialize"),
TARGET = c("couplet_2",
"couplet_1"),key = c("SOURCE","TARGET")))
})