-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-CRAN-all.R
More file actions
196 lines (179 loc) · 6.08 KB
/
test-CRAN-all.R
File metadata and controls
196 lines (179 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
library(nc)
library(testthat)
library(data.table)
context("all")
source(system.file("test_engines.R", package="nc", mustWork=TRUE), local=TRUE)
test_engines("capture_all_str has no warning about no such file", {
expect_warning({
capture_all_str("chr1:100", baz="sars")
}, regexp=NA)
})
subject <- c("foobar", "FOOBAR")
test_engines("capture_all_str returns data.frame with 0 rows, 1 chr col", {
computed <- capture_all_str(subject, baz="sars")
expect_identical(computed$baz, character())
})
test_engines("capture_all_str returns data.frame with 0 rows, 1 int col", {
subject <- c("foobar", "FOOBAR")
computed <- capture_all_str(subject, baz="sars", as.integer)
expect_identical(computed$baz, integer())
})
keep.digits <- function(x)as.integer(gsub("[^0-9]", "", x))
test_engines("capture_all_str returns data.table", {
chr.pos.vec <- c(
"chr10:213,054,000-213,055,000",
"chrM:111,000-222,000",
"this will not match",
NA, # neither will this.
"chr1:110-111 chr2:220-222") # two possible matches.
computed <- capture_all_str(
chr.pos.vec,
chrom="chr.*?",
":",
chromStart=".*?", keep.digits,
"-",
chromEnd="[0-9,]*", keep.digits)
expected <- rbind(
data.table(
chrom="chr10", chromStart=213054000L, chromEnd=213055000L),
data.table(
chrom="chrM", chromStart=111000L, chromEnd=222000L),
data.table(
chrom=c("chr1", "chr2"),
chromStart=as.integer(c("110", "220")),
chromEnd=as.integer(c("111", "222"))))
expect_identical(computed, expected)
})
test_engines("capture_all_str(type.convert=TRUE) returns one int column", {
computed <- capture_all_str(
"chr1:2-3,000 chr4:5-6,000",
chrom="chr.*?",
":",
chromStart=".*?",
"-",
chromEnd="[0-9,]*",
type.convert=TRUE)
expected <- data.table(
chrom=c("chr1","chr4"),
chromStart=c(2L,5L),
chromEnd=c("3,000","6,000"))
expect_identical(computed, expected)
})
test_engines("capture_all_str(type.convert=TRUE) returns two int columns", {
computed <- capture_all_str(
"chr1:2-3,000 chr4:5-6,000",
chrom="chr.*?",
":",
chromStart=".*?",
"-",
chromEnd="[0-9,]*", keep.digits,
type.convert=TRUE)
expected <- data.table(
chrom=c("chr1","chr4"),
chromStart=c(2L,5L),
chromEnd=c(3000L,6000L))
expect_identical(computed, expected)
})
test_engines("capture_all_str errors for one argument", {
expect_error({
capture_all_str("foo")
}, "must have at least one named argument")
})
test_engines("capture_all_str errors for multi-dim patterns", {
expect_error({
capture_all_str("foo", sars=c("bar", "baz"))
}, "patterns must be character vectors of length 1")
})
test_engines("capture_all_str errors for 0-length patterns", {
expect_error({
capture_all_str("foo", bar=character())
}, "patterns must be character vectors of length 1")
})
test_engines("capture_all_str errors for non char/fun args", {
expect_error({
capture_all_str("foo", "bar", 1)
}, "arguments must be", fixed=TRUE)
})
test_engines("capture_all_str errors for two funs in a row", {
expect_error({
capture_all_str("foo", g="bar", as.integer, as.numeric)
},
"too many functions; up to one function may follow each named pattern")
})
test_engines("capture_all_str errors for fun at start", {
expect_error({
capture_all_str("foo", as.numeric)
},
"too many functions; up to one function may follow each named pattern")
})
test_engines("capture_all_str errors for NA pattern", {
expect_error({
capture_all_str("foo", g="bar", NA_character_, "baz")
}, "patterns must not be missing/NA")
})
test_engines("nested capture groups works", {
trackDb.txt.gz <- system.file("extdata", "trackDb.txt.gz", package="nc")
track.pattern <- list(
cellType=".*?",
"_",
sampleName=list(as.factor,
"McGill",
sampleID="[0-9]+", as.integer),
dataType="Coverage|Peaks",
"|",
"[^\n]+")
match.dt <- capture_all_str(
trackDb.txt.gz,
"track ",
track=track.pattern,
"(?:\n[^\n]+)*",
"\\s+bigDataUrl ",
bigDataUrl="[^\n]+")
expect_equal(nrow(match.dt), 78)
expect_is(match.dt, "data.table")
expect_identical(
names(match.dt),
c("track", "cellType", "sampleName", "sampleID",
"dataType", "bigDataUrl"))
expect_is(match.dt$sampleName, "factor")
expect_is(match.dt$sampleID, "integer")
})
test_engines("error for capture all regex with literal groups, match", {
expect_error({
capture_all_str(
c("chr1:100-200", "chr2:5-6"),
chrom="chr.",
":",
"([0-9]+)")
}, "regex contains more groups than names; please remove literal groups (parentheses) from the regex pattern, and use named arguments in R code instead", fixed=TRUE)
})
test_engines("error for capture all regex with literal groups, no match", {
expect_error({
nc::capture_all_str("alias(es)", foo="alias(es)")
}, "regex contains more groups than names; please remove literal groups (parentheses) from the regex pattern, and use named arguments in R code instead", fixed=TRUE)
})
test_engines("before_match markdown link", {
markdown_link <- list(
"\\[",
title="[^]]+?",
"\\]\\(",
url="http.*?",
"\\)")
markdown_subject <- "before [foo](http) between [bar text](http) after\n"
expected_dt <- rowwiseDT(
before=, match=, title=, url=,
"before ", "[foo](http)", "foo", "http",
" between ", "[bar text](http)", "bar text", "http",
" after\n", "", "", "")
markdown_dt <- nc::capture_all_str(markdown_subject, markdown_link)
expect_identical(markdown_dt, expected_dt[1:2, .(title, url)])
before_link <- nc::before_match(markdown_link)
before_dt <- nc::capture_all_str(markdown_subject, before_link)
expect_identical(before_dt, expected_dt)
ch14.qmd <- system.file(package="nc", "extdata", "ch14.qmd", mustWork=TRUE)
ch14.lines <- readLines(ch14.qmd)
ch14.str <- paste(ch14.lines, collapse="\n")
ch14_dt <- nc::capture_all_str(ch14.str, before_link)
expect_equal(nrow(ch14_dt), 3)
expect_identical(ch14_dt[, paste(paste0(before, match), collapse="")], ch14.str)
})