tests/testthat/test_addLeadingZeros.R

### set up test data frames
dat <- data.frame(v1 = c("0","300","e",NA),
                  v2=c("0","90","10000",NA),
                  v3=c("k","kk","kkk",NA),
                  v4=NA,
                  v5=c("0","90","100","1"))
dat <- set.col.type(dat)
addLeadingZerosToCharInt(dat)

test_that("Errors", {
  expect_error(addLeadingZerosToCharInt(1),
               "Argument dat must be a data.frame.")
  expect_error(addLeadingZerosToCharInt(mtcars),
               "All columns in dat need to be of class character.")
})

test_that("Factor vector drop levels", {
  out <- addLeadingZerosToCharInt(dat)

  expect_equal(out[["v5"]], c("000", "090", "100", "001"))
  expect_equal(out[["v1"]], c("0", "300", "e", NA))
})

Try the eatTools package in your browser

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

eatTools documentation built on Nov. 23, 2023, 5:06 p.m.