Skip to content

Commit

Permalink
add tests for vec_proxy_missing()
Browse files Browse the repository at this point in the history
  • Loading branch information
khusmann committed Apr 15, 2024
1 parent 5b5caad commit 8fac6c2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/testthat/test-complete.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ test_that("takes the equality proxy", {
expect_identical(vec_detect_complete(df), expect)
})

test_that("takes the missing proxy if defined", {
local_methods(
vec_proxy_missing.vctrs_foobar = function(x, ...) (
data_frame(a=ifelse(x$a == -99, NA, x$a), b=x$b)
),
)

df <- foobar(data_frame(a = c(1, 2, -99), b = c(1, NA, 2)))

expect <- c(TRUE, FALSE, FALSE)

expect_identical(vec_detect_complete(df), expect)
})

test_that("columns with a data frame proxy are incomplete if any columns of the proxy are incomplete (#1404)", {
df <- data_frame(
x = c(NA, 0, 1, 2, 3),
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,25 @@ test_that(">0 row, 0 col data frame always returns `TRUE` (#1585)", {
any(vec_detect_missing(df))
)
})

# ------------------------------------------------------------------------------
# vec_proxy_missing()

test_that("vec_proxy_missing()/vec_any_missing() takes vec_proxy_equal() by default", {
local_methods(
vec_proxy_equal.vctrs_foobar = function(x, ...) (ifelse(x == -99, NA, x)),
)

expect_identical(vec_detect_missing(foobar(c(1, 2, -99, 3))), c(FALSE, FALSE, TRUE, FALSE))
expect_identical(vec_any_missing(foobar(c(1, 2, -99, 3))), TRUE)
expect_identical(vec_any_missing(foobar(c(1, 2, 3))), FALSE)
})

test_that("vec_detect_missing() calls vec_proxy_missing(), if implemented", {
local_methods(
vec_proxy_missing.vctrs_foobar = function(x, ...) (ifelse(x == -99, NA, x)),
)
expect_identical(vec_detect_missing(foobar(c(1, 2, -99, 3))), c(FALSE, FALSE, TRUE, FALSE))
expect_identical(vec_any_missing(foobar(c(1, 2, -99, 3))), TRUE)
expect_identical(vec_any_missing(foobar(c(1, 2, 3))), FALSE)
})
1 change: 1 addition & 0 deletions tests/testthat/test-vctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ test_that("generics are extensible", {
expect_error(vec_restore(NA, NA, NA), class = "rlib_error_dots_nonempty")
expect_error(vec_proxy(NA, NA), class = "rlib_error_dots_nonempty")
expect_error(vec_proxy_equal(NA, NA), class = "rlib_error_dots_nonempty")
expect_error(vec_proxy_missing(NA, NA), class = "rlib_error_dots_nonempty")
expect_error(vec_proxy_compare(NA, NA), class = "rlib_error_dots_nonempty")
expect_error(vec_ptype2(NA, NA, NA), class = "rlib_error_dots_nonempty")
expect_error(vec_ptype_abbr(NA, NA), class = "rlib_error_dots_nonempty")
Expand Down

0 comments on commit 8fac6c2

Please sign in to comment.