| 1 |
library(testit) |
| 2 |
|
| 3 |
# helper function to convert raw src to params list |
| 4 |
read_params = function(src, evaluate = TRUE) { |
| 5 |
lines = strsplit(src, "\n")[[1]] |
| 6 |
knit_params(lines, evaluate = evaluate) |
| 7 |
} |
| 8 |
|
| 9 |
# helper function to convert raw src yaml to params list |
| 10 |
read_params_yaml = function(src, evaluate = TRUE) { |
| 11 |
knit_params_yaml(src, evaluate = evaluate) |
| 12 |
} |
| 13 |
|
| 14 |
params = read_params(' |
| 15 |
--- |
| 16 |
params: |
| 17 |
a: 10 |
| 18 |
b: 20 |
| 19 |
--- |
| 20 |
' |
| 21 |
) |
| 22 |
assert('basic params parsing works', { |
| 23 |
(params[[1]]$name %==% 'a') |
| 24 |
(params[[1]]$value %==% 10L) |
| 25 |
(params[[2]]$name %==% 'b') |
| 26 |
(params[[2]]$value %==% 20L) |
| 27 |
(flatten_params(params) %==% list(a = 10L, b = 20L)) |
| 28 |
}) |
| 29 |
|
| 30 |
# test date custom type (these deprecated and here for backwards compt) -- |
| 31 |
params = read_params(' |
| 32 |
--- |
| 33 |
params: |
| 34 |
start: !date 2015-01-01 |
| 35 |
end: !datetime 2015-01-01 12:30:00 |
| 36 |
--- |
| 37 |
' |
| 38 |
) |
| 39 |
assert('date/time params parsing works', { |
| 40 |
(params[[1]]$name %==% 'start') |
| 41 |
(inherits(params[[1]]$value, 'Date')) |
| 42 |
(params[[1]]$value %==% as.Date("2015-01-01")) |
| 43 |
(params[[2]]$name %==% 'end') |
| 44 |
(inherits(params[[2]]$value, 'POSIXct')) |
| 45 |
(params[[2]]$value %==% as.POSIXct("2015-01-01 12:30:00", tz = "GMT")) |
| 46 |
}) |
| 47 |
|
| 48 |
params = read_params(' |
| 49 |
--- |
| 50 |
params: |
| 51 |
file1: |
| 52 |
value: data1.csv |
| 53 |
--- |
| 54 |
' |
| 55 |
) |
| 56 |
assert('sub-option params parsing works', { |
| 57 |
(params[[1]]$name %==% 'file1') |
| 58 |
(params[[1]]$value %==% 'data1.csv') |
| 59 |
}) |
| 60 |
|
| 61 |
params = read_params(' |
| 62 |
--- |
| 63 |
params: |
| 64 |
regions: |
| 65 |
value: [North, South] |
| 66 |
--- |
| 67 |
' |
| 68 |
) |
| 69 |
assert('prams with length > 1 works', { |
| 70 |
(length(params[[1]]$value) %==% 2L) |
| 71 |
(params[[1]]$value[[2]] %==% 'South') |
| 72 |
}) |
| 73 |
|
| 74 |
## test including additional parameter attributes -------------------------- |
| 75 |
|
| 76 |
params = read_params(' |
| 77 |
--- |
| 78 |
params: |
| 79 |
regions: |
| 80 |
value: [North, South] |
| 81 |
choices: [North, South, East, West] |
| 82 |
label: "Select Regions" |
| 83 |
--- |
| 84 |
' |
| 85 |
) |
| 86 |
assert('other types of params can be parsed', { |
| 87 |
(params[[1]]$choices %==% c('North', 'South', 'East', 'West')) |
| 88 |
(params[[1]]$label %==% "Select Regions") |
| 89 |
}) |
| 90 |
|
| 91 |
params = read_params(' |
| 92 |
--- |
| 93 |
params: |
| 94 |
x: 1 |
| 95 |
y: 2 |
| 96 |
z: 3 |
| 97 |
n: 4 |
| 98 |
Y: 5 |
| 99 |
N: 6 |
| 100 |
--- |
| 101 |
' |
| 102 |
) |
| 103 |
|
| 104 |
assert('y/Y/n/N are not converted to booleans', { |
| 105 |
(unname(unlist(lapply(params, `[[`, 'name'))) %==% c('x', 'y', 'z', 'n', 'Y', 'N')) |
| 106 |
}) |
| 107 |
|
| 108 |
|
| 109 |
params = read_params(' |
| 110 |
--- |
| 111 |
params: |
| 112 |
today: !r Sys.Date() |
| 113 |
now: !expr Sys.time() |
| 114 |
x: 10 |
| 115 |
--- |
| 116 |
' |
| 117 |
) |
| 118 |
assert('params as expressions can be parsed', { |
| 119 |
(!is.null(params[[1]]$expr)) |
| 120 |
(inherits(params[[1]]$value, 'Date')) |
| 121 |
(!is.null(params[[2]]$expr)) |
| 122 |
(inherits(params[[2]]$value, 'POSIXct')) |
| 123 |
(is.null(params[[3]]$expr)) |
| 124 |
}) |
| 125 |
|
| 126 |
params = read_params(' |
| 127 |
--- |
| 128 |
params: |
| 129 |
today: !r Sys.Date() |
| 130 |
--- |
| 131 |
', evaluate = FALSE) |
| 132 |
assert('unevaluated expressions in params can be parsed', { |
| 133 |
(params$today$expr %==% "Sys.Date()") |
| 134 |
(class(params$today$value) %==% "expression") |
| 135 |
}) |
| 136 |
|
| 137 |
params = read_params_yaml(' |
| 138 |
params: |
| 139 |
x: 1 |
| 140 |
today: !r Sys.Date() |
| 141 |
posixlt: !r strptime("2015-01-01", format = "%Y-%m-%d") |
| 142 |
list: [1,2,3] |
| 143 |
# map: { a: 1, b: 2 } |
| 144 |
map: { value: { a: 1, b: 2 } } |
| 145 |
') |
| 146 |
# The direct map value is not supported; an explicit value field is necessary |
| 147 |
assert('yaml parameters can be handled', { |
| 148 |
(params$x$value %==% 1L) |
| 149 |
(params$today$expr %==% "Sys.Date()") |
| 150 |
(inherits(params$today$value, 'Date')) |
| 151 |
(params$posixlt$expr %==% 'strptime("2015-01-01", format = "%Y-%m-%d")') |
| 152 |
(inherits(params$posixlt$value, 'POSIXlt')) |
| 153 |
}) |