patternapply

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

commit 6fb53f77727b9af8c5b7e5ac22e1586057aa5d75
parent e66c54f6c680727c9599776d4ebf7c2d25ca1c46
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date:   Fri, 26 Feb 2016 16:35:34 -0500

Using testthat!

Diffstat:
MDESCRIPTION | 1+
MR/patternapply.R | 3+++
Atests/testthat.R | 4++++
Atests/testthat/test_patternapply.R | 17+++++++++++++++++
4 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION @@ -8,3 +8,4 @@ Maintainer: Eamon Caddigan <eamon.caddigan@gmail.com> Description: Extracts information from vectors (or columns) of text data that can take one of several formats by applying regular expressions in turn until a match is found. Mostly useful for ultimately turning such a vector into a data.frame. License: BSD 3 LazyData: TRUE +Suggests: testthat diff --git a/R/patternapply.R b/R/patternapply.R @@ -66,5 +66,8 @@ patternapply <- function(X, patterns, # This will probably be useful for people. attr(replacementList, "match_index") <- matchIndex + # S3 Object + class(replacementList) <- "replacement_list" + return(replacementList) } diff --git a/tests/testthat.R b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(patternapply) + +test_check("patternapply") diff --git a/tests/testthat/test_patternapply.R b/tests/testthat/test_patternapply.R @@ -0,0 +1,17 @@ +library(patternapply) +context("Calling patternapply()") + +inputStrings <- paste(letters, seq_along(letters), sep = "_") +patterns <- c("([[:digit:]]{2})", "([[:alpha:]])") +replacements <- list("\\1", "\\1") +patternapplyResult <- patternapply(inputStrings, patterns, replacements) +patternapplyResultVector <- patternapplyResult +attributes(patternapplyResultVector) <- NULL + +test_that("Simple call returned correct results", { + expect_equal(patternapplyResultVector, as.list(c(letters[1:9], 10:26))) + expect_equal(class(patternapplyResult), "replacement_list") + expect_equal(attr(patternapplyResult, "col_names"), "match_1") + expect_equal(attr(patternapplyResult, "match_index"), + c(rep(2, 9), rep(1, 26-9))) +})