diff --git a/R/expect-named.R b/R/expect-named.R index 4f8cb10c6..4a71ca121 100644 --- a/R/expect-named.R +++ b/R/expect-named.R @@ -35,7 +35,6 @@ expect_named <- function( check_bool(ignore.order) check_bool(ignore.case) - act <- quasi_label(enquo(object), label) if (missing(expected)) { diff --git a/R/expectation.R b/R/expectation.R index 55f00845a..98d89de6f 100644 --- a/R/expectation.R +++ b/R/expectation.R @@ -227,13 +227,14 @@ single_letter_summary <- function(x) { } expectation_location <- function(x, prefix = "", suffix = "") { - srcref <- x$srcref + srcref_location(x$srcref, prefix = prefix, suffix = suffix) +} +srcref_location <- function(srcref, prefix = "", suffix = "") { if (!inherits(srcref, "srcref")) { return("") } filename <- attr(srcref, "srcfile")$filename - cli::format_inline( - "{prefix}{.file {filename}:{srcref[1]}:{srcref[2]}}{suffix}" - ) + link <- cli::format_inline("{.file {filename}:{srcref[1]}:{srcref[2]}}") + paste0(prefix, link, suffix) } diff --git a/R/parallel.R b/R/parallel.R index f82be232d..a7d5270f9 100644 --- a/R/parallel.R +++ b/R/parallel.R @@ -376,10 +376,10 @@ SubprocessReporter <- R6::R6Class( private$filename <- filename private$event("start_file", filename) }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { private$context <- context private$test <- test - private$event("start_test", context, test) + private$event("start_test", context, test, srcref) }, start_context = function(context) { private$context <- context diff --git a/R/reporter-junit.R b/R/reporter-junit.R index 12442f894..df9293ea8 100644 --- a/R/reporter-junit.R +++ b/R/reporter-junit.R @@ -69,7 +69,7 @@ JunitReporter <- R6::R6Class( self$file_name <- file }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { if (is.null(context)) { context_start_file(self$file_name) } diff --git a/R/reporter-list.R b/R/reporter-list.R index f02214a0d..4c78f5c91 100644 --- a/R/reporter-list.R +++ b/R/reporter-list.R @@ -24,7 +24,7 @@ ListReporter <- R6::R6Class( self$results <- Stack$new() }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { if ( !identical(self$current_context, context) || !identical(self$current_test, test) diff --git a/R/reporter-location.R b/R/reporter-location.R index 5e5c384b5..10cd3f587 100644 --- a/R/reporter-location.R +++ b/R/reporter-location.R @@ -10,8 +10,16 @@ LocationReporter <- R6::R6Class( "LocationReporter", inherit = Reporter, public = list( - start_test = function(context, test) { - self$cat_line("Start test: ", test) + depth = 0, + + start_test = function(context, test, srcref = NULL) { + self$depth <- self$depth + 1 + + self$cat_line( + "Start test: ", + test, + srcref_location(srcref, prefix = " (", suffix = ")") + ) }, add_result = function(context, test, result) { @@ -20,8 +28,12 @@ LocationReporter <- R6::R6Class( }, end_test = function(context, test) { + self$depth <- self$depth - 1 self$cat_line("End test: ", test) - self$cat_line() + + if (self$depth == 0) { + self$cat_line() + } } ) ) diff --git a/R/reporter-multi.R b/R/reporter-multi.R index 0a49f3e39..5aa641bba 100644 --- a/R/reporter-multi.R +++ b/R/reporter-multi.R @@ -26,8 +26,8 @@ MultiReporter <- R6::R6Class( start_context = function(context) { o_apply(self$reporters, "start_context", context) }, - start_test = function(context, test) { - o_apply(self$reporters, "start_test", context, test) + start_test = function(context, test, srcref = NULL) { + o_apply(self$reporters, "start_test", context, test, srcref) }, add_result = function(context, test, result) { o_apply( diff --git a/R/reporter-slow.R b/R/reporter-slow.R index bd228d274..27d6540c0 100644 --- a/R/reporter-slow.R +++ b/R/reporter-slow.R @@ -38,7 +38,7 @@ SlowReporter <- R6::R6Class( self$current_file <- file }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { self$current_test_start <- proc.time()[[3]] }, diff --git a/R/reporter-stop.R b/R/reporter-stop.R index 4c9f2a67b..e169c22eb 100644 --- a/R/reporter-stop.R +++ b/R/reporter-stop.R @@ -31,7 +31,7 @@ StopReporter <- R6::R6Class( self$stop_reporter <- stop_reporter }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { self$issues <- Stack$new() }, diff --git a/R/reporter-teamcity.R b/R/reporter-teamcity.R index d14bdae60..8125fcb21 100644 --- a/R/reporter-teamcity.R +++ b/R/reporter-teamcity.R @@ -27,7 +27,7 @@ TeamcityReporter <- R6::R6Class( self$cat_line() }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { private$report_event("testSuiteStarted", test) self$i <- 1L }, diff --git a/R/reporter-timing.R b/R/reporter-timing.R index 84a89633e..026966a12 100644 --- a/R/reporter-timing.R +++ b/R/reporter-timing.R @@ -31,7 +31,7 @@ SlowReporter <- R6::R6Class( self$current_file <- file }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { self$current_test_start <- proc.time()[[3]] }, diff --git a/R/reporter.R b/R/reporter.R index dc7998255..6f849af39 100644 --- a/R/reporter.R +++ b/R/reporter.R @@ -29,7 +29,7 @@ Reporter <- R6::R6Class( capabilities = list(parallel_support = FALSE, parallel_updates = FALSE), start_reporter = function() {}, start_context = function(context) {}, - start_test = function(context, test) {}, + start_test = function(context, test, srcref = NULL) {}, start_file = function(filename) {}, add_result = function(context, test, result) {}, end_test = function(context, test) {}, diff --git a/R/snapshot-reporter-parallel.R b/R/snapshot-reporter-parallel.R index 4306e48c6..665cbbb89 100644 --- a/R/snapshot-reporter-parallel.R +++ b/R/snapshot-reporter-parallel.R @@ -34,10 +34,10 @@ SubprocessSnapshotReporter <- R6::R6Class( super$end_test(context, test) }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { private$context <- context private$test <- test - super$start_test(context, test) + super$start_test(context, test, srcref) }, announce_file_snapshot = function(name) { diff --git a/R/snapshot-reporter.R b/R/snapshot-reporter.R index 4c45a0f32..9afaf657b 100644 --- a/R/snapshot-reporter.R +++ b/R/snapshot-reporter.R @@ -58,7 +58,7 @@ SnapshotReporter <- R6::R6Class( } }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { if (is.null(test)) { return() } diff --git a/R/test-that.R b/R/test-that.R index efafcb108..94e423437 100644 --- a/R/test-that.R +++ b/R/test-that.R @@ -51,7 +51,12 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) { test <- test_description() if (!is.null(test)) { - reporter$start_test(context = reporter$.context, test = test) + srcref <- attr(code, "srcref")[[1]] + reporter$start_test( + context = reporter$.context, + test = test, + srcref = srcref + ) withr::defer(reporter$end_test(context = reporter$.context, test = test)) } diff --git a/tests/testthat/_snaps/reporter-location.md b/tests/testthat/_snaps/reporter-location.md index 9122561c1..c332d85da 100644 --- a/tests/testthat/_snaps/reporter-location.md +++ b/tests/testthat/_snaps/reporter-location.md @@ -1,36 +1,54 @@ # reporter as expected - Start test: Success + Start test: Success ('reporters/tests.R:5:22') 'reporters/tests.R:6:3' [success] End test: Success - Start test: Failure:1 + Start test: Failure:1 ('reporters/tests.R:11:24') 'reporters/tests.R:13:3' [failure] End test: Failure:1 - Start test: Failure:2a + Start test: Failure:2a ('reporters/tests.R:16:25') 'reporters/tests.R:17:8' [failure] End test: Failure:2a - Start test: Error:1 + Start test: Error:1 ('reporters/tests.R:23:22') 'reporters/tests.R:24:3' [error] End test: Error:1 - Start test: errors get tracebacks + Start test: errors get tracebacks ('reporters/tests.R:27:36') 'reporters/tests.R:30:8' [error] End test: errors get tracebacks - Start test: explicit skips are reported + Start test: explicit skips are reported ('reporters/tests.R:37:42') 'reporters/tests.R:38:3' [skip] End test: explicit skips are reported - Start test: empty tests are implicitly skipped + Start test: empty tests are implicitly skipped ('reporters/tests.R:41:49') 'reporters/tests.R:41:1' [skip] End test: empty tests are implicitly skipped - Start test: warnings get backtraces + Start test: warnings get backtraces ('reporters/tests.R:45:38') 'reporters/tests.R:47:5' [warning] 'reporters/tests.R:45:1' [skip] End test: warnings get backtraces +--- + + Start test: x1 ('reporters/nested.R:1:16') + Start test: x1 / y1 ('reporters/nested.R:2:18') + Start test: x1 / y1 / z1 ('reporters/nested.R:3:20') + 'reporters/nested.R:4:7' [success] + End test: x1 / y1 / z1 + Start test: x1 / y1 / z2 ('reporters/nested.R:7:20') + 'reporters/nested.R:8:7' [failure] + End test: x1 / y1 / z2 + End test: x1 / y1 + End test: x1 + + Start test: x2 ('reporters/nested.R:13:16') + 'reporters/nested.R:14:3' [success] + End test: x2 + + diff --git a/tests/testthat/reporters/nested.R b/tests/testthat/reporters/nested.R new file mode 100644 index 000000000..f4de69f42 --- /dev/null +++ b/tests/testthat/reporters/nested.R @@ -0,0 +1,15 @@ +describe("x1", { + describe("y1", { + describe("z1", { + expect_true(TRUE) + }) + + describe("z2", { + expect_true(FALSE) + }) + }) +}) + +describe("x2", { + expect_true(TRUE) +}) diff --git a/tests/testthat/test-context.R b/tests/testthat/test-context.R index 9322a2adb..e0fc6aadd 100644 --- a/tests/testthat/test-context.R +++ b/tests/testthat/test-context.R @@ -15,7 +15,7 @@ CountReporter <- R6::R6Class( self$context_i <- self$context_i - 1 stopifnot(self$context_i >= 0) }, - start_test = function(context, test) { + start_test = function(context, test, srcref = NULL) { self$test_count <- self$test_count + 1 self$test_i <- self$test_i + 1 }, diff --git a/tests/testthat/test-reporter-location.R b/tests/testthat/test-reporter-location.R index 2eb33022d..2a11a283d 100644 --- a/tests/testthat/test-reporter-location.R +++ b/tests/testthat/test-reporter-location.R @@ -1,3 +1,10 @@ test_that("reporter as expected", { expect_snapshot_reporter(LocationReporter$new()) }) + +test_that("reporter as expected", { + expect_snapshot_reporter( + LocationReporter$new(), + test_path("reporters/nested.R") + ) +})