🔍 An RStudio addin slash regex utility belt
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

76 lines
3.2KB

  1. context("test-wrap_result.R")
  2. test_that("wrap_result handles zero length groups", {
  3. # Issue #9
  4. # Type 'q()' to quit R.
  5. # (?<=\()([^)]*)(?=\))
  6. text <- "Type 'q()' to quit R."
  7. pattern <- "(?<=\\()([^)]*)(?=\\))"
  8. res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
  9. expect_equal(res, "Type 'q(<span class=\"group g00\"><span class=\"group g01\"></span></span>)' to quit R.")
  10. })
  11. test_that("wrap_results generally works", {
  12. text <- "apples"
  13. pattern <- "apples"
  14. res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
  15. expect_equal(res, "<span class=\"group g00\">apples</span>")
  16. text <- "He wheeled the bike past the winding road."
  17. pattern <- "(a|the) ([^ ]+)"
  18. res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
  19. expect_equal(res, "He wheeled <span class=\"group g00\"><span class=\"group g01\">the</span> <span class=\"group g02\">bike</span></span> past the winding road.")
  20. text <- ".15in"
  21. pattern <- "^(auto|inherit|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px|vh|vw|vmin|vmax))$"
  22. res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
  23. expect_equal(res, "<span class=\"group g00\"><span class=\"group g01\"><span class=\"group g02 pad01\"><span class=\"group g03 pad02\">.15</span></span><span class=\"group g06 pad01\">in</span></span></span>")
  24. })
  25. test_that("wrap_results works when groups start and end at same index", {
  26. text <- "7282298386"
  27. pattern <- "\\(?(\\d{3})[-). ]?(\\d{3})[- .]?(\\d{4})"
  28. res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
  29. expect_equal(res, "<span class=\"group g00\"><span class=\"group g01\">728</span><span class=\"group g02\">229</span><span class=\"group g03\">8386</span></span>")
  30. })
  31. test_that("wrap_result searches globally", {
  32. text <- "ab ab"
  33. pattern <- "(a)(b)"
  34. result <- paste(rep("<span class=\"group g00\"><span class=\"group g01\">a</span><span class=\"group g02\">b</span></span>", 2), collapse = " ")
  35. expect_equal(wrap_result(regex(text, pattern, global = TRUE)[[1]]), result)
  36. })
  37. test_that("wrap_result starts/ends correctly with touching groups", {
  38. text <- "The big red apple fell to the ground."
  39. pattern <- "(\\w+) (\\w+) "
  40. result <- paste0(
  41. '<span class=\"group g00\"><span class=\"group g01\">The</span> <span class=\"group g02\">big</span> </span>',
  42. '<span class=\"group g00\"><span class=\"group g01\">red</span> <span class=\"group g02\">apple</span> </span>',
  43. '<span class=\"group g00\"><span class=\"group g01\">fell</span> <span class=\"group g02\">to</span> </span>',
  44. "the ground."
  45. )
  46. expect_equal(wrap_result(regex(text, pattern, global = TRUE)[[1]]), result)
  47. })
  48. test_that("handles escaped parentheses", {
  49. expect_equal(
  50. wrap_regex("\\((word)\\)"),
  51. '\\\\(<span class=\"g01\">(word)</span>\\\\)'
  52. )
  53. })
  54. test_that("wrap_result with exact = TRUE", {
  55. # input/output standard R characters
  56. expect_equal(
  57. wrap_result(regex("a\\b", "\\\\")[[1]]),
  58. "a<span class=\"group g00\">\\</span>b"
  59. )
  60. # input is "literal", output is R string to match literal input
  61. expect_equal(
  62. wrap_result(regex("a\\b", "\\\\")[[1]], exact = TRUE),
  63. "a<span class=\"group g00\">\\\\</span>b"
  64. )
  65. })