| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('strict_list() is really strict', { |
| 4 |
s_list = strict_list(aaa = 1:3, bbb = c('hey', 'mom')) |
| 5 |
# the class name better not be changed in the future |
| 6 |
(inherits(s_list, 'xfun_strict_list')) |
| 7 |
# `$` returns the expected value if the name provided is complete |
| 8 |
(s_list$aaa %==% 1:3) |
| 9 |
# but partial match is prohibited |
| 10 |
(s_list$a %==% NULL) |
| 11 |
# `[` will return a list - will it be better if returns a strict list? |
| 12 |
(inherits(s_list['aaa'], 'list')) |
| 13 |
# and add an element won't change the class by accident |
| 14 |
s_list$ccc = 4 |
| 15 |
s_list$ddd = 'abcd' |
| 16 |
(inherits(s_list, 'xfun_strict_list')) |
| 17 |
}) |
| 18 |
|
| 19 |
assert('as_strict_list() converts a list to a strict list', { |
| 20 |
normal_list = list(aaa = 1:3, bbb = c('hey', 'mom')) |
| 21 |
s_list = as_strict_list(normal_list) |
| 22 |
# does the strict list have the same length as the normal list? |
| 23 |
(length(normal_list) %==% length(s_list)) |
| 24 |
# is the converted strict list equal to the same object created by `strict_list()`? |
| 25 |
s_list = strict_list(aaa = 1:3, bbb = c('hey', 'mom')) |
| 26 |
(as_strict_list(normal_list) %==% s_list) |
| 27 |
}) |
| 28 |
|
| 29 |
assert('print() returns the same output for strict and normal list', { |
| 30 |
normal_list = list(aaa = 1:2, bbb = c('hey', 'dad')) |
| 31 |
s_list = as_strict_list(normal_list) |
| 32 |
(capture.output(print(normal_list)) %==% capture.output(print(s_list))) |
| 33 |
}) |
| 34 |
|
| 35 |
assert('raw_string() prints as expected', { |
| 36 |
rs = raw_string(c('a "b"', 'hello\tworld!')) |
| 37 |
(inherits(rs, 'xfun_raw_string')) |
| 38 |
output = capture.output(rs) |
| 39 |
(output %==% c('a "b"', 'hello\tworld!')) |
| 40 |
}) |
| 41 |
|
| 42 |
assert('raw_string() returns 0-length xfun_raw_string when input is NULL', { |
| 43 |
rs1 = raw_string(NULL) |
| 44 |
(inherits(rs1, 'xfun_raw_string')) |
| 45 |
(length(rs1) == 0) |
| 46 |
(capture.output(rs1) %==% character(0)) |
| 47 |
|
| 48 |
# string 'NULL' is treated appropriately |
| 49 |
rs2 = raw_string('NULL') |
| 50 |
(inherits(rs2, 'xfun_raw_string')) |
| 51 |
(length(rs2) == 1) |
| 52 |
(capture.output(rs2) %==% 'NULL') |
| 53 |
}) |
| 54 |
|
| 55 |
assert('raw_string() inherits from character', { |
| 56 |
rs = raw_string(c('a "b"', 'hello\tworld!')) |
| 57 |
(inherits(rs, 'character')) |
| 58 |
}) |