Skip to content

Commit

Permalink
Treat NULL like character() in +
Browse files Browse the repository at this point in the history
This is different from what's planned for `glue_data()` / `glue()` in #246, but I think it's what we want.

In any case, it preserves existing behaviour. If we want something else, it would need to happen in #246, possibly as part of an edition.
  • Loading branch information
jennybc committed Sep 22, 2023
1 parent 02f5571 commit fb82499
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions R/glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ")"))
Expand Down

0 comments on commit fb82499

Please sign in to comment.