From 957236b756930bd69430fef9b9c6c4963f5d4872 Mon Sep 17 00:00:00 2001 From: Leonidas Zhak <70497898+LeonidasZhak@users.noreply.github.com> Date: Sun, 7 Jun 2026 13:50:07 +0800 Subject: [PATCH] tests: add edge-case tests for str_wrap() Add tests covering: - NA value preservation (single NA, mixed NA/vector, NA in middle) - Empty string and empty character vector handling - Vector input processing - indent parameter (first line indentation) - exdent parameter (subsequent line indentation) - Single word longer than width (no wrapping) - Multiple whitespace collapsing - Input validation (width, indent, exdent, whitespace_only) --- tests/testthat/_snaps/wrap.md | 23 ++++++++++++++++ tests/testthat/test-wrap.R | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/testthat/_snaps/wrap.md diff --git a/tests/testthat/_snaps/wrap.md b/tests/testthat/_snaps/wrap.md new file mode 100644 index 00000000..7e8b293d --- /dev/null +++ b/tests/testthat/_snaps/wrap.md @@ -0,0 +1,23 @@ +# str_wrap() validates its inputs + + Code + str_wrap("x", width = "a") + Condition + Error in `str_wrap()`: + ! `width` must be a number, not the string "a". + Code + str_wrap("x", indent = "a") + Condition + Error in `str_wrap()`: + ! `indent` must be a whole number, not the string "a". + Code + str_wrap("x", exdent = "a") + Condition + Error in `str_wrap()`: + ! `exdent` must be a whole number, not the string "a". + Code + str_wrap("x", whitespace_only = 1) + Condition + Error in `str_wrap()`: + ! `whitespace_only` must be `TRUE` or `FALSE`, not the number 1. + diff --git a/tests/testthat/test-wrap.R b/tests/testthat/test-wrap.R index 3584f3e2..e1bcdfbb 100644 --- a/tests/testthat/test-wrap.R +++ b/tests/testthat/test-wrap.R @@ -21,3 +21,54 @@ test_that("str_wrap() preserves names", { x <- c(C = "3", B = "2", A = "1") expect_equal(names(str_wrap(x)), names(x)) }) + +test_that("str_wrap() handles NA values", { + expect_equal(str_wrap(NA_character_), NA_character_) + expect_equal(str_wrap(c(NA, "hello world")), c(NA, "hello world")) + expect_equal(str_wrap(c("foo", NA, "bar")), c("foo", NA, "bar")) +}) + +test_that("str_wrap() handles empty input", { + expect_equal(str_wrap(""), "") + expect_equal(str_wrap(character()), character()) +}) + +test_that("str_wrap() works with vector input", { + out <- str_wrap(c("hello world", "goodbye world"), width = 10) + expect_length(out, 2) + expect_true(grepl("\n", out[1])) + expect_true(grepl("\n", out[2])) +}) + +test_that("str_wrap() respects indent parameter", { + out <- str_wrap("The quick brown fox jumps over the lazy dog", width = 20, indent = 2) + lines <- str_split(out, "\n")[[1]] + expect_true(grepl("^ The", lines[1])) +}) + +test_that("str_wrap() respects exdent parameter", { + out <- str_wrap("The quick brown fox jumps over the lazy dog", width = 20, exdent = 2) + lines <- str_split(out, "\n")[[1]] + if (length(lines) > 1) { + expect_true(grepl("^ ", lines[2])) + } +}) + +test_that("str_wrap() handles single word longer than width", { + out <- str_wrap("supercalifragilisticexpialidocious", width = 10) + expect_equal(out, "supercalifragilisticexpialidocious") +}) + +test_that("str_wrap() collapses multiple whitespace", { + out <- str_wrap(" hello world ", width = 80) + expect_equal(out, "hello world") +}) + +test_that("str_wrap() validates its inputs", { + expect_snapshot(error = TRUE, { + str_wrap("x", width = "a") + str_wrap("x", indent = "a") + str_wrap("x", exdent = "a") + str_wrap("x", whitespace_only = 1) + }) +})