| Package: regexplain | Package: regexplain | ||||
| Title: Rstudio Addin to Explain, Test and Build Regular Expressions | Title: Rstudio Addin to Explain, Test and Build Regular Expressions | ||||
| Version: 0.2.1 | |||||
| Version: 0.2.1.9000 | |||||
| Date: 2018-04-04 | Date: 2018-04-04 | ||||
| Authors@R: c( | Authors@R: c( | ||||
| person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre")), | person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre")), |
| ## regexplain 0.2.x | ## 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 | ### 0.2.1 | ||||
| * Fix addin crash when replacement function is visited but replacement field | * Fix addin crash when replacement function is visited but replacement field | ||||
| is not initated (#8). | |||||
| is not initiated (#8). | |||||
| ### 0.2.0 | ### 0.2.0 | ||||
| inserts <- inserts %>% | inserts <- inserts %>% | ||||
| tidyr::gather(type, loc, start:end) %>% | tidyr::gather(type, loc, start:end) %>% | ||||
| filter(!is.na(.data$loc)) %>% | filter(!is.na(.data$loc)) %>% | ||||
| dplyr::arrange(loc, class, desc(type)) %>% | |||||
| mutate( | mutate( | ||||
| class = ifelse(.data$pad > 0, sprintf("%s pad%02d", .data$class, .data$pad), .data$class), | 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>") | 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) %>% | 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 | # inserts now gives html (span open and close) to insert and loc | ||||
| # first split text at inserts$loc locations, | # first split text at inserts$loc locations, |
| 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>") | |||||
| }) |