From 9819757cf81fca31c37fde65a69c88e29e020eba Mon Sep 17 00:00:00 2001 From: Leonidas Zhak <70497898+LeonidasZhak@users.noreply.github.com> Date: Sun, 7 Jun 2026 14:31:49 +0800 Subject: [PATCH] tests: add edge-case tests for str_pad() Add 12 new test blocks covering common data cleaning scenarios: - NA handling (single NA, mixed vector, all sides) - Empty string and empty character(0) input - Custom pad characters (0, -, ., *) - Width of 0 (no padding) - Width equal to string width (no padding needed) - Width less than string width (unchanged) - Vectorised width parameter - Vectorised pad parameter - Names preservation for all sides (left, right, both) - Input validation (invalid side, invalid use_width type) Total: 33 new test expectations (9 existing + 33 new = 42 total). --- tests/testthat/_snaps/pad.md | 16 +++++++ tests/testthat/test-pad.R | 83 ++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/testthat/_snaps/pad.md diff --git a/tests/testthat/_snaps/pad.md b/tests/testthat/_snaps/pad.md new file mode 100644 index 00000000..079fe97e --- /dev/null +++ b/tests/testthat/_snaps/pad.md @@ -0,0 +1,16 @@ +# str_pad() validates inputs + + Code + str_pad("a", 5, side = "top") + Condition + Error in `str_pad()`: + ! `side` must be one of "left", "right", or "both", not "top". + +--- + + Code + str_pad("a", 5, use_width = "yes") + Condition + Error in `str_pad()`: + ! `use_width` must be `TRUE` or `FALSE`, not the string "yes". + diff --git a/tests/testthat/test-pad.R b/tests/testthat/test-pad.R index 79a4b089..39a31ee9 100644 --- a/tests/testthat/test-pad.R +++ b/tests/testthat/test-pad.R @@ -43,3 +43,86 @@ test_that("str_pad() preserves names", { x <- c(C = "3", B = "2", A = "1") expect_equal(names(str_pad(x, 2, side = "left")), names(x)) }) + +test_that("str_pad() handles NA values", { + expect_equal(str_pad(NA_character_, 5), NA_character_) + expect_equal(str_pad(c("a", NA, "b"), 5), c(" a", NA, " b")) + expect_equal( + str_pad(c("a", NA), 5, side = "right"), + c("a ", NA) + ) + expect_equal( + str_pad(c("a", NA), 5, side = "both"), + c(" a ", NA) + ) +}) + +test_that("str_pad() handles empty strings", { + expect_equal(str_pad("", 5), " ") + expect_equal(str_pad("", 5, side = "right"), " ") + expect_equal(str_pad("", 5, side = "both"), " ") + expect_equal(str_pad("", 0), "") + expect_equal(str_pad("", 1), " ") +}) + +test_that("str_pad() handles empty character(0)", { + expect_equal(str_pad(character(0), 5), character(0)) + expect_equal(str_pad(character(0), 5, side = "right"), character(0)) +}) + +test_that("str_pad() respects custom pad character", { + expect_equal(str_pad("a", 5, pad = "0"), "0000a") + expect_equal(str_pad("a", 5, pad = "-"), "----a") + expect_equal(str_pad("a", 5, side = "right", pad = "."), "a....") + expect_equal(str_pad("a", 5, side = "both", pad = "*"), "**a**") +}) + +test_that("str_pad() handles width of 0", { + expect_equal(str_pad("hello", 0), "hello") + expect_equal(str_pad("a", 0), "a") + expect_equal(str_pad("", 0), "") +}) + +test_that("str_pad() does not pad when width equals string width", { + expect_equal(str_pad("abc", 3), "abc") + expect_equal(str_pad("abc", 3, side = "right"), "abc") + expect_equal(str_pad("abc", 3, side = "both"), "abc") +}) + +test_that("str_pad() does not pad when width is less than string width", { + expect_equal(str_pad("hello", 3), "hello") + expect_equal(str_pad("hello", 1), "hello") + expect_equal(str_pad("hello", 0), "hello") +}) + +test_that("str_pad() vectorises width", { + expect_equal(str_pad("a", c(3, 5, 7)), c(" a", " a", " a")) + expect_equal( + str_pad("a", c(3, 5), side = "right"), + c("a ", "a ") + ) +}) + +test_that("str_pad() vectorises pad", { + expect_equal(str_pad("a", 5, pad = c("0", "-")), c("0000a", "----a")) + expect_equal( + str_pad("a", 5, side = "right", pad = c(".", "*")), + c("a....", "a****") + ) +}) + +test_that("str_pad() preserves names for all sides", { + x <- c(A = "a", B = "b") + expect_equal(names(str_pad(x, 5, side = "left")), c("A", "B")) + expect_equal(names(str_pad(x, 5, side = "right")), c("A", "B")) + expect_equal(names(str_pad(x, 5, side = "both")), c("A", "B")) +}) + +test_that("str_pad() validates inputs", { + expect_snapshot(error = TRUE, { + str_pad("a", 5, side = "top") + }) + expect_snapshot(error = TRUE, { + str_pad("a", 5, use_width = "yes") + }) +})