context("test-wrap_result.R")
test_that("wrap_result handles zero length groups", {
# Issue #9
# Type 'q()' to quit R.
# (?<=\()([^)]*)(?=\))
text <- "Type 'q()' to quit R."
pattern <- "(?<=\\()([^)]*)(?=\\))"
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "Type 'q()' to quit R.")
})
test_that("wrap_results generally works", {
text <- "apples"
pattern <- "apples"
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "apples")
text <- "He wheeled the bike past the winding road."
pattern <- "(a|the) ([^ ]+)"
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "He wheeled the bike past the winding road.")
text <- ".15in"
pattern <- "^(auto|inherit|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px|vh|vw|vmin|vmax))$"
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, ".15in")
})
test_that("wrap_results works when groups start and end at same index", {
text <- "7282298386"
pattern <- "\\(?(\\d{3})[-). ]?(\\d{3})[- .]?(\\d{4})"
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "7282298386")
})
test_that("wrap_result searches globally", {
text <- "ab ab"
pattern <- "(a)(b)"
result <- paste(rep("ab", 2), collapse = " ")
expect_equal(wrap_result(regex(text, pattern, global = TRUE)[[1]]), result)
})
test_that("wrap_result starts/ends correctly with touching groups", {
text <- "The big red apple fell to the ground."
pattern <- "(\\w+) (\\w+) "
result <- paste0(
'The big ',
'red apple ',
'fell to ',
'the ground.'
)
expect_equal(wrap_result(regex(text, pattern, global = TRUE)[[1]]), result)
})
test_that("handles escaped parentheses", {
expect_equal(
wrap_regex("\\((word)\\)"),
'\\\\((word)\\\\)'
)
})
test_that("wrap_result with exact = TRUE", {
# input/output standard R characters
expect_equal(
wrap_result(regex("a\\b", "\\\\")[[1]]),
"a\\b"
)
# input is "literal", output is R string to match literal input
expect_equal(
wrap_result(regex("a\\b", "\\\\")[[1]], exact = TRUE),
"a\\\\b"
)
})