Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/testthat/_snaps/wrap.md
Original file line number Diff line number Diff line change
@@ -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.

51 changes: 51 additions & 0 deletions tests/testthat/test-wrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Loading