From e5b5f9b0f5ccdb37ec6b8429ee06f35fcefd8235 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Thu, 5 Apr 2018 19:29:28 -0700 Subject: [PATCH] Fixes #203. Duplicate of pull request #206, which was broken because TravisCI was using a different serialization format for R-devel checks. See http://r.789695.n4.nabble.com/R-CMD-build-then-check-fails-on-R-devel-due-to-serialization-version-td4747582.html for details. --- R/trunc.R | 2 +- tests/testthat/test-trunc.r | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/R/trunc.R b/R/trunc.R index 387495df..f7b860f0 100644 --- a/R/trunc.R +++ b/R/trunc.R @@ -18,7 +18,7 @@ str_trunc <- function(string, width, side = c("right", "left", "center"), ellipsis = "...") { side <- match.arg(side) - too_long <- !is.na(string) && str_length(string) > width + too_long <- !is.na(string) & str_length(string) > width width... <- width - str_length(ellipsis) if (width... < 0) stop("`width` is shorter than `ellipsis`", .call = FALSE) diff --git a/tests/testthat/test-trunc.r b/tests/testthat/test-trunc.r index b349b58c..4d632d7a 100644 --- a/tests/testthat/test-trunc.r +++ b/tests/testthat/test-trunc.r @@ -11,6 +11,13 @@ test_that("NA values in input pass through unchanged", { ) }) +test_that("truncations work for all elements of a vector", { + expect_equal( + str_trunc(c("abcd", "abcde", "abcdef"), width = 5), + c("abcd", "abcde", "ab...") + ) +}) + test_that("truncations work for all sides", { trunc <- function(direction) str_trunc(