tests/testthat/test-scalar.R

context("Scalar assertions")

test_that("is.scalar works correctly", {
  expect_true(is.scalar(1))
  expect_true(is.scalar(-1))
  expect_true(is.scalar(1.5))
  expect_false(is.scalar(1:5))
  expect_true(is.scalar('a'))
  expect_false(is.scalar(c('a', 'b')))
  expect_true(is.scalar(TRUE))
  expect_false(is.scalar(c(TRUE, FALSE)))
  expect_false(is.scalar(NULL))
  expect_true(is.scalar(NA))
  expect_true(is.scalar(Inf))
  expect_true(is.scalar(-Inf))
})

test_that("is.string works correctly", {
  expect_false(is.string(1))
  expect_true(is.string('a'))
  expect_false(is.string(c('a', 'b')))
  expect_false(is.string(TRUE))
  expect_false(is.string(NULL))
  expect_false(is.string(NA))
  expect_false(is.string(Inf))
  expect_false(is.string(-Inf))
})

test_that("is.number works correctly", {
  expect_true(is.number(1))
  expect_true(is.number(-1))
  expect_true(is.number(1.5))
  expect_false(is.number(1:5))
  expect_false(is.number('a'))
  expect_false(is.number(TRUE))
  expect_false(is.number(NULL))
  expect_false(is.number(NA))
  expect_true(is.number(Inf))
  expect_true(is.number(-Inf))
})

test_that("is.flag works correctly", {
  expect_false(is.flag(1))
  expect_false(is.flag('a'))
  expect_true(is.flag(TRUE))
  expect_true(is.flag(FALSE))
  expect_false(is.flag(c(TRUE, FALSE)))
  expect_false(is.flag(NULL))
  expect_equal(is.flag(NA), is.logical(NA)) # not obvious
  expect_false(is.flag(Inf))
  expect_false(is.flag(-Inf))
})

test_that("is.count works correctly", {
  expect_true(is.count(1))
  expect_false(is.count(-1))
  expect_false(is.count(1.5))
  expect_false(is.count(1:5))
  expect_false(is.count('a'))
  expect_false(is.count(TRUE))
  expect_false(is.count(NULL))
  expect_false(is.count(NA))
  expect_false(is.count(NA_real_))
  expect_false(is.count(NA_integer_))
  expect_false(is.count(NaN))
  expect_true(is.count(Inf))
  expect_false(is.count(-Inf))
  expect_false(is.count(1e10 + 0.0001))
  expect_false(is.count(1e10 - 0.1))
})

Try the assertthat package in your browser

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

assertthat documentation built on May 1, 2019, 10:25 p.m.