| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('attr2() is strict', { |
| 4 |
z = structure(list(a = 1), foo = 2) |
| 5 |
(attr2(z, 'foo') %==% 2) |
| 6 |
(attr2(z, 'f') %==% NULL) |
| 7 |
}) |
| 8 |
|
| 9 |
assert('in_dir() preserves the working directory', { |
| 10 |
owd = getwd() |
| 11 |
in_dir('.', setwd(tempdir())) |
| 12 |
(same_path(owd, getwd())) |
| 13 |
}) |
| 14 |
|
| 15 |
assert('set_envvar() sets and restores environment variables', { |
| 16 |
old = set_envvar(c(XFUN_TEST_VAR_123 = 'hello')) |
| 17 |
(Sys.getenv('XFUN_TEST_VAR_123') %==% 'hello') |
| 18 |
set_envvar(old) |
| 19 |
(is.na(old['XFUN_TEST_VAR_123'])) |
| 20 |
(Sys.getenv('XFUN_TEST_VAR_123') %==% '') |
| 21 |
}) |
| 22 |
|
| 23 |
assert('env_option() reads from options() and env vars', { |
| 24 |
# clean state |
| 25 |
options(xfun.test.opt = NULL) |
| 26 |
Sys.unsetenv('R_XFUN_TEST_OPT') |
| 27 |
(is.null(env_option('xfun.test.opt'))) |
| 28 |
(env_option('xfun.test.opt', 42) %==% 42) |
| 29 |
|
| 30 |
Sys.setenv(R_XFUN_TEST_OPT = 'from_env') |
| 31 |
(env_option('xfun.test.opt') %==% 'from_env') |
| 32 |
|
| 33 |
options(xfun.test.opt = 'from_opt') |
| 34 |
(env_option('xfun.test.opt') %==% 'from_opt') |
| 35 |
|
| 36 |
options(xfun.test.opt = NULL) |
| 37 |
Sys.unsetenv('R_XFUN_TEST_OPT') |
| 38 |
}) |
| 39 |
|
| 40 |
assert('parse_only() parses R code without keeping source', { |
| 41 |
(parse_only(character(0)) %==% expression()) |
| 42 |
(parse_only('1+1') %==% expression(1+1)) |
| 43 |
(parse_only(c('y~x', '1:5')) %==% expression(y~x, 1:5)) |
| 44 |
}) |
| 45 |
|
| 46 |
assert('try_silent() suppresses errors', { |
| 47 |
z = try_silent(stop('oops')) |
| 48 |
(inherits(z, 'try-error')) |
| 49 |
(try_silent(1 + 1) %==% 2) |
| 50 |
}) |
| 51 |
|
| 52 |
assert('try_error() returns TRUE on error, FALSE otherwise', { |
| 53 |
(try_error(stop('foo'))) |
| 54 |
(!try_error(1:10)) |
| 55 |
}) |
| 56 |
|
| 57 |
assert('bump_version() increments the last digit', { |
| 58 |
(bump_version('0.1') %==% as.numeric_version('0.2')) |
| 59 |
(bump_version('7.23.9') %==% as.numeric_version('7.23.10')) |
| 60 |
(bump_version(c('0.1', '91.2.14')) %==% as.numeric_version(c('0.2', '91.2.15'))) |
| 61 |
}) |
| 62 |
|
| 63 |
assert('strip_html() removes HTML tags and comments', { |
| 64 |
(strip_html('<a href="#">Hello <!-- comment -->world!</a>') %==% 'Hello world!') |
| 65 |
(strip_html('<b>bold</b> and <i>italic</i>') %==% 'bold and italic') |
| 66 |
(strip_html('no tags') %==% 'no tags') |
| 67 |
}) |
| 68 |
|
| 69 |
assert('html_escape() escapes special HTML characters', { |
| 70 |
(html_escape('& < >') %==% '& < >') |
| 71 |
(html_escape('" & < > \r \n') %==% '" & < > \r \n') |
| 72 |
(html_escape('" & < > \r \n', attr = TRUE) %==% '" & < > ') |
| 73 |
(html_escape('plain text') %==% 'plain text') |
| 74 |
}) |
| 75 |
|
| 76 |
assert('html_tag() generates correct HTML', { |
| 77 |
(inherits(html_tag('br'), 'xfun_html')) |
| 78 |
(c(html_tag('br')) %==% '<br />') |
| 79 |
(c(html_tag('p', 'hello')) %==% '<p>hello</p>') |
| 80 |
(c(html_tag('a', 'click', href = 'https://example.com')) %==% '<a href="https://example.com">click</a>') |
| 81 |
(c(html_tag('p', '<b>bold</b>')) %==% '<p><b>bold</b></p>') |
| 82 |
# html_value content should not be escaped |
| 83 |
(c(html_tag('p', html_value('<b>bold</b>'))) %==% '<p><b>bold</b></p>') |
| 84 |
(has_error(html_tag('p', .attrs = list(1)))) |
| 85 |
# NULL attribute value: only the attribute name is emitted |
| 86 |
(c(html_tag('input', type = 'checkbox', checked = NULL)) %==% '<input type="checkbox" checked />') |
| 87 |
}) |
| 88 |
|
| 89 |
assert('format_bytes() formats byte counts', { |
| 90 |
res = format_bytes(c(1, 1024)) |
| 91 |
(res %==% c('1 bytes', '1 Kb')) |
| 92 |
}) |
| 93 |
|
| 94 |
assert('msg_cat() prints messages suppressibly', { |
| 95 |
out = capture.output(suppressMessages(msg_cat('hello'))) |
| 96 |
(length(out) %==% 0L) |
| 97 |
# msg_cat() signals a message condition with the correct content |
| 98 |
msg = tryCatch(msg_cat('world'), message = function(m) conditionMessage(m)) |
| 99 |
(msg %==% 'world') |
| 100 |
}) |
| 101 |
|
| 102 |
assert('%|% returns x if non-empty, otherwise y', { |
| 103 |
(('hello' %|% 'world') %==% 'hello') |
| 104 |
((character(0) %|% 'fallback') %==% 'fallback') |
| 105 |
((NULL %|% 'fallback') %==% 'fallback') |
| 106 |
((1:3 %|% 99) %==% 1:3) |
| 107 |
}) |
| 108 |
|
| 109 |
assert('stop2() and warning2() do not include the call', { |
| 110 |
err = tryCatch(stop2('test error'), error = identity) |
| 111 |
(conditionMessage(err) %==% 'test error') |
| 112 |
(is.null(err$call)) |
| 113 |
wrn = tryCatch(warning2('test warn'), warning = identity) |
| 114 |
(conditionMessage(wrn) %==% 'test warn') |
| 115 |
(is.null(wrn$call)) |
| 116 |
}) |
| 117 |
|
| 118 |
assert('gsubi() performs case-insensitive substitution', { |
| 119 |
(gsubi('hello', 'Hi', 'Hello World') %==% 'Hi World') |
| 120 |
(gsubi('world', 'R', 'hello WORLD') %==% 'hello R') |
| 121 |
}) |
| 122 |
|
| 123 |
assert('retry() retries a failing function', { |
| 124 |
i = 0L |
| 125 |
# succeeds on the 2nd call |
| 126 |
res = retry(function() { i <<- i + 1L; if (i < 2L) stop('fail'); i }, .times = 3, .pause = 0) |
| 127 |
(res %==% 2L) |
| 128 |
# fails all .times times and stops |
| 129 |
(has_error(retry(function() stop('always'), .times = 2, .pause = 0))) |
| 130 |
}) |
| 131 |
|
| 132 |
assert('handle_error() calls handler and shows message on error', { |
| 133 |
msgs = c() |
| 134 |
tryCatch( |
| 135 |
withCallingHandlers( |
| 136 |
handle_error(stop('oops'), function(loc) c('caught', loc)), |
| 137 |
message = function(m) { |
| 138 |
msgs <<- c(msgs, conditionMessage(m)); invokeRestart('muffleMessage') |
| 139 |
} |
| 140 |
), |
| 141 |
error = function(e) NULL |
| 142 |
) |
| 143 |
(any(grepl('caught', msgs))) |
| 144 |
}) |
| 145 |
|
| 146 |
assert('is_windows/unix/macos/linux/arm64 return logical(1)', { |
| 147 |
(is.logical(is_windows()) && length(is_windows()) == 1L) |
| 148 |
(is.logical(is_unix()) && length(is_unix()) == 1L) |
| 149 |
(is.logical(is_macos()) && length(is_macos()) == 1L) |
| 150 |
(is.logical(is_linux()) && length(is_linux()) == 1L) |
| 151 |
(is.logical(is_arm64()) && length(is_arm64()) == 1L) |
| 152 |
# exactly one of windows / (unix && macos) / (unix && linux) / other unix is true |
| 153 |
(is_windows() || is_unix()) |
| 154 |
}) |
| 155 |
|
| 156 |
assert('set_envvar() errors on unnamed or partially unnamed vars', { |
| 157 |
(has_error(set_envvar(c(1, 2)))) # unnamed |
| 158 |
(has_error(set_envvar(c(A = '1', ''))) # partial names |
| 159 |
) |
| 160 |
}) |
| 161 |
|
| 162 |
assert('tree() produces a character tree diagram', { |
| 163 |
x = list(a = 1, b = list(c = 2, d = 3)) |
| 164 |
out = tree(x) |
| 165 |
(inherits(out, 'xfun_raw_string')) |
| 166 |
(is.character(out)) |
| 167 |
(length(out) > 0) |
| 168 |
}) |
| 169 |
|
| 170 |
assert('connect_pipes() fills vertical pipes in tree diagrams', { |
| 171 |
# early return for m < 3 (too few rows) |
| 172 |
(connect_pipes(c('|- a', ' ')) %==% c('|- a', ' ')) |
| 173 |
# pipe connection: blank rows between two |-, first column gets filled with '|' |
| 174 |
x = c('|- a', ' ', ' ', '|- c') |
| 175 |
res = connect_pipes(x) |
| 176 |
(substr(res[2], 1, 1) %==% '|') |
| 177 |
(substr(res[3], 1, 1) %==% '|') |
| 178 |
# k is cleared when a non-matching cell is found (line 272 path) |
| 179 |
x2 = c('|- a', ' ', 'xy', '|- c') |
| 180 |
res2 = connect_pipes(x2) |
| 181 |
(substr(res2[2], 1, 1) %==% ' ') |
| 182 |
# k is cleared when last row reached with all spaces (line 267 path) |
| 183 |
x3 = c('|- a', ' ', ' ', ' ') |
| 184 |
res3 = connect_pipes(x3) |
| 185 |
(substr(res3[2], 1, 1) %==% ' ') |
| 186 |
}) |
| 187 |
|
| 188 |
assert('func_name() returns the name of the calling function', { |
| 189 |
f = function() func_name(sys.nframe()) |
| 190 |
(f() %==% 'f') |
| 191 |
}) |
| 192 |
|
| 193 |
assert('exit_call() registers on.exit in the specified parent frame', { |
| 194 |
e = new.env(parent = emptyenv()); e$cleaned = FALSE |
| 195 |
inner = function() { |
| 196 |
# n=1: register in inner() itself (exits when inner returns) |
| 197 |
exit_call(function() e$cleaned = TRUE, n = 1) |
| 198 |
} |
| 199 |
inner() |
| 200 |
(e$cleaned) |
| 201 |
|
| 202 |
# n=2 (default): register on.exit in the caller of the function that called exit_call |
| 203 |
e2 = new.env(parent = emptyenv()); e2$cleaned = FALSE |
| 204 |
inner2 = function() exit_call(function() e2$cleaned = TRUE, n = 2) |
| 205 |
outer2 = function() { inner2() } |
| 206 |
outer2() |
| 207 |
(e2$cleaned) |
| 208 |
}) |
| 209 |
|
| 210 |
assert('format_bytes() formats various byte sizes', { |
| 211 |
(format_bytes(c(1, 1024, 1e6, 1e9)) %==% c('1 bytes', '1 Kb', '976.6 Kb', '953.7 Mb')) |
| 212 |
}) |
| 213 |
|
| 214 |
assert('gsubf() does fixed-string substitution', { |
| 215 |
(gsubf('a.b', 'X', 'a.b c a.b') %==% 'X c X') |
| 216 |
# should NOT treat '.' as regex wildcard |
| 217 |
(gsubf('a.b', 'X', 'axb') %==% 'axb') |
| 218 |
}) |
| 219 |
|
| 220 |
assert('parse_only() on empty character returns empty expression', { |
| 221 |
(parse_only(character(0)) %==% expression()) |
| 222 |
}) |
| 223 |
|
| 224 |
assert('try_error() handles warnings gracefully (does not return TRUE for warnings)', { |
| 225 |
# a warning is not an error |
| 226 |
(!try_error(warning('just a warning'))) |
| 227 |
}) |
| 228 |
|
| 229 |
assert('pkg_file() returns the path to a file in the xfun package', { |
| 230 |
p = pkg_file('DESCRIPTION') |
| 231 |
(file.exists(p)) |
| 232 |
(grepl('xfun', p, fixed = TRUE)) |
| 233 |
}) |