Skip to content

Commit eec4dc4

Browse files
committed
in comparisons, print difference also for dates and times
1 parent 97d1ebd commit eec4dc4

File tree

3 files changed

+26
-101
lines changed

3 files changed

+26
-101
lines changed

R/expect-comparison.R

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,22 @@ failure_compare <- function(act, exp, operator) {
8282
actual_op,
8383
exp$val
8484
)
85-
msg_diff <- NULL
85+
86+
if (inherits(act$val, c("Date", "POSIXt"))) {
87+
diff <- act$val - exp$val
88+
if (is.na(diff)) {
89+
msg_diff <- NULL
90+
} else {
91+
msg_diff <- sprintf(
92+
"Difference: %s %s 0 %s",
93+
dt_diff(diff),
94+
actual_op,
95+
attr(diff, "unit")
96+
)
97+
}
98+
} else {
99+
msg_diff <- NULL
100+
}
86101
}
87102

88103
c(msg_exp, msg_act, msg_diff)
@@ -186,3 +201,9 @@ digits <- function(x) {
186201
ceiling(round(scale, digits = 2))
187202
}
188203
}
204+
205+
dt_diff <- function(x) {
206+
val <- unclass(x)
207+
digits <- digits(abs(val)) + 1
208+
paste(num_exact(val, digits), attr(x, "unit"))
209+
}

tests/testthat/_snaps/expect-comparison.md

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -83,107 +83,28 @@
8383
Condition
8484
Error:
8585
! Expected `time2` < `time`.
86-
Actual comparison: "2020-01-01 01:00:01" >= "2020-01-01 01:00:00"
87-
88-
---
89-
90-
Code
91-
expect_lte(time2, time)
92-
Condition
93-
Error:
94-
! Expected `time2` <= `time`.
95-
Actual comparison: "2020-01-01 01:00:01" > "2020-01-01 01:00:00"
96-
97-
---
98-
99-
Code
100-
expect_gt(time, time2)
101-
Condition
102-
Error:
103-
! Expected `time` > `time2`.
104-
Actual comparison: "2020-01-01 01:00:00" <= "2020-01-01 01:00:01"
105-
106-
---
107-
108-
Code
109-
expect_gte(time, time2)
110-
Condition
111-
Error:
112-
! Expected `time` >= `time2`.
113-
Actual comparison: "2020-01-01 01:00:00" < "2020-01-01 01:00:01"
86+
Actual comparison: "2020-01-01 01:00:01.5" >= "2020-01-01 01:00:00"
87+
Difference: 1.5 secs >= 0 secs
11488

11589
# comparisons with Date objects work
11690

117-
Code
118-
expect_lt(date2, date)
119-
Condition
120-
Error:
121-
! Expected `date2` < `date`.
122-
Actual comparison: "2020-01-02" >= "2020-01-01"
123-
124-
---
125-
126-
Code
127-
expect_lte(date2, date)
128-
Condition
129-
Error:
130-
! Expected `date2` <= `date`.
131-
Actual comparison: "2020-01-02" > "2020-01-01"
132-
133-
---
134-
13591
Code
13692
expect_gt(date, date2)
13793
Condition
13894
Error:
13995
! Expected `date` > `date2`.
14096
Actual comparison: "2020-01-01" <= "2020-01-02"
141-
142-
---
143-
144-
Code
145-
expect_gte(date, date2)
146-
Condition
147-
Error:
148-
! Expected `date` >= `date2`.
149-
Actual comparison: "2020-01-01" < "2020-01-02"
97+
Difference: -1.0 days <= 0 days
15098

15199
# comparisons with character objects work
152100

153-
Code
154-
expect_lt("b", "a")
155-
Condition
156-
Error:
157-
! Expected "b" < "a".
158-
Actual comparison: "b" >= "a"
159-
160-
---
161-
162101
Code
163102
expect_lte("b", "a")
164103
Condition
165104
Error:
166105
! Expected "b" <= "a".
167106
Actual comparison: "b" > "a"
168107

169-
---
170-
171-
Code
172-
expect_gt("a", "b")
173-
Condition
174-
Error:
175-
! Expected "a" > "b".
176-
Actual comparison: "a" <= "b"
177-
178-
---
179-
180-
Code
181-
expect_gte("a", "b")
182-
Condition
183-
Error:
184-
! Expected "a" >= "b".
185-
Actual comparison: "a" < "b"
186-
187108
# comparison must yield a single logical
188109

189110
Code

tests/testthat/test-expect-comparison.R

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,42 +60,25 @@ test_that("comparisons with NA work", {
6060

6161
test_that("comparisons with POSIXct objects work", {
6262
time <- as.POSIXct("2020-01-01 01:00:00")
63-
time2 <- time + 1
63+
time2 <- time + 1.5
6464
expect_success(expect_lt(time, time2))
65-
expect_success(expect_lte(time, time2))
66-
expect_success(expect_gt(time2, time))
67-
expect_success(expect_gte(time2, time))
6865

6966
expect_snapshot_failure(expect_lt(time2, time))
70-
expect_snapshot_failure(expect_lte(time2, time))
71-
expect_snapshot_failure(expect_gt(time, time2))
72-
expect_snapshot_failure(expect_gte(time, time2))
7367
})
7468

7569
test_that("comparisons with Date objects work", {
7670
date <- as.Date("2020-01-01")
7771
date2 <- date + 1
78-
expect_success(expect_lt(date, date2))
79-
expect_success(expect_lte(date, date2))
8072
expect_success(expect_gt(date2, date))
8173
expect_success(expect_gte(date2, date))
8274

83-
expect_snapshot_failure(expect_lt(date2, date))
84-
expect_snapshot_failure(expect_lte(date2, date))
8575
expect_snapshot_failure(expect_gt(date, date2))
86-
expect_snapshot_failure(expect_gte(date, date2))
8776
})
8877

8978
test_that("comparisons with character objects work", {
90-
expect_success(expect_lt("a", "b"))
9179
expect_success(expect_lte("a", "b"))
92-
expect_success(expect_gt("b", "a"))
93-
expect_success(expect_gte("b", "a"))
9480

95-
expect_snapshot_failure(expect_lt("b", "a"))
9681
expect_snapshot_failure(expect_lte("b", "a"))
97-
expect_snapshot_failure(expect_gt("a", "b"))
98-
expect_snapshot_failure(expect_gte("a", "b"))
9982
})
10083

10184
test_that("comparison must yield a single logical", {

0 commit comments

Comments
 (0)