make expect_lt() etc. work properly for non-numeric data#2269
Conversation
hadley
left a comment
There was a problem hiding this comment.
Thanks for discovering this problem and proposing a fix! I do think it would be useful to include a comparison for dates & date-times, especially since those data types often don't print small decimals. I suspect the default difftime() output should be adequate.
| expect_snapshot_failure(expect_gte(time, time2)) | ||
| }) | ||
|
|
||
| test_that("comparisons with Date objects work", { |
There was a problem hiding this comment.
This level of tests feels a bit heavy to me, I think because you're just repeatedly testing the same bit of code:
msg_act <- sprintf(
"Actual comparison: \"%s\" %s \"%s\"",
act$val,
actual_op,
exp$val
)
msg_diff <- NULLI'd suggest testing just one of the four expectations for non-numeric inputs, something like this:
test_that("informative failure for non-numeric inputs", {
char1 <- "x"
chat2 <- "y"
expect_snapshot_failure(expect_gt(x1, x2))
date1 <- ...
}You might also consider refactoring the function a bit so you could just do snapshot tests of failure_compare("x", "y", ">") rather than having to do the complete test. To do that you'd need generate msg_exp in expect_compare_(), then `failure_compare() could just take the values, rather than labelled values.
|
Thanks for your inputs. This is my second attempt: I also output the difference for dates and times now and reduced the number of tests. In the meantime I found another problem in these expectations: the error messages fail for negative numbers: This can be fixed easily by taking the absolute value in |
|
Just fix it please 😄 |
|
Thanks for working on this! Much appreciated 😄 |
This is an attempt to make
expect_lt(),expect_lte(),expect_gt(), andexpect_gte()work properly for non-numeric data by ensuring that the failure messages are created correctly also for those. It fixes #2268.With these changes, the examples mentioned in #2268 lead to the following output:
I decided to differentiate between numeric inputs and all others. For numeric inputs, nothing changes, but for anything else, the messages are created differently. I made two decisions that others might disagree with and that I could change if requested:
print-method fordifftime.