Skip to content

Commit 4b2f974

Browse files
authored
Merge branch 'main' into issue_2237
2 parents 620035a + 2af856e commit 4b2f974

33 files changed

+5736
-4572
lines changed

NEWS.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# testthat (development version)
22

33
* `set_state_inspector()` gains `tolerance` argument and ignores minor FP differences by default (@mcol, #2237).
4+
* `expect_vector()` fails, instead of erroring, if `object` is not a vector (@plietar, #2224).
45
* New `vignette("mocking")` explains mocking in detail (#1265).
56
* New `vignette("challenging-functions")` provides an index to other documentation organised by testing challenges (#1265).
67
* When running a test interactively, testthat now reports the number of succeses. The results should also be more useful if you are using nested tests.
78
* The hints generated by `expect_snapshot()` and `expect_snapshot_file()` now include the path to the package, if its not in the current working directory (#1577).
8-
* `expect_snapshot_file()` now clearly errors if the `path` doesnt exist (#2191).
9+
* `expect_snapshot_file()` now clearly errors if the `path` doesn't exist (#2191).
910
* `expect_snapshot_file()` now considers `.json` to be a text file (#1593).
1011
* `expect_snapshot_file()` now shows differences for text files (#1593).
1112
* The failure messages for all `expect_` functions have been rewritten to first state what was expected and then what was actually received (#2142).
@@ -20,7 +21,7 @@
2021
* `local_mock()` and `with_mock()` have been deprecated because they are no longer permitted in R 4.5.
2122
* `snapshot_review()` now passes `...` on to `shiny::runApp()` (#1928).
2223
* `expect_named()` now gives more informative errors (#2091).
23-
* `expect_*()` functions consistently and rigorously check their inputs (#1754).
24+
* `expect_*()` functions consistently and rigorously check their inputs (#1754).
2425
* `test_that()` no longer warns about the absence of `{}` since it no longer seems to be necessary.
2526
* `test_that()`, `describe()`, and `it()` can now be arbitrarily nested. Each component will skip only if it and its subtests don't contain any expectations. The interactive stop reporter has been fixed so it doesn't duplicate failures. (#2063, #2188).
2627
* Test filtering now works with `it()`, and the `desc` argument can take a character vector in order to recursively filter subtests (i.e. `it()` nested inside of `describe()`) (#2118).
@@ -39,7 +40,7 @@
3940
* New `expect_r6_class()` (#2030).
4041
* `expect_*()` functions consistently and rigorously check their inputs (#1754).
4142
* `JunitReporter()` no longer fails with `"no applicable method for xml_add_child"` for warnings outside of tests (#1913). Additionally, warnings now save their backtraces.
42-
* `JunitReporter()` strips ANSI escapes in more placese (#1852, #2032).
43+
* `JunitReporter()` strips ANSI escapes in more places (#1852, #2032).
4344
* `try_again()` is now publicised. The first argument is now the number of retries, not tries (#2050).
4445
* `vignette("custom-expectations)` has been overhauled to make it much clearer how to create high-quality expectations (#2113, #2132, #2072).
4546
* `expect_snapshot()` and friends will now fail when creating a new snapshot on CI. This is usually a signal that you've forgotten to run it locally before committing (#1461).
@@ -988,7 +989,7 @@ This release mostly focusses on an overhaul of how testthat works with condition
988989
* In `expect_equal_to_reference()`, the default value for `update` is
989990
now `FALSE` (@BrodieG, #683).
990991

991-
* `expect_error()` now returns the error object as documentated (#724).
992+
* `expect_error()` now returns the error object as documented (#724).
992993
It also now warns if you're using a classed expectation and you're
993994
not using the `class` argument. This is good practice as it decouples the
994995
error object (which tends to be stable) from its rendering to the user
@@ -1680,7 +1681,7 @@ The reporters system class has been considerably refactored to make existing rep
16801681
* `safe_digest()` uses a better strategy, and returns NA for directories
16811682
(#138, #146).
16821683

1683-
* Random praise is renabled by default (again!) (#164).
1684+
* Random praise is re-enabled by default (again!) (#164).
16841685

16851686
* Teamcity reporter now correctly escapes output messages (#150, @windelinckx).
16861687
It also uses nested suites to include test names.

R/expect-self-test.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ capture_success_failure <- function(expr) {
3939
#' Use `show_failure()` in examples to print the failure message without
4040
#' throwing an error.
4141
#'
42-
#' @param expr Code to evalute
42+
#' @param expr Code to evaluate
4343
#' @param message Check that the failure message matches this regexp.
4444
#' @param ... Other arguments passed on to [expect_match()].
4545
#' @export

R/expect-setequal.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ expect_setequal <- function(object, expected) {
2828
act <- quasi_label(enquo(object))
2929
exp <- quasi_label(enquo(expected))
3030

31-
check_vector(object)
32-
check_vector(expected)
31+
check_vector(act$val, error_arg = "object")
32+
check_vector(exp$val, error_arg = "expected")
3333
if (!is.null(names(act$val)) && !is.null(names(exp$val))) {
3434
testthat_warn("expect_setequal() ignores names")
3535
}

R/expect-that.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#' @param srcref Location of the failure. Should only needed to be explicitly
1616
#' supplied when you need to forward a srcref captured elsewhere.
1717
#' @param trace_env If `trace` is not specified, this is used to generate an
18-
#' informative traceack for failures. You should only need to set this if
18+
#' informative traceback for failures. You should only need to set this if
1919
#' you're calling `fail()` from a helper function; see
2020
#' `vignette("custom-expectation")` for details.
2121
#' @param trace An optional backtrace created by [rlang::trace_back()].

R/expect-vector.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
expect_vector <- function(object, ptype = NULL, size = NULL) {
1818
check_installed("vctrs")
1919
check_number_whole(size, min = 0, allow_null = TRUE)
20-
2120
act <- quasi_label(enquo(object))
21+
# vec_assert() automatically adds backticks so we hack out the ones
22+
# added by as_label()
23+
act$lab <- gsub("^`|`$", "", act$lab)
2224

23-
message <- NULL
24-
tryCatch(
25+
withCallingHandlers(
2526
vctrs::vec_assert(act$val, ptype = ptype, size = size, arg = act$lab),
27+
vctrs_error_scalar_type = function(e) {
28+
fail(e$message)
29+
},
2630
vctrs_error_assert = function(e) {
27-
message <<- e$message
31+
fail(e$message)
2832
}
2933
)
3034

31-
if (!is.null(message)) {
32-
return(fail(message))
33-
}
3435
pass(act$val)
3536
}

R/mock2.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
#'
101101
#' To mock a function that returns different values in sequence,
102102
#' for instance an API call whose status would be 502 then 200,
103-
#' or an user intput to `readline()`, you can use [mock_output_sequence()]
103+
#' or an user input to `readline()`, you can use [mock_output_sequence()]
104104
#'
105105
#' ```R
106106
#' local_mocked_bindings(readline = mock_output_sequence("3", "This is a note", "n"))

R/quasi-label.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ quasi_label <- function(quo, label = NULL, arg = NULL) {
5959
}
6060

6161
labelled_value <- function(value, label) {
62-
list(
63-
val = value,
64-
lab = label
65-
)
62+
if (missing(value)) {
63+
list(val = missing_arg(), lab = label)
64+
} else {
65+
list(val = value, lab = label)
66+
}
6667
}
6768

6869
quasi_capture <- function(.quo, .label, .capture, ...) {

R/reporter-list.R

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,52 @@ ListReporter <- R6::R6Class(
1111
"ListReporter",
1212
inherit = Reporter,
1313
public = list(
14-
current_start_time = NA,
15-
current_expectations = NULL,
16-
current_file = NULL,
17-
current_context = NULL,
18-
current_test = NULL,
14+
running = NULL,
15+
current_file = "", # so we can still subset with this
1916
results = NULL,
2017

2118
initialize = function() {
2219
super$initialize()
2320
self$capabilities$parallel_support <- TRUE
21+
self$capabilities$parallel_updates <- TRUE
2422
self$results <- Stack$new()
23+
self$running <- new.env(parent = emptyenv())
2524
},
2625

2726
start_test = function(context, test) {
27+
# is this a new test block?
2828
if (
29-
!identical(self$current_context, context) ||
30-
!identical(self$current_test, test)
29+
!identical(self$running[[self$current_file]]$context, context) ||
30+
!identical(self$running[[self$current_file]]$test, test)
3131
) {
32-
self$current_context <- context
33-
self$current_test <- test
34-
self$current_expectations <- Stack$new()
35-
self$current_start_time <- proc.time()
32+
self$running[[self$current_file]]$context <- context
33+
self$running[[self$current_file]]$test <- test
34+
self$running[[self$current_file]]$expectations <- Stack$new()
35+
self$running[[self$current_file]]$start_time <- proc.time()
3636
}
3737
},
3838

3939
add_result = function(context, test, result) {
40-
if (is.null(self$current_expectations)) {
40+
if (is.null(self$running[[self$current_file]]$expectations)) {
4141
# we received a result outside of a test:
4242
# could be a bare expectation or an exception/error
4343
if (!inherits(result, 'error')) {
4444
return()
4545
}
46-
self$current_expectations <- Stack$new()
46+
self$running[[self$current_file]]$expectations <- Stack$new()
4747
}
4848

49-
self$current_expectations$push(result)
49+
self$running[[self$current_file]]$expectations$push(result)
5050
},
5151

5252
end_test = function(context, test) {
53-
elapsed <- as.double(proc.time() - self$current_start_time)
53+
elapsed <- as.double(
54+
proc.time() - self$running[[self$current_file]]$start_time
55+
)
5456

5557
results <- list()
56-
if (!is.null(self$current_expectations)) {
57-
results <- self$current_expectations$as_list()
58+
if (!is.null(self$running[[self$current_file]]$expectations)) {
59+
results <- self$running[[self$current_file]]$expectations$as_list()
5860
}
5961

6062
self$results$push(list(
@@ -67,28 +69,39 @@ ListReporter <- R6::R6Class(
6769
results = results
6870
))
6971

70-
self$current_expectations <- NULL
72+
self$running[[self$current_file]]$expectations <- NULL
7173
},
7274

7375
start_file = function(name) {
76+
if (!name %in% names(self$running)) {
77+
newfile <- list(
78+
start_time = NA,
79+
expectations = NULL,
80+
context = NULL,
81+
test = NULL
82+
)
83+
assign(name, newfile, envir = self$running)
84+
}
7485
self$current_file <- name
7586
},
7687

7788
end_file = function() {
7889
# fallback in case we have errors but no expectations
7990
self$end_context(self$current_file)
91+
rm(list = self$current_file, envir = self$running)
8092
},
8193

8294
end_context = function(context) {
83-
results <- self$current_expectations
95+
results <- self$running[[self$current_file]]$expectations
8496
if (is.null(results)) {
8597
return()
8698
}
8799

88-
self$current_expectations <- NULL
100+
self$running[[self$current_file]]$expectations <- NULL
89101

90102
# look for exceptions raised outside of tests
91-
# they happened just before end_context since they interrupt the test_file execution
103+
# they happened just before end_context since they interrupt the test_
104+
# file execution
92105
results <- results$as_list()
93106
if (length(results) == 0) {
94107
return()

R/reporter-stop.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' @description
44
#' The default reporter used when [expect_that()] is run interactively.
5-
#' It responds by displaying a summary of the number of successes and faiures
5+
#' It responds by displaying a summary of the number of successes and failures
66
#' and [stop()]ping on if there are any failures.
77
#'
88
#' @export

R/test-compiled-code.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ run_cpp_tests <- function(package) {
230230
#' \strong{Function} \tab \strong{Catch} \tab \strong{Description} \cr
231231
#' `context` \tab `CATCH_TEST_CASE` \tab The context of a set of tests. \cr
232232
#' `test_that` \tab `CATCH_SECTION` \tab A test section. \cr
233-
#' `expect_true` \tab `CATCH_CHECK` \tab Test that an expression evaluates to `true`. \cr
234-
#' `expect_false` \tab `CATCH_CHECK_FALSE` \tab Test that an expression evalutes to `false`. \cr
233+
#' `expect_true` \tab `CATCH_CHECK` \tab Test that an expression evaluates to `TRUE`. \cr
234+
#' `expect_false` \tab `CATCH_CHECK_FALSE` \tab Test that an expression evaluates to `FALSE`. \cr
235235
#' `expect_error` \tab `CATCH_CHECK_THROWS` \tab Test that evaluation of an expression throws an exception. \cr
236236
#' `expect_error_as` \tab `CATCH_CHECK_THROWS_AS` \tab Test that evaluation of an expression throws an exception of a specific class. \cr
237237
#' }

0 commit comments

Comments
 (0)