Skip to content

Commit cdf59be

Browse files
committed
More comparison tweaking
1 parent ecd5029 commit cdf59be

File tree

3 files changed

+56
-117
lines changed

3 files changed

+56
-117
lines changed

R/expect-comparison.R

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,31 @@ expect_compare_ <- function(
4141
diff <- act$val - exp$val
4242
msg_exp <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab)
4343

44+
digits <- max(
45+
digits(act$val),
46+
digits(exp$val),
47+
min_digits(act$val, exp$val)
48+
)
49+
50+
msg_act <- sprintf(
51+
"Actual comparison: %s %s %s",
52+
num_exact(act$val, digits),
53+
actual_op,
54+
num_exact(exp$val, digits)
55+
)
56+
4457
if (is.nan(diff)) {
45-
msg_act <- "Actual values are incomparable."
58+
msg_diff <- "Difference: incomparable."
4659
} else if (is.na(diff)) {
47-
msg_act <- "Actual comparison is NA."
60+
msg_diff <- "Difference: NA."
4861
} else {
49-
digits <- max(
50-
digits(act$val),
51-
digits(exp$val),
52-
min_digits(act$val, exp$val)
53-
)
54-
55-
msg_act <- c(
56-
sprintf(
57-
"Actual %s %s %s",
58-
num_exact(act$val, digits),
59-
actual_op,
60-
num_exact(exp$val, digits)
61-
),
62-
sprintf("Difference %s %s 0", num_exact(diff, digits), actual_op)
62+
msg_diff <- sprintf(
63+
"Difference: %s %s 0",
64+
num_exact(diff, digits),
65+
actual_op
6366
)
6467
}
65-
return(fail(c(msg_exp, msg_act), trace_env = trace_env))
68+
return(fail(c(msg_exp, msg_act, msg_diff), trace_env = trace_env))
6669
}
6770
pass(act$val)
6871
}

tests/testthat/_snaps/expect-comparison.md

Lines changed: 20 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Condition
66
Error:
77
! Expected `x` < 10.
8-
Actual 10.0 >= 10.0
9-
Difference 0.0 >= 0
8+
Actual comparison: 10.0 >= 10.0
9+
Difference: 0.0 >= 0
1010

1111
---
1212

@@ -15,8 +15,8 @@
1515
Condition
1616
Error:
1717
! Expected `x` > 10.
18-
Actual 10.0 <= 10.0
19-
Difference 0.0 <= 0
18+
Actual comparison: 10.0 <= 10.0
19+
Difference: 0.0 <= 0
2020

2121
# useful output when numbers are very small
2222

@@ -25,8 +25,8 @@
2525
Condition
2626
Error:
2727
! Expected `1.1 * x` <= `x`.
28-
Actual 0.0000110 > 0.0000100
29-
Difference 0.0000010 > 0
28+
Actual comparison: 0.0000110 > 0.0000100
29+
Difference: 0.0000010 > 0
3030

3131
---
3232

@@ -35,8 +35,8 @@
3535
Condition
3636
Error:
3737
! Expected `x` > `1.1 * x`.
38-
Actual 0.0000100 <= 0.0000110
39-
Difference -0.0000010 <= 0
38+
Actual comparison: 0.0000100 <= 0.0000110
39+
Difference: -0.0000010 <= 0
4040

4141
# useful output when difference is zero
4242

@@ -45,8 +45,8 @@
4545
Condition
4646
Error:
4747
! Expected `x` < 100.
48-
Actual 100.0 >= 100.0
49-
Difference 0.0 >= 0
48+
Actual comparison: 100.0 >= 100.0
49+
Difference: 0.0 >= 0
5050

5151
# useful output when differnce is large
5252

@@ -55,98 +55,28 @@
5555
Condition
5656
Error:
5757
! Expected `x` < 0.001.
58-
Actual 100.000 >= 0.001
59-
Difference 99.999 >= 0
58+
Actual comparison: 100.000 >= 0.001
59+
Difference: 99.999 >= 0
6060

6161
# comparisons with Inf work
6262

6363
Code
64-
expect_lt(Inf, Inf)
64+
expect_lt(x, Inf)
6565
Condition
6666
Error:
67-
! Expected Inf < Inf.
68-
Actual values are incomparable.
69-
70-
---
71-
72-
Code
73-
expect_gt(Inf, Inf)
74-
Condition
75-
Error:
76-
! Expected Inf > Inf.
77-
Actual values are incomparable.
67+
! Expected `x` < Inf.
68+
Actual comparison: Inf >= Inf
69+
Difference: incomparable.
7870

7971
# comparisons with NA work
8072

8173
Code
82-
expect_lt(10, NA_real_)
83-
Condition
84-
Error:
85-
! Expected 10 < NA_real_.
86-
Actual comparison is NA.
87-
88-
---
89-
90-
Code
91-
expect_lt(NA_real_, 10)
92-
Condition
93-
Error:
94-
! Expected NA_real_ < 10.
95-
Actual comparison is NA.
96-
97-
---
98-
99-
Code
100-
expect_lt(NA_real_, NA_real_)
101-
Condition
102-
Error:
103-
! Expected NA_real_ < NA_real_.
104-
Actual comparison is NA.
105-
106-
---
107-
108-
Code
109-
expect_lte(NA_real_, NA_real_)
110-
Condition
111-
Error:
112-
! Expected NA_real_ <= NA_real_.
113-
Actual comparison is NA.
114-
115-
---
116-
117-
Code
118-
expect_gt(10, NA_real_)
119-
Condition
120-
Error:
121-
! Expected 10 > NA_real_.
122-
Actual comparison is NA.
123-
124-
---
125-
126-
Code
127-
expect_gt(NA_real_, 10)
128-
Condition
129-
Error:
130-
! Expected NA_real_ > 10.
131-
Actual comparison is NA.
132-
133-
---
134-
135-
Code
136-
expect_gt(NA_real_, NA_real_)
137-
Condition
138-
Error:
139-
! Expected NA_real_ > NA_real_.
140-
Actual comparison is NA.
141-
142-
---
143-
144-
Code
145-
expect_gte(NA_real_, NA_real_)
74+
expect_lt(x, 10)
14675
Condition
14776
Error:
148-
! Expected NA_real_ >= NA_real_.
149-
Actual comparison is NA.
77+
! Expected `x` < 10.
78+
Actual comparison: NA >= 10.0
79+
Difference: NA.
15080

15181
# comparison must yield a single logical
15282

tests/testthat/test-expect-comparison.R

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,30 @@ test_that("comparison result object invisibly", {
3232

3333
test_that("comparisons with Inf work", {
3434
expect_success(expect_lt(10, Inf))
35-
expect_snapshot_failure(expect_lt(Inf, Inf))
35+
expect_failure(expect_lt(Inf, Inf))
3636
expect_success(expect_lte(Inf, Inf))
3737

3838
expect_success(expect_gt(Inf, 10))
39-
expect_snapshot_failure(expect_gt(Inf, Inf))
39+
expect_failure(expect_gt(Inf, Inf))
4040
expect_success(expect_gte(Inf, Inf))
41+
42+
x <- Inf
43+
expect_snapshot_failure(expect_lt(x, Inf))
4144
})
4245

4346
test_that("comparisons with NA work", {
44-
expect_snapshot_failure(expect_lt(10, NA_real_))
45-
expect_snapshot_failure(expect_lt(NA_real_, 10))
46-
expect_snapshot_failure(expect_lt(NA_real_, NA_real_))
47-
expect_snapshot_failure(expect_lte(NA_real_, NA_real_))
47+
expect_failure(expect_lt(10, NA_real_))
48+
expect_failure(expect_lt(NA_real_, 10))
49+
expect_failure(expect_lt(NA_real_, NA_real_))
50+
expect_failure(expect_lte(NA_real_, NA_real_))
51+
52+
expect_failure(expect_gt(10, NA_real_))
53+
expect_failure(expect_gt(NA_real_, 10))
54+
expect_failure(expect_gt(NA_real_, NA_real_))
55+
expect_failure(expect_gte(NA_real_, NA_real_))
4856

49-
expect_snapshot_failure(expect_gt(10, NA_real_))
50-
expect_snapshot_failure(expect_gt(NA_real_, 10))
51-
expect_snapshot_failure(expect_gt(NA_real_, NA_real_))
52-
expect_snapshot_failure(expect_gte(NA_real_, NA_real_))
57+
x <- NA_real_
58+
expect_snapshot_failure(expect_lt(x, 10))
5359
})
5460

5561
test_that("comparisons with more complicated objects work", {

0 commit comments

Comments
 (0)