-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathreporter-list.R
150 lines (122 loc) Β· 3.81 KB
/
reporter-list.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
#' @include reporter.R
NULL
methods::setOldClass("proc_time")
#' List reporter: gather all test results along with elapsed time and
#' file information.
#'
#' This reporter gathers all results, adding additional information such as
#' test elapsed time, and test filename if available. Very useful for reporting.
#'
#' @export
#' @family reporters
ListReporter <- R6::R6Class("ListReporter",
inherit = Reporter,
public = list(
current_start_time = NA,
current_expectations = NULL,
current_file = NULL,
results = NULL,
initialize = function() {
super$initialize()
self$results <- Stack$new()
},
start_test = function(context, test) {
self$current_expectations <- Stack$new()
self$current_start_time <- proc.time()
},
add_result = function(context, test, result) {
if (!is.null(self$current_expectations)) {
self$current_expectations$push(result)
}
},
end_test = function(context, test) {
elapsed <- as.double(proc.time() - self$current_start_time)
self$results$push(list(
file = self$current_file %||% NA_character_,
context = context,
test = test,
user = elapsed[1],
system = elapsed[2],
real = elapsed[3],
results = self$current_expectations$as_list()
))
},
start_file = function(name) {
self$current_file <- name
},
get_results = function() {
testthat_results(self$results$as_list())
}
)
)
#' Create a `testthat_results` object from the test results
#' as stored in the ListReporter results field.
#'
#' @param results a list as stored in ListReporter
#' @return its list argument as a `testthat_results` object
#' @seealso ListReporter
#' @keywords internal
testthat_results <- function(results) {
stopifnot(is.list(results))
structure(results, class = "testthat_results")
}
# return if all tests are successful w/o error
all_passed <- function(res) {
if (length(res) == 0) {
return(TRUE)
}
df <- as.data.frame.testthat_results(res)
sum(df$failed) == 0 && all(!df$error)
}
any_warnings <- function(res) {
if (length(res) == 0) {
return(FALSE)
}
df <- as.data.frame.testthat_results(res)
any(df$warning > 0)
}
#' @export
as.data.frame.testthat_results <- function(x, ...) {
if (length(x) == 0) {
return(data.frame())
}
rows <- lapply(x, summarize_one_test_results)
do.call(rbind, rows)
}
summarize_one_test_results <- function(test) {
test_results <- test$results
nb_tests <- length(test_results)
nb_failed <- nb_skipped <- nb_warning <- nb_passed <- 0L
error <- FALSE
if (nb_tests > 0) {
# error reports should be handled differently.
# They may not correspond to an expect_that() test so remove them
last_test <- test_results[[nb_tests]]
error <- expectation_error(last_test)
if (error) {
test_results <- test_results[-nb_tests]
nb_tests <- length(test_results)
}
nb_passed <- sum(vapply(test_results, expectation_success, logical(1)))
nb_skipped <- sum(vapply(test_results, expectation_skip, logical(1)))
nb_failed <- sum(vapply(test_results, expectation_failure, logical(1)))
nb_warning <- sum(vapply(test_results, expectation_warning, logical(1)))
}
context <- if (length(test$context) > 0) test$context else ""
res <- data.frame(
file = test$file, context = context, test = test$test,
nb = nb_tests, failed = nb_failed, skipped = as.logical(nb_skipped),
error = error, warning = nb_warning,
user = test$user, system = test$system, real = test$real,
stringsAsFactors = FALSE
)
# Added at end for backward compatibility
res$passed <- nb_passed
# Cannot easily add list columns in data.frame()
res$result <- list(test_results)
res
}
#' @export
print.testthat_results <- function(x, ...) {
print(as.data.frame(x))
}