/usr/share/doc/r-cran-knitr/tests/testit/test-params.R is in r-cran-knitr 1.17-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 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)
}
## test basic parameter parsing --------------------------------------------
params <- read_params('
---
params:
a: 10
b: 20
---
'
)
assert(params[[1]]$name == 'a')
assert(params[[1]]$value == 10)
assert(params[[2]]$name == 'b')
assert(params[[2]]$value == 20)
assert(identical(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(params[[1]]$name == 'start')
assert('Date' %in% class(params[[1]]$value))
assert(params[[1]]$value == as.Date("2015-01-01"))
assert(params[[2]]$name == 'end')
assert('POSIXct' %in% class(params[[2]]$value))
assert(params[[2]]$value == as.POSIXct("2015-01-01 12:30:00", tz = "GMT"))
## test specifying value in sub-object and type at object level ------------
params <- read_params('
---
params:
file1:
value: data1.csv
---
'
)
assert(params[[1]]$name == 'file1')
assert(params[[1]]$value == 'data1.csv')
## test parameters with length(value) > 1 ----------------------------------
params <- read_params('
---
params:
regions:
value: [North, South]
---
'
)
assert(length(params[[1]]$value) == 2)
assert(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(identical(params[[1]]$choices, c('North', 'South', 'East', 'West')))
assert(params[[1]]$label == "Select Regions")
## test y/Y/n/N ------------------------------------------------------------
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',
identical(unname(unlist(lapply(params, `[[`, 'name'))), c('x', 'y', 'z', 'n', 'Y', 'N'))
)
## test handling of expressions --------------------------------------------
params <- read_params('
---
params:
today: !r Sys.Date()
now: !expr Sys.time()
x: 10
---
'
)
assert(!is.null(params[[1]]$expr))
assert('Date' %in% class(params[[1]]$value))
assert(!is.null(params[[2]]$expr))
assert('POSIXct' %in% class(params[[2]]$value))
assert(is.null(params[[3]]$expr))
## test handling of unevaluated expressions --------------------------------------------
params <- read_params('
---
params:
today: !r Sys.Date()
---
', evaluate = FALSE)
assert(identical(params$today$expr, "Sys.Date()"))
assert(identical(class(params$today$value), "expression"))
## test handling of yaml parameters --------------------------------------------
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(params$x$value == 1)
assert(identical(class(params$x$value), "integer"))
assert(identical(params$today$expr, "Sys.Date()"))
assert('Date' %in% class(params$today$value))
assert(identical(params$posixlt$expr, 'strptime("2015-01-01", format = "%Y-%m-%d")'))
assert('POSIXlt' %in% class(params$posixlt$value))
## test handling of unevaluated yaml parameters --------------------------------------------
params <- read_params_yaml('
params:
today: !r Sys.Date()
', evaluate = FALSE)
assert(identical(params$today$expr, "Sys.Date()"))
assert(identical(class(params$today$value), "expression"))
|