| 52960 |
jmc |
1 |
## simple call, only field names
|
|
|
2 |
fg <- setRefClass("foo", c("bar", "flag"))
|
|
|
3 |
f1<- new("foo")
|
|
|
4 |
f1$bar
|
|
|
5 |
f1 <- fg$new(flag = "testing")
|
|
|
6 |
f1$bar <- 1
|
|
|
7 |
stopifnot(identical(f1$bar, 1))
|
|
|
8 |
fg$methods(showAll = function() c(bar, flag))
|
|
|
9 |
stopifnot(all.equal(f1$showAll(), c(1, "testing")))
|
|
|
10 |
|
| 56045 |
jmc |
11 |
fg <- setRefClass("foo", list(bar = "numeric", flag = "character",tag = "ANY"),
|
| 52960 |
jmc |
12 |
methods = list(
|
| 52816 |
jmc |
13 |
addToBar = function(incr) {
|
| 52960 |
jmc |
14 |
b = bar + incr
|
|
|
15 |
bar <<- b
|
| 52816 |
jmc |
16 |
b
|
|
|
17 |
}
|
|
|
18 |
))
|
|
|
19 |
ff = new("foo", bar = 1.5)
|
|
|
20 |
stopifnot(identical(ff$bar, 1.5))
|
|
|
21 |
ff$bar <- pi
|
|
|
22 |
stopifnot(identical(ff$bar, pi))
|
| 52903 |
jmc |
23 |
## test against generator
|
| 52816 |
jmc |
24 |
|
| 52903 |
jmc |
25 |
f2 <- fg$new(bar = pi)
|
|
|
26 |
## identical does not return TRUE if *contents* of env are identical
|
|
|
27 |
stopifnot(identical(ff$bar, f2$bar), identical(ff$flag, f2$flag))
|
|
|
28 |
|
| 52960 |
jmc |
29 |
f2$flag <- "standard flag"
|
|
|
30 |
stopifnot(identical(f2$flag, "standard flag"))
|
| 52903 |
jmc |
31 |
|
| 52960 |
jmc |
32 |
## fg$lock("flag")
|
|
|
33 |
## tryCatch(f2$flag <- "other", error = function(e)e)
|
| 52816 |
jmc |
34 |
|
| 52960 |
jmc |
35 |
|
|
|
36 |
## add some accessor methods
|
|
|
37 |
fg$accessors("bar")
|
|
|
38 |
|
| 52816 |
jmc |
39 |
ff$setBar(1:3)
|
| 52960 |
jmc |
40 |
stopifnot(identical(ff$getBar(), 1:3))
|
| 52816 |
jmc |
41 |
|
|
|
42 |
ff$getBar()
|
|
|
43 |
stopifnot(all.equal(ff$addToBar(1), 2:4))
|
|
|
44 |
|
| 52960 |
jmc |
45 |
|
| 52903 |
jmc |
46 |
## Add a method
|
|
|
47 |
fg$methods(barTimes = function(x) {
|
|
|
48 |
"This method multiples field bar by argument x
|
|
|
49 |
and this string is self-documentation"
|
|
|
50 |
setBar(getBar() * x)})
|
|
|
51 |
|
|
|
52 |
ffbar <- ff$getBar()
|
|
|
53 |
ff$barTimes(10)
|
|
|
54 |
stopifnot(all.equal(ffbar * 10, ff$getBar()))
|
|
|
55 |
ff$barTimes(.1)
|
|
|
56 |
|
| 52846 |
jmc |
57 |
## inheritance. redefines flag so should fail:
|
| 52903 |
jmc |
58 |
stopifnot(is(tryCatch(setRefClass("foo2", list(b2 = "numeric", flag = "complex"),
|
| 52816 |
jmc |
59 |
contains = "foo",
|
| 52903 |
jmc |
60 |
refMethods = list(addBoth = function(incr) {
|
| 52816 |
jmc |
61 |
addToBar(incr) #uses inherited class method
|
|
|
62 |
setB2(getB2() + incr)
|
| 52903 |
jmc |
63 |
})),
|
|
|
64 |
error = function(e)e), "error"))
|
| 52846 |
jmc |
65 |
## but with flag as a subclass of "character", should work
|
| 56045 |
jmc |
66 |
## Also subclasses "tag" which had class "ANY before
|
| 52871 |
jmc |
67 |
setClass("ratedChar", contains = "character", representation(score = "numeric"))
|
| 56045 |
jmc |
68 |
foo2 <- setRefClass("foo2", list(b2 = "numeric", flag = "ratedChar", tag = "numeric"),
|
| 52846 |
jmc |
69 |
contains = "foo",
|
| 52960 |
jmc |
70 |
methods = list(addBoth = function(incr) {
|
| 52846 |
jmc |
71 |
addToBar(incr) #uses inherited class method
|
| 52960 |
jmc |
72 |
b2<<- b2 + incr
|
| 52816 |
jmc |
73 |
}))
|
| 52960 |
jmc |
74 |
## now lock the flag field; should still allow one write
|
|
|
75 |
foo2$lock("flag")
|
| 56045 |
jmc |
76 |
f2 <- foo2$new(bar = -3, flag = as("ANY", "ratedChar"), b2 = ff$bar, tag = 1.5)
|
| 52960 |
jmc |
77 |
## but not a second one
|
|
|
78 |
stopifnot(is(tryCatch(f2$flag <- "Try again",
|
|
|
79 |
error = function(e)e), "error"))
|
|
|
80 |
f22 <- foo2$new(bar = f2$bar)
|
|
|
81 |
## same story if assignment follows the initialization
|
|
|
82 |
f22$flag <- f2$flag
|
|
|
83 |
stopifnot(is(tryCatch(f22$flag <- "Try again",
|
|
|
84 |
error = function(e)e), "error"))
|
|
|
85 |
## Exporting superclass object
|
| 52903 |
jmc |
86 |
f22 <- fg$new(bar = f2$bar, flag = f2$flag)
|
|
|
87 |
f2e <- f2$export("foo")
|
|
|
88 |
stopifnot(identical(f2e$bar, f22$bar), identical(f2e$flag, f22$flag),
|
|
|
89 |
identical(class(f2e), class(f22)))
|
| 52871 |
jmc |
90 |
stopifnot(identical(f2$flag, as("ANY", "ratedChar")), identical(f2$bar, -3),
|
| 52816 |
jmc |
91 |
all.equal(f2$b2, 2:4+0))
|
|
|
92 |
f2$addBoth(-1)
|
|
|
93 |
stopifnot(all.equal(f2$bar, -4), all.equal(f2$b2, 1:3+0))
|
|
|
94 |
|
| 52960 |
jmc |
95 |
## test callSuper()
|
|
|
96 |
setRefClass("foo3", fields = list(flag2 = "ratedChar"),
|
|
|
97 |
contains = "foo2",
|
|
|
98 |
methods = list(addBoth = function(incr) {
|
| 52846 |
jmc |
99 |
callSuper(incr)
|
| 52960 |
jmc |
100 |
flag2 <<- as(paste(flag, paste(incr, collapse = ", "), sep = "; "), "ratedChar")
|
| 52816 |
jmc |
101 |
incr
|
|
|
102 |
}))
|
|
|
103 |
|
| 52960 |
jmc |
104 |
f2 <- foo2$new(bar = -3, flag = as("ANY", "ratedChar"), b2 = 1:3)
|
| 52846 |
jmc |
105 |
f3 <- new("foo3")
|
|
|
106 |
f3$import(f2)
|
|
|
107 |
stopifnot(all.equal(f3$b2, f2$b2), all.equal(f3$bar, f2$bar), all.equal(f3$flag, f2$flag))
|
| 52871 |
jmc |
108 |
f3$addBoth(1)
|
| 52960 |
jmc |
109 |
stopifnot(all.equal(f3$bar, -2), all.equal(f3$b2, 2:4+0),
|
|
|
110 |
all.equal(f3$flag2, as("ANY; 1", "ratedChar")))
|
| 52871 |
jmc |
111 |
|
| 52960 |
jmc |
112 |
## but the import should have used up the one write for $flag
|
|
|
113 |
stopifnot(is(tryCatch(f3$flag <- "Try again",
|
|
|
114 |
error = function(e)e), "error"))
|
|
|
115 |
|
|
|
116 |
## a class with an initialize method, and an extra slot
|
|
|
117 |
setOldClass(c("simple.list", "list"))
|
|
|
118 |
fg4 <- setRefClass("foo4",
|
|
|
119 |
contains = "foo2",
|
|
|
120 |
methods = list(
|
|
|
121 |
initialize = function(...) {
|
| 55779 |
jmc |
122 |
.self$initFields(...)
|
|
|
123 |
.self@made <<- R.version
|
| 52960 |
jmc |
124 |
.self
|
|
|
125 |
}),
|
|
|
126 |
representation = list(made = "simple.list")
|
|
|
127 |
)
|
|
|
128 |
|
|
|
129 |
f4 <- new("foo4", flag = "another test", bar = 1:3)
|
|
|
130 |
stopifnot(identical(f4@made, R.version))
|
| 52985 |
jmc |
131 |
|
| 56036 |
jmc |
132 |
## a trivial class with no fields, using fields = list(), failed up to rev 56035
|
|
|
133 |
foo5 <- setRefClass("foo5", fields = list(),
|
|
|
134 |
methods = list(bar = function(test) paste("*",test,"*")))
|
|
|
135 |
|
|
|
136 |
f5 <- foo5$new()
|
|
|
137 |
stopifnot(identical( f5$bar("xxx"), paste("*","xxx", "*")))
|
|
|
138 |
|
|
|
139 |
|
| 52985 |
jmc |
140 |
## simple active binding test
|
|
|
141 |
abGen <- setRefClass("ab",
|
|
|
142 |
fields = list(a = "ANY",
|
|
|
143 |
b = function(x) if(missing(x)) a else {a <<- x; x}))
|
|
|
144 |
|
|
|
145 |
ab1 <- abGen$new(a = 1)
|
|
|
146 |
|
|
|
147 |
stopifnot(identical(ab1$a, 1), identical(ab1$b, 1))
|
|
|
148 |
|
|
|
149 |
ab1$b <- 2
|
|
|
150 |
|
|
|
151 |
stopifnot(identical(ab1$a, 2), identical(ab1$b, 2))
|
| 53068 |
jmc |
152 |
|
|
|
153 |
## a simple editor for matrix objects. Method $edit() changes some
|
|
|
154 |
## range of values; method $undo() undoes the last edit.
|
|
|
155 |
mEditor <- setRefClass("matrixEditor",
|
|
|
156 |
fields = list( data = "matrix",
|
|
|
157 |
edits = "list"),
|
|
|
158 |
methods = list(
|
|
|
159 |
edit = function(i, j, value) {
|
|
|
160 |
## the following string documents the edit method
|
|
|
161 |
'Replaces the range [i, j] of the
|
|
|
162 |
object by value.
|
|
|
163 |
'
|
|
|
164 |
backup <-
|
|
|
165 |
list(i, j, data[i,j])
|
|
|
166 |
data[i,j] <<- value
|
|
|
167 |
edits <<- c(list(backup),
|
|
|
168 |
edits)
|
|
|
169 |
invisible(value)
|
|
|
170 |
},
|
|
|
171 |
undo = function() {
|
|
|
172 |
'Undoes the last edit() operation
|
|
|
173 |
and update the edits field accordingly.
|
|
|
174 |
'
|
|
|
175 |
prev <- edits
|
|
|
176 |
if(length(prev)) prev <- prev[[1]]
|
|
|
177 |
else stop("No more edits to undo")
|
|
|
178 |
edit(prev[[1]], prev[[2]], prev[[3]])
|
|
|
179 |
## trim the edits list
|
|
|
180 |
length(edits) <<- length(edits) - 2
|
|
|
181 |
invisible(prev)
|
|
|
182 |
}
|
|
|
183 |
))
|
|
|
184 |
xMat <- matrix(1:12,4,3)
|
|
|
185 |
xx <- mEditor$new(data = xMat)
|
|
|
186 |
xx$edit(2, 2, 0)
|
|
|
187 |
xx$data
|
|
|
188 |
xx$undo()
|
|
|
189 |
mEditor$help("undo")
|
|
|
190 |
stopifnot(all.equal(xx$data, xMat))
|
|
|
191 |
|
|
|
192 |
## add a method to save the object
|
|
|
193 |
mEditor$methods(
|
|
|
194 |
save = function(file) {
|
|
|
195 |
'Save the current object on the file
|
|
|
196 |
in R external object format.
|
|
|
197 |
'
|
|
|
198 |
base::save(.self, file = file)
|
|
|
199 |
}
|
|
|
200 |
)
|
|
|
201 |
|
|
|
202 |
tf <- tempfile()
|
|
|
203 |
xx$save(tf) #$
|
|
|
204 |
load(tf)
|
|
|
205 |
unlink(tf)
|
|
|
206 |
stopifnot(identical(xx$data, .self$data))
|
|
|
207 |
|
|
|
208 |
markViewer <- ""
|
|
|
209 |
setMarkViewer <- function(what)
|
|
|
210 |
markViewer <<- what
|
|
|
211 |
|
|
|
212 |
## Inheriting a reference class: a matrix viewer
|
|
|
213 |
mv <- setRefClass("matrixViewer",
|
|
|
214 |
fields = c("viewerDevice", "viewerFile"),
|
|
|
215 |
contains = "matrixEditor",
|
|
|
216 |
methods = list( view = function() {
|
|
|
217 |
dd <- dev.cur(); dev.set(viewerDevice)
|
|
|
218 |
devAskNewPage(FALSE)
|
|
|
219 |
matplot(data, main = paste("After",length(edits),"edits"))
|
|
|
220 |
dev.set(dd)},
|
|
|
221 |
edit = # invoke previous method, then replot
|
|
|
222 |
function(i, j, value) {
|
|
|
223 |
callSuper(i, j, value)
|
|
|
224 |
view()
|
|
|
225 |
}))
|
|
|
226 |
|
|
|
227 |
## initialize and finalize methods
|
| 53470 |
jmc |
228 |
mv$methods( initialize = function(file = "./matrixView.pdf", ...) {
|
|
|
229 |
viewerFile <<- file
|
| 53068 |
jmc |
230 |
pdf(viewerFile)
|
|
|
231 |
viewerDevice <<- dev.cur()
|
|
|
232 |
message("Plotting to ", viewerFile)
|
|
|
233 |
dev.set(dev.prev())
|
|
|
234 |
setMarkViewer("ON")
|
|
|
235 |
initFields(...)
|
|
|
236 |
},
|
|
|
237 |
finalize = function() {
|
|
|
238 |
dev.off(viewerDevice)
|
|
|
239 |
setMarkViewer("OFF")
|
|
|
240 |
})
|
|
|
241 |
|
| 53669 |
jmc |
242 |
ff = mv$new( data = xMat)
|
| 53068 |
jmc |
243 |
stopifnot(identical(markViewer, "ON")) # check initialize
|
|
|
244 |
ff$edit(2,2,0)
|
|
|
245 |
ff$data
|
|
|
246 |
ff$undo()
|
|
|
247 |
stopifnot(all.equal(ff$data, xMat))
|
|
|
248 |
rm(ff)
|
|
|
249 |
gc()
|
|
|
250 |
stopifnot(identical(markViewer, "OFF")) #check finalize
|
| 53212 |
jmc |
251 |
|
| 53669 |
jmc |
252 |
## tests of copying
|
|
|
253 |
viewerPlus <- setRefClass("viewerPlus",
|
|
|
254 |
fields = list( text = "character",
|
|
|
255 |
viewer = "matrixViewer"))
|
|
|
256 |
ff <- mv$new( data = xMat)
|
|
|
257 |
v1 <- viewerPlus$new(text = letters, viewer = ff)
|
|
|
258 |
v2 <- v1$copy()
|
|
|
259 |
v3 <- v1$copy(TRUE)
|
|
|
260 |
v2$text <- "Hello, world"
|
|
|
261 |
v2$viewer$data <- t(xMat) # change a field in v2$viewer
|
|
|
262 |
v3$text <- LETTERS
|
|
|
263 |
v3$viewer <- mv$new( data = matrix(nrow=1,ncol=1))
|
|
|
264 |
## with a deep copy all is protected, with a shallow copy
|
|
|
265 |
## the environment of a copied field remains the same,
|
|
|
266 |
## but replacing the whole field should be local
|
|
|
267 |
stopifnot(identical(v1$text, letters),
|
|
|
268 |
identical(v1$viewer, ff),
|
|
|
269 |
identical(v2$text, "Hello, world"))
|
|
|
270 |
v3 <- v1$copy(TRUE)
|
|
|
271 |
v3$viewer$data <- t(xMat) # should modify v1$viewer as well
|
|
|
272 |
stopifnot(identical(v1$viewer$data, t(xMat)))
|
|
|
273 |
|
| 53693 |
jmc |
274 |
## the field() method
|
|
|
275 |
stopifnot(identical(v1$text, v1$field("text")))
|
|
|
276 |
v1$field("text", "Now is the time")
|
|
|
277 |
stopifnot(identical(v1$field("text"), "Now is the time"))
|
|
|
278 |
|
|
|
279 |
## setting a non-existent field, or a method, should throw an error
|
|
|
280 |
stopifnot(is(tryCatch(v1$field("foobar", 0), error = function(e)e), "error"),
|
|
|
281 |
is(tryCatch(v1$field("copy", 0), error = function(e)e), "error") )
|
|
|
282 |
|
| 53669 |
jmc |
283 |
## the methods to extract class definition and generator
|
| 53693 |
jmc |
284 |
stopifnot(identical(v3$getRefClass()$def, getRefClass("viewerPlus")$def),
|
|
|
285 |
identical(v3$getClass(), getClass("viewerPlus")))
|
| 53669 |
jmc |
286 |
|
| 53212 |
jmc |
287 |
## deal correctly with inherited methods and overriding existing
|
|
|
288 |
## methods from $methods(...)
|
|
|
289 |
refClassA <- setRefClass("refClassA", methods=list(foo=function() "A"))
|
|
|
290 |
refClassB <- setRefClass("refClassB", contains="refClassA")
|
|
|
291 |
mnames <- objects(getClass("refClassB")@refMethods)
|
|
|
292 |
refClassB$methods(foo=function() callSuper())
|
|
|
293 |
stopifnot(identical(refClassB$new()$foo(), "A"))
|
|
|
294 |
mnames2 <- objects(getClass("refClassB")@refMethods)
|
|
|
295 |
stopifnot(identical(mnames2[is.na(match(mnames2,mnames))], "foo#refClassA"))
|
|
|
296 |
refClassB$methods(foo=function() paste(callSuper(), "Version 2"))
|
|
|
297 |
stopifnot(identical(refClassB$new()$foo(), "A Version 2"))
|
|
|
298 |
stopifnot(identical(mnames2, objects(getClass("refClassB")@refMethods)))
|
| 53274 |
jmc |
299 |
|
|
|
300 |
if(methods:::.hasCodeTools()) {
|
| 54575 |
jmc |
301 |
## code warnings assigning locally to field names
|
| 53274 |
jmc |
302 |
stopifnot(is(tryCatch(mv$methods(test = function(x) {data <- x[!is.na(x)]; mean(data)}), warning = function(e)e), "warning"))
|
|
|
303 |
|
| 54575 |
jmc |
304 |
## warnings for nonlocal assignment that is not a field
|
|
|
305 |
stopifnot(is(tryCatch(mv$methods(test2 = function(x) {something <<- data[!is.na(x)]}), warning = function(e)e), "warning"))
|
|
|
306 |
|
|
|
307 |
## error for trying to assign to a method name
|
|
|
308 |
stopifnot(is(tryCatch(mv$methods(test3 = function(x) {edit <<- data[!is.na(x)]}), error = function(e)e), "error"))
|
| 53274 |
jmc |
309 |
} else
|
|
|
310 |
warning("Can't run some tests: recommended package codetools is not available")
|
| 53330 |
jmc |
311 |
|
|
|
312 |
## tests (fragmentary by necessity) of promptClass for reference class
|
| 53470 |
jmc |
313 |
ccon <- textConnection("ctxt", "w")
|
|
|
314 |
suppressMessages(promptClass("refClassB", filename = ccon))
|
| 53330 |
jmc |
315 |
## look for a method, inheritance, inherited method
|
|
|
316 |
stopifnot(length(c(grep("foo.*refClassA", ctxt),
|
|
|
317 |
grep("code{foo()}", ctxt, fixed = TRUE),
|
|
|
318 |
grep("linkS4class{refClassA", ctxt, fixed = TRUE))) >= 3)
|
| 53470 |
jmc |
319 |
close(ccon)
|
| 53330 |
jmc |
320 |
rm(ctxt)
|
| 53382 |
jmc |
321 |
|
|
|
322 |
|
|
|
323 |
## tests related to subclassing environments. These really test code in the core, viz. builtin.c
|
|
|
324 |
a <- refClassA$new()
|
|
|
325 |
ev <- new.env(parent = a) # parent= arg
|
|
|
326 |
stopifnot(is.environment(ev))
|
|
|
327 |
foo <- function()"A"; environment(foo) <- a # environment of function
|
|
|
328 |
stopifnot(identical(as.environment(a), environment(foo)))
|
|
|
329 |
xx <- 1:10; environment(xx) <- a # environment attribute
|
|
|
330 |
stopifnot(identical(as.environment(a), environment(xx)))
|
|
|
331 |
|
|
|
332 |
|
| 53385 |
jmc |
333 |
## tests of [[<- and $<- for subclasses of environment. At one point methods for these assignments were defined
|
|
|
334 |
## and caused inf. recursion when the arguments to the [[<- case were changed in base.
|
|
|
335 |
setClass("myEnv", contains = "environment")
|
| 53641 |
jmc |
336 |
m <- new("myEnv", a="test")
|
|
|
337 |
m2 <- new("myEnv"); m3 <- new("myEnv")
|
|
|
338 |
## test that new.env() is called for each new object
|
|
|
339 |
stopifnot(!identical(as.environment(m), as.environment(m2)),
|
|
|
340 |
!identical(as.environment(m3), as.environment(m2)))
|
| 53385 |
jmc |
341 |
m[["x"]] <- 1; m$y <- 2
|
|
|
342 |
stopifnot(identical(c(m[["x"]], m$y), c(1,2)))
|
| 53641 |
jmc |
343 |
rm(x, envir = m) # check rm() works, does not clobber class
|
|
|
344 |
stopifnot(identical(sort(objects(m)), sort(c("a", "y"))),
|
|
|
345 |
is(m, "myEnv"))
|
| 53465 |
jmc |
346 |
|
| 53564 |
jmc |
347 |
## tests of binding & environment tools with subclases of environment
|
|
|
348 |
lockBinding("y", m)
|
|
|
349 |
stopifnot(bindingIsLocked("y", m))
|
|
|
350 |
unlockBinding("y", m)
|
|
|
351 |
stopifnot(!bindingIsLocked("y", m))
|
|
|
352 |
|
|
|
353 |
makeActiveBinding("z", function(value) {
|
|
|
354 |
if(missing(value))
|
|
|
355 |
"dummy"
|
|
|
356 |
else
|
|
|
357 |
"dummy assignment"
|
|
|
358 |
}, m)
|
|
|
359 |
stopifnot(identical(get("z", m),"dummy"))
|
|
|
360 |
## assignment will return the value but do nothing
|
|
|
361 |
stopifnot(identical(assign("z","other", m), "other"),
|
|
|
362 |
identical(get("z", m),"dummy"))
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
## this has to be last--Seems no way to unlock an environment!
|
|
|
366 |
lockEnvironment(m)
|
|
|
367 |
stopifnot(environmentIsLocked(m))
|
|
|
368 |
|
| 53645 |
maechler |
369 |
rm(m)
|
|
|
370 |
m <- new("myEnv")
|
|
|
371 |
stopifnot(length(ls(m)) == 0)
|
|
|
372 |
## used to contain the previous content
|
| 53564 |
jmc |
373 |
|
| 53645 |
maechler |
374 |
|
| 53465 |
jmc |
375 |
## test of callSuper() to a hidden default method for initialize() (== initFields)
|
|
|
376 |
TestClass <- setRefClass ("TestClass",
|
| 53470 |
jmc |
377 |
fields = list (text = "character"),
|
|
|
378 |
methods = list(
|
|
|
379 |
print = function () {cat(text)},
|
|
|
380 |
initialize = function(text = "", ...) callSuper(text = paste(text, ":", sep=""),...)
|
|
|
381 |
))
|
|
|
382 |
tt <- TestClass$new("hello world")
|
|
|
383 |
stopifnot(identical(tt$text, "hello world:"))
|
|
|
384 |
## now a subclass with another field & another layer of callSuper()
|
|
|
385 |
TestClass2 <- setRefClass("TestClass2",
|
|
|
386 |
contains = "TestClass",
|
|
|
387 |
fields = list( version = "integer"),
|
|
|
388 |
methods = list(
|
|
|
389 |
initialize = function(..., version = 1)
|
|
|
390 |
callSuper(..., version = version+1))
|
|
|
391 |
)
|
|
|
392 |
tt <- TestClass2$new("test", version = 1)
|
|
|
393 |
stopifnot(identical(tt$text, "test:"), identical(tt$version, as.integer(2)))
|
|
|
394 |
tt <- TestClass2$new(version=3) # default text
|
|
|
395 |
stopifnot(identical(tt$text, ":"), identical(tt$version, as.integer(4)))
|
| 54575 |
jmc |
396 |
|
|
|
397 |
|
| 55779 |
jmc |
398 |
## test some capabilities but read-only for .self
|
|
|
399 |
.changeAllFields <- function(replacement) {
|
|
|
400 |
fields <- names(.refClassDef@fieldClasses)
|
|
|
401 |
for(field in fields)
|
|
|
402 |
eval(substitute(.self$FIELD <- replacement$FIELD,
|
|
|
403 |
list(FIELD = field)))
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
mEditor$methods(change = .changeAllFields)
|
|
|
407 |
xx <- mEditor$new(data = xMat)
|
|
|
408 |
xx$edit(2, 2, 0)
|
|
|
409 |
|
|
|
410 |
yy <- mEditor$new(data = xMat+1)
|
|
|
411 |
yy$change(xx)
|
|
|
412 |
stopifnot(identical(yy$data, xx$data), identical(yy$edits, xx$edits))
|
|
|
413 |
|
|
|
414 |
## but don't allow assigment
|
|
|
415 |
if(methods:::.hasCodeTools())
|
|
|
416 |
stopifnot(is(tryCatch(yy$.self$data <- xMat, error = function(e)e), "error"))
|
| 55988 |
jmc |
417 |
|
|
|
418 |
## the locked binding of refObjectGenerator class should prevent modifying
|
|
|
419 |
## methods, locking fields or setting accessor methods
|
|
|
420 |
evr <- getRefClass("refObjectGenerator") # in methods
|
|
|
421 |
stopifnot(is(tryCatch(evr$methods(foo = function()"..."), error = function(e)e), "error"),
|
|
|
422 |
is(tryCatch(evr$lock("def"), error = function(e)e), "error"),
|
|
|
423 |
is(tryCatch(evr$accessors("def"), error = function(e)e), "error"))
|