1#' Test reporter: stop on error
2#'
3#' The default reporter used when [expect_that()] is run interactively.
4#' It responds by [stop()]ping on failures and doing nothing otherwise. This
5#' will ensure that a failing test will raise an error.
6#'
7#' This should be used when doing a quick and dirty test, or during the final
8#' automated testing of R CMD check.  Otherwise, use a reporter that runs all
9#' tests and gives you more context about the problem.
10#'
11#' @export
12#' @family reporters
13StopReporter <- R6::R6Class("StopReporter",
14  inherit = Reporter,
15  public = list(
16    failures = NULL,
17    n_fail = 0L,
18    stop_reporter = TRUE,
19
20    initialize = function(stop_reporter = TRUE) {
21      super$initialize()
22      self$failures <- Stack$new()
23      self$stop_reporter <- stop_reporter
24    },
25
26    start_test = function(context, test) {
27      self$failures <- Stack$new()
28    },
29
30    add_result = function(context, test, result) {
31      if (expectation_success(result)) {
32        return()
33      }
34
35      if (expectation_broken(result)) {
36        self$n_fail <- self$n_fail + 1
37      }
38
39      self$failures$push(result)
40    },
41
42    end_test = function(context, test) {
43      self$local_user_output()
44
45      failures <- self$failures$as_list()
46      if (length(failures) == 0) {
47        self$cat_line(colourise("Test passed", "success"), " ", praise_emoji())
48        return()
49      }
50
51      messages <- vapply(failures, issue_summary, rule = TRUE, character(1))
52      self$cat_line(messages, "\n")
53    },
54    stop_if_needed = function() {
55      if (self$stop_reporter && self$n_fail > 0) {
56        abort("Test failed")
57      }
58    }
59  )
60)
61