Skip to content

Commit f3291e7

Browse files
committed
Show everything in snapshot. Fix failures
1 parent 61f8bab commit f3291e7

26 files changed

+1080
-587
lines changed

R/expect-self-test.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ expect_failure <- function(expr, message = NULL, ...) {
9494
#' @export
9595
#' @rdname expect_success
9696
expect_snapshot_failure <- function(expr) {
97-
expect_snapshot_condition_("expectation_failure", expr)
97+
expr <- enquo0(expr)
98+
expect_snapshot_(expr, error = TRUE, error_class = "expectation_failure")
9899
}
99100

100101
#' Test for absence of success or failure

R/snapshot.R

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,38 @@ expect_snapshot <- function(
6868
variant = NULL,
6969
cnd_class = FALSE
7070
) {
71-
check_bool(cran)
72-
check_bool(error)
73-
check_bool(cnd_class)
74-
7571
edition_require(3, "expect_snapshot()")
72+
73+
x <- enquo0(x)
74+
expect_snapshot_(
75+
x,
76+
cran = cran,
77+
error = error,
78+
transform = transform,
79+
variant = variant,
80+
cnd_class = cnd_class
81+
)
82+
}
83+
84+
expect_snapshot_ <- function(
85+
x,
86+
cran = TRUE,
87+
error = FALSE,
88+
error_class = NULL,
89+
transform = NULL,
90+
variant = NULL,
91+
cnd_class = FALSE,
92+
error_frame = caller_env()
93+
) {
94+
check_bool(cran, call = error_frame)
95+
check_bool(error, call = error_frame)
96+
check_bool(cnd_class, call = error_frame)
97+
7698
variant <- check_variant(variant)
7799
if (!is.null(transform)) {
78100
transform <- as_function(transform)
79101
}
80102

81-
x <- enquo0(x)
82-
83103
# Execute code, capturing last error
84104
state <- new_environment(list(error = NULL))
85105
replay <- function(x) {
@@ -95,7 +115,13 @@ expect_snapshot <- function(
95115
)
96116

97117
# Use expect_error() machinery to confirm that error is as expected
98-
msg <- compare_condition_3e("error", NULL, state$error, quo_label(x), error)
118+
msg <- compare_condition_3e(
119+
cond_type = "error",
120+
cond_class = error_class,
121+
cond = state$error,
122+
lab = quo_label(x),
123+
expected = error
124+
)
99125
if (!is.null(msg)) {
100126
if (error) {
101127
return(fail(msg, trace = state$error[["trace"]]))
@@ -112,7 +138,7 @@ expect_snapshot <- function(
112138
save = function(x) paste0(x, collapse = "\n"),
113139
load = function(x) split_by_line(x)[[1]],
114140
variant = variant,
115-
trace_env = caller_env()
141+
trace_env = error_frame
116142
)
117143
}
118144

tests/testthat/_snaps/expect-comparison.md

Lines changed: 102 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,152 @@
11
# basic comparisons work
22

3-
Expected 10 < 10.
4-
Actual 10.0 >= 10.0
5-
Difference 0.0 >= 0
3+
Code
4+
expect_lt(10, 10)
5+
Condition
6+
Error:
7+
! Expected 10 < 10.
8+
Actual 10.0 >= 10.0
9+
Difference 0.0 >= 0
610

711
---
812

9-
Expected 10 > 10.
10-
Actual 10.0 <= 10.0
11-
Difference 0.0 <= 0
13+
Code
14+
expect_gt(10, 10)
15+
Condition
16+
Error:
17+
! Expected 10 > 10.
18+
Actual 10.0 <= 10.0
19+
Difference 0.0 <= 0
1220

1321
# useful output when numbers are very small
1422

15-
Expected `1.1 * x` <= `x`.
16-
Actual 0.0000110 > 0.0000100
17-
Difference 0.0000010 > 0
23+
Code
24+
expect_lte(1.1 * x, x)
25+
Condition
26+
Error:
27+
! Expected `1.1 * x` <= `x`.
28+
Actual 0.0000110 > 0.0000100
29+
Difference 0.0000010 > 0
1830

1931
---
2032

21-
Expected `x` > `1.1 * x`.
22-
Actual 0.0000100 <= 0.0000110
23-
Difference -0.0000010 <= 0
33+
Code
34+
expect_gt(x, 1.1 * x)
35+
Condition
36+
Error:
37+
! Expected `x` > `1.1 * x`.
38+
Actual 0.0000100 <= 0.0000110
39+
Difference -0.0000010 <= 0
2440

2541
# useful output when difference is zero
2642

27-
Expected `x` < 100.
28-
Actual 100.0 >= 100.0
29-
Difference 0.0 >= 0
43+
Code
44+
expect_lt(x, 100)
45+
Condition
46+
Error:
47+
! Expected `x` < 100.
48+
Actual 100.0 >= 100.0
49+
Difference 0.0 >= 0
3050

3151
# useful output when differnce is large
3252

33-
Expected `x` < 0.001.
34-
Actual 100.000 >= 0.001
35-
Difference 99.999 >= 0
53+
Code
54+
expect_lt(x, 0.001)
55+
Condition
56+
Error:
57+
! Expected `x` < 0.001.
58+
Actual 100.000 >= 0.001
59+
Difference 99.999 >= 0
3660

3761
# comparisons with Inf work
3862

39-
Expected Inf < Inf.
40-
Actual values are incomparable.
63+
Code
64+
expect_lt(Inf, Inf)
65+
Condition
66+
Error:
67+
! Expected Inf < Inf.
68+
Actual values are incomparable.
4169

4270
---
4371

44-
Expected Inf > Inf.
45-
Actual values are incomparable.
72+
Code
73+
expect_gt(Inf, Inf)
74+
Condition
75+
Error:
76+
! Expected Inf > Inf.
77+
Actual values are incomparable.
4678

4779
# comparisons with NA work
4880

49-
Expected 10 < NA_real_.
50-
Actual comparison is NA.
81+
Code
82+
expect_lt(10, NA_real_)
83+
Condition
84+
Error:
85+
! Expected 10 < NA_real_.
86+
Actual comparison is NA.
5187

5288
---
5389

54-
Expected NA_real_ < 10.
55-
Actual comparison is NA.
90+
Code
91+
expect_lt(NA_real_, 10)
92+
Condition
93+
Error:
94+
! Expected NA_real_ < 10.
95+
Actual comparison is NA.
5696

5797
---
5898

59-
Expected NA_real_ < NA_real_.
60-
Actual comparison is NA.
99+
Code
100+
expect_lt(NA_real_, NA_real_)
101+
Condition
102+
Error:
103+
! Expected NA_real_ < NA_real_.
104+
Actual comparison is NA.
61105

62106
---
63107

64-
Expected NA_real_ <= NA_real_.
65-
Actual comparison is NA.
108+
Code
109+
expect_lte(NA_real_, NA_real_)
110+
Condition
111+
Error:
112+
! Expected NA_real_ <= NA_real_.
113+
Actual comparison is NA.
66114

67115
---
68116

69-
Expected 10 > NA_real_.
70-
Actual comparison is NA.
117+
Code
118+
expect_gt(10, NA_real_)
119+
Condition
120+
Error:
121+
! Expected 10 > NA_real_.
122+
Actual comparison is NA.
71123

72124
---
73125

74-
Expected NA_real_ > 10.
75-
Actual comparison is NA.
126+
Code
127+
expect_gt(NA_real_, 10)
128+
Condition
129+
Error:
130+
! Expected NA_real_ > 10.
131+
Actual comparison is NA.
76132

77133
---
78134

79-
Expected NA_real_ > NA_real_.
80-
Actual comparison is NA.
135+
Code
136+
expect_gt(NA_real_, NA_real_)
137+
Condition
138+
Error:
139+
! Expected NA_real_ > NA_real_.
140+
Actual comparison is NA.
81141

82142
---
83143

84-
Expected NA_real_ >= NA_real_.
85-
Actual comparison is NA.
144+
Code
145+
expect_gte(NA_real_, NA_real_)
146+
Condition
147+
Error:
148+
! Expected NA_real_ >= NA_real_.
149+
Actual comparison is NA.
86150

87151
# comparison must yield a single logical
88152

0 commit comments

Comments
 (0)