Skip to content

Commit

Permalink
test type.convert=fun
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Dylan Hocking committed Dec 17, 2024
1 parent 1f364b1 commit 9716fac
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Authors@R: c(
person("Toby", "Hocking",
email="[email protected]",
role=c("aut", "cre")))
Version: 2024.12.16
Version: 2024.12.17
License: GPL-3
Title: Named Capture to Data Tables
Description: User-friendly functions for extracting a data
Expand Down
4 changes: 2 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Changes in version 2024.12.16
Changes in version 2024.12.17

- type.convert=TRUE means to use type.convert() as default conversion function.
- type.convert=TRUE means to use utils::type.convert(x,as.is=TRUE) as default conversion function (as.is=TRUE means to return character instead of factor), FALSE means identity, and otherwise can be any function to use as default conversion.

Changes in version 2024.9.20

Expand Down
8 changes: 6 additions & 2 deletions R/capture_all_str.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ capture_all_str <- structure(function # Capture all matches in a single subject
### separator string for combining elements of subject into a single
### string, used as collapse argument of base::paste.
type.convert=getOption("nc.type.convert", FALSE)
### If TRUE, use utils::type.convert instead of base::identity for the
### default conversion.
### Default conversion function, which will be used on each capture
### group, unless a specific conversion is specified for that
### group. If TRUE, use utils::type.convert; if FALSE, use
### base::identity; otherwise must be a function of at least one
### argument (character), returning an atomic vector of the same
### length.
){
stop_for_engine(engine)
L <- subject_var_args(..., type.convert=type.convert)
Expand Down
45 changes: 45 additions & 0 deletions tests/testthat/test-CRAN-vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,51 @@ test_engines("capture_first_vec(type.convert=TRUE) returns two int columns", {
expect_identical(computed, expected)
})

test_engines("capture_first_vec(type.convert=constant fun) returns all num columns", {
computed <- capture_first_vec(
"chr1:2-3,000",
chrom="chr.*?",
":",
chromStart=".*?",
"-",
chromEnd="[0-9,]*",
type.convert=function(...)5)
expected <- data.table(
chrom=5,
chromStart=5,
chromEnd=5)
expect_identical(computed, expected)
})

test_engines("capture_first_vec(type.convert=as.integer) returns all int columns", {
computed <- capture_first_vec(
"chr1:2-3,000",
chrom="chr.*?",
":",
chromStart=".*?",
"-",
chromEnd="[0-9,]*",
type.convert=as.integer)
expected <- data.table(
chrom=NA_integer_,
chromStart=2L,
chromEnd=NA_integer_)
expect_identical(computed, expected)
})

test_engines("capture_first_vec(type.convert=invalid) errors", {
expect_error({
capture_first_vec(
"chr1:2-3,000",
chrom="chr.*?",
":",
chromStart=".*?",
"-",
chromEnd="[0-9,]*",
type.convert=function()5)
}, "type conversion functions should take one argument (character vector of captured text) and return an atomic vector of the same size; function for group 1(chrom) raised an error: unused argument (match.vec)", fixed=TRUE)
})

test_engines("capture_all_str(type.convert=TRUE) returns one int column", {
computed <- capture_all_str(
"chr1:2-3,000 chr4:5-6,000",
Expand Down

0 comments on commit 9716fac

Please sign in to comment.