File: test-params.R

package info (click to toggle)
r-cran-knitr 1.50%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,864 kB
  • sloc: makefile: 16; sh: 10; javascript: 8
file content (153 lines) | stat: -rw-r--r-- 3,253 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
library(testit)

# helper function to convert raw src to params list
read_params = function(src, evaluate = TRUE) {
  lines = strsplit(src, "\n")[[1]]
  knit_params(lines, evaluate = evaluate)
}

# helper function to convert raw src yaml to params list
read_params_yaml = function(src, evaluate = TRUE) {
  knit_params_yaml(src, evaluate = evaluate)
}

params = read_params('
---
params:
  a: 10
  b: 20
---
'
)
assert('basic params parsing works', {
  (params[[1]]$name %==% 'a')
  (params[[1]]$value %==% 10L)
  (params[[2]]$name %==% 'b')
  (params[[2]]$value %==% 20L)
  (flatten_params(params) %==% list(a = 10L, b = 20L))
})

# test date custom type (these deprecated and here for backwards compt) --
params = read_params('
---
params:
  start: !date 2015-01-01
  end: !datetime 2015-01-01 12:30:00
---
'
)
assert('date/time params parsing works', {
  (params[[1]]$name %==% 'start')
  (inherits(params[[1]]$value, 'Date'))
  (params[[1]]$value %==% as.Date("2015-01-01"))
  (params[[2]]$name %==% 'end')
  (inherits(params[[2]]$value, 'POSIXct'))
  (params[[2]]$value %==% as.POSIXct("2015-01-01 12:30:00", tz = "GMT"))
})

params = read_params('
---
params:
  file1:
    value: data1.csv
---
'
)
assert('sub-option params parsing works', {
  (params[[1]]$name %==% 'file1')
  (params[[1]]$value %==% 'data1.csv')
})

params = read_params('
---
params:
  regions:
    value: [North, South]
---
'
)
assert('prams with length > 1 works', {
  (length(params[[1]]$value) %==% 2L)
  (params[[1]]$value[[2]] %==% 'South')
})

## test including additional parameter attributes --------------------------

params = read_params('
---
params:
  regions:
    value: [North, South]
    choices: [North, South, East, West]
    label: "Select Regions"
---
'
)
assert('other types of params can be parsed', {
  (params[[1]]$choices %==% c('North', 'South', 'East', 'West'))
  (params[[1]]$label %==% "Select Regions")
})

params = read_params('
---
params:
  x: 1
  y: 2
  z: 3
  n: 4
  Y: 5
  N: 6
---
'
)

assert('y/Y/n/N are not converted to booleans', {
  (unname(unlist(lapply(params, `[[`, 'name'))) %==% c('x', 'y', 'z', 'n', 'Y', 'N'))
})


params = read_params('
---
params:
  today: !r Sys.Date()
  now: !expr Sys.time()
  x: 10
---
'
)
assert('params as expressions can be parsed', {
  (!is.null(params[[1]]$expr))
  (inherits(params[[1]]$value, 'Date'))
  (!is.null(params[[2]]$expr))
  (inherits(params[[2]]$value, 'POSIXct'))
  (is.null(params[[3]]$expr))
})

params = read_params('
---
params:
  today: !r Sys.Date()
---
', evaluate = FALSE)
assert('unevaluated expressions in params can be parsed', {
  (params$today$expr %==% "Sys.Date()")
  (class(params$today$value) %==% "expression")
})

params = read_params_yaml('
params:
  x: 1
  today: !r Sys.Date()
  posixlt: !r strptime("2015-01-01", format = "%Y-%m-%d")
  list: [1,2,3]
#  map: { a: 1, b: 2 }
  map: { value: { a: 1, b: 2 } }
')
# The direct map value is not supported; an explicit value field is necessary
assert('yaml parameters can be handled', {
  (params$x$value %==% 1L)
  (params$today$expr %==% "Sys.Date()")
  (inherits(params$today$value, 'Date'))
  (params$posixlt$expr %==% 'strptime("2015-01-01", format = "%Y-%m-%d")')
  (inherits(params$posixlt$value, 'POSIXlt'))
})