-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtest-base.R
More file actions
368 lines (308 loc) · 9.3 KB
/
Copy pathtest-base.R
File metadata and controls
368 lines (308 loc) · 9.3 KB
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
test_that("validation uses typeof", {
expect_equal(class_integer$validator(1L), NULL)
expect_equal(class_integer$validator(factor()), NULL)
expect_snapshot(class_integer$validator(TRUE))
expect_equal(class_function$validator(`[`), NULL)
expect_equal(class_function$validator(sum), NULL)
expect_equal(class_function$validator(mean), NULL)
})
test_that("base class display as expected", {
expect_snapshot({
class_integer
str(class_integer)
})
})
test_that("classes can inherit from base types", {
base_classes <- c(class_vector$classes, list(class_function))
for (class in base_classes) {
foo := new_class(parent = class)
expect_error(foo(), NA)
}
})
test_that("Base classes can be a parent class", {
expect_no_error({
Foo := new_class(class_logical)
Foo()
Foo(TRUE)
})
expect_error(Foo(1), "must be an instance of <logical>, not <double>")
expect_no_error({
Foo := new_class(class_integer)
Foo()
Foo(1L)
})
expect_error(Foo(1), "must be an instance of <integer>, not <double>")
expect_no_error({
Foo := new_class(class_double)
Foo()
Foo(1)
})
expect_error(Foo(1L), "must be an instance of <double>, not <integer>")
expect_no_error({
Foo := new_class(class_complex)
Foo()
Foo(1 + 1i)
})
expect_error(Foo(1), "must be an instance of <complex>, not <double>")
expect_no_error({
Foo := new_class(class_character)
Foo()
Foo("a")
})
expect_error(Foo(1), "must be an instance of <character>, not <double>")
expect_no_error({
Foo := new_class(class_raw)
Foo()
Foo(charToRaw("a"))
})
expect_error(Foo(1), "must be an instance of <raw>, not <double>")
expect_no_error({
Foo := new_class(class_list)
Foo()
Foo(list())
})
expect_error(Foo(1), "must be an instance of <list>, not <double>")
expect_no_error({
Foo := new_class(class_expression)
Foo()
Foo(expression(1))
})
expect_error(Foo(1), "must be an instance of <expression>, not <double>")
expect_no_error({
Foo := new_class(class_call)
Foo()
Foo(quote(a()))
})
expect_error(Foo(1), "must be an instance of <call>, not <double>")
expect_no_error({
Foo := new_class(class_function)
Foo()
Foo(identity)
})
expect_error(Foo(1), "must be an instance of <function>, not <double>")
# union types cannot be a parent:
#
# class_numeric
# class_atomic
# class_vector
# class_language
# class_name cannot be a parent because:
# 'Error: cannot set attribute on a symbol'
# class_environment cannot currently be a parent
# (this is expected to change in the future)
})
test_that("All base classes can be a property class", {
expect_no_error({
Foo := new_class(properties = list(x = class_logical))
Foo(x = TRUE)
})
expect_error(Foo(x = 1), "@x must be <logical>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_integer))
Foo(x = 1L)
})
expect_error(Foo(x = 1), "@x must be <integer>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_double))
Foo(x = 1)
})
expect_error(Foo(x = 1L), "@x must be <double>, not <integer>")
expect_no_error({
Foo := new_class(properties = list(x = class_complex))
Foo(x = 1 + 1i)
})
expect_error(Foo(x = 1), "@x must be <complex>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_character))
Foo(x = "a")
})
expect_error(Foo(x = 1), "@x must be <character>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_raw))
Foo(x = charToRaw("a"))
})
expect_error(Foo(x = 1), "@x must be <raw>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_list))
Foo(x = list())
})
expect_error(Foo(x = 1), "@x must be <list>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_expression))
Foo(x = expression(1))
})
expect_error(Foo(x = 1), "@x must be <expression>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_call))
Foo(x = quote(a()))
})
expect_error(Foo(x = 1), "@x must be <call>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_function))
Foo(x = identity)
})
expect_error(Foo(x = 1), "@x must be <function>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_name))
Foo(x = quote(a))
})
expect_error(Foo(x = 1), "@x must be <name>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_environment))
Foo(x = new.env())
})
expect_error(Foo(x = 1), "@x must be <environment>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_atomic))
Foo(x = 1)
})
expect_error(Foo(x = list(TRUE)), "@x must be .*, not <list>")
expect_no_error({
Foo := new_class(properties = list(x = class_vector))
Foo(x = 1)
})
expect_error(Foo(x = quote(x)), "@x must be .*, not <symbol>")
expect_no_error({
Foo := new_class(properties = list(x = class_language))
Foo(x = quote(a()))
})
expect_error(Foo(x = 1), "@x must be .*, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_numeric))
Foo(x = 1)
})
expect_error(Foo(x = TRUE), "@x must be .*, not <logical>")
})
test_that("Base S3 classes can be parents", {
expect_no_error({
Foo := new_class(class_factor)
Foo()
Foo(1L, levels = letters[1:3])
Foo(factor(letters[1:3]))
})
expect_no_error({
Foo := new_class(class_Date)
Foo()
Foo(Sys.Date())
Foo(rep(Sys.Date(), 3))
Foo(1)
})
expect_error(Foo("a"), "Underlying data must be numeric")
expect_no_error({
Foo := new_class(class_POSIXct)
Foo()
Foo(Sys.time())
Foo(rep(Sys.time(), 3))
Foo(1)
})
expect_error(Foo("a"), "Underlying data must be numeric")
expect_no_error({
Foo := new_class(class_data.frame)
Foo()
Foo(data.frame(x = 1))
Foo(list(x = 1))
Foo(list(x = 1), "rowname")
})
expect_error(
Foo(list(x = 1:3, y = 1:4)),
"All variables should have the same length."
)
# expect_no_error({
# Foo := new_class(class_matrix)
# Foo(1:4, nrow = 2)
# Foo(NA)
# Foo(matrix(1:4, nrow = 2))
# })
# expect_no_error({
# Foo := new_class(class_array)
#
# Foo(array(1:4, dim = c(2, 2)))
# Foo(1:4, dim = c(2, 2))
#
# Foo(array(1:24, dim = c(2, 3, 4)))
# Foo(1:24, dim = c(2, 3, 4))
#
# Foo(array(1))
# Foo(1)
# })
expect_no_error({
Foo := new_class(class_formula)
Foo(~x)
Foo("~ x")
Foo(call("~", 1, 2))
Foo(quote(~x))
})
})
test_that("Base S3 classes can be properties", {
expect_no_error({
Foo := new_class(properties = list(x = class_factor))
Foo(x = factor())
})
expect_error(Foo(x = 1), "@x must be S3<factor>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_data.frame))
Foo(x = data.frame())
})
expect_error(Foo(x = 1), "@x must be S3<data.frame>, not <double>")
# expect_no_error({
# Foo := new_class(properties = list(x = class_matrix))
# Foo(x = matrix())
# })
# expect_error(Foo(x = 1), "@x must be S3<matrix>, not <double>")
# expect_no_error({
# Foo := new_class(properties = list(x = class_array))
# Foo(x = array())
# })
# expect_error(Foo(x = 1), "@x must be S3<array>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_formula))
Foo(x = ~x)
})
expect_error(Foo(x = 1), "@x must be S3<formula>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_Date))
Foo(x = Sys.Date())
})
expect_error(Foo(x = 1), "@x must be S3<Date>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_POSIXct))
Foo(x = Sys.time())
})
expect_error(Foo(x = 1), "@x must be S3<POSIXct/POSIXt>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_POSIXlt))
Foo(x = as.POSIXlt(Sys.time()))
})
expect_error(Foo(x = 1), "@x must be S3<POSIXlt/POSIXt>, not <double>")
expect_no_error({
Foo := new_class(properties = list(x = class_POSIXt))
Foo(x = Sys.time())
Foo(x = as.POSIXlt(Sys.time()))
})
expect_error(Foo(x = 1), "@x must be S3<POSIXt>, not <double>")
})
test_that("ALTREP vectors aren't materialised (#607)", {
skip_on_cran()
# The bug only triggers when `new_object()` is byte-compiled, which is the
# case for the installed package but not under `devtools::load_all()`. Force
# compilation here so the test reproduces in both contexts.
local_mocked_bindings(new_object = compiler::cmpfun(new_object))
# parent
myint := new_class(parent = class_integer)
expect_true(is_altrep_preserved(myint(seq_len(1e6))))
# properties, set during construction and via @<-
Foo := new_class(properties = list(x = class_integer))
y <- Foo(x = seq_len(1e6))
expect_true(is_altrep_preserved(y@x))
y@x <- seq_len(1e6)
expect_true(is_altrep_preserved(y@x))
})
test_that("inherits() works with S7_base_class", {
# nameOfClass() introduced in R 4.3
skip_if(getRversion() < "4.3")
# test nameOfClass.S7_base_class
expect_true(inherits("foo", class_character))
Foo := new_class(class_character)
expect_true(inherits(Foo(), "character"))
expect_true(inherits(Foo(), class_character))
})