Skip to content

Commit 7628c35

Browse files
authored
add unit test suite to check fct_* behaviour when input is not a factor (#19)
closes #18 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] If a standalone script was updated, a comment is added to the script header (changelog) AND the `last-updated` field has been updated. - [ ] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [ ] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] If a standalone script was updated, a comment is added to the script header (changelog) AND the `last-updated` field has been updated. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge". - [ ] Create an issue in any repositories using {standalone} to update the standalone scripts.
1 parent 44c927d commit 7628c35

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

tests/testthat/test-standalone-forcats.R

+17
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ test_that("fct_relevel() works", {
5454
f_new <- fct_relevel(f, "b", "a", "d") # "d" is unobserved
5555
expect_equal(levels(f_new), c("b", "a", "d", "c")) # "d" should be added as a level, even though not observed
5656
})
57+
58+
test_that("fct_* function behaviour when input is not a factor ", {
59+
f <- factor(c("b", "b", "a", "c", "c", "c"))
60+
c <- as.character(c("b", "b", "a", "c", "c", "c"))
61+
expect_equal(forcats::fct_infreq(f), fct_infreq(c))
62+
expect_equal(forcats::fct_inorder(f), fct_inorder(c))
63+
expect_equal(forcats::fct_rev(f), fct_rev(c))
64+
expect_equal(forcats::fct_relevel(forcats::fct_relevel(f, "b", "a")), fct_relevel(fct_relevel(c, "b", "a")))
65+
66+
f <- factor(sample(letters[1:3], 20, replace = TRUE))
67+
c <- as.character(f)
68+
expect_equal(forcats::fct_expand(f, letters[1:6]), fct_expand(c, letters[1:6]))
69+
70+
f <- as.factor(c("a", "b", NA, "c", "b", NA))
71+
c <- as.character(f)
72+
expect_equal(forcats::fct_na_value_to_level(f, level = NA), fct_na_value_to_level(c, level = NA))
73+
})

0 commit comments

Comments
 (0)