Skip to content

Commit

Permalink
add basic tidyselect tests for has_columns()
Browse files Browse the repository at this point in the history
  • Loading branch information
yjunechoe committed Nov 2, 2023
1 parent 070bb61 commit ad289ca
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/testthat/test-has_columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,34 @@ test_that("the `has_columns()` function works with tidyselect", {
expect_true(small_table %>% has_columns(everything()))
expect_true(small_table %>% has_columns(matches(".*te")))

# Empty selection from helpers is FALSE
expect_false(small_table %>% has_columns(contains("z")))
expect_false(small_table %>% has_columns(starts_with("z")))
expect_false(small_table %>% has_columns(ends_with("z")))
expect_false(small_table %>% has_columns(last_col() + 1))
expect_false(small_table %>% has_columns(matches("z")))

# Genuine evaluation errors are rethrown
expect_error(small_table %>% has_columns(stop("Oh no!")), "Oh no!")
expect_error(small_table %>% has_columns(c(stop("Oh no!"))), "Oh no!")
expect_error(small_table %>% has_columns(c(a, stop("Oh no!"))), "Oh no!")

# Mix of selections work like AND
expect_true(small_table %>% has_columns(c(contains("da"), contains("te"))))
expect_true(small_table %>% has_columns(c(a, contains("te"))))
expect_false(small_table %>% has_columns(c(z, contains("te"))))
expect_false(small_table %>% has_columns(c(a, contains("z"))))
expect_false(small_table %>% has_columns(c(contains("da"), contains("z"))))

# `any_of()`/`all_of()` patterns work
has_one <- c("a", "y", "z")
has_all <- c("a", "b", "c")
has_none <- c("x", "y", "z")
expect_true(small_table %>% has_columns(any_of(has_one)))
expect_true(small_table %>% has_columns(any_of(has_all)))
expect_false(small_table %>% has_columns(any_of(has_none)))
expect_false(small_table %>% has_columns(all_of(has_one)))
expect_true(small_table %>% has_columns(all_of(has_all)))
expect_false(small_table %>% has_columns(all_of(has_none)))

})

0 comments on commit ad289ca

Please sign in to comment.