Skip to content

Commit 029208a

Browse files
committed
Refine find_reporter()
1 parent d8d8887 commit 029208a

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

R/reporter-zzz.R

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,31 @@ find_reporter <- function(reporter) {
8585
reporter$new()
8686
} else if (inherits(reporter, "Reporter")) {
8787
reporter
88+
} else if (is_string(reporter)) {
89+
find_reporter_one(reporter)
8890
} else if (is.character(reporter)) {
89-
if (length(reporter) <= 1L) {
90-
find_reporter_one(reporter)
91-
} else {
92-
MultiReporter$new(reporters = lapply(reporter, find_reporter_one))
93-
}
91+
reporters <- lapply(reporter, find_reporter_one, call = current_env())
92+
MultiReporter$new(reporters)
9493
} else {
95-
cli::cli_abort("Invalid input.")
94+
stop_input_type(
95+
reporter,
96+
c(
97+
"a string",
98+
"a character vector",
99+
"a reporter object",
100+
"a reporter class"
101+
)
102+
)
96103
}
97104
}
98105

99-
find_reporter_one <- function(reporter, ...) {
100-
check_string(reporter)
101-
106+
find_reporter_one <- function(reporter, ..., call = caller_env()) {
102107
name <- reporter
103108
substr(name, 1, 1) <- toupper(substr(name, 1, 1))
104109
name <- paste0(name, "Reporter")
105110

106111
if (!exists(name)) {
107-
cli::cli_abort("Cannot find test reporter {.arg {reporter}}.")
112+
cli::cli_abort("Cannot find test reporter {.arg {reporter}}.", call = call)
108113
}
109114

110115
get(name)$new(...)
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# useful error message if can't find reporter
22

3+
Code
4+
find_reporter(1)
5+
Condition
6+
Error in `find_reporter()`:
7+
! `reporter` must be a string, a character vector, a reporter object, or a reporter class, not the number 1.
8+
Code
9+
find_reporter("blah")
10+
Condition
11+
Error in `find_reporter()`:
12+
! Cannot find test reporter `blah`.
313
Code
414
find_reporter(c("summary", "blah"))
515
Condition
6-
Error in `FUN()`:
16+
Error in `find_reporter()`:
717
! Cannot find test reporter `blah`.
818

tests/testthat/test-reporter-zzz.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ test_that("can locate reporter from name", {
55

66
test_that("useful error message if can't find reporter", {
77
expect_snapshot(error = TRUE, {
8+
find_reporter(1)
9+
find_reporter("blah")
810
find_reporter(c("summary", "blah"))
911
})
1012
})

0 commit comments

Comments
 (0)