-
Notifications
You must be signed in to change notification settings - Fork 36
/
test-PackageReport.R
205 lines (172 loc) · 5.97 KB
/
test-PackageReport.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
context("PackageReport Class Tests")
rm(list = ls())
##### TEST SET UP #####
# Set flag to suppress browser opening
Sys.setenv(PKGNET_SUPPRESS_BROWSER = TRUE)
REPORTERS <- c("SummaryReporter"
, "DependencyReporter"
, "FunctionReporter"
, "InheritanceReporter"
)
MILNE_PKG_PATH <- system.file(
"milne"
, package = "pkgnet"
, lib.loc = Sys.getenv('PKGNET_TEST_LIB')
)
##### TESTS #####
## PUBLIC INTERFACE ##
test_that('PackageReport public interface is as expected', {
publicInterfaceExpected <- c(
# R6 Special Methods
".__enclos_env__"
, "initialize"
, "clone"
# Class fields and methods
, "pkg_name"
, "pkg_path"
, "report_path"
, "add_reporter"
, "render_report"
, REPORTERS
)
reportObj <- PackageReport$new(pkg_name = 'baseballstats')
expect_setequal(object = names(reportObj)
, expected = publicInterfaceExpected)
})
### USAGE OF PUBLIC METHODS AND FIELDS ###
test_that('PackageReport can correctly initialize reporters with active bindings', {
reporterObj <- PackageReport$new(pkg_name = 'milne')
for (reporter in REPORTERS) {
# Reporters are null if not assigned
expect_true(is.null(reporterObj[[reporter]]))
# Assign reporter
reporterObj[[reporter]] <- getNamespace('pkgnet')[[reporter]]$new()
# Reporter assigned correctly
expect_true(inherits(reporterObj[[reporter]], 'AbstractPackageReporter'))
expect_true(inherits(reporterObj[[reporter]], reporter))
expect_true(reporterObj[[reporter]]$pkg_name == 'milne')
}
})
test_that('PackageReport can correctly initialize reporters with active bindings', {
reporterObj <- PackageReport$new(pkg_name = 'milne')
for (reporter in REPORTERS) {
# Reporters are null if not assigned
expect_true(is.null(reporterObj[[reporter]]))
# Assign reporter
reporterObj$add_reporter(getNamespace('pkgnet')[[reporter]]$new())
# Reporter assigned correctly
expect_true(inherits(reporterObj[[reporter]], 'AbstractPackageReporter'))
expect_true(inherits(reporterObj[[reporter]], reporter))
expect_true(reporterObj[[reporter]]$pkg_name == 'milne')
}
})
test_that('PackageReport works with pkg_path', {
# Package path
reporterObj <- PackageReport$new(
pkg_name = "milne"
, pkg_path = MILNE_PKG_PATH
)
reporterObj$DependencyReporter <- DependencyReporter$new()
expect_true(reporterObj$pkg_path == MILNE_PKG_PATH)
# The below test fails on macOS CI for some mysterious reason.
#expect_true(reporterObj$DependencyReporter$.__enclos_env__$private$pkg_path == MILNE_PKG_PATH)
})
test_that('PackageReport correctly renders reports', {
testReportPath <- tempfile(
pattern = "baseball"
, fileext = ".html"
)
reporterObj <- PackageReport$new(
pkg_name = 'milne'
, report_path = testReportPath
)
for (reporter in REPORTERS) {
reporterObj[[reporter]] <- getNamespace('pkgnet')[[reporter]]$new()
}
reporterObj$render_report()
expect_true(file.exists(testReportPath) && file.size(testReportPath) > 0)
})
test_that('PackageReport correctly renders reports with pkg_path', {
testReportPath <- tempfile(
pattern = "baseball"
, fileext = ".html"
)
reporterObj <- PackageReport$new(
pkg_name = 'milne'
, pkg_path = MILNE_PKG_PATH
, report_path = testReportPath
)
for (reporter in REPORTERS) {
reporterObj[[reporter]] <- getNamespace('pkgnet')[[reporter]]$new()
}
reporterObj$render_report()
expect_true(file.exists(testReportPath) && file.size(testReportPath) > 0)
})
### EXPECTED ERRORS ###
test_that("PackageReport rejects bad package names with an informative error", {
expect_error({
PackageReport$new(
pkg_name = "w0uldNEverB33aPackageName"
)
}, regexp = "pkgnet could not find an installed package named 'w0uldNEverB33aPackageName'. Please install the package first.")
})
test_that("PackageReport rejects bad pkg_path arguments", {
expect_error({
PackageReport$new(
pkg_name = "baseballstats"
, pkg_path = "over_there"
)
})
})
test_that("PackageReport rejects bad report_path arguments", {
expect_error({
PackageReport$new(
pkg_name = "base"
, report_path = "~"
)
}, regexp = "report_path must be a \\.html file")
expect_error({
PackageReport$new(
pkg_name = "base"
, report_path = "blegh.pdf"
)
}, regexp = "report_path must be a \\.html file")
expect_error({
PackageReport$new(
pkg_name = "base"
, report_path = list("blegh.html")
)
}, regexp = "report_path is not a string")
})
test_that("PackageReport rejects wrong reporter assignments", {
expect_error({
reporterObj <- PackageReport$new("baseballstats")
reporterObj$DependencyReporter <- 'foo'
})
expect_error({
reporterObj <- PackageReport$new("baseballstats")
reporterObj$add_reporter('foo')
})
expect_error({
reporterObj <- PackageReport$new("baseballstats")
reporterObj$DependencyReporter <- DependencyReporter
}
, regexp = "You specified an R6 class generator for class DependencyReporter"
)
expect_error({
reporterObj <- PackageReport$new("baseballstats")
reporterObj$add_reporter(DependencyReporter)
}
, regexp = "You specified an R6 class generator for class DependencyReporter"
)
expect_error({
reporterObj <- PackageReport$new("baseballstats")
reporterObj$DependencyReporter <- FunctionReporter$new()
}
, regexp = "reporter does not inherit from class DependencyReporter"
)
})
##### TEST TEAR DOWN #####
Sys.unsetenv("PKGNET_SUPPRESS_BROWSER")
rm(list = ls())
closeAllConnections()