Skip to content

Commit 42d29d4

Browse files
jennybchadley
andauthored
Refine + method (#309)
* Refine `+` method * Add a test * Update tests/testthat/test-glue.R Co-authored-by: Hadley Wickham <[email protected]> * Add a test for `+`ing size incompatible inputs * Tidy up these tests * Treat `NULL` like `character()` in `+` 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. * Tweak NEWS --------- Co-authored-by: Hadley Wickham <[email protected]>
1 parent 740ca7f commit 42d29d4

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* `+` now works in more situations, and gives errors when one side isn't a
44
character vector. It no longer automatically applies glue interpolation to
5-
both sides; you'll need to do that yourself as needed (#286).
5+
a non-glue input, if there is one. You'll need to do that yourself (#286).
66

77
* `glue_collapse(character())` (and hence `glue_sql_collapse(character())`) now
88
return `""`, so that they always return a single string (#88).

R/glue.R

+7-3
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,18 @@ as.character.glue <- function(x, ...) {
354354

355355
#' @export
356356
`+.glue` <- function(e1, e2) {
357-
if (!is.character(e1)) {
357+
if (!is.null(e1) && !is.character(e1)) {
358358
stop("LHS must be a character vector.")
359359
}
360-
if (!is.character(e2)) {
360+
if (!is.null(e2) && !is.character(e2)) {
361361
stop("RHS must be a character vector.")
362362
}
363363

364-
as_glue(paste0(e1, e2))
364+
glue_data(
365+
"{e1}{e2}",
366+
.x = list(e1 = e1, e2 = e2),
367+
.envir = parent.frame()
368+
)
365369
}
366370

367371
#' @importFrom methods setOldClass

tests/testthat/_snaps/glue.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# + method requires character vectors
1+
# `+` method requires character vectors
22

33
Code
44
as_glue("a") + 1
@@ -9,6 +9,13 @@
99
Error <simpleError>
1010
LHS must be a character vector.
1111

12+
# `+` method errors for inputs of incompatible size
13+
14+
Code
15+
as_glue(letters[1:2]) + letters[1:3]
16+
Error <simpleError>
17+
Variables must be length 1 or 3
18+
1219
# unterminated comment
1320

1421
Code

tests/testthat/test-glue.R

+27-9
Original file line numberDiff line numberDiff line change
@@ -507,22 +507,40 @@ test_that("throws informative error if interpolating a function", {
507507
}
508508
})
509509

510-
test_that("+ method for glue works", {
511-
expect_identical(glue("foo") + "bar", as_glue("foobar"))
512-
expect_identical(glue("x = ") + "{x}", as_glue("x = {x}"))
510+
test_that("`+` method for glue works", {
511+
expect_identical(glue("foo") + "bar", "foobar")
512+
expect_identical("foo" + glue("bar"), "foobar")
513+
})
513514

514-
x <- c("a", "b", "c")
515-
expect_identical("(" + as_glue(x) + ")", paste0("(", x, ")"))
515+
test_that("`+` method requires character vectors", {
516+
expect_snapshot(error = TRUE, {
517+
as_glue("a") + 1
518+
1 + as_glue("a")
519+
})
516520
})
517521

518522
test_that("`+` method does not interpolate twice", {
519-
expect_identical(glue("{x}", x = "{wut}") + "y", as_glue("{wut}y"))
523+
expect_identical(glue("{x}", x = "{wut}") + "y", "{wut}y")
524+
})
525+
526+
test_that("`+` method returns length-0 if there is a length-0 input", {
527+
expect_identical(as_glue("hello") + character(), character())
520528
})
521529

522-
test_that("+ method requires character vectors", {
530+
test_that("`+` method returns length-0 if there is a `NULL` input", {
531+
expect_identical(as_glue("hello") + NULL, character())
532+
})
533+
534+
test_that("`+` recycles", {
535+
x <- c("a", "b", "c")
536+
expect_identical("(" + as_glue(x) + ")", paste0("(", x, ")"))
537+
y <- as.character(1:3)
538+
expect_identical(as_glue(x) + y, c("a1", "b2", "c3"))
539+
})
540+
541+
test_that("`+` method errors for inputs of incompatible size", {
523542
expect_snapshot(error = TRUE, {
524-
as_glue("a") + 1
525-
1 + as_glue("a")
543+
as_glue(letters[1:2]) + letters[1:3]
526544
})
527545
})
528546

0 commit comments

Comments
 (0)