Debian Codegraph @

tests/testit/test-closure.R

raw
1 library(testit)
2
3 z = new_defaults(list(a = 1))
4
5 assert('$set() and $get() methods set/get values', {
6 (z$get() %==% list(a = 1))
7 ({z$set(a = 2); z$get('a')} %==% 2)
8 ({z$set(b = 'f'); z$get(c('a', 'b'))} %==% list(a = 2, b = 'f'))
9 })
10
11 assert('$merge() does not change the object but returns a list of new values', {
12 ({z$merge(list(b = 'g')); z$get('b')} %==% 'f')
13 (z$merge(list(b = 'g')) %==% list(a = 2, b = 'g'))
14 })
15
16 assert('$get(default=TRUE) returns the initial value', {
17 (z$get(default = TRUE) %==% list(a = 1))
18 })
19
20 assert('$get(names) keeps the names if drop = FALSE and one or more names do not exist', {
21 (z$get(c('a', 'c')) %==% list(a = 2, c = NULL))
22 (z$get('c') %==% NULL)
23 (z$get('c', drop = FALSE) %==% list(c = NULL))
24 })
25
26 assert('$delete() deletes keys from the list', {
27 z$set(b1 = TRUE, b2 = FALSE); z$delete(c('b1', 'b2'))
28 (intersect(c('b1', 'b2'), names(z$get())) %==% character())
29 })
30
31 assert('$restore() restores to the initial value', {
32 ({z$restore(); z$get()} %==% list(a = 1))
33 })
34
35 assert('$append() returns appended the chunk option', {
36 z$set(d = 1)
37 z$append(d = 2)
38 (z$get('d') %==% c(1, 2))
39 })
40
41 z = new_defaults()
42
43 # named argument in set()
44 z$set(a = list(b = 2, c = 'qwer'))
45
46 assert('a named argument of list in $set() method is not treated as a list of options', {
47 (length(z$get()) %==% 1L)
48 # unnamed argument in set()
49 ({z$restore(); z$set(list(b = 2, c = 'qwer')); z$get()} %==% list(b = 2, c = 'qwer'))
50 })