Skip to content

Commit

Permalink
Process check.attributes in compare.numeric
Browse files Browse the repository at this point in the history
Fixes #485
  • Loading branch information
hadley committed Jul 5, 2016
1 parent b394894 commit cd90427
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# testthat 1.0.2.9000

* `compare.numeric()` respects `check.attributes()` so `expect_equivalent()`
correctly ignores attributes of numeric vectors (#485).

* Properly report endless recursion, limiting the stack trace to the first and last 10 entries (#474).

* Fix context test (#494).
Expand Down
10 changes: 7 additions & 3 deletions R/compare-numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
#' # Compare ignores minor numeric differences in the same way
#' # as all.equal.
#' compare(x, x + 1e-9)
compare.numeric <- function(x, y, tolerance = .Machine$double.eps ^ 0.5, ..., max_diffs = 9) {
all_equal <- all.equal(x, y, tolerance = tolerance, ...)
compare.numeric <- function(x, y,
tolerance = .Machine$double.eps ^ 0.5,
check.attributes = TRUE,

This comment has been minimized.

Copy link
@krlmlr

krlmlr Jul 5, 2016

Member

Do we need to do the same for compare.default()?

..., max_diffs = 9) {
all_equal <- all.equal(x, y, tolerance = tolerance,
check.attributes = check.attributes, ...)
if (isTRUE(all_equal)) {
return(no_difference())
}
Expand All @@ -31,7 +35,7 @@ compare.numeric <- function(x, y, tolerance = .Machine$double.eps ^ 0.5, ..., ma
if (!same_length(x, y)) {
return(diff_length(x, y))
}
if (!same_attr(x, y)) {
if (check.attributes && !same_attr(x, y)) {
return(diff_attr(x, y))
}

Expand Down
4 changes: 2 additions & 2 deletions man/compare.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-compare-numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ test_that("attributes must be identical", {
expect_match(compare(x4, x5)$message, "Names: 1 string mismatch")
})

test_that("unless check.attributes is FALSE", {
x1 <- 1L
x2 <- c(a = 1L)
x3 <- structure(1L, a = 1)

expect_equal(compare(x1, x2, check.attributes = FALSE)$message, "Equal")
expect_equal(compare(x1, x3, check.attributes = FALSE)$message, "Equal")
expect_equal(compare(x2, x3, check.attributes = FALSE)$message, "Equal")
})

# Values ------------------------------------------------------------------

test_that("two identical vectors are the same", {
Expand Down

0 comments on commit cd90427

Please sign in to comment.