Debian Codegraph @

tests/testthat/test-helpers-pattern.R

raw
1 test_that("no set variables throws error", {
2 expect_error(starts_with("z"), "`starts_with()` must be used within a *selecting* function", fixed = TRUE)
3 })
4
5 test_that("failed match removes all columns", {
6 local_vars(c("x", "y"))
7
8 expect_equal(starts_with("z"), integer(0))
9 expect_equal(ends_with("z"), integer(0))
10 expect_equal(contains("z"), integer(0))
11 expect_equal(matches("z"), integer(0))
12 expect_equal(num_range("z", 1:3), integer(0))
13 })
14
15 test_that("matches return integer locations", {
16 local_vars(c("abc", "acd", "bbc", "bbd", "eee"))
17
18 expect_equal(starts_with("a"), c(1L, 2L))
19 expect_equal(ends_with("d"), c(2L, 4L))
20 expect_equal(contains("eee"), 5L)
21 expect_equal(matches(".b."), c(1L, 3L, 4L))
22 expect_equal(matches("(?<!a)b", perl = TRUE), c(3L, 4L))
23 })
24
25 test_that("throws with empty pattern is provided", {
26 # error messages from rlang
27 expect_error(starts_with(""))
28 expect_error(ends_with(""))
29 expect_error(contains(""))
30 expect_error(matches(""))
31 })
32
33 test_that("can use a variable", {
34 vars <- "x"
35 names(vars) <- vars
36
37 expect_equal(select_loc(vars, starts_with(vars)), c(x = 1L))
38 expect_equal(select_loc(vars, ends_with(vars)), c(x = 1L))
39 expect_equal(select_loc(vars, contains(vars)), c(x = 1L))
40 expect_equal(select_loc(vars, matches(vars)), c(x = 1L))
41 })
42
43 test_that("can use a variable even if it exists in the data (#2266)", {
44 vars <- c("x", "y")
45 names(vars) <- vars
46
47 y <- "x"
48 expected_result <- c(x = 1L)
49
50 expect_equal(select_loc(vars, starts_with(y)), expected_result)
51 expect_equal(select_loc(vars, ends_with(y)), expected_result)
52 expect_equal(select_loc(vars, contains(y)), expected_result)
53 expect_equal(select_loc(vars, matches(y)), expected_result)
54 })
55
56 test_that("num_range selects numeric ranges", {
57 vars <- c("x1", "x2", "x01", "x02", "x10", "x11")
58 names(vars) <- vars
59
60 expect_equal(select_loc(vars, num_range("x", 1:2)), c(x1 = 1L, x2 = 2L))
61 expect_equal(select_loc(vars, num_range("x", 1:2, width = 2)), c(x01 = 3L, x02 = 4L))
62 expect_equal(select_loc(vars, num_range("x", 10:11)), c(x10 = 5L, x11 = 6L))
63 expect_equal(select_loc(vars, num_range("x", 10:11, width = 2)), c(x10 = 5L, x11 = 6L))
64 })
65
66 test_that("num_range can use a suffix (#229)", {
67 vars <- set_names(c("x1", "x2", "x1_y", "x2_y"))
68 expect_named(select_loc(vars, num_range("x", 1:2, "_y")), c("x1_y", "x2_y"))
69 })
70
71
72 test_that("matchers accept length > 1 vectors (#50)", {
73 expect_identical(
74 select_loc(iris, starts_with(c("Sep", "Petal"))),
75 select_loc(iris, starts_with("Sep") | starts_with("Petal"))
76 )
77 expect_identical(
78 select_loc(iris, ends_with(c("gth", "Width"))),
79 select_loc(iris, ends_with("gth") | ends_with("Width"))
80 )
81 expect_identical(
82 select_loc(iris, contains(c("epal", "eta"))),
83 select_loc(iris, contains("epal") | contains("eta")),
84 )
85 expect_identical(
86 select_loc(iris, matches(c("epal", "eta"))),
87 select_loc(iris, matches("epal") | contains("eta")),
88 )
89 })
90
91
92 test_that("initial (single) selector defaults correctly (issue #2275)", {
93 cn <- setNames(nm = c("x", "y", "z"))
94
95 ### Single Column Selected
96
97 # single columns (present), explicit
98 expect_equal(select_loc(cn, x), c(x = 1L))
99 expect_equal(select_loc(cn, -x), c(y = 2L, z = 3L))
100
101 # single columns (present), matched
102 expect_equal(select_loc(cn, contains("x")), c(x = 1L))
103 expect_equal(select_loc(cn, -contains("x")), c(y = 2L, z = 3L))
104
105 # single columns (not present), explicit
106 expect_error(select_loc(cn, foo), class = "vctrs_error_subscript_oob")
107 expect_error(select_loc(cn, -foo), class = "vctrs_error_subscript_oob")
108
109 # single columns (not present), matched
110 expect_equal(select_loc(cn, contains("foo")), named(int()))
111 expect_equal(select_loc(cn, -contains("foo")), set_names(seq_along(cn), cn))
112 })
113
114 test_that("initial (of multiple) selectors default correctly (issue #2275)", {
115 cn <- setNames(nm = c("x", "y", "z"))
116
117 ### Multiple Columns Selected
118
119 # explicit(present) + matched(present)
120 expect_equal(select_loc(cn, c(x, contains("y"))), c(x = 1L, y = 2L))
121 expect_equal(select_loc(cn, c(x, -contains("y"))), c(x = 1L))
122 expect_equal(select_loc(cn, c(-x, contains("y"))), c(y = 2L, z = 3L))
123 expect_equal(select_loc(cn, c(-x, -contains("y"))), c(z = 3L))
124
125 # explicit(present) + matched(not present)
126 expect_equal(select_loc(cn, c(x, contains("foo"))), c(x = 1L))
127 expect_equal(select_loc(cn, c(x, -contains("foo"))), c(x = 1L))
128 expect_equal(select_loc(cn, c(-x, contains("foo"))), c(y = 2L, z = 3L))
129 expect_equal(select_loc(cn, c(-x, -contains("foo"))), c(y = 2L, z = 3L))
130
131 # matched(present) + explicit(present)
132 expect_equal(select_loc(cn, c(contains("x"), y)), c(x = 1L, y = 2L))
133 expect_equal(select_loc(cn, c(contains("x"), -y)), c(x = 1L))
134 expect_equal(select_loc(cn, c(-contains("x"), y)), c(y = 2L, z = 3L))
135 expect_equal(select_loc(cn, c(-contains("x"), -y)), c(z = 3L))
136
137 # matched(not present) + explicit(not present)
138 expect_error(select_loc(cn, c(contains("foo"), bar)), class = "vctrs_error_subscript_oob")
139 expect_error(select_loc(cn, c(contains("foo"), -bar)), class = "vctrs_error_subscript_oob")
140 expect_error(select_loc(cn, c(-contains("foo"), bar)), class = "vctrs_error_subscript_oob")
141 expect_error(select_loc(cn, c(-contains("foo"), -bar)), class = "vctrs_error_subscript_oob")
142
143 # matched(present) + matched(present)
144 expect_equal(select_loc(cn, c(contains("x"), contains("y"))), c(x = 1L, y = 2L))
145 expect_equal(select_loc(cn, c(contains("x"), -contains("y"))), c(x = 1L))
146 expect_equal(select_loc(cn, c(-contains("x"), contains("y"))), c(y = 2L, z = 3L))
147 expect_equal(select_loc(cn, c(-contains("x"), -contains("y"))), c(z = 3L))
148
149 # matched(present) + matched(not present)
150 expect_equal(select_loc(cn, c(contains("x"), contains("foo"))), c(x = 1L))
151 expect_equal(select_loc(cn, c(contains("x"), -contains("foo"))), c(x = 1L))
152 expect_equal(select_loc(cn, c(-contains("x"), contains("foo"))), c(y = 2L, z = 3L))
153 expect_equal(select_loc(cn, c(-contains("x"), -contains("foo"))), c(y = 2L, z = 3L))
154
155 # matched(not present) + matched(present)
156 expect_equal(select_loc(cn, c(contains("foo"), contains("x"))), c(x = 1L))
157 expect_equal(select_loc(cn, c(contains("foo"), -contains("x"))), named(int()))
158 expect_equal(select_loc(cn, c(-contains("foo"), contains("x"))), c(x = 1L, y = 2L, z = 3L))
159 expect_equal(select_loc(cn, c(-contains("foo"), -contains("x"))), c(y = 2L, z = 3L))
160
161 # matched(not present) + matched(not present)
162 expect_equal(select_loc(cn, c(contains("foo"), contains("bar"))), named(int()))
163 expect_equal(select_loc(cn, c(contains("foo"), -contains("bar"))), named(int()))
164 expect_equal(select_loc(cn, c(-contains("foo"), contains("bar"))), c(x = 1L, y = 2L, z = 3L))
165 expect_equal(select_loc(cn, c(-contains("foo"), -contains("bar"))), c(x = 1L, y = 2L, z = 3L))
166 })
167
168 test_that("matches() can use stringr patterns", {
169 local_vars(c(".", "x", "X"))
170
171 expect_equal(matches(stringr::fixed(".")), 1)
172 expect_equal(matches(stringr::coll("x", ignore_case = TRUE)), c(2, 3))
173 })
174
175 test_that("matches() complains about bad stringr pattern usage", {
176 local_vars(c("x", "y"))
177
178 expect_snapshot(error = TRUE, {
179 matches(stringr::fixed("a"), perl = TRUE)
180 matches(stringr::fixed("a"), ignore.case = TRUE)
181 matches(stringr::fixed(c("a", "b")))
182 })
183 })
184
185 test_that("can supply named vectors to pattern helpers (#250)", {
186 exp <- c(cyl = 2L, carb = 11L)
187
188 expect_equal(
189 select_loc(mtcars, matches(c(foo = "^c"))),
190 exp
191 )
192
193 expect_equal(
194 select_loc(mtcars, starts_with(c(foo = "c"))),
195 exp
196 )
197
198 expect_equal(
199 select_loc(mtcars, ends_with(c(foo = "l", bar = "b"))),
200 exp
201 )
202
203 expect_equal(
204 select_loc(mtcars, contains(c(foo = "yl", bar = "rb"))),
205 exp
206 )
207 })