Skip to content

Commit 06aa28c

Browse files
committed
Use in the vignette; update examples
And add a test
1 parent 991e5d4 commit 06aa28c

File tree

12 files changed

+94
-89
lines changed

12 files changed

+94
-89
lines changed

β€ŽNAMESPACEβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export(make_expectation)
164164
export(mock_output_sequence)
165165
export(new_expectation)
166166
export(not)
167+
export(pass)
167168
export(prints_text)
168169
export(quasi_label)
169170
export(run_cpp_tests)

β€ŽNEWS.mdβ€Ž

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

3+
* New `pass()` function to use in place of `succeed()` (#2113).
34
* `expectation()` is now a combination of `new_expectation()` and `exp_signal()` (#2125).
45
* `is_null()`/`matches()` deprecated in 2.0.0 (2017-12-19) and `is_true()`/`is_false()` deprecated in 2.1.0 (2019-04-23) have been removed (#2109).
56
* `local_edition()` now gives a useful error for bad values (#1547).

β€ŽR/expect-that.Rβ€Ž

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ expect_that <- function(object, condition, info = NULL, label = NULL) {
3636
condition(object)
3737
}
3838

39-
#' Default expectations that always succeed or fail.
39+
#' `pass()` or `fail()` a test
4040
#'
41-
#' These allow you to manually trigger success or failure. Failure is
42-
#' particularly useful to a pre-condition or mark a test as not yet
43-
#' implemented.
41+
#' @description
42+
#' These are the primitives that you can use to implement your own expectations.
43+
#' Every branch of code inside an expectation must call either `pass()` or
44+
#' `fail()`; learn more in `vignette("custom-expectation")`.
4445
#'
4546
#' @param message a string to display.
4647
#' @inheritParams expect
@@ -71,6 +72,19 @@ fail <- function(
7172

7273
#' @rdname fail
7374
#' @export
75+
pass <- function(value) {
76+
expectation("success", "success")
77+
invisible(value)
78+
}
79+
80+
#' Mark a test as successful
81+
#'
82+
#' This is an older version of [pass()] that exists for backwards compatibility.
83+
#' You should now use `pass()` instead`
84+
#'
85+
#' @export
86+
#' @inheritParams fail
87+
#' @keywords internal
7488
succeed <- function(message = "Success has been forced", info = NULL) {
7589
message <- paste(c(message, info), collapse = "\n")
7690

β€ŽR/expectation.Rβ€Ž

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,8 @@
1515
#' generate a traceback running from `test_code()`/`test_file()` to
1616
#' `trace_env`. You'll generally only need to set this if you're wrapping
1717
#' an expectation inside another function.
18-
#' @return An expectation object. Signals the expectation condition
18+
#' @return An expectation object from either `succeed()` or `fail()`.
1919
#' with a `continue_test` restart.
20-
#'
21-
#' @details
22-
#'
23-
#' While `expect()` creates and signals an expectation in one go,
24-
#' `exp_signal()` separately signals an expectation that you
25-
#' have manually created with [new_expectation()]. Expectations are
26-
#' signalled with the following protocol:
27-
#'
28-
#' * If the expectation is a failure or an error, it is signalled with
29-
#' [base::stop()]. Otherwise, it is signalled with
30-
#' [base::signalCondition()].
31-
#'
32-
#' * The `continue_test` restart is registered. When invoked, failing
33-
#' expectations are ignored and normal control flow is resumed to
34-
#' run the other tests.
35-
#'
3620
#' @seealso [exp_signal()]
3721
#' @export
3822
expect <- function(
@@ -60,7 +44,7 @@ expect <- function(
6044
#'
6145
#' @description
6246
#' For advanced use only. If you are creating your own expectation, you should
63-
#' call [expect()] instead. See `vignette("custom-expectation")` for more
47+
#' call [pass()] or [fail()]. See `vignette("custom-expectation")` for more
6448
#' details.
6549
#'
6650
#' `new_expectation()` creates an expectation object and `exp_signal()` signals
@@ -70,6 +54,7 @@ expect <- function(
7054
#' "skip", "warning".
7155
#' @param message Message describing test failure
7256
#' @param srcref Optional `srcref` giving location of test.
57+
#' @keywords internal
7358
#' @inheritParams expect
7459
#' @export
7560
expectation <- function(type, message, srcref = NULL, trace = NULL) {

β€Ž_pkgdown.ymlβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ reference:
7878
- title: Expectation internals
7979
contents:
8080
- expect
81-
- expectation
8281
- fail
8382
- expect_success
8483

β€Žman/expect.Rdβ€Ž

Lines changed: 1 addition & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/expectation.Rdβ€Ž

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/fail.Rdβ€Ž

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/succeed.Rdβ€Ž

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test_that("pass() succeeds and returns value", {
2+
expect_success(pass("x"))
3+
expect_invisible(pass("x"))
4+
expect_equal(pass("x"), "x")
5+
})

0 commit comments

Comments
Β (0)