tests/testthat/test-style_ratio.R

skip_on_cran()

test_that("correct rounding near one", {
  vec <- c(0.99, 0.999, 1.1)
  expect_equal(
    style_ratio(vec),
    c("0.99", "1.00", "1.10")
  )

  expect_equal(
    seq(0.999, 1.001, by = 0.0005) %>% style_ratio(digits = 3),
    c("0.999", "1.000", "1.000", "1.001", "1.001")
  )
  expect_equal(
    -seq(0.999, 1.001, by = 0.0005) %>% style_ratio(digits = 3),
    c("-0.999", "-1.000", "-1.000", "-1.001", "-1.001")
  )

  expect_false(
    style_ratio(0.99) == "1.0"
  )

  vec2 <- c(one = 0.99, two = 1.1)
  expect_equal(
    attributes(vec2),
    style_ratio(vec2) %>% attributes()
  )
})

Try the gtsummary package in your browser

Any scripts or data that you put into this service are public.

gtsummary documentation built on July 26, 2023, 5:27 p.m.