File: test-cloning-inheritance.R

package info (click to toggle)
r-cran-r6 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: sh: 10; makefile: 2
file content (30 lines) | stat: -rw-r--r-- 1,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
test_that("Subclass can override superclass' cloneable property", {
  # superclass cloneable ---------------------

  Creature <- R6Class("Creature", cloneable = TRUE)

  Sheep <- R6Class("Sheep", inherit = Creature, cloneable = TRUE)
  expect_message(sheep <- Sheep$new(), NA)
  expect_s3_class(sheep$clone(), "Sheep")
  expect_true("clone" %in% names(Creature$public_methods))

  Human <- R6Class("Human", inherit = Creature, cloneable = FALSE)
  expect_message(human <- Human$new(), NA)
  expect_error(human$clone(), "attempt to apply non-function")
  expect_true("clone" %in% names(Creature$public_methods))

  # superclass non-cloneable  ---------------------

  Creature <- R6Class("Creature", cloneable = FALSE)

  Sheep <- R6Class("Sheep", inherit = Creature, cloneable = TRUE)
  expect_message(sheep <- Sheep$new(), "Superclass Creature has cloneable=FALSE, but subclass Sheep has cloneable=TRUE.")
  expect_error(sheep$clone(), "attempt to apply non-function")
  # Make sure that the superclass wasn't inadvertantly modified.
  expect_false("clone" %in% names(Creature$public_methods))

  Human <- R6Class("Human", inherit = Creature, cloneable = FALSE)
  expect_message(human <- Human$new(), NA)
  expect_error(human$clone(), "attempt to apply non-function")
  expect_false("clone" %in% names(Creature$public_methods))
})