Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve expect_setequal() feedback #1745

Merged
merged 3 commits into from
Mar 1, 2023
Merged

Improve expect_setequal() feedback #1745

merged 3 commits into from
Mar 1, 2023

Conversation

hadley
Copy link
Member

@hadley hadley commented Mar 1, 2023

Fixes #1657

@hadley hadley requested a review from DavisVaughan March 1, 2023 14:35
Comment on lines 40 to 43
if (any(exp_miss))
paste0("* Missing from expected: ", strings(act$val[act_miss]), "\n"),
if (any(act_miss))
paste0("* Missing from actual: ", strings(exp$val[exp_miss]), "\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you feel about framing this as Only in expected: instead of Missing from expected:?

I have trouble reading this example:

x <- c("a", "b")
y <- c("b", "c")

expect_snapshot_failure(expect_setequal(x, y))
#> `x` and `y` don't have the same values
#> * Missing from expected: "a"
#> * Missing from actual: "c"

and I think it is because my brain is juggling too many things. I have to realize that y is "expected" but then look over at x to see that it did indeed have "a" which isn't in y.

This makes more sense to me

x <- c("a", "b")
y <- c("b", "c")

expect_snapshot_failure(expect_setequal(x, y))
#> `x` (`actual`) and `y` (`expected`) don't have the same values
#> * Only in `actual`: "a"
#> * Only in `expected`: "c"

I also like seeing `x` (`actual`) because I can't ever remember which is which. It is also what expect_equal() and expect_identical() seem to do

> testthat::expect_identical(1, 1.5)
Error: 1 (`actual`) not identical to 1.5 (`expected`).

  `actual`: 1.0
`expected`: 1.5
> testthat::expect_equal(1, 1.5)
Error: 1 (`actual`) not equal to 1.5 (`expected`).

  `actual`: 1.0
`expected`: 1.5

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it.

loc <- which(i)
if (length(loc) == 1) {
return(loc)
strings <- function(x) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you use expect_setequal() on more than strings though? How does this look on numeric values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yes.

@hadley hadley merged commit 7ea2245 into main Mar 1, 2023
@hadley hadley deleted the setequal-output branch March 1, 2023 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

expect_setequal() should make more of an effort to display actual values
2 participants