Skip to content

Commit abb5cf0

Browse files
committed
Test whether functions preserve names (cf #575).
Principles: - Preserve names where the output has a 1-to-1 correspondence with the input. - Preserve names for functions where base behavior suggests keeping names (e.g., grepl/grep(value=TRUE), sort/unique). - Source of names: use names from the primary `string` argument only; ignore names on `pattern`/`replacement`/others and never merge. - For 1-row-per-input matrices/lists (e.g., str_locate/str_match/str_split_fixed), set row/list names from input names. - Don't preserve names where strings are combined, or the return values are indices (e.g., str_c, str_flatten, str_glue, str_which, str_order, str_equal). Currently passing: - str_subset - str_trunc Currently failing: - str_count - str_detect - str_starts - str_ends - str_like - str_escape - str_replace - str_remove - str_conv - str_trim - str_pad - str_sub - str_to_lower - str_to_upper - str_to_title - str_extract - str_locate - str_match - str_extract_all - str_locate_all - str_match_all - str_split - str_split_fixed - str_split_i - str_sub_all - str_length - str_width - str_dup - word - str_unique - str_replace_na - str_sort - str_wrap - str_replace_all
1 parent 90f8ba7 commit abb5cf0

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
x <- c(C = "3", B = "2", A = "1")
2+
3+
test_that("elementwise logical/numeric functions preserve names", {
4+
5+
expect_equal(names(str_count(x, ".")), names(x))
6+
expect_equal(names(str_detect(x, "[123]")), names(x))
7+
expect_equal(names(str_starts(x, "1")), names(x))
8+
expect_equal(names(str_ends(x, "1")), names(x))
9+
expect_equal(names(str_like(x, "%")), names(x))
10+
})
11+
12+
test_that("elementwise string transforms preserve names", {
13+
expect_equal(names(str_escape(x)), names(x))
14+
expect_equal(names(str_replace(x, "[0-9]", "x")), names(x))
15+
expect_equal(names(str_replace_all(x, "[0-9]", "x")), names(x))
16+
expect_equal(names(str_remove(x, "[0-9]")), names(x))
17+
expect_equal(names(str_conv(x, "UTF-8")), names(x))
18+
expect_equal(names(str_trim(x)), names(x))
19+
expect_equal(names(str_trunc(x, 3)), names(x))
20+
expect_equal(names(str_pad(x, 2, side = "left")), names(x))
21+
expect_equal(names(str_sub(x, 1, 1)), names(x))
22+
})
23+
24+
test_that("case conversions preserve names", {
25+
expect_equal(names(str_to_lower(x)), names(x))
26+
expect_equal(names(str_to_upper(x)), names(x))
27+
expect_equal(names(str_to_title(x)), names(x))
28+
})
29+
30+
test_that("extraction functions preserve names (vector)", {
31+
expect_equal(names(str_extract(x, "[0-9]")), names(x))
32+
})
33+
34+
test_that("location/match matrices preserve row names", {
35+
expect_equal(rownames(str_locate(x, "[0-9]")), names(x))
36+
expect_equal(rownames(str_match(x, "([0-9])")), names(x))
37+
})
38+
39+
test_that("list/matrix outputs preserve names on outer structure", {
40+
# Lists
41+
expect_equal(names(str_extract_all(x, "[0-9]")), names(x))
42+
expect_equal(names(str_locate_all(x, "[0-9]")), names(x))
43+
expect_equal(names(str_match_all(x, "([0-9])")), names(x))
44+
expect_equal(names(str_split(x, "")), names(x))
45+
expect_equal(names(str_sub_all(x, 1, 1)), names(x))
46+
47+
# Matrices (rows correspond to inputs)
48+
expect_equal(rownames(str_split(x, "", simplify = TRUE)), names(x))
49+
expect_equal(rownames(str_split_fixed(x, "", 1)), names(x))
50+
})
51+
52+
test_that("length and duplication preserve names", {
53+
expect_equal(names(str_length(x)), names(x))
54+
expect_equal(names(str_width(x)), names(x))
55+
expect_equal(names(str_dup(x, 2)), names(x))
56+
})
57+
58+
test_that("word() preserves names", {
59+
expect_equal(names(word(x, 1)), names(x))
60+
})
61+
62+
test_that("str_unique() preserves names of first occurrences", {
63+
y <- c(A = "a", A2 = "a", B = "b")
64+
out <- str_unique(y)
65+
expect_equal(names(out), c("A", "B"))
66+
})
67+
68+
test_that("str_replace_na() preserves names", {
69+
y <- c(A = NA, B = "x")
70+
expect_equal(names(str_replace_na(y)), names(y))
71+
})
72+
73+
test_that("str_subset() preserves names of retained elements", {
74+
out <- str_subset(x, "[12]")
75+
expect_equal(names(out), c("B", "A"))
76+
})
77+
78+
test_that("str_sort() preserves names", {
79+
out <- str_sort(x)
80+
expect_equal(names(out), c("A", "B", "C"))
81+
})
82+
83+
test_that("str_wrap() preserves names", {
84+
expect_equal(names(str_wrap(x)), names(x))
85+
})
86+
87+
test_that("str_split_i() preserves names", {
88+
expect_equal(names(str_split_i(x, " ", 1)), names(x))
89+
})

0 commit comments

Comments
 (0)