Преглед изворни кода

A few more test and some code fiddling

main
Garrick Aden-Buie пре 7 година
родитељ
комит
9b9588db41
4 измењених фајлова са 40 додато и 6 уклоњено
  1. +7
    -6
      R/regex.R
  2. +5
    -0
      tests/testthat/test-regex.R
  3. +7
    -0
      tests/testthat/test-wrap_regex.R
  4. +21
    -0
      tests/testthat/test-wrap_result.R

+ 7
- 6
R/regex.R Прегледај датотеку

@@ -252,7 +252,7 @@ view_regex <- function(
result_pad <- sprintf("pad%02d", max_pad_level - 3)
}
}
paste("<p class='results", result_pad, "'>", resi, "</p>")
paste('<p class="results', result_pad, '">', resi, "</p>")
})
res <- paste(res, collapse = "")
if (!nchar(pattern)) res <- paste("<p class='results'>", text, "</p>")
@@ -278,11 +278,12 @@ view_regex <- function(
tmp_html <- suppressWarnings(
rmarkdown::render(
tmp,
output_format = rmarkdown::html_document(css = c(system.file("styles", 'skeleton.css', package='regexplain'),
system.file("styles", 'view_regex.css', package='regexplain'),
system.file("styles", 'groups.css', package='regexplain')),
theme = NULL,
md_extensions = "-autolink_bare_uris"),
output_format = rmarkdown::html_document(
css = c(system.file("styles", 'skeleton.css', package='regexplain'),
system.file("styles", 'view_regex.css', package='regexplain'),
system.file("styles", 'groups.css', package='regexplain')),
theme = NULL,
md_extensions = "-autolink_bare_uris"),
quiet = TRUE
))
rstudioapi::viewer(tmp_html)

+ 5
- 0
tests/testthat/test-regex.R Прегледај датотеку

@@ -31,3 +31,8 @@ test_that("results group (pass) is calculated correctly", {
m <- regex(text, pattern, global = TRUE)
expect_equal(unique(m[[1]]$idx$pass), c(1L, 2L))
})

test_that("view_regex generall works", {
result <- "<p class=\"results \"> <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>"
expect_equal(view_regex("test", "(\\w)\\w", render = FALSE), result)
})

+ 7
- 0
tests/testthat/test-wrap_regex.R Прегледај датотеку

@@ -14,3 +14,10 @@ test_that("wrap_regex doesn't wrap non-capture groups", {
expect_equal(wrap_regex("((?:a(b))c)"),
"<span class=\"g01\">((?:a<span class=\"g02\">(b)</span>)c)</span>")
})

test_that("wrap_regex returns text if no matches", {
expect_equal(
wrap_result(list(text = "no result")),
"no result"
)
})

+ 21
- 0
tests/testthat/test-wrap_result.R Прегледај датотеку

@@ -52,3 +52,24 @@ test_that("wrap_result starts/ends correctly with touching groups", {
)
expect_equal(wrap_result(regex(text, pattern, global = TRUE)[[1]]), result)
})

test_that("handles escaped parentheses", {
expect_equal(
wrap_regex("\\((word)\\)"),
'\\\\(<span class=\"g01\">(word)</span>\\\\)'
)
})

test_that("wrap_result with exact = TRUE", {
# input/output standard R characters
expect_equal(
wrap_result(regex("a\\b", "\\\\")[[1]]),
"a<span class=\"group g00\">\\</span>b"
)

# input is "literal", output is R string to match literal input
expect_equal(
wrap_result(regex("a\\b", "\\\\")[[1]], exact = TRUE),
"a<span class=\"group g00\">\\\\</span>b"
)
})

Loading…
Откажи
Сачувај