Skip to content

Commit 6c944c8

Browse files
authored
Merge branch 'main' into expect-visible
2 parents 960c667 + dd39c65 commit 6c944c8

30 files changed

+108
-197
lines changed

NEWS.md

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

33
* `expect_visible()` and `expect_invisible()` have improved failure messages (#1966).
4+
* `expect_snapshot()` now strips line breaks in test descriptions (@LDSamson, #1900).
5+
* `expect_snapshot()` now errors when called from a `test_that()` that has an empty description (@kevinushey, #1980).
6+
* `skip_if_not_installed()` produces a clearer message (@MichaelChirico, #1959).
7+
* `with_mock()` and `local_mock()` have been unconditionally deprecated as they will no longer work in future versions of R (#1999).
8+
* `expect_condition()` and friends now include the `class` of the expected condition in the failure mesage, if used (#1987).
49
* `LANGUAGE` is now set to `"C"` in reprocucible environments (i.e.
510
`test_that()` blocks) to disable translations. This fixes warnings
611
about being unable to set the language to `"en"` (#1925).

R/edition.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ edition_name <- function(x) {
6060
#' @export
6161
#' @param x Edition Should be a single integer.
6262
#' @param .env Environment that controls scope of changes. For expert use only.
63-
#' @keywords internal
6463
local_edition <- function(x, .env = parent.frame()) {
6564
stopifnot(is_zap(x) || (is.numeric(x) && length(x) == 1))
6665

R/expect-condition.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ expect_condition_matching <- function(base_class,
278278
)
279279

280280
expected <- !identical(regexp, NA)
281-
msg <- compare_condition_3e(base_class, act$cap, act$lab, expected)
281+
msg <- compare_condition_3e(base_class, class, act$cap, act$lab, expected)
282282

283283
# Access error fields with `[[` rather than `$` because the
284284
# `$.Throwable` from the rJava package throws with unknown fields
@@ -375,10 +375,14 @@ capture_matching_condition <- function(expr, matches) {
375375

376376
# Helpers -----------------------------------------------------------------
377377

378-
compare_condition_3e <- function(cond_type, cond, lab, expected) {
378+
compare_condition_3e <- function(cond_type, cond_class, cond, lab, expected) {
379379
if (expected) {
380380
if (is.null(cond)) {
381-
sprintf("%s did not throw the expected %s.", lab, cond_type)
381+
if (is.null(cond_class)) {
382+
sprintf("%s did not throw the expected %s.", lab, cond_type)
383+
} else {
384+
sprintf("%s did not throw a %s with class <%s>.", lab, cond_type, cond_class)
385+
}
382386
} else {
383387
NULL
384388
}

R/expect-constant.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ expect_false <- function(object, info = NULL, label = NULL) {
5050
#' check for it either with `expect_equal(x, NULL)` or `expect_type(x, "NULL")`.
5151
#'
5252
#' @inheritParams expect_that
53-
#' @keywords internal
5453
#' @export
5554
#' @family expectations
5655
#' @examples
@@ -83,4 +82,3 @@ expect_waldo_constant <- function(act, constant, info) {
8382

8483
invisible(act$val)
8584
}
86-

R/expect-self-test.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#' @param expr Expression that evaluates a single expectation.
88
#' @param message Check that the failure message matches this regexp.
99
#' @param ... Other arguments passed on to [expect_match()].
10-
#' @keywords internal
1110
#' @export
1211
expect_success <- function(expr) {
1312
exp <- capture_expectation(expr)
@@ -114,4 +113,3 @@ local_output_override <- function(width = 80, crayon = TRUE, unicode = TRUE,
114113
reporter$unicode <- old_unicode
115114
}, .env)
116115
}
117-

R/expectation.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ expect <- function(ok, failure_message,
8989
#' @param message Message describing test failure
9090
#' @param srcref Optional `srcref` giving location of test.
9191
#' @inheritParams expect
92-
#' @keywords internal
9392
#' @export
9493
expectation <- function(type, message, srcref = NULL, trace = NULL) {
9594
new_expectation(type, message, srcref = srcref, trace = trace)

R/expectations-matches.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#' (not a regular expressions). Overrides `perl`.
1616
#' @inheritDotParams base::grepl -pattern -x -perl -fixed
1717
#' @family expectations
18-
#' @keywords internal
1918
#' @export
2019
#' @examples
2120
#' expect_match("Testing is fun", "fun")

R/mock.R

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
#' Mock functions in a package.
22
#'
33
#' @description
4-
#' `r lifecycle::badge("superseded")`
4+
#' `r lifecycle::badge("deprecated")`
55
#'
6-
#' `with_mock()` and `local_mock()` are superseded in favour of
6+
#' `with_mock()` and `local_mock()` are deprecated in favour of
77
#' [with_mocked_bindings()] and [local_mocked_bindings()].
88
#'
9-
#' These works by using some C code to temporarily modify the mocked function
10-
#' _in place_. This is abusive of R's internals, which makes it dangerous, and
11-
#' no longer recommended.
12-
#'
13-
#' @section 3rd edition:
14-
#' `r lifecycle::badge("deprecated")`
15-
#'
16-
#' `with_mock()` and `local_mock()` are deprecated in the third edition.
9+
#' These functions worked by using some C code to temporarily modify the mocked
10+
#' function _in place_. This was an abuse of R's internals and it is no longer
11+
#' permitted.
1712
#'
1813
#' @param ... named parameters redefine mocked functions, unnamed parameters
1914
#' will be evaluated after mocking the functions
@@ -26,7 +21,7 @@
2621
#' @return The result of the last unnamed parameter
2722
#' @export
2823
with_mock <- function(..., .env = topenv()) {
29-
edition_deprecate(3, "with_mock()", "Please use with_mocked_bindings() instead")
24+
lifecycle::deprecate_warn("3.3.0", "with_mock()", "with_mocked_bindings()")
3025

3126
dots <- eval(substitute(alist(...)))
3227
mock_qual_names <- names(dots)
@@ -61,7 +56,7 @@ with_mock <- function(..., .env = topenv()) {
6156
#' @export
6257
#' @rdname with_mock
6358
local_mock <- function(..., .env = topenv(), .local_envir = parent.frame()) {
64-
edition_deprecate(3, "local_mock()", "Please use local_mocked_bindings() instead")
59+
lifecycle::deprecate_warn("3.3.0", "local_mock()", "local_mocked_bindings()")
6560

6661
mocks <- extract_mocks(list(...), .env = .env)
6762
on_exit <- bquote(

R/skip.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#'
2020
#' * `skip_if_offline()` skips if an internet connection is not available
2121
#' (using [curl::nslookup()]) or if the test is run on CRAN. Requires
22-
#' the curl packages to be installed.
22+
#' \{curl\} to be installed and included in the dependencies of your package.
2323
#'
2424
#' * `skip_if_translated("msg")` skips tests if the "msg" is translated.
2525
#'
@@ -100,8 +100,14 @@ skip_if <- function(condition, message = NULL) {
100100
#' @param minimum_version Minimum required version for the package
101101
#' @rdname skip
102102
skip_if_not_installed <- function(pkg, minimum_version = NULL) {
103+
# most common case: it's not installed
104+
tryCatch(
105+
find.package(pkg),
106+
error = function(e) skip(paste0("{", pkg, "} is not installed"))
107+
)
108+
# rarer: it's installed, but fails to load
103109
if (!requireNamespace(pkg, quietly = TRUE)) {
104-
skip(paste0(pkg, " cannot be loaded"))
110+
skip(paste0("{", pkg, "} cannot be loaded"))
105111
}
106112

107113
if (!is.null(minimum_version)) {

R/snapshot-reporter.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
3535
},
3636

3737
start_test = function(context, test) {
38-
self$test <- test
38+
self$test <- gsub("\n", "", test)
3939
},
4040

4141
# Called by expectation
@@ -46,6 +46,7 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
4646
tolerance = testthat_tolerance(),
4747
variant = NULL,
4848
trace_env = NULL) {
49+
check_string(self$test, allow_empty = FALSE)
4950
i <- self$new_snaps$append(self$test, variant, save(value))
5051

5152
old_raw <- self$old_snaps$get(self$test, variant, i)

0 commit comments

Comments
 (0)