Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion R/expect-named.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ expect_named <- function(
check_bool(ignore.order)
check_bool(ignore.case)


act <- quasi_label(enquo(object), label)

if (missing(expected)) {
Expand Down
9 changes: 5 additions & 4 deletions R/expectation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions R/parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/reporter-junit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion R/reporter-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions R/reporter-location.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
}
}
)
)
4 changes: 2 additions & 2 deletions R/reporter-multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion R/reporter-slow.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
},

Expand Down
2 changes: 1 addition & 1 deletion R/reporter-stop.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},

Expand Down
2 changes: 1 addition & 1 deletion R/reporter-teamcity.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion R/reporter-timing.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
},

Expand Down
2 changes: 1 addition & 1 deletion R/reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {},
Expand Down
4 changes: 2 additions & 2 deletions R/snapshot-reporter-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion R/snapshot-reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SnapshotReporter <- R6::R6Class(
}
},

start_test = function(context, test) {
start_test = function(context, test, srcref = NULL) {
if (is.null(test)) {
return()
}
Expand Down
7 changes: 6 additions & 1 deletion R/test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
34 changes: 26 additions & 8 deletions tests/testthat/_snaps/reporter-location.md
Original file line number Diff line number Diff line change
@@ -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


15 changes: 15 additions & 0 deletions tests/testthat/reporters/nested.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
describe("x1", {
describe("y1", {
describe("z1", {
expect_true(TRUE)
})

describe("z2", {
expect_true(FALSE)
})
})
})

describe("x2", {
expect_true(TRUE)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-context.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-reporter-location.R
Original file line number Diff line number Diff line change
@@ -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")
)
})
Loading