🔍 An RStudio addin slash regex utility belt
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

46 rindas
1.4KB

  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 <- regex(text, pattern, global = TRUE)
  16. expect_type(m[[1]]$idx$start, "integer")
  17. expect_type(m[[1]]$idx$end, "integer")
  18. expect_type(m[[1]]$idx$group, "integer")
  19. })
  20. test_that("length-zero match is NULL", {
  21. m <- regex(c("other", "thing"), "thing|")
  22. expect_null(m[[1]]$idx)
  23. expect_equal(m[[2]]$idx$start, 1L)
  24. expect_equal(m[[2]]$idx$end, 6L)
  25. })
  26. test_that("max_match_index works", {
  27. m <- regex(c("abcaba", "aba", "z"), c("(a)(b)(d)?c?"), global = FALSE)
  28. expect_equal(max_match_index(m), c(4, 3, NA_integer_))
  29. })
  30. test_that("results group (pass) is calculated correctly", {
  31. text <- "ab ab"
  32. pattern <- "(a)(b)"
  33. m <- regex(text, pattern, global = TRUE)
  34. expect_equal(unique(m[[1]]$idx$pass), c(1L, 2L))
  35. })
  36. test_that("view_regex generally works", {
  37. result <- "<p class=\"regexplain \"> <span class=\"group g00\"><span class=\"group g01\">t</span>e</span><span class=\"group g00\"><span class=\"group g01\">s</span>t</span> </p>"
  38. expect_equal(view_regex("test", "(\\w)\\w", render = FALSE), result)
  39. })