patternapply

Iteratively try patterns against a character vector.
git clone https://git.eamoncaddigan.net/patternapply.git
Log | Files | Refs | README | LICENSE

test_patternapply.R (1406B)


      1 library(patternapply)
      2 context("Calling patternapply()")
      3 
      4 inputStrings <- paste(letters, seq_along(letters), sep = "_")
      5 patterns <- c("([[:digit:]]{2})", "([[:alpha:]])")
      6 replacements <- list("\\1", "\\1")
      7 patternapplyResult <- patternapply(inputStrings, patterns, replacements)
      8 patternapplyResultVector <- patternapplyResult
      9 attributes(patternapplyResultVector) <- NULL
     10 
     11 test_that("Simple call returned correct results", {
     12   expect_equal(patternapplyResultVector, as.list(c(letters[1:9], 10:26)))
     13   expect_is(patternapplyResult, "replacement_list")
     14   expect_equal(attr(patternapplyResult, "col_names"), "match_1")
     15   expect_equal(attr(patternapplyResult, "match_index"),
     16                c(rep(2, 9), rep(1, 26-9)))
     17 })
     18 
     19 test_that("Bad inputs raise errors", {
     20   expect_error(patternapply(letters, 1, list("a")),
     21                "'patterns' must be a character vector")
     22   expect_error(patternapply(letters, "a", "a"),
     23                "'replacements' must be a list")
     24   expect_error(patternapply(letters, c("a", "b"), list(1, "a")),
     25                "'replacements' can only contain character vectors")
     26   expect_error(patternapply(letters, c("a", "b"), list("c")),
     27                "'patterns' and 'replacements' must have the same length")
     28 })
     29 
     30 test_that("Default arguments work", {
     31   expect_equal(patternapply(paste(1:20), c("^[0-9]$", "^[0-9]{2}$"))[1:20], 
     32                as.list(c(rep("1", 9), rep("2", 11))))
     33 })
     34