1context("Replacements")
2
3test_that("basic replacement works", {
4  expect_equal(str_replace_all("abababa", "ba", "BA"), "aBABABA")
5  expect_equal(str_replace("abababa", "ba", "BA"), "aBAbaba")
6})
7
8test_that("replacement strings with capture groups refs and dollar signs work", {
9  expect_equal(str_replace_all("abc$a$1$2", fixed("a"), "$1"), "$1bc$$1$1$2")
10  expect_equal(str_replace("abc$a$1$2", fixed("a"), "$1"), "$1bc$a$1$2")
11
12  expect_equal(str_replace_all("abcde", "(b)(c)(d)", "\\1"), "abe")
13  expect_equal(str_replace_all("abcde", "(b)(c)(d)", "\\2"), "ace")
14  expect_equal(str_replace_all("abcde", "(b)(c)(d)", "\\3"), "ade")
15
16  # gsub("(b)(c)(d)", "\\0", "abcde", perl=TRUE) gives a0e,
17  # in ICU regex $0 refers to the whole pattern match
18  expect_equal(str_replace_all("abcde", "(b)(c)(d)", "\\0"), "abcde")
19
20  # gsub("(b)(c)(d)", "\\4", "abcde", perl=TRUE) is legal,
21  # in ICU regex this gives an U_INDEX_OUTOFBOUNDS_ERROR
22  expect_error(
23    str_replace_all("abcde", "(b)(c)(d)", "\\4"),
24    "index that is out of bounds"
25  )
26
27  expect_equal(str_replace_all("abcde", "bcd", "\\\\1"), "a\\1e")
28
29  expect_equal(str_replace_all("a!1!2!b", "!", "$"), "a$1$2$b")
30  expect_equal(str_replace("aba", "b", "$"), "a$a")
31  expect_equal(str_replace("aba", "b", "$$$"), "a$$$a")
32  expect_equal(str_replace("aba", "(b)", "\\1$\\1$\\1"), "ab$b$ba")
33  expect_equal(str_replace("aba", "(b)", "\\1$\\\\1$\\1"), "ab$\\1$ba")
34  expect_equal(str_replace("aba", "(b)", "\\\\1$\\1$\\\\1"), "a\\1$b$\\1a")
35})
36
37test_that("can replace multiple matches", {
38  x <- c("a1", "b2")
39  y <- str_replace_all(x, c("a" = "1", "b" = "2"))
40  expect_equal(y, c("11", "22"))
41})
42
43test_that("multiple matches respects class", {
44  x <- c("x", "y")
45  y <- str_replace_all(x, regex(c("X" = "a"), ignore_case = TRUE))
46  expect_equal(y, c("a", "y"))
47})
48
49test_that("replacement must be a string", {
50  expect_error(str_replace("x", "x", 1), "must be a character vector")
51})
52
53test_that("replacement must be a string", {
54  expect_equal(str_replace("xyz", "x", NA_character_), NA_character_)
55})
56
57test_that("can replace all types of NA values", {
58  expect_equal(str_replace_na(NA), "NA")
59  expect_equal(str_replace_na(NA_character_), "NA")
60  expect_equal(str_replace_na(NA_complex_), "NA")
61  expect_equal(str_replace_na(NA_integer_), "NA")
62  expect_equal(str_replace_na(NA_real_), "NA")
63})
64
65
66# functions ---------------------------------------------------------------
67
68test_that("can supply replacement function", {
69  expect_equal(str_replace("abc", "a|c", toupper), "Abc")
70  expect_equal(str_replace_all("abc", "a|c", toupper), "AbC")
71})
72
73test_that("replacement can be different length", {
74  double <- function(x) str_dup(x, 2)
75  expect_equal(str_replace_all("abc", "a|c", double), "aabcc")
76})
77
78test_that("replacement with NA works", {
79  expect_equal(str_replace("abc", "z", toupper), "abc")
80})
81
82# fix_replacement ---------------------------------------------------------
83
84test_that("$ are escaped", {
85  expect_equal(fix_replacement("$"), "\\$")
86  expect_equal(fix_replacement("\\$"), "\\\\$")
87})
88
89test_that("\1 converted to $1 etc", {
90  expect_equal(fix_replacement("\\1"), "$1")
91  expect_equal(fix_replacement("\\9"), "$9")
92})
93
94test_that("\\1 left as is", {
95  expect_equal(fix_replacement("\\\\1"), "\\\\1")
96})
97