tests/testthat/test-excel.R

context("Excel like functions")

test_that("right function works", {
  x <- c("test", "someting", "very", "closely")
  expect_equal(right(x[1], 1), "t")
  expect_equal(right(x[2], 2), "ng")
  expect_equal(right(x[3], 3), "ery")
  expect_equal(right(x[4], 4), "sely")
  expect_equal(right(x[1], 4), "test")
  expect_equal(right(x[1], 5), "test")
  expect_equal(right(x, 1), c("t", "g", "y", "y"))
  expect_equal(right(x[2], 3), c("ing"))
  expect_equal(right(x, 3), c("est", "ing", "ery", "ely"))
  expect_equal(right(x, 30), c("test", "someting", "very", "closely"))
  expect_equal(right("abcdefg", 5), "cdefg")
  expect_error(right(x[1], "a"), "chars is not a numeric or integer vector")
  expect_error(right(1:2, 2), "vec1 is not a character vector")
})

test_that("left function works", {
  x <- c("test", "someting", "very", "closely")
  expect_equal(left(x[1], 1), "t")
  expect_equal(left(x[2], 2), "so")
  expect_equal(left(x[3], 3), "ver")
  expect_equal(left(x[4], 4), "clos")
  expect_equal(left(x[1], 4), "test")
  expect_equal(left(x[1], 5), "test")
  expect_equal(left(x, 1), c("t", "s", "v", "c"))
  expect_equal(left(x, 3), c("tes", "som", "ver", "clo"))
  expect_equal(left(x, 30), c("test", "someting", "very", "closely"))
  expect_error(left(x[1], "a"), "chars is not a numeric or integer vector")
  expect_error(left(1:10, 2), "vec1 is not a character vector")
})

test_that("ifna function works", {
  text <- c("there", "once", "was", "a", NA, "from", NA)
  number <- c(1:5, NA, 7:10, NA)
  expect_equal(ifna(text, "boston"), c("there", "once", "was", "a", "boston", "from", "boston"))
  expect_equal(ifna(number, 999), c(1:5, 999, 7:10, 999))
  expect_equal(ifna(number, "monkey"), c("1", "2", "3", "4", "5", "monkey", "7", "8", "9", "10", "monkey"))
  expect_equal(ifna(text, 5), c("there", "once", "was", "a", "5", "from", "5"))
  expect_equal(ifna(c(1:10), 999), c(1:10))
  # expect_error(ifna("a", "b"), "x is only 1 element long.")
})
MattKelliher-Gibson/prepr documentation built on March 21, 2020, 3:16 p.m.