Skip to content

Commit

Permalink
make template does not fill sci format
Browse files Browse the repository at this point in the history
  • Loading branch information
mschubert committed Nov 13, 2024
1 parent 57e2ebe commit fbce8df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/util.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ fill_template = function(template, values, required=c()) {
paste(setdiff(required, keys), collapse=", "))

upd = keys %in% names(values)
is_num = sapply(values, is.numeric)
values[is_num] = format(values[is_num], scientific=FALSE, trim=TRUE)
vals[upd] = unlist(values)[keys[upd]]
if (any(is.na(vals)))
stop("Template values required but not provided: ",
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-0-util.r
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ test_that("template required key", {

expect_error(fill_template(tmpl, values, required="missing"))
})

test_that("template filling works with vectors", {
tmpl = "{{ var1 }} and {{ var2 }}"
values = c(var1=1, var2=2)

expect_equal(fill_template(tmpl, values), "1 and 2")
})

test_that("template numbers are not converted to sci format", {
tmpl = "this is my {{ template }}"
values = list(template = 100000)

expect_equal(fill_template(tmpl, values), "this is my 100000")
})

test_that("no sci format when passing vectors", {
tmpl = "{{ var1 }} and {{ var2 }}"
values = c(var1=1, var2=1e6)

expect_equal(fill_template(tmpl, values), "1 and 1000000")
})

0 comments on commit fbce8df

Please sign in to comment.