| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('invalid_utf8() works and respects NA', { |
| 4 |
(invalid_utf8(character()) %==% integer()) |
| 5 |
x = 'fa\xE7ile'; Encoding(x) = 'latin1' |
| 6 |
(invalid_utf8(c('aaa', x, NA_character_, '', '\u4e2d\u6587')) %==% 2L) |
| 7 |
}) |
| 8 |
|
| 9 |
assert('read/write_utf8() works', { |
| 10 |
ascii_txt = c('aa', 'bb', 'cc') |
| 11 |
utf8_txt = c('\u4e2d\u6587', '\u5927\u5bb6\u597d', '\u65e9\u996d') |
| 12 |
latin_txt = local({x = 'fa\xE7ile'; Encoding(x) = 'latin1'; x}) |
| 13 |
mixed_txt = c(ascii_txt, latin_txt, utf8_txt) |
| 14 |
|
| 15 |
write_utf8(ascii_txt, con = (ascii_file = tempfile())) |
| 16 |
write_utf8(utf8_txt, con = (utf8_file = tempfile())) |
| 17 |
write_utf8(latin_txt, con = (latin_file = tempfile())) |
| 18 |
write_utf8(mixed_txt, con = (mixed_file = tempfile())) |
| 19 |
|
| 20 |
(read_utf8(ascii_file) %==% ascii_txt) |
| 21 |
(read_utf8(utf8_file) %==% utf8_txt) |
| 22 |
(read_utf8(latin_file) %==% latin_txt) # identical will not compare Encoding |
| 23 |
(Encoding(read_utf8(latin_file)) %==% 'UTF-8') |
| 24 |
(read_utf8(mixed_file) %==% mixed_txt) |
| 25 |
(Encoding(read_utf8(mixed_file)) %==% c(rep('unknown', 3), rep('UTF-8', 4))) |
| 26 |
|
| 27 |
mixed_file2 = tempfile() |
| 28 |
local({ |
| 29 |
opts = options(encoding = 'native.enc'); on.exit(options(opts), add = TRUE) |
| 30 |
writeLines(mixed_txt, con = mixed_file2, useBytes = TRUE) |
| 31 |
}) |
| 32 |
(suppressWarnings(read_utf8(mixed_file2)[4] != mixed_txt[4])) |
| 33 |
has_warning(read_utf8(mixed_file2)) |
| 34 |
has_error(read_utf8(mixed_file2, error = TRUE)) |
| 35 |
}) |
| 36 |
|
| 37 |
assert('read_utf8(binary = TRUE) preserves Ctrl+Z bytes and trailing content', { |
| 38 |
# a Ctrl+Z (\x1a) byte embedded in a line; on Windows, a text-mode connection |
| 39 |
# would treat it as end-of-file and drop this line's tail and all lines after |
| 40 |
f = tempfile() |
| 41 |
writeBin(charToRaw('head\nx\x1ay\ntail\n'), f) |
| 42 |
|
| 43 |
# binary mode reads the whole file and keeps the \x1a byte |
| 44 |
(read_utf8(f, binary = TRUE) %==% c('head', 'x\x1ay', 'tail')) |
| 45 |
(grepl('\x1a', read_utf8(f, binary = TRUE)[2])) |
| 46 |
|
| 47 |
unlink(f) |
| 48 |
}) |
| 49 |
|
| 50 |
assert('empty files produce character() via file_string()', { |
| 51 |
tmp = tempfile() |
| 52 |
writeLines(character(), tmp) |
| 53 |
(file_string(tmp) %==% raw_string(character())) |
| 54 |
}) |
| 55 |
|
| 56 |
assert('file_string() concatenates non-empty file lines with newlines', { |
| 57 |
f = tempfile() |
| 58 |
write_utf8(c('a', 'b', 'c'), f) |
| 59 |
(file_string(f) %==% raw_string('a\nb\nc')) |
| 60 |
unlink(f) |
| 61 |
}) |
| 62 |
|
| 63 |
assert('grep_sub() matches elements and do substitution on them', { |
| 64 |
(grep_sub('a([b]+)c', 'a\\U\\1c', c('abc', 'abbbc', 'addc', '123'), perl = TRUE) %==% |
| 65 |
c('aBc', 'aBBBc')) |
| 66 |
}) |
| 67 |
|
| 68 |
assert('read_bin() reads binary files', { |
| 69 |
f = tempfile() |
| 70 |
writeBin(as.raw(1:10), f) |
| 71 |
(read_bin(f) %==% as.raw(1:10)) |
| 72 |
unlink(f) |
| 73 |
}) |
| 74 |
|
| 75 |
assert('append_utf8() appends content to a file and sorts it by default', { |
| 76 |
f = tempfile() |
| 77 |
write_utf8(c('b', 'a'), f) |
| 78 |
append_utf8('c', f) |
| 79 |
(read_utf8(f) %==% c('a', 'b', 'c')) |
| 80 |
}) |
| 81 |
|
| 82 |
assert('append_unique() appends only lines not already in the file and sorts', { |
| 83 |
f = tempfile() |
| 84 |
write_utf8(c('b', 'a'), f) |
| 85 |
append_unique(c('a', 'c'), f) # 'a' is already there; only 'c' is new |
| 86 |
(read_utf8(f) %==% c('a', 'b', 'c')) |
| 87 |
}) |
| 88 |
|
| 89 |
assert('process_file() applies a function to file contents', { |
| 90 |
f = tempfile() |
| 91 |
write_utf8('Hello World', f) |
| 92 |
process_file(f, function(x) gsub('World', 'R', x)) |
| 93 |
(read_utf8(f) %==% 'Hello R') |
| 94 |
unlink(f) |
| 95 |
}) |
| 96 |
|
| 97 |
assert('process_file() returns transformed result when file is missing', { |
| 98 |
(process_file(fun = toupper, x = 'hello') %==% 'HELLO') |
| 99 |
}) |
| 100 |
|
| 101 |
assert('sort_file() sorts lines in a file', { |
| 102 |
f = tempfile() |
| 103 |
write_utf8(c('b', 'a', 'c'), f) |
| 104 |
sort_file(f) |
| 105 |
(read_utf8(f) %==% c('a', 'b', 'c')) |
| 106 |
unlink(f) |
| 107 |
}) |
| 108 |
|
| 109 |
assert('gsub_file() replaces patterns in a file', { |
| 110 |
f = tempfile() |
| 111 |
writeLines(c('hello', 'world'), f) |
| 112 |
gsub_file(f, 'world', 'R', fixed = TRUE) |
| 113 |
(readLines(f) %==% c('hello', 'R')) |
| 114 |
unlink(f) |
| 115 |
}) |
| 116 |
|
| 117 |
assert('gsub_file() skips file silently when rw_error=FALSE and read fails', { |
| 118 |
# a file with invalid UTF-8 content cannot be read; with rw_error=FALSE it is silently skipped |
| 119 |
f = tempfile() |
| 120 |
writeBin(as.raw(c(0x68, 0x65, 0x6c, 0x6c, 0xc0, 0x80)), f) # invalid UTF-8 bytes |
| 121 |
result = gsub_file(f, 'a', 'b', rw_error = FALSE) |
| 122 |
(is.null(result)) |
| 123 |
unlink(f) |
| 124 |
}) |
| 125 |
|
| 126 |
assert('read_all() reads and concatenates files', { |
| 127 |
f1 = tempfile(); f2 = tempfile() |
| 128 |
write_utf8(c('a', 'b'), f1) |
| 129 |
write_utf8(c('c', 'd'), f2) |
| 130 |
res = read_all(c(f1, f2)) |
| 131 |
(as.character(res) %==% c('a', 'b', 'c', 'd')) |
| 132 |
# with before/after: 1 before + 2 content + 1 after = 4 per file, 8 total |
| 133 |
res2 = read_all(c(f1, f2), before = function(f) paste('#', f), after = '') |
| 134 |
(length(res2) %==% 8L) |
| 135 |
unlink(c(f1, f2)) |
| 136 |
}) |
| 137 |
|
| 138 |
assert('gsub_dir() and gsub_ext() replace patterns in files within a dir', { |
| 139 |
d = tempfile() |
| 140 |
dir.create(d) |
| 141 |
f1 = file.path(d, 'a.txt') |
| 142 |
f2 = file.path(d, 'b.R') |
| 143 |
writeLines('hello world', f1) |
| 144 |
writeLines('foo bar', f2) |
| 145 |
gsub_dir('world', 'R', dir = d, fixed = TRUE) |
| 146 |
(readLines(f1) %==% 'hello R') |
| 147 |
(readLines(f2) %==% 'foo bar') # unchanged |
| 148 |
# gsub_ext only processes files with specified extension |
| 149 |
gsub_ext('R', 'bar', 'BAR', dir = d, fixed = TRUE) |
| 150 |
(readLines(f2) %==% 'foo BAR') |
| 151 |
# filter by mimetype: only text files |
| 152 |
gsub_dir('R', 'REPLACED', dir = d, fixed = TRUE, mimetype = '^text/') |
| 153 |
(readLines(f1) %==% 'hello REPLACED') |
| 154 |
unlink(d, recursive = TRUE) |
| 155 |
}) |
| 156 |
|
| 157 |
assert('lazy_save() and lazy_load() round-trip objects', { |
| 158 |
d = tempdir() |
| 159 |
path = file.path(d, 'lazy-test-') |
| 160 |
x = 1:10 |
| 161 |
y = list(a = 'hello') |
| 162 |
lazy_save(c('x', 'y'), path) |
| 163 |
# verify index file exists |
| 164 |
(file.exists(paste0(path, '0.rds'))) |
| 165 |
# load into new env |
| 166 |
e = new.env(parent = emptyenv()) |
| 167 |
lazy_load(path, envir = e) |
| 168 |
(e$x %==% x) |
| 169 |
(e$y %==% y) |
| 170 |
unlink(list.files(d, pattern = '^lazy-test-', full.names = TRUE)) |
| 171 |
}) |
| 172 |
|
| 173 |
assert('write_utf8() handles NULL input', { |
| 174 |
f = tempfile() |
| 175 |
write_utf8(NULL, f) |
| 176 |
(read_utf8(f) %==% character(0)) |
| 177 |
unlink(f) |
| 178 |
}) |
| 179 |
|
| 180 |
assert('gsub_file() with rw_error=FALSE does not stop on read errors', { |
| 181 |
# a non-existent file should return invisible with rw_error=FALSE |
| 182 |
f = tempfile() # doesn't exist |
| 183 |
result = gsub_file(f, 'a', 'b', rw_error = FALSE) |
| 184 |
(is.null(result)) |
| 185 |
}) |
| 186 |
|
| 187 |
assert('io_method() returns a list method as-is', { |
| 188 |
m = list(name = 'custom', save = saveRDS, load = readRDS) |
| 189 |
(io_method(m, '.') %==% m) |
| 190 |
}) |
| 191 |
|
| 192 |
assert('write_utf8() to empty string prints to stdout', { |
| 193 |
out = capture.output(write_utf8(c('hello', 'world'), '')) |
| 194 |
(out %==% c('hello', 'world')) |
| 195 |
}) |
| 196 |
|
| 197 |
assert('append_utf8() with sort = FALSE does not sort', { |
| 198 |
f = tempfile() |
| 199 |
write_utf8(c('b', 'a'), f) |
| 200 |
append_utf8('c', f, sort = FALSE) |
| 201 |
(read_utf8(f) %==% c('b', 'a', 'c')) |
| 202 |
unlink(f) |
| 203 |
}) |
| 204 |
|
| 205 |
assert('read_all() with constant before/after works', { |
| 206 |
f1 = tempfile(); f2 = tempfile() |
| 207 |
write_utf8('line1', f1) |
| 208 |
write_utf8('line2', f2) |
| 209 |
res = read_all(c(f1, f2), before = '/*', after = '*/') |
| 210 |
(as.character(res) %==% c('/*', 'line1', '*/', '/*', 'line2', '*/')) |
| 211 |
unlink(c(f1, f2)) |
| 212 |
}) |
| 213 |
|
| 214 |
assert('process_file() does not write when content is unchanged', { |
| 215 |
f = tempfile() |
| 216 |
write_utf8('hello', f) |
| 217 |
mtime1 = file.mtime(f) |
| 218 |
Sys.sleep(0.1) |
| 219 |
process_file(f, identity) |
| 220 |
(file.mtime(f) %==% mtime1) |
| 221 |
unlink(f) |
| 222 |
}) |
| 223 |
|
| 224 |
assert('gsub_file() errors on unreadable file with rw_error = TRUE', { |
| 225 |
(has_error(gsub_file(tempfile(), 'a', 'b', rw_error = TRUE))) |
| 226 |
}) |
| 227 |
|
| 228 |
assert('gsub_files() processes multiple files', { |
| 229 |
f1 = tempfile(); f2 = tempfile() |
| 230 |
writeLines('hello world', f1) |
| 231 |
writeLines('world hello', f2) |
| 232 |
gsub_files(c(f1, f2), 'world', 'R', fixed = TRUE) |
| 233 |
(readLines(f1) %==% 'hello R') |
| 234 |
(readLines(f2) %==% 'R hello') |
| 235 |
unlink(c(f1, f2)) |
| 236 |
}) |