@@ -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
0 commit comments