Skip to content

Commit 806c68a

Browse files
committed
Allow to set a tolerance for set_state_inspector().
1 parent 387580b commit 806c68a

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

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+
* `set_state_inspector()` now allows to control the tolerance (#2237).
34
* New `vignette("mocking")` explains mocking in detail (#1265).
45
* New `vignette("challenging-functions")` provides an index to other documentation organised by testing challenges (#1265).
56
* 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.

R/test-state.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
#' @export
4343
#' @param callback Either a zero-argument function that returns an object
4444
#' capturing global state that you're interested in, or `NULL`.
45-
set_state_inspector <- function(callback) {
45+
#' @param tolerance Numerical tolerance: any differences (in the sense of
46+
#' [base::all.equal()]) smaller than this value will be ignored.
47+
set_state_inspector <- function(callback, tolerance = testthat_tolerance()) {
4648
if (
4749
!is.null(callback) &&
4850
!(is.function(callback) && length(formals(callback)) == 0)
@@ -51,11 +53,13 @@ set_state_inspector <- function(callback) {
5153
}
5254

5355
the$state_inspector <- callback
56+
the$state_inspector_tolerance <- tolerance
5457
invisible()
5558
}
5659

5760
testthat_state_condition <- function(before, after, call) {
58-
diffs <- waldo_compare(before, after, x_arg = "before", y_arg = "after")
61+
diffs <- waldo_compare(before, after, x_arg = "before", y_arg = "after",
62+
tolerance = the$state_inspector_tolerance)
5963

6064
if (length(diffs) == 0) {
6165
return(NULL)

0 commit comments

Comments
 (0)