tests/testthat/test-searches.R

test_that("multi_grepl() examples are correct", {
  x <- c("apple", "banana", "lemon")

  expect_equal(
    multi_grepl(x, c("a" = "^[ab]", "b" = "lem")),
    c("a", "a", "b")
  )

  expect_equal(
    # lemon not matches on either
    multi_grepl(x, c("a" = "^[ab]", "b" = "q")),
    c("a", "a", NA_character_)
  )

  expect_equal(
    # apple matches "a" before "b"
    multi_grepl(x, c("a" = "^[ab]", "b" = "e")),
    c("a", "a", "b")
  )

  expect_equal(
    multi_grepl(x, c("a" = "^[ab]", "b" = "e"), simplify = FALSE),
    list(c("a", "b"), "a", "b")
  )

  expect_equal(
    multi_grepl(x, c("^[ab]", "e"), simplify = FALSE),
    list(c(1L, 2L), 1L, 2L)
  )

  expect_equal(
    # returned as positions
    multi_grepl(x, c("^[ab]", "e")),
    c(1L, 1L, 2L)
  )
})
jmbarbone/jordan documentation built on April 1, 2024, 7:46 p.m.