Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support shorthand syntax reporter1+reporter2+... #307

Merged
merged 11 commits into from
Feb 19, 2016
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# testthat 0.11.0.9000

* `find_reporter()` (and also all high-level testing functions) support a vector of reporters. For more than one reporter, a `MultiReporter` is created (#307, @krlmlr).

* `with_reporter()` is used internally and gains new argument `start_end_reporter = TRUE` (@krlmlr, 355).

* `expect_success()` and `expect_failure()` are new expectations designed
Expand Down
13 changes: 12 additions & 1 deletion R/reporter-zzz.r
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,23 @@ with_reporter <- function(reporter, code, start_end_reporter = TRUE) {
#' Find reporter object given name or object.
#'
#' If not found, will return informative error message.
#' Pass a character vector to create a \code{\link{MultiReporter}} composed
#' of individual reporters.
#' Will return null if given NULL.
#'
#' @param reporter name of reporter
#' @param reporter name of reporter(s), or reporter object(s)
#' @keywords internal
find_reporter <- function(reporter) {
if (is.null(reporter)) return(NULL)

if (length(reporter) <= 1L) {
find_reporter_one(reporter)
} else {
MultiReporter$new(reporters = lapply(reporter, find_reporter_one))
}
}

find_reporter_one <- function(reporter) {
if (inherits(reporter, "Reporter")) return(reporter)

name <- reporter
Expand Down
4 changes: 3 additions & 1 deletion man/find_reporter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions tests/testthat/test-reporter.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ test_that("can locate reporter from name", {
expect_that(find_reporter("tap"), equals(TapReporter$new()))
expect_that(find_reporter("list"), equals(ListReporter$new()))
expect_that(find_reporter("multi"), equals(MultiReporter$new()))
expect_that(find_reporter(""), equals(Reporter$new()))
expect_equal(
find_reporter(c("summary", "stop")),
MultiReporter$new(
reporters = list(SummaryReporter$new(), StopReporter$new())))
expect_equal(
find_reporter(c("teamcity", "summary", "list")),
MultiReporter$new(
reporters = list(TeamcityReporter$new(), SummaryReporter$new(),
ListReporter$new())))

expect_that(find_reporter("blah"),
throws_error("Can not find test reporter blah"))
expect_error(find_reporter(c("summary", "blah")),
"Can not find test reporter blah")
})