ソースを参照

Merge branch 'fix-issue-9' into dev

tags/v0.2.2
コミット
af4fe0988a
4個のファイルの変更56行の追加4行の削除
  1. +1
    -1
      DESCRIPTION
  2. +7
    -1
      NEWS.md
  3. +13
    -2
      R/run_regex.R
  4. +35
    -0
      tests/testthat/test-wrap_result.R

+ 1
- 1
DESCRIPTION ファイルの表示

@@ -1,6 +1,6 @@
Package: regexplain
Title: Rstudio Addin to Explain, Test and Build Regular Expressions
Version: 0.2.1
Version: 0.2.1.9000
Date: 2018-04-04
Authors@R: c(
person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre")),

+ 7
- 1
NEWS.md ファイルの表示

@@ -1,9 +1,15 @@
## regexplain 0.2.x

### 0.2.2

* Fix issues with coloring of groups in regexplain gadget when matched groups
start and end at the same index, especially when there are zero-length
groups (thanks, @HanOostdijk).

### 0.2.1

* Fix addin crash when replacement function is visited but replacement field
is not initated (#8).
is not initiated (#8).

### 0.2.0


+ 13
- 2
R/run_regex.R ファイルの表示

@@ -60,12 +60,23 @@ wrap_result <- function(x, escape = FALSE, exact = FALSE) {
inserts <- inserts %>%
tidyr::gather(type, loc, start:end) %>%
filter(!is.na(.data$loc)) %>%
dplyr::arrange(loc, class, desc(type)) %>%
mutate(
class = ifelse(.data$pad > 0, sprintf("%s pad%02d", .data$class, .data$pad), .data$class),
insert = ifelse(.data$type == 'start', sprintf('<span class="%s">', .data$class), "</span>")
) %>%
)
inserts_g0 <- filter(inserts, class == "group g00")
inserts_other <- filter(inserts, class != "group g00")
inserts <- dplyr::bind_rows(
filter(inserts_g0, type == "start"),
inserts_other,
filter(inserts_g0, type == "end")
) %>%
mutate(type = sprintf("%05d%s", 1:nrow(.), type)) %>%
group_by(.data$loc, .data$type) %>%
summarize(insert = paste(.data$insert, collapse = ''))
summarize(insert = paste(.data$insert, collapse = '')) %>%
dplyr::ungroup() %>%
mutate(type = sub("^\\d{5}", "", type))

# inserts now gives html (span open and close) to insert and loc
# first split text at inserts$loc locations,

+ 35
- 0
tests/testthat/test-wrap_result.R ファイルの表示

@@ -0,0 +1,35 @@
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(run_regex(text, pattern, perl = TRUE)[[1]])
expect_equal(res, "Type 'q(<span class=\"group g00\"><span class=\"group g01\"></span></span>)' to quit R.")
})

test_that("wrap_results generally works", {
text <- "apples"
pattern <- "apples"
res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]])
expect_equal(res, "<span class=\"group g00\">apples</span>")

text <- "He wheeled the bike past the winding road."
pattern <- "(a|the) ([^ ]+)"
res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]])
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.")

text <- ".15in"
pattern <- "^(auto|inherit|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px|vh|vw|vmin|vmax))$"
res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]])
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>")
})

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(run_regex(text, pattern, perl = TRUE)[[1]])
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>")
})

読み込み中…
キャンセル
保存