tests/testthat/test-enum.R

context("test enums")

test_that("Enum values are equal only to themselves", {
  e1 <- enum(X, Y, Z)
  e2 <- enum(X, Y, Z)
  expect_equal(e1$X, e1$X)
  expect_false(e1$X == e2$X)
  expect_false(e1$X == "X")
})

test_that("Enums values cannot be partially matched or missing", {
  e1 <- enum(FOO, BAR)
  expect_error(e1$FO)
  expect_error(e1$BAZ)
})

test_that("enum_case dispatches to the appropriate branch", {
  e1 <- enum(FOO, BAR)
  expect_equal(123, enum_case(e1$FOO,
    FOO = 123,
    BAR = 321
  ))
})

test_that("enum_case does not evaluate other branches", {
  e1 <- enum(FOO, BAR)
  x <- 0
  fun <- function(val) {
    enum_case(val,
      FOO = 123,
      BAR = (x <<- x+1)
    )
  }
  expect_equal(123, fun(e1$FOO))
  expect_equal(x, 0)
  expect_equal(1, fun(e1$BAR))
  expect_equal(x, 1)
})

test_that("enum_case complains about unmatched values", {
  e1 <- enum(FOO, BAR)
  expect_error({
    enum_case(val,
      FOO = 123,
    )
  })
})

test_that("enum_case complains about unknown values", {
  e1 <- enum(FOO, BAR)
  expect_error({
    enum_case(val,
      FOO = 123,
      BAR = 321,
      BAZ = 99
    )
  })
})

Try the shinyloadtest package in your browser

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

shinyloadtest documentation built on Feb. 11, 2021, 5:05 p.m.