From 2036d57a5baab37aff0f0a813b8dcab6b8b53c90 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Wed, 10 Jul 2024 12:05:52 -0400 Subject: [PATCH] [todo msg] wrapi_text: count fix --- R/util.R | 9 +++++---- tests/testthat/test-util.R | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/R/util.R b/R/util.R index 9617dc7..56c7532 100644 --- a/R/util.R +++ b/R/util.R @@ -181,22 +181,23 @@ wrapi_text <- function( if(is.null(start)) add else paste(start, add, sep = sep) } - start <- NULL; shift <- 0 + start <- NULL + shift <- 1 npieces <- length(pieces) for (i in 2:npieces) { - pieces_sep <- pieces[(1 + shift):i] + pieces_sep <- pieces[shift:i] add <- paste0(pieces_sep, collapse = wrap_chr) s <- cat_start(start = start, add = add, sep = wrap_chr) if(max_line_char(s) <= width){ next }else{ - pieces_start <- pieces[(1 + shift):(i-1)] + pieces_start <- pieces[shift:(i - 1)] add <- paste(paste0(pieces_start, collapse = wrap_chr), pieces[i], sep = newline_sep) if (i == npieces) { s <- cat_start(start = start, add = add, sep = "") }else{ start <- cat_start(start = start, add = add, sep = "") - shift <- shift + i + shift <- min(i + 1, npieces) } } } diff --git a/tests/testthat/test-util.R b/tests/testthat/test-util.R index 1ee2d01..922f574 100644 --- a/tests/testthat/test-util.R +++ b/tests/testthat/test-util.R @@ -40,4 +40,25 @@ test_that("wrap_text(): past offenders", { wrap_text("abcde", width = 2, wrap_sym = NULL, strict = TRUE), "ab\ncd\ne" ) + + expect_identical( + wrap_text("abcdefghij", width = 4, wrap_sym = NULL, strict = TRUE), + "abcd\nefgh\nij" + ) + + expect_identical( + wrap_text( + "test-foo.R\ntest-foo_bar.R", + width = 8, wrap_sym = NULL, strict = TRUE + ), + "test-foo\n.R\ntest-foo\n_bar.R" + ) + + expect_identical( + wrap_text( + "test-foo.R\ntest-foo_bar.R", + width = 4, wrap_sym = NULL, strict = TRUE + ), + "test\n-foo\n.R\ntest\n-foo\n_bar\n.R" + ) })