🔍 An RStudio addin slash regex utility belt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
975B

  1. context("test-regex")
  2. test_that("expand_matches gives data frame of indices with groups", {
  3. m <- regexec("(a)(b)(d)?", "abcaba")
  4. idx <- data.frame(
  5. start = c(1L, 1L, 2L, NA_integer_),
  6. end = c(3L, 2L, 3L, NA_integer_),
  7. group = c(0L, 1L, 2L, 3L),
  8. pass = rep(1L, 4)
  9. )
  10. expect_equal(expand_matches(m[[1]]), idx)
  11. })
  12. test_that("start/end indices are integers", {
  13. text <- "ab ab"
  14. pattern <- "(a)(b)"
  15. m <- run_regex(text, pattern, global = TRUE)
  16. expect_is(m[[1]]$idx$start, "integer")
  17. expect_is(m[[1]]$idx$end, "integer")
  18. expect_is(m[[1]]$idx$group, "integer")
  19. })
  20. test_that("max_match_index works", {
  21. m <- run_regex(c("abcaba", "aba", "z"), c("(a)(b)(d)?c?"), global = FALSE)
  22. expect_equal(max_match_index(m), c(4, 3, NA_integer_))
  23. })
  24. test_that("results group (pass) is calculated correctly", {
  25. text <- "ab ab"
  26. pattern <- "(a)(b)"
  27. m <- run_regex(text, pattern, global = TRUE)
  28. expect_equal(unique(m[[1]]$idx$pass), c(1L, 2L))
  29. })