File: test-s3-methods.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 (45 lines) | stat: -rw-r--r-- 1,236 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
test_that("`$` and `[[` methods don't interfere with R6 operations", {
  # Make sure that these method aren't used anywhere in internal R6 code
  `$.AC`  <- function(x, name) stop("Attempted to use `$.AC`")
  `[[.AC` <- function(x, name) stop("Attempted to use `[[.AC`")

  `$<-.AC`  <- function(x, name, value) stop("Attempted to use `$<-.AC`")
  `[[<-.AC` <- function(x, name, value) stop("Attempted to use `[[<-.AC`")


  AC <- R6Class("AC",
    public = list(
      x = 1,
      gety = function() private$y
    ),
    private = list(
      y = 2,
      y2 = function() y * 2
    ),
    active = list(
      z = function(value) 3
    )
  )

  expect_no_error(a <- AC$new())
  expect_no_error(b <- .subset2(a, "clone")())
})


test_that("Cloning avoids names() S3 method", {
  # A names() method can be defined for a class. We need to avoid it during
  # initialization and cloning -- need to use ls() instead, which does not get
  # dispatched based on class.
  names.A <- function(x) stop("Oops")

  A <- R6Class("A",
    public = list(x = 1),
    private = list(
      deep_clone = function(name, value) value
    )
  )

  expect_silent(a <- A$new())
  expect_silent(a1 <- a$clone())
  expect_silent(a2 <- a$clone(deep = TRUE))
})