Skip to content

Commit

Permalink
Test argument to collapse
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
jimhester committed Jan 18, 2017
1 parent f17ffc4 commit f907462
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion R/fstrings.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ f <- fstring
#' # Wide values can be truncated
#' collapse(f("{1:10}"), width = 5)
#' @export
collapse <- function(x, sep = "", width = Inf) {
collapse <- function(x, sep = "", width = Inf, last = "") {
if (nzchar(last) && length(x) > 1) {
res <- collapse(x[seq(1, length(x) - 1)], sep = sep, width = Inf)
return(collapse(f(res, last, x[length(x)]), width = width))
}
x <- paste0(x, collapse = sep)
if (width < Inf) {
x_width <- nchar(x, "width")
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-collapse.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ test_that("collapse truncates", {
expect_identical("1...", collapse(1:10, width = 4))
expect_identical("...", collapse(1:10, width = 0))
})

test_that("last argument to collapse", {
expect_equal(collapse(character(), last = " and "), "")
expect_equal(collapse("", last = " and "), "")
expect_equal(collapse(1, last = " and "), "1")
expect_equal(collapse(1:2, last = " and "), "1 and 2")
expect_equal(collapse(1:4, ", ", last = " and "), "1, 2, 3 and 4")

expect_equal(collapse(1:4, ", ", last = " and ", width = 5), "1,...")

expect_equal(collapse(1:4, ", ", last = " and ", width = 10), "1, 2, 3...")
})

0 comments on commit f907462

Please sign in to comment.