Skip to content

add unit test suite to check fct_* behaviour when input is not a factor #19

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

Merged
merged 3 commits into from
May 5, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/testthat/test-standalone-forcats.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ test_that("fct_relevel() works", {
f_new <- fct_relevel(f, "b", "a", "d") # "d" is unobserved
expect_equal(levels(f_new), c("b", "a", "d", "c")) # "d" should be added as a level, even though not observed
})

test_that("fct_* function behaviour when input is not a factor ", {
f <- factor(c("b", "b", "a", "c", "c", "c"))
c <- as.character(c("b", "b", "a", "c", "c", "c"))
expect_equal(forcats::fct_infreq(f), fct_infreq(c))
expect_equal(forcats::fct_inorder(f), fct_inorder(c))
expect_equal(forcats::fct_rev(f), fct_rev(c))
expect_equal(forcats::fct_relevel(fct_relevel(f, "b", "a")), fct_relevel(fct_relevel(c, "b", "a")))

f <- factor(sample(letters[1:3], 20, replace = TRUE))
c <- as.character(f)
expect_equal(forcats::fct_expand(f, letters[1:6]), fct_expand(c, letters[1:6]))

f <- as.factor(c("a", "b", NA, "c", "b", NA))
c <- as.character(f)
expect_equal(forcats::fct_na_value_to_level(f, level = NA), fct_na_value_to_level(c, level = NA))
})