diff --git a/tests/testthat/test-flatten.R b/tests/testthat/test-flatten.R index 14ceb4cd..6b9d2a32 100644 --- a/tests/testthat/test-flatten.R +++ b/tests/testthat/test-flatten.R @@ -24,3 +24,43 @@ test_that("str_flatten_oxford removes comma iif necessary", { expect_equal(str_flatten_comma(letters[1:3], " or "), "a, b or c") expect_equal(str_flatten_comma(letters[1:3]), "a, b, c") }) + +test_that("str_flatten handles empty input with last", { + expect_equal(str_flatten(character(), ", ", " and "), "") + expect_equal(str_flatten(character(), "-"), "") +}) + +test_that("str_flatten handles single element with last", { + expect_equal(str_flatten("a", ", ", " and "), "a") + expect_equal(str_flatten("a", "-"), "a") +}) + +test_that("str_flatten handles all-NA input with na.rm", { + expect_equal(str_flatten(c(NA, NA), na.rm = TRUE), "") + expect_equal(str_flatten(NA_character_, na.rm = TRUE), "") +}) + +test_that("str_flatten_comma handles empty input with last", { + expect_equal(str_flatten_comma(character(), ", or "), "") + expect_equal(str_flatten_comma(character()), "") +}) + +test_that("str_flatten_comma handles single element with last", { + expect_equal(str_flatten_comma("a", ", or "), "a") + expect_equal(str_flatten_comma("a"), "a") +}) + +test_that("str_flatten_comma handles NA with last and na.rm", { + expect_equal( + str_flatten_comma(c("a", NA, "b"), ", or ", na.rm = TRUE), + "a, or b" + ) + expect_equal( + str_flatten_comma(c(NA, "a", "b"), ", or ", na.rm = TRUE), + "a, or b" + ) + expect_equal( + str_flatten_comma(c("a", "b", NA), ", or ", na.rm = TRUE), + "a, or b" + ) +})