tests/testthat/test-utils.R

test_that("match_arg() works as expected", {

  test <- function(hello = c("choice1", "choice2"), assert = FALSE) {
    res <- match_arg(hello, assert = assert)
    return(res)
  }

  expect_null(test())
  expect_null(test(NULL))
  expect_equal(test("choice2"), "choice2")
  expect_equal(test("invalid"), "invalid")
  expect_error(test("invalid", assert = TRUE))
  expect_error(test("choice1", assert = TRUE), NA)

})

test_that("assert_route_handler() works as expected", {

  expect_error(assert_route_handler(function(a, b, c) NULL))
  expect_error(assert_route_handler(function(request,
                                             response,
                                             keys, ...) NULL), NA)

})

test_that("when() works", {

  1 %>%
    when(TRUE, . + 1) %>%
    expect_equal(2)

  1 %>%
    when(FALSE, . + 1) %>%
    expect_equal(1)

})

test_that("add_attr() works", {

  list() %>%
    add_attr(attribute = "option") %>%
    attributes() %>%
    expect_identical(list(attribute = "option"))

})

test_that("assert_tags works as expected", {

  expect_class(assert_tags(tags$a()), "shiny.tag")
  expect_error(assert_tags("wrong"))

})
tjpalanca/webtools documentation built on Dec. 23, 2021, 11 a.m.