Skip to content

Commit a0025ea

Browse files
committed
Move tests from test-preserve-names.R into each function's test file.
1 parent 6efaf97 commit a0025ea

23 files changed

+240
-147
lines changed

tests/testthat/test-case.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ test_that("to_sentence capitalizes just the first letter", {
1313
x <- "This is a sentence."
1414
expect_identical(str_to_sentence("a Test"), "A test")
1515
})
16+
17+
test_that("case conversions preserve names", {
18+
x <- c(C = "3", B = "2", A = "1")
19+
expect_equal(names(str_to_lower(x)), names(x))
20+
expect_equal(names(str_to_upper(x)), names(x))
21+
expect_equal(names(str_to_title(x)), names(x))
22+
})

tests/testthat/test-conv.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ test_that("encoding conversion works", {
88
test_that("check encoding argument", {
99
expect_snapshot(str_conv("A", c("ISO-8859-1", "ISO-8859-2")), error = TRUE)
1010
})
11+
12+
test_that("str_conv() preserves names", {
13+
x <- c(C = "3", B = "2", A = "1")
14+
expect_equal(names(str_conv(x, "UTF-8")), names(x))
15+
})

tests/testthat/test-count.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@ test_that("can count boundaries", {
2020
expect_equal(str_count("a b c", ""), 5)
2121
expect_equal(str_count("a b c", boundary("word")), 3)
2222
})
23+
24+
test_that("str_count() preserves names", {
25+
x <- c(C = "3", B = "2", A = "1")
26+
expect_equal(names(str_count(x, ".")), names(x))
27+
})
28+
29+
test_that("str_count() drops names when pattern is vector and string is scalar", {
30+
x1 <- c(A = "ab")
31+
p2 <- c("a", "b")
32+
expect_null(names(str_count(x1, p2)))
33+
})
34+
35+
test_that("str_count() preserves names when pattern and string have same length", {
36+
x2 <- c(A = "ab", B = "cd")
37+
p2 <- c("a", "c")
38+
expect_equal(names(str_count(x2, p2)), names(x2))
39+
})

tests/testthat/test-detect.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,35 @@ test_that("functions use tidyverse recycling rules", {
5252
})
5353
})
5454

55+
test_that("detection functions preserve names", {
56+
x <- c(C = "3", B = "2", A = "1")
57+
expect_equal(names(str_detect(x, "[123]")), names(x))
58+
expect_equal(names(str_starts(x, "1")), names(x))
59+
expect_equal(names(str_ends(x, "1")), names(x))
60+
expect_equal(names(str_like(x, "%")), names(x))
61+
expect_equal(names(str_ilike(x, "%")), names(x))
62+
})
63+
64+
test_that("detection drops names when pattern is vector and string is scalar", {
65+
x1 <- c(A = "ab")
66+
p2 <- c("a", "b")
67+
expect_null(names(str_detect(x1, p2)))
68+
expect_null(names(str_starts(x1, p2)))
69+
expect_null(names(str_ends(x1, p2)))
70+
expect_null(names(str_like(x1, p2)))
71+
expect_null(names(str_ilike(x1, p2)))
72+
})
73+
74+
test_that("detection preserves names when pattern and string have same length", {
75+
x2 <- c(A = "ab", B = "cd")
76+
p2 <- c("a", "c")
77+
expect_equal(names(str_detect(x2, p2)), names(x2))
78+
expect_equal(names(str_starts(x2, p2)), names(x2))
79+
expect_equal(names(str_ends(x2, p2)), names(x2))
80+
expect_equal(names(str_like(x2, p2)), names(x2))
81+
expect_equal(names(str_ilike(x2, p2)), names(x2))
82+
})
83+
5584
# str_like ----------------------------------------------------------------
5685

5786

tests/testthat/test-dup.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ test_that("separator must be a single string", {
3131
str_dup("a", 3, sep = c("-", ";"))
3232
})
3333
})
34+
35+
test_that("str_dup() preserves names", {
36+
x <- c(C = "3", B = "2", A = "1")
37+
expect_equal(names(str_dup(x, 2)), names(x))
38+
})

tests/testthat/test-escape.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ test_that("multiplication works", {
66
expect_equal(str_escape("\\"), "\\\\")
77
})
88

9+
test_that("str_escape() preserves names", {
10+
x <- c(C = "3", B = "2", A = "1")
11+
expect_equal(names(str_escape(x)), names(x))
12+
})

tests/testthat/test-extract.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,23 @@ test_that("can extract boundaries", {
6363
)
6464
})
6565

66+
test_that("str_extract() preserves names", {
67+
x <- c(C = "3", B = "2", A = "1")
68+
expect_equal(names(str_extract(x, "[0-9]")), names(x))
69+
})
70+
71+
test_that("str_extract_all() preserves names on outer structure", {
72+
x <- c(C = "3", B = "2", A = "1")
73+
expect_equal(names(str_extract_all(x, "[0-9]")), names(x))
74+
})
75+
76+
test_that("str_extract and extract_all handle vectorised patterns and names", {
77+
x1 <- c(A = "ab")
78+
p2 <- c("a", "b")
79+
expect_null(names(str_extract(x1, p2)))
80+
expect_null(names(str_extract_all(x1, p2)))
81+
82+
x2 <- c(A = "ab", B = "cd")
83+
expect_equal(names(str_extract(x2, p2)), names(x2))
84+
expect_equal(names(str_extract_all(x2, p2)), names(x2))
85+
})

tests/testthat/test-length.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ test_that("str_width returns display width", {
2020
x <- c("\u0308", "x", "\U0001f60a")
2121
expect_equal(str_width(x), c(0, 1, 2))
2222
})
23+
24+
test_that("length/width preserve names", {
25+
x <- c(C = "3", B = "2", A = "1")
26+
expect_equal(names(str_length(x)), names(x))
27+
expect_equal(names(str_width(x)), names(x))
28+
})

tests/testthat/test-locate.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,24 @@ test_that("can use boundaries", {
6767
list(cbind(start = c(2, 6), end = c(3, 7)))
6868
)
6969
})
70+
71+
test_that("str_locate() preserves row names when 1:1 with input", {
72+
x <- c(C = "3", B = "2", A = "1")
73+
expect_equal(rownames(str_locate(x, "[0-9]")), names(x))
74+
})
75+
76+
test_that("str_locate_all() preserves names on outer structure", {
77+
x <- c(C = "3", B = "2", A = "1")
78+
expect_equal(names(str_locate_all(x, "[0-9]")), names(x))
79+
})
80+
81+
test_that("locate handles vectorised patterns and names", {
82+
x1 <- c(A = "ab")
83+
p2 <- c("a", "b")
84+
expect_null(rownames(str_locate(x1, p2)))
85+
expect_null(names(str_locate_all(x1, p2)))
86+
87+
x2 <- c(A = "ab", B = "cd")
88+
expect_equal(rownames(str_locate(x2, p2)), names(x2))
89+
expect_equal(names(str_locate_all(x2, p2)), names(x2))
90+
})

tests/testthat/test-match.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,24 @@ test_that("match can't use other modifiers", {
8888
str_match_all("x", coll("y"))
8989
})
9090
})
91+
92+
test_that("str_match() preserves row names when 1:1 with input", {
93+
x <- c(C = "3", B = "2", A = "1")
94+
expect_equal(rownames(str_match(x, "([0-9])")), names(x))
95+
})
96+
97+
test_that("str_match_all() preserves names on outer structure", {
98+
x <- c(C = "3", B = "2", A = "1")
99+
expect_equal(names(str_match_all(x, "([0-9])")), names(x))
100+
})
101+
102+
test_that("match handles vectorised patterns and names", {
103+
x1 <- c(A = "ab")
104+
p2 <- c("a", "b")
105+
expect_null(rownames(str_match(x1, p2)))
106+
expect_null(names(str_match_all(x1, p2)))
107+
108+
x2 <- c(A = "ab", B = "cd")
109+
expect_equal(rownames(str_match(x2, p2)), names(x2))
110+
expect_equal(names(str_match_all(x2, p2)), names(x2))
111+
})

0 commit comments

Comments
 (0)