Skip to content

Commit

Permalink
add unit tests on the new digits argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryO committed Dec 8, 2024
1 parent 35ad6dd commit 43dbedc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ meta.integer <- function(x, ...) {
#' @export
#' @importFrom assertthat assert_that is.count
meta.numeric <- function(x, ..., digits) {
assert_that(is.count(digits))
stopifnot("`digits` must be a strict positive integer" = is.count(digits))
x <- signif(x, digits = digits)
list(class = "numeric", digits = as.integer(digits)) -> m
class(m) <- "meta_detail"
Expand Down
61 changes: 61 additions & 0 deletions tests/testthat/test_a_basics.R
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,64 @@ test_that("meta attributes are printed as yaml", {
"class: factor.*\nordered: no"
)
})

test_that("digits works as expected", {
x <- data.frame(
a = c(exp(1), pi), b = c(1.23456789, 1.23456789),
stringsAsFactors = FALSE
)
root <- tempfile("digits")
dir.create(root)
expect_warning(
fn <- write_vc(x, "default", root, sorting = "a"),
"`digits` was not set."
)
expect_equal(
read_vc(fn[1], root), check.attributes = FALSE,
signif(x, 6)
)

expect_is(
fn <- write_vc(x, "digits", root, digits = 4, sorting = "a"),
"character"
)
expect_equal(
read_vc(fn[1], root), check.attributes = FALSE,
signif(x, 4)
)
write_vc(x, "digits", root, digits = 6)
expect_equal(
read_vc(fn[1], root), check.attributes = FALSE,
signif(x, 6)
)

expect_is(
fn <- write_vc(x, "delta", root, digits = c(a = 4, b = 5), sorting = "a"),
"character"
)
expect_equal(
read_vc(fn[1], root), check.attributes = FALSE,
data.frame(a = signif(x$a, 4), b = signif(x$b, 5))
)
expect_is(
fn <- write_vc(x, "delta", root, digits = c(a = 5, b = 4)),
"character"
)
expect_equal(
read_vc(fn[1], root), check.attributes = FALSE,
data.frame(a = signif(x$a, 5), b = signif(x$b, 4))
)

expect_error(
write_vc(x, "faults", root, digits = c(4, 5), sorting = "a"),
"`digits` must be either named"
)
expect_error(
write_vc(x, "faults", root, digits = c(a = 4, b = NA), sorting = "a"),
"`digits` must be a strict positive integer"
)
expect_error(
write_vc(x, "faults", root, digits = c(a = 4, c = 5), sorting = "a"),
"`digits` must contain all numeric variables"
)
})

0 comments on commit 43dbedc

Please sign in to comment.