File: test-portable-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 (416 lines) | stat: -rw-r--r-- 10,773 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
test_that("Inheritance", {
  AC <- R6Class("AC",
    portable = TRUE,
    public = list(
      x = 0,
      z = 0,
      initialize = function(x) self$x <- x,
      getx = function() self$x,
      getx2 = function() self$x*2,
      getprivateA = function() private
    ),
    private = list(
      getz = function() self$z,
      getz2 = function() self$z*2
    ),
    active = list(
      x2 = function(value) {
        if (missing(value)) return(self$x * 2)
        else self$x <- value/2
      },
      x3 = function(value) {
        if (missing(value)) return(self$x * 3)
        else self$x <- value/3
      }
    )
  )
  BC <- R6Class("BC",
    portable = TRUE,
    inherit = AC,
    public = list(
      y = 0,
      z = 3,
      initialize = function(x, y) {
        super$initialize(x)
        self$y <- y
      },
      getx = function() self$x + 10,
      getprivateB = function() private
    ),
    private = list(
      getz = function() self$z + 10
    ),
    active = list(
      x2 = function(value) {
        if (missing(value)) return(self$x + 2)
        else self$x <- value-2
      }
    )
  )
  B <- BC$new(1, 2)

  # Environment checks
  eval_env <- environment(B$getx)
  super_bind_env <- eval_env$super
  super_eval_env <- environment(super_bind_env$getx)

  expect_identical(parent.env(super_bind_env), emptyenv())
  expect_identical(parent.env(super_eval_env), environment())
  expect_identical(super_eval_env$self, B)
  expect_identical(super_eval_env$private, B$getprivateA())
  expect_identical(B$getprivateA(), B$getprivateB())

  # Overridden public method
  expect_identical(eval_env, environment(B$getx))
  # Inherited public method
  environment(B$getx2)
  expect_identical(B, environment(B$getx2)$self)
  # Overridden private method
  expect_identical(eval_env, environment(B$getprivateA()$getz))
  # Inherited private method - should have same eval env as inherited public
  expect_identical(environment(B$getx2), environment(B$getprivateA()$getz2))

  # Behavioral tests
  # Overriding literals
  expect_identical(B$x, 1)
  expect_identical(B$y, 2)
  expect_identical(B$z, 3) # Subclass value overrides superclass value
  # Methods
  expect_identical(B$getx(), 11)                # Overridden public method
  expect_identical(B$getx2(), 2)                # Inherited public method
  expect_identical(B$getprivateA()$getz(), 13)  # Overriden private method
  expect_identical(B$getprivateA()$getz2(), 6)  # Inherited private method

  # Active bindings
  expect_identical(B$x2, 3) # Overridden
  expect_identical(B$x3, 3) # Inherited

  # Classes
  expect_identical(class(B), c("BC", "AC", "R6"))
})


test_that("Inheritance: superclass methods", {
  AC <- R6Class("AC",
    portable = TRUE,
    public = list(
      x = 0,
      initialize = function() {
        self$inc_x()
        private$inc_y()
        self$incz
      },
      inc_x = function() self$x <- self$x + 1,
      inc = function(val) val + 1,
      pinc = function(val) private$priv_inc(val), # Call private inc method
      gety = function() private$y,
      z = 0
    ),
    private = list(
      y = 0,
      inc_y = function() private$y <- private$y + 1,
      priv_inc = function(val) val + 1
    ),
    active = list(
      incz = function(value) {
        self$z <- z + 1
      }
    )
  )
  BC <- R6Class("BC",
    portable = TRUE,
    inherit = AC,
    public = list(
      inc_x = function() self$x <- self$x + 2,
      inc = function(val) super$inc(val) + 20
    ),
    private = list(
      inc_y = function() private$y <- private$y + 2,
      priv_inc = function(val) super$priv_inc(val) + 20
    ),
    active = list(
      incz = function(value) {
        self$z <- self$z + 2
      }
    )
  )
  B <- BC$new()

  # Testing overrides
  expect_identical(B$x, 2)       # Public
  expect_identical(B$gety(), 2)  # Private
  expect_identical(B$z, 2)       # Active
  # Calling superclass methods
  expect_identical(B$inc(0), 21)
  expect_identical(B$pinc(0), 21)


  # Multi-level inheritance
  CC <- R6Class("CC",
    portable = TRUE,
    inherit = BC,
    public = list(
      inc_x = function() self$x <- self$x + 3,
      inc = function(val) super$inc(val) + 300
    ),
    private = list(
      inc_y = function() private$y <- private$y + 3,
      priv_inc = function(val) super$priv_inc(val) + 300
    ),
    active = list(
      incz = function(value) {
        self$z <- self$z + 3
      }
    )
  )
  C <- CC$new()

  # Testing overrides
  expect_identical(C$x, 3)       # Public
  expect_identical(C$gety(), 3)  # Private
  expect_identical(C$z, 3)       # Active
  # Calling superclass methods (two levels)
  expect_identical(C$inc(0), 321)
  expect_identical(C$pinc(0), 321)

  # Classes
  expect_identical(class(C), c("CC", "BC", "AC", "R6"))
})


test_that("Inheritance: enclosing environments for super$ methods", {
  encA <- new.env()
  encB <- new.env()
  encC <- new.env()

  encA$n <- 1
  encB$n <- 20
  encC$n <- 300

  AC <- R6Class("AC",
    portable = TRUE,
    parent_env = encA,
    public = list(
      x = 0,
      initialize = function() {
        self$x <- self$get_n()
      },
      get_n = function() n,
      priv_get_n = function(val) private$get_n_priv()
    ),
    private = list(
      get_n_priv = function() n
    ),
    active = list(
      active_get_n = function() n
    )
  )
  A <- AC$new()
  expect_identical(A$x, 1)
  expect_identical(A$get_n(), 1)
  expect_identical(A$priv_get_n(), 1)
  expect_identical(A$active_get_n, 1)

  BC <- R6Class("BC",
    portable = TRUE,
    parent_env = encB,
    inherit = AC,
    public = list(
      x = 0,
      initialize = function() {
        super$initialize()
      },
      get_n = function() n + super$get_n(),
      priv_get_n = function(val) private$get_n_priv()
    ),
    private = list(
      get_n_priv = function() n + super$get_n_priv()
    ),
    active = list(
      active_get_n = function() n + super$active_get_n
    )
  )
  B <- BC$new()
  expect_identical(B$x, 21)
  expect_identical(B$get_n(), 21)
  expect_identical(B$priv_get_n(), 21)
  expect_identical(B$active_get_n, 21)

  CC <- R6Class("CC",
    portable = TRUE,
    parent_env = encC,
    inherit = BC,
    public = list(
      x = 0,
      initialize = function() {
        super$initialize()
      },
      get_n = function() n + super$get_n(),
      priv_get_n = function(val) private$get_n_priv()
    ),
    private = list(
      get_n_priv = function() n + super$get_n_priv()
    ),
    active = list(
      active_get_n = function() n + super$active_get_n
    )
  )
  C <- CC$new()
  expect_identical(C$x, 321)
  expect_identical(C$get_n(), 321)
  expect_identical(C$priv_get_n(), 321)
  expect_identical(C$active_get_n, 321)
})


test_that("Inheritance: enclosing environments for inherited methods", {
  encA <- new.env()
  encB <- new.env()
  encC <- new.env()

  encA$n <- 1
  encB$n <- 20
  encC$n <- 300

  AC <- R6Class("AC",
    portable = TRUE,
    parent_env = encA,
    public = list(
      get_n = function() n
    )
  )
  A <- AC$new()
  expect_identical(A$get_n(), 1)

  BC <- R6Class("BC",
    portable = TRUE,
    parent_env = encB,
    inherit = AC
  )
  B <- BC$new()
  # Since this inherits A's get_n() method, it should also inherit the
  # environment in which get_n() runs. This is necessary for inherited methods
  # to find methods from the correct namespace.
  expect_identical(B$get_n(), 1)

  CC <- R6Class("CC",
    portable = TRUE,
    parent_env = encC,
    inherit = BC,
    public = list(
      get_n = function() n + super$get_n()
    )
  )
  C <- CC$new()
  # When this calls super$get_n(), it should get B's version of get_n(), which
  # should in turn run in A's environment, returning 1. Add C's value of n, and
  # the total is 301.
  expect_identical(C$get_n(), 301)
})


test_that("Inheritance hierarchy for super$ methods", {
  AC <- R6Class("AC", portable = TRUE,
    public = list(n = function() 0 + 1)
  )
  expect_identical(AC$new()$n(), 1)

  BC <- R6Class("BC", portable = TRUE,
    public = list(n = function() super$n() + 10),
    inherit = AC
  )
  expect_identical(BC$new()$n(), 11)

  CC <- R6Class("CC", portable = TRUE,
    inherit = BC
  )
  # This should equal 11 because it inherits BC's n(), which adds 1 to AC's n()
  expect_identical(CC$new()$n(), 11)

  # Skipping one level of inheritance ---------------------------------
  AC <- R6Class("AC", portable = TRUE,
    public = list(n = function() 0 + 1)
  )
  expect_identical(AC$new()$n(), 1)

  BC <- R6Class("BC", portable = TRUE,
    inherit = AC
  )
  expect_identical(BC$new()$n(), 1)

  CC <- R6Class("CC", portable = TRUE,
    public = list(n = function() super$n() + 100),
    inherit = BC
  )
  # This should equal 101 because BC inherits AC's n()
  expect_identical(CC$new()$n(), 101)

  DC <- R6Class("DC", portable = TRUE,
    inherit = CC
  )
  # This should equal 101 because DC inherits CC's n(), and BC inherits AC's n()
  expect_identical(DC$new()$n(), 101)

  # Skipping two level of inheritance ---------------------------------
  AC <- R6Class("AC", portable = TRUE,
    public = list(n = function() 0 + 1)
  )
  expect_identical(AC$new()$n(), 1)

  BC <- R6Class("BC", portable = TRUE, inherit = AC)
  expect_identical(BC$new()$n(), 1)

  CC <- R6Class("CC", portable = TRUE, inherit = BC)
  expect_identical(CC$new()$n(), 1)
})


test_that("sub and superclass must both be portable or non-portable", {
  AC <- R6Class("AC", portable = FALSE, public = list(x=1))
  BC <- R6Class("BC", portable = TRUE, inherit = AC)
  expect_error(BC$new())

  AC <- R6Class("AC", portable = TRUE, public = list(x=1))
  BC <- R6Class("BC", portable = FALSE, inherit = AC)
  expect_error(BC$new())
})


test_that("Inheritance is dynamic", {
  AC <- R6Class("AC",
    public = list(x = 1, initialize = function() self$x <<- self$x + 10)
  )
  BC <- R6Class("BC", inherit = AC)
  expect_identical(BC$new()$x, 11)

  AC <- R6Class("AC",
    public = list(x = 2, initialize = function() self$x <<- self$x + 20)
  )
  expect_identical(BC$new()$x, 22)

  # BC doesn't contain AC, and it has less stuff in it, so it should be smaller
  # than AC.
  if (requireNamespace("lobstr", quietly = TRUE)) {
    expect_true(lobstr::obj_size(BC) < lobstr::obj_size(AC))
  }
})


test_that("Private env is created when all private members are inherited", {
  # Private contains fields only
  AC <- R6Class("AC",
    public = list(getx = function() private$x),
    private = list(x = 1)
  )
  BC <- R6Class("BC", inherit = AC)
  expect_identical(BC$new()$getx(), 1)


  # Private contains functions only
  AC <- R6Class("AC",
    public = list(getx = function() private$x()),
    private = list(x = function() 1)
  )
  BC <- R6Class("BC", inherit = AC)
  expect_identical(BC$new()$getx(), 1)
})