Skip to content

Commit ee43c0f

Browse files
committed
Merged origin/main into snap-test-path
2 parents 4fe76fa + 71a33cd commit ee43c0f

File tree

81 files changed

+1989
-816
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1989
-816
lines changed

CLAUDE.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@ General advice:
1212
* When running R from the console, always run it with `--quiet --vanilla`
1313
* Always run `air format .` after generating code
1414

15-
### Development tools
15+
### Testing
1616

17-
- `devtools::test()` - Run all tests
18-
- `devtools::test_file("tests/testthat/test-filename.R")` - Run tests in a specific file
17+
- Use `devtools::test()` to run all tests
18+
- Use `devtools::test_file("tests/testthat/test-filename.R")` to run tests in a specific file
1919
- DO NOT USE `devtools::test_active_file()`
20-
- `devtools::load_all()` - Load package for development
21-
- `devtools::check()` - Run R CMD check
22-
- `devtools::install()` - Install package locally
20+
- All testing functions automatically load code; you don't needs to.
21+
22+
- All new code should have an accompanying test.
23+
- Tests for `R/{name}.R` go in `tests/testthat/test-{name}.R`.
24+
- If there are existing tests, place new tests next to similar existing tests.
2325

2426
### Documentation
2527

2628
- Always run `devtools::document()` after changing any roxygen2 docs.
29+
- Every user facing function should be exported and have roxygen2 documentation.
30+
- Whenever you add a new documentation file, make sure to also add the topic name to `_pkgdown.yml`.
31+
- Run `pkgdown::check_pkgdown()` to check that all topics are included in the reference index.
2732

2833
## Core Architecture
2934

NEWS.md

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

3+
* `expect_snapshot_file()` now considers `.json` to be a text file (#1593).
4+
* `expect_snapshot_file()` now shows differences for text files (#1593).
5+
* The failure messages for all `expect_` functions have been rewritten to first state what was expected and then what was actually received (#2142).
6+
* `test_file(desc = ...)` no longer loses snapshot results (#2066).
37
* In `R CMD check`, snapshots now only advise on how to resolve failures once (#2207).
48
* `snapshot_review()` includes a reject button and only displays the file navigation and the skip button if there are multiple files to review (#2025).
59
* New `snapshot_download_gh()` makes it easy to get snapshots off GitHub and into your local package (#1779).

R/expect-comparison.R

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ expect_compare_ <- function(
2828
operator <- match.arg(operator)
2929
op <- match.fun(operator)
3030

31-
msg <- c(
32-
"<" = "not strictly less than",
33-
"<=" = "not less than",
34-
">" = "not strictly greater than",
35-
">=" = "not greater than"
36-
)[[operator]]
37-
38-
negated_op <- switch(operator, "<" = ">=", "<=" = ">", ">" = "<=", ">=" = "<")
31+
actual_op <- switch(operator, "<" = ">=", "<=" = ">", ">" = "<=", ">=" = "<")
3932

4033
cmp <- op(act$val, exp$val)
4134
if (length(cmp) != 1 || !is.logical(cmp)) {
@@ -45,22 +38,32 @@ expect_compare_ <- function(
4538
)
4639
}
4740
if (!isTRUE(cmp)) {
41+
diff <- act$val - exp$val
42+
msg_exp <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab)
43+
4844
digits <- max(
4945
digits(act$val),
5046
digits(exp$val),
5147
min_digits(act$val, exp$val)
5248
)
53-
msg <- sprintf(
54-
"%s is %s %s.\n%s - %s = %s %s 0",
55-
act$lab,
56-
msg,
57-
exp$lab,
49+
50+
msg_act <- sprintf(
51+
"Actual comparison: %s %s %s",
5852
num_exact(act$val, digits),
59-
num_exact(exp$val, digits),
60-
num_exact(act$val - exp$val, digits),
61-
negated_op
53+
actual_op,
54+
num_exact(exp$val, digits)
6255
)
63-
return(fail(msg, trace_env = trace_env))
56+
57+
if (is.na(diff)) {
58+
msg_diff <- NULL
59+
} else {
60+
msg_diff <- sprintf(
61+
"Difference: %s %s 0",
62+
num_exact(diff, digits),
63+
actual_op
64+
)
65+
}
66+
return(fail(c(msg_exp, msg_act, msg_diff), trace_env = trace_env))
6467
}
6568
pass(act$val)
6669
}

R/expect-condition.R

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,10 @@ compare_condition_3e <- function(cond_type, cond_class, cond, lab, expected) {
440440
if (expected) {
441441
if (is.null(cond)) {
442442
if (is.null(cond_class)) {
443-
sprintf("%s did not throw the expected %s.", lab, cond_type)
443+
sprintf("Expected %s to throw a %s.", lab, cond_type)
444444
} else {
445445
sprintf(
446-
"%s did not throw a %s with class <%s>.",
446+
"Expected %s to throw a %s with class <%s>.",
447447
lab,
448448
cond_type,
449449
cond_class
@@ -454,12 +454,9 @@ compare_condition_3e <- function(cond_type, cond_class, cond, lab, expected) {
454454
}
455455
} else {
456456
if (!is.null(cond)) {
457-
sprintf(
458-
"%s threw an unexpected %s.\nMessage: %s\nClass: %s",
459-
lab,
460-
cond_type,
461-
cnd_message(cond),
462-
paste(class(cond), collapse = "/")
457+
c(
458+
sprintf("Expected %s not to throw any %ss.", lab, cond_type),
459+
actual_condition(cond)
463460
)
464461
} else {
465462
NULL
@@ -493,7 +490,7 @@ compare_condition_2e <- function(
493490

494491
# Otherwise we're definitely expecting a condition
495492
if (is.null(cond)) {
496-
return(sprintf("%s did not throw an %s.", lab, cond_type))
493+
return(sprintf("Expected %s to throw a %s.", lab, cond_type))
497494
}
498495

499496
matches <- cnd_matches_2e(cond, class, regexp, inherit, ...)
@@ -562,15 +559,20 @@ compare_messages <- function(
562559
# Expecting no messages
563560
if (identical(regexp, NA)) {
564561
if (length(messages) > 0) {
565-
return(sprintf("%s generated %s:\n%s", lab, cond_type, bullets))
562+
return(sprintf(
563+
"Expected %s not to generate %s.\nActually generated:\n%s",
564+
lab,
565+
cond_type,
566+
bullets
567+
))
566568
} else {
567569
return()
568570
}
569571
}
570572

571573
# Otherwise we're definitely expecting messages
572574
if (length(messages) == 0) {
573-
return(sprintf("%s did not produce any %s.", lab, cond_type))
575+
return(sprintf("Expected %s to produce %s.", lab, cond_type))
574576
}
575577

576578
if (is.null(regexp)) {
@@ -625,3 +627,12 @@ check_condition_dots <- function(
625627
call = error_call
626628
)
627629
}
630+
631+
actual_condition <- function(cond) {
632+
paste0(
633+
"Actually got a <",
634+
class(cond)[[1]],
635+
"> with message:\n",
636+
indent_lines(cnd_message(cond))
637+
)
638+
}

R/expect-constant.R

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ NULL
3131
expect_true <- function(object, info = NULL, label = NULL) {
3232
act <- quasi_label(enquo(object), label)
3333
exp <- labelled_value(TRUE, "TRUE")
34-
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
34+
expect_waldo_constant_(act, exp, info = info, ignore_attr = TRUE)
3535
}
3636

3737
#' @export
3838
#' @rdname logical-expectations
3939
expect_false <- function(object, info = NULL, label = NULL) {
4040
act <- quasi_label(enquo(object), label)
4141
exp <- labelled_value(FALSE, "FALSE")
42-
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
42+
expect_waldo_constant_(act, exp, info = info, ignore_attr = TRUE)
4343
}
4444

4545
#' Do you expect `NULL`?
@@ -58,6 +58,31 @@ expect_false <- function(object, info = NULL, label = NULL) {
5858
#' show_failure(expect_null(y))
5959
expect_null <- function(object, info = NULL, label = NULL) {
6060
act <- quasi_label(enquo(object), label)
61-
exp <- labelled_value(NULL, "FALSE")
62-
expect_waldo_equal_("equal", act, exp, info = info)
61+
exp <- labelled_value(NULL, "NULL")
62+
expect_waldo_constant_(act, exp, info = info)
63+
}
64+
65+
expect_waldo_constant_ <- function(
66+
act,
67+
exp,
68+
...,
69+
info = NULL,
70+
trace_env = caller_env()
71+
) {
72+
comp <- waldo_compare(
73+
act$val,
74+
exp$val,
75+
...,
76+
x_arg = "actual",
77+
y_arg = "expected"
78+
)
79+
if (length(comp) != 0) {
80+
msg <- c(
81+
sprintf("Expected %s to be %s.", act$lab, exp$lab),
82+
"Differences:",
83+
paste0(comp, collpase = "\n")
84+
)
85+
return(fail(msg, info = info, trace_env = trace_env))
86+
}
87+
pass(act$val)
6388
}

R/expect-equality.R

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,18 @@ expect_equal <- function(
7777
}
7878

7979
if (!comp$equal) {
80-
msg <- sprintf("%s not equal to %s.\n%s", act$lab, exp$lab, comp$message)
80+
msg <- c(
81+
sprintf("Expected %s to equal %s.", act$lab, exp$lab),
82+
"Differences:",
83+
comp$message
84+
)
8185
return(fail(msg, info = info))
8286
}
8387
pass(act$val)
8488
}
8589
}
8690

91+
8792
#' @export
8893
#' @rdname equality-expectations
8994
expect_identical <- function(
@@ -102,18 +107,22 @@ expect_identical <- function(
102107
} else {
103108
ident <- identical(act$val, exp$val, ...)
104109
if (ident) {
105-
msg <- ""
110+
msg_act <- NULL
106111
} else {
107112
compare <- compare(act$val, exp$val)
108113
if (compare$equal) {
109-
msg <- "Objects equal but not identical"
114+
msg_act <- "Objects equal but not identical"
110115
} else {
111-
msg <- compare$message
116+
msg_act <- compare$message
112117
}
113118
}
114119

115120
if (!ident) {
116-
msg <- sprintf("%s not identical to %s.\n%s", act$lab, exp$lab, msg)
121+
msg <- c(
122+
sprintf("Expected %s to be identical to %s.", act$lab, exp$lab),
123+
"Differences:",
124+
msg_act
125+
)
117126
return(fail(msg, info = info))
118127
}
119128
pass(act$val)
@@ -126,8 +135,7 @@ expect_waldo_equal_ <- function(
126135
exp,
127136
info = NULL,
128137
...,
129-
trace_env = caller_env(),
130-
error_prefix = NULL
138+
trace_env = caller_env()
131139
) {
132140
comp <- waldo_compare(
133141
act$val,
@@ -137,16 +145,11 @@ expect_waldo_equal_ <- function(
137145
y_arg = "expected"
138146
)
139147
if (length(comp) != 0) {
140-
msg <- sprintf(
141-
"%s (%s) is not %s to %s (%s).\n\n%s",
142-
act$lab,
143-
"`actual`",
144-
type,
145-
exp$lab,
146-
"`expected`",
147-
paste0(comp, collapse = "\n\n")
148+
msg <- c(
149+
sprintf("Expected %s to be %s to %s.", act$lab, type, exp$lab),
150+
"Differences:",
151+
paste0(comp, collpase = "\n")
148152
)
149-
msg <- paste0(error_prefix, msg)
150153
return(fail(msg, info = info, trace_env = trace_env))
151154
}
152155
pass(act$val)
@@ -195,7 +198,7 @@ expect_equivalent <- function(
195198
comp <- compare(act$val, exp$val, ..., check.attributes = FALSE)
196199
if (!comp$equal) {
197200
msg <- sprintf(
198-
"%s not equivalent to %s.\n%s",
201+
"Expected %s to be equivalent to %s.\n%s",
199202
act$lab,
200203
exp$lab,
201204
comp$message

0 commit comments

Comments
 (0)