Skip to content

Commit

Permalink
Add tests for behavior of count() (#6904)
Browse files Browse the repository at this point in the history
* Add tests for behavior of count()

* Add new test

* Tweaks

---------

Co-authored-by: Davis Vaughan <[email protected]>
  • Loading branch information
krlmlr and DavisVaughan committed Aug 23, 2023
1 parent cc86e9c commit 41501e4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/testthat/test-count-tally.R
Original file line number Diff line number Diff line change
@@ -1,5 1,25 @@
# count -------------------------------------------------------------------

test_that("count sorts output by keys by default", {
# Due to usage of `summarise()` internally
df <- tibble(x = c(2, 1, 1, 2, 1))
out <- count(df, x)
expect_equal(out, tibble(x = c(1, 2), n = c(3, 2)))
})

test_that("count can sort output by `n`", {
df <- tibble(x = c(1, 1, 2, 2, 2))
out <- count(df, x, sort = TRUE)
expect_equal(out, tibble(x = c(2, 1), n = c(3, 2)))
})

test_that("count can rename grouping columns", {
# But should it really allow this?
df <- tibble(x = c(2, 1, 1, 2, 1))
out <- count(df, y = x)
expect_equal(out, tibble(y = c(1, 2), n = c(3, 2)))
})

test_that("informs if n column already present, unless overridden", {
df1 <- tibble(n = c(1, 1, 2, 2, 2))
expect_message(out <- count(df1, n), "already present")
Expand Down

0 comments on commit 41501e4

Please sign in to comment.