tests/testthat/test-inherits.R

test_that("it works", {
  foo1 <- new_class("foo1")
  foo2 <- new_class("foo2", parent = foo1)

  expect_true(S7_inherits(foo1(), NULL))
  expect_true(S7_inherits(foo1(), foo1))
  expect_true(S7_inherits(foo2(), foo1))
  expect_false(S7_inherits(foo1(), foo2))
  expect_false(S7_inherits(1, NULL))
})

test_that("checks that input is a class", {
  expect_snapshot(S7_inherits(1:10, "x"), error = TRUE)
})

test_that("throws informative error", {
  expect_snapshot(error = TRUE, {
    foo1 <- new_class("foo1", package = NULL)
    foo2 <- new_class("foo2", package = NULL)
    check_is_S7(foo1(), foo2)
  })
  expect_snapshot(check_is_S7("a"), error = TRUE)
})

Try the S7 package in your browser

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

S7 documentation built on April 3, 2025, 10:50 p.m.