diff --git a/R/glue.R b/R/glue.R index 13a2c27..5384a76 100644 --- a/R/glue.R +++ b/R/glue.R @@ -354,10 +354,10 @@ as.character.glue <- function(x, ...) { #' @export `+.glue` <- function(e1, e2) { - if (!is.character(e1)) { + if (!is.null(e1) && !is.character(e1)) { stop("LHS must be a character vector.") } - if (!is.character(e2)) { + if (!is.null(e2) && !is.character(e2)) { stop("RHS must be a character vector.") } diff --git a/tests/testthat/test-glue.R b/tests/testthat/test-glue.R index 57d893d..4f4bb6d 100644 --- a/tests/testthat/test-glue.R +++ b/tests/testthat/test-glue.R @@ -523,10 +523,14 @@ test_that("`+` method does not interpolate twice", { expect_identical(glue("{x}", x = "{wut}") + "y", "{wut}y") }) -test_that("`+` method returns length-0 for a length-0 input", { +test_that("`+` method returns length-0 if there is a length-0 input", { expect_identical(as_glue("hello") + character(), character()) }) +test_that("`+` method returns length-0 if there is a `NULL` input", { + expect_identical(as_glue("hello") + NULL, character()) +}) + test_that("`+` recycles", { x <- c("a", "b", "c") expect_identical("(" + as_glue(x) + ")", paste0("(", x, ")"))