Skip to content

Commit a5a6df2

Browse files
authored
Add tolerance argument to set_state_inspector() (#2238)
With default that ignores minor differences. Fixes #2237.
1 parent 2af856e commit a5a6df2

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
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()` gains `tolerance` argument and ignores minor FP differences by default (@mcol, #2237).
34
* `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).

R/test-state.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
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+
#' @inheritParams waldo::compare
46+
set_state_inspector <- function(callback, tolerance = testthat_tolerance()) {
4647
if (
4748
!is.null(callback) &&
4849
!(is.function(callback) && length(formals(callback)) == 0)
@@ -51,11 +52,18 @@ set_state_inspector <- function(callback) {
5152
}
5253

5354
the$state_inspector <- callback
55+
the$state_inspector_tolerance <- tolerance
5456
invisible()
5557
}
5658

5759
testthat_state_condition <- function(before, after, call) {
58-
diffs <- waldo_compare(before, after, x_arg = "before", y_arg = "after")
60+
diffs <- waldo_compare(
61+
before,
62+
after,
63+
x_arg = "before",
64+
y_arg = "after",
65+
tolerance = the$state_inspector_tolerance
66+
)
5967

6068
if (length(diffs) == 0) {
6169
return(NULL)

man/set_state_inspector.Rd

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

0 commit comments

Comments
 (0)