| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('is_blank() tests if a vector is blank', { |
| 4 |
(is_blank('')) |
| 5 |
(is_blank(NULL) %==% logical(0)) |
| 6 |
(is_blank(c('', ' ', '\n', '\t'))) |
| 7 |
(!is_blank('abc')) |
| 8 |
(is_blank(c('', 'abc')) %==% c(TRUE, FALSE)) |
| 9 |
}) |
| 10 |
|
| 11 |
assert('n2w converts numbers to words', { |
| 12 |
(n2w(0) %==% 'zero') |
| 13 |
# cap capitalizes the first letter |
| 14 |
(n2w(0, cap = TRUE) %==% 'Zero') |
| 15 |
# hyphen adds '-' for 21-99 (except 30, 40, ...) |
| 16 |
(n2w(21, cap = TRUE, hyphen = TRUE) %==% 'Twenty-one') |
| 17 |
(n2w(21, cap = TRUE, hyphen = FALSE) %==% 'Twenty one') |
| 18 |
# x can be negative integers |
| 19 |
(n2w(-21, cap = TRUE, hyphen = TRUE) %==% 'Minus twenty-one') |
| 20 |
(n2w(-21, cap = TRUE, hyphen = FALSE) %==% 'Minus twenty one') |
| 21 |
# and controls whether to have 'add' between hundreds and double digits |
| 22 |
(n2w(121, and = FALSE) %==% 'one hundred twenty-one') |
| 23 |
(n2w(121, and = TRUE) %==% 'one hundred and twenty-one') |
| 24 |
# x can be a vector with length > 1 |
| 25 |
(n2w(c(10, 13, 99, 1e6)) %==% c('ten', 'thirteen', 'ninety-nine', 'one million')) |
| 26 |
# the number should be less than 1e15 |
| 27 |
(has_error(n2w(1e15))) |
| 28 |
}) |
| 29 |
|
| 30 |
assert('join_words() joins multiple words into a single string', { |
| 31 |
jw = function(...) unclass(join_words(...)) |
| 32 |
(jw(NULL) %==% character()) |
| 33 |
(jw(c('a')) %==% 'a') |
| 34 |
(jw(c('a', 'b')) %==% 'a and b') |
| 35 |
(jw(c('a', 'b'), and = "") %==% 'a, b') |
| 36 |
(jw(c('a', 'b', 'c')) %==% 'a, b, and c') |
| 37 |
(jw(c('a', 'b', 'c'), and = '') %==% 'a, b, c') |
| 38 |
(jw(c('a', 'b', 'c'), ' / ', '') %==% 'a / b / c') |
| 39 |
(jw(c('a', 'b', 'c'), before = '"') %==% '"a", "b", and "c"') |
| 40 |
(jw(c('a', 'b', 'c'), before = '``', after = "''") %==% "``a'', ``b'', and ``c''") |
| 41 |
(jw(c('a', 'b', 'c'), before = '``', after = "''", oxford_comma = FALSE) %==% "``a'', ``b'' and ``c''") |
| 42 |
rm(list = 'jw') |
| 43 |
}) |
| 44 |
|
| 45 |
assert('split_lines() splits a character vector into lines', { |
| 46 |
(split_lines('a') %==% 'a') |
| 47 |
(split_lines('') %==% '') |
| 48 |
(split_lines(NULL) %==% NULL) |
| 49 |
(split_lines('a\n') %==% c('a', '')) |
| 50 |
(split_lines(c('a', 'b\nc')) %==% c('a', 'b', 'c')) |
| 51 |
(split_lines(c('', '\n')) %==% c('', '', '')) |
| 52 |
(split_lines('a\nb') %==% c('a', 'b')) |
| 53 |
(split_lines('a\nb\n\n') %==% c('a', 'b', '', '')) |
| 54 |
(split_lines(c('a\nb', '', ' ', 'c')) %==% c('a', 'b', '', ' ', 'c')) |
| 55 |
}) |
| 56 |
|
| 57 |
assert('split_source() puts lines of the same expression into a list element', { |
| 58 |
(split_source('1+1') %==% list('1+1')) |
| 59 |
(split_source(c('1+1+', '1')) %==% list(c('1+1+', '1'))) |
| 60 |
(split_source(c('1+1+', '1', 'TRUE')) %==% list(c('1+1+', '1'), 'TRUE')) |
| 61 |
x = c('# a', '# b', '1', '# c', 'if (T)', 'F') |
| 62 |
(split_source(x) %==% c(as.list(x[1:4]), list(x[5:6]))) |
| 63 |
(split_source(x, merge_comments = TRUE) %==% list(x[1:3], x[4:6])) |
| 64 |
x = c(x, '1+1') |
| 65 |
(split_source(x, TRUE, line_number = TRUE)[2:3] %==% list( |
| 66 |
structure(x[4:6], lines = c(4L, 6L)), |
| 67 |
structure(x[7], lines = c(7L, 7L)) |
| 68 |
)) |
| 69 |
}) |
| 70 |
|
| 71 |
assert('split_source() returns list with input for empty input', { |
| 72 |
# empty input (length < 1) should return list(x) immediately |
| 73 |
(split_source(character(0)) %==% list(character(0))) |
| 74 |
}) |
| 75 |
|
| 76 |
assert('split_source() should signal an error for incomplete code', { |
| 77 |
(has_error(split_source('1+1+'))) |
| 78 |
(has_error(split_source(c('1+1', '1+1+')))) |
| 79 |
}) |
| 80 |
|
| 81 |
assert('valid_syntax() tells if a code fragment is syntactically valid', { |
| 82 |
(valid_syntax('1+1')) |
| 83 |
(!valid_syntax('1+1+')) |
| 84 |
(valid_syntax('if(TRUE)1')) |
| 85 |
(!valid_syntax(c('if(T){', 'F'))) |
| 86 |
(valid_syntax(c('if(T){', 'F}'))) |
| 87 |
}) |
| 88 |
|
| 89 |
assert('alnum_id() generates ID strings', { |
| 90 |
x = c('Hello world 123!', 'a &b*^##c 456') |
| 91 |
(alnum_id(x) %==% c('hello-world-123', 'a-b-c-456')) |
| 92 |
(alnum_id(x, '[^[:alpha:]]+') %==% c('hello-world', 'a-b-c')) |
| 93 |
}) |
| 94 |
|
| 95 |
assert('str_wrap() wraps text and returns same-length output', { |
| 96 |
x = c('hello world foo bar baz', 'another long string here') |
| 97 |
res = str_wrap(x, width = 10) |
| 98 |
(length(res) %==% length(x)) |
| 99 |
(is.character(res)) |
| 100 |
# each element should contain newlines when wrapping was needed |
| 101 |
(grepl('\n', res[1])) |
| 102 |
}) |
| 103 |
|
| 104 |
assert('decimal_dot() forces dot as the decimal separator', { |
| 105 |
old = options(OutDec = ',') |
| 106 |
r = decimal_dot(as.character(1.234)) |
| 107 |
options(old) # restore OutDec |
| 108 |
(r %==% '1.234') |
| 109 |
}) |
| 110 |
|
| 111 |
assert('strip_blank() removes blank elements from both ends', { |
| 112 |
(strip_blank(character(0)) %==% character(0)) |
| 113 |
(strip_blank(c('', 'a', 'b', '')) %==% c('a', 'b')) |
| 114 |
(strip_blank(c('', ' ', 'a', '', 'b', '', '')) %==% c('a', '', 'b')) |
| 115 |
(strip_blank(c('a', 'b')) %==% c('a', 'b')) |
| 116 |
(strip_blank(c('', '')) %==% character(0)) |
| 117 |
}) |
| 118 |
|
| 119 |
assert('query_params() creates URL query strings', { |
| 120 |
(query_params() %==% '') |
| 121 |
(query_params(a = 1, b = 'foo') %==% '?a=1&b=foo') |
| 122 |
(query_params(.list = list(x = 'bar')) %==% '?x=bar') |
| 123 |
}) |
| 124 |
|
| 125 |
assert('pair_chars() checks balanced paired characters', { |
| 126 |
# balanced quotes |
| 127 |
x = c('He said \u201chello\u201d.', 'No quotes here.') |
| 128 |
(pair_chars(x) %==% x) |
| 129 |
# unbalanced quotes produce a warning |
| 130 |
(has_warning(pair_chars(c('\u201chello.')))) |
| 131 |
# wrong chars length errors |
| 132 |
(has_error(pair_chars('text', chars = c('\u201c')))) |
| 133 |
# file mode: file content unchanged (quotes are already balanced) |
| 134 |
f = tempfile() |
| 135 |
writeLines(c('\u201chello\u201d', '\u201cworld\u201d'), f) |
| 136 |
pair_chars(file = f) |
| 137 |
(read_utf8(f) %==% c('\u201chello\u201d', '\u201cworld\u201d')) |
| 138 |
# file mode: content changed (mismatched quotes get replaced) |
| 139 |
f2 = tempfile() |
| 140 |
writeLines(c('\u201chello\u201d', '\u2018world\u2018'), f2) # second line has wrong closing quote |
| 141 |
has_warning(pair_chars(file = f2)) |
| 142 |
unlink(c(f, f2)) |
| 143 |
}) |
| 144 |
|
| 145 |
assert('html_content() resolves HTML content recursively', { |
| 146 |
# list with mixed html and plain text |
| 147 |
result = html_content(list('hello', html_value('<b>world</b>'))) |
| 148 |
(result %==% c(html_escape('hello'), '<b>world</b>')) |
| 149 |
}) |
| 150 |
|
| 151 |
assert('numbers_to_words() handles more cases', { |
| 152 |
# non-numeric input errors |
| 153 |
(has_error(n2w('abc'))) |
| 154 |
# value >= 1e15 errors |
| 155 |
(has_error(n2w(1e15))) |
| 156 |
# 10-19 range (x_cs[1] == 1) |
| 157 |
(n2w(10) %==% 'ten') |
| 158 |
(n2w(15) %==% 'fifteen') |
| 159 |
(n2w(19) %==% 'nineteen') |
| 160 |
# x00 pattern (100, 200, ...) and x0x (101, 201, ...) |
| 161 |
(n2w(100) %==% 'one hundred') |
| 162 |
(n2w(200) %==% 'two hundred') |
| 163 |
(n2w(101) %==% 'one hundred one') |
| 164 |
# float with decimal part |
| 165 |
(n2w(1.5) %==% 'one point five') |
| 166 |
(n2w(3.14) %==% 'three point one four') |
| 167 |
# round tens like 30, 40, 50 (x_cs[2] == 0 branch in convert_2) |
| 168 |
(n2w(30) %==% 'thirty') |
| 169 |
(n2w(40) %==% 'forty') |
| 170 |
(n2w(50) %==% 'fifty') |
| 171 |
}) |
| 172 |
|
| 173 |
assert('encrypt() and decrypt() are inverses', { |
| 174 |
key = 'fedcba9876543210' |
| 175 |
x = 'hello' |
| 176 |
(decrypt(encrypt(x, key), key) %==% x) |
| 177 |
}) |
| 178 |
|
| 179 |
assert('n2w() handles large numbers and trillion range', { |
| 180 |
(n2w(1e12) %==% 'one trillion') |
| 181 |
(n2w(1e9) %==% 'one billion') |
| 182 |
(n2w(1e6) %==% 'one million') |
| 183 |
(n2w(1000) %==% 'one thousand') |
| 184 |
(n2w(1e14) %==% 'one hundred trillion') |
| 185 |
# number just below the limit |
| 186 |
(is.character(n2w(1e15 - 1))) |
| 187 |
}) |
| 188 |
|
| 189 |
assert('n2w() handles vectors and zero correctly', { |
| 190 |
(n2w(0:3) %==% c('zero', 'one', 'two', 'three')) |
| 191 |
}) |
| 192 |
|
| 193 |
assert('join_words() with length-1 input and oxford_comma edge cases', { |
| 194 |
jw = function(...) unclass(join_words(...)) |
| 195 |
# single word |
| 196 |
(jw('hello') %==% 'hello') |
| 197 |
# oxford comma with sep not ending in space |
| 198 |
(jw(c('a', 'b', 'c'), sep = ', ', and = ' and ', oxford_comma = TRUE) %==% 'a, b, and c') |
| 199 |
(jw(c('a', 'b', 'c'), sep = ', ', and = ' and ', oxford_comma = FALSE) %==% 'a, b and c') |
| 200 |
rm(list = 'jw') |
| 201 |
}) |
| 202 |
|
| 203 |
assert('split_lines() handles \\r\\n line endings', { |
| 204 |
# CRLF should be split the same as LF |
| 205 |
(split_lines('a\r\nb') %==% c('a', 'b')) |
| 206 |
(split_lines('a\r\nb\r\n') %==% c('a', 'b', '')) |
| 207 |
}) |
| 208 |
|
| 209 |
assert('alnum_id() strips HTML tags before generating ID', { |
| 210 |
(alnum_id('<h1>Hello <strong>world</strong>!') %==% 'hello-world') |
| 211 |
}) |
| 212 |
|
| 213 |
assert('strip_html() handles multiline-like patterns and nested tags', { |
| 214 |
(strip_html('<!-- comment --><b>text</b>') %==% 'text') |
| 215 |
# nested tags |
| 216 |
(strip_html('<div><p>hello</p></div>') %==% 'hello') |
| 217 |
}) |