tests/testthat/test-construct.R

test_that("construct() and deconstruct() work for S3 (functions)", {

  car <- function(.x = list(), name, mpg, cyl, .class = character()) {
    .x$name <- name
    .x$mpg  <- mpg
    .x$cyl  <- cyl
    structure(
      .x,
      class = c(.class, "car")
    )
  }

  test_construct(test_cars, "car")
  test_construct(test_cars, car, "car")

  rm(car)

})

test_that("construct() and deconstruct() work for S4", {

  setClass(
    "Car",
    slots = c(
      name = "character",
      mpg  = "numeric",
      cyl  = "numeric"
    )
  )

  test_construct(test_cars, "Car")
  test_construct(test_cars, getClass("Car"), "Car")
  methods::removeClass("Car")

})

test_that("construct() and deconstruct() work for R6", {

  Car <- R6Class(
    classname = "Car",
    public = list(
      initialize = function(name, mpg, cyl) {
        self$name <- name
        self$mpg  <- mpg
        self$cyl  <- cyl
      },
      name = NULL,
      mpg  = NULL,
      cyl  = NULL
    )
  )

  test_construct(test_cars, "Car")
  test_construct(test_cars, Car, Car$classname)
  rm(Car)

})
tjpalanca/dbtools documentation built on Oct. 7, 2021, 6:43 a.m.