-
Notifications
You must be signed in to change notification settings - Fork 321
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
Fix comparison of integers #309
Conversation
@@ -145,7 +145,7 @@ compare.numeric <- function(x, y, ..., max_diffs = 10) { | |||
|
|||
# If they're not the same type or length, fallback to default method | |||
equal <- paste0(equal, collapse = "\n") | |||
if (!is.integer(x) && !is.numeric(y)) return(comparison(FALSE, equal)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a better check would be !identical(class(x), class(y))
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently:
> testthat::expect_equal(1L, 0)
Error: 1L not equal to 0
1 - 0 == 1
I guess this case (and others) will be handled differently if comparing classes of x and y.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's probably reasonable - it doesn't really matter what the values are if the classes are different. But a bigger problem is expect_equal(1, 1L)
which for convenience shouldn't fail. Maybe we should special case integer vs. numeric comparison?
This current behaviour is also bad:
expect_equal(1L, factor(1))
# Error: 1L not equal to factor(1)
# 1 - 1 == NA In addition: Warning message:
# In Ops.factor(x, y) : ‘-’ not meaningful for factors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this gives an uninformative message:
x <- structure(1L, class = "myclass")
expect_equal(1L, x)
# Error: 1L not equal to x
# 1 - 1 == 0
- if length different, fallback to string comparison - otherwise, comparison works only for integer, numeric and complex classes
Updated: Added support for complex, fallback to string comparison if length different, otherwise use numeric comparison only for "true" numbers. |
This probably conflicts with #313. |
Merged with master. |
Revert to default comparison also if x is integer.
Current behavior: