tests/testthat/test-param_subset.R

context("test-param_subset")

test_that("correlation method test", {
  p <- subset_parameter(
    id = "method",
    default = "pearson",
    values = c("pearson", "spearman", "kendall"),
    description = "Which correlation coefficient to compute."
  )

  expect_is(p, "subset_parameter")
  expect_equal(p$id, "method", check.environment = FALSE)
  expect_equal(p$default, "pearson", check.environment = FALSE)
  expect_equal(p$values, c("pearson", "spearman", "kendall"), check.environment = FALSE)
  expect_equal(p$description, "Which correlation coefficient to compute.", check.environment = FALSE)

  expect_match(as.character(p), "subset")
  expect_match(as.character(p), "method")
  expect_match(as.character(p), "\\{pearson, spearman, kendall\\}")
  expect_match(as.character(p), "default=pearson")

  li <- as.list(p)

  expect_equal(li$type, "subset", check.environment = FALSE)
  expect_equal(li$id, "method", check.environment = FALSE)
  expect_equal(li$default, "pearson", check.environment = FALSE)
  expect_equal(li$values, c("pearson", "spearman", "kendall"), check.environment = FALSE)
  expect_equal(li$description, "Which correlation coefficient to compute.", check.environment = FALSE)

  p2 <- as_parameter(li)
  expect_equal(p2, p, check.environment = FALSE)

  ph <- as_paramhelper(p)
  expect_equal(ph$id, "method", check.environment = FALSE)
  #expect_equal(ph$default %>% unlist(), c("TRUE", "FALSE", "FALSE"), check.environment = FALSE)
  expect_equal(ph$default %>% unlist(), c(1L, 0L, 0L), check.environment = FALSE)
  expect_equal(ph$len, 3, check.environment = FALSE)

  ps <- ParamHelpers::makeParamSet(ph)
  tval <-
    ParamHelpers::generateDesign(par.set = ps, n = 1) %>%
    ParamHelpers::dfRowToList(par.set = ps, i = 1) %>%
    ParamHelpers::trafoValue(par = ps, .)

  expect_equal(names(tval), "method", check.environment = FALSE)
  expect_true(all(tval$method %in% c("spearman", "pearson", "kendall")))
})

test_that("wrong parse fails gracefully", {
  expect_error(subset_parameter(id = "a", default = "b", values = "c", description = "d"), "default is missing 1 element from values")
  expect_error(subset_parameter(id = "a", default = "b", values = c("c", "d"), description = "d"), "default is missing 1 element from values")
  expect_error(subset_parameter(id = 1, default = "b", values = "b", description = "d"), "id is not a character vector")
  expect_error(subset_parameter(id = "a", default = 1, values = "b", description = "d"), "default is not a character vector")
  expect_error(subset_parameter(id = "a", default = "b", values = 1, description = "d"), "values is not a character vector")
  expect_error(subset_parameter(id = "a", default = "b", values = "b", description = 1), "description is not NULL or description is not a character vector")
})

Try the dynparam package in your browser

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

dynparam documentation built on Jan. 5, 2021, 1:06 a.m.