Skip to content

Commit cf82d6d

Browse files
Add comprehensive S4 formatter tests for setClass, setGeneric, and setMethod
Co-authored-by: felix-andreas <[email protected]>
1 parent 44b74c1 commit cf82d6d

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

crates/roughly/tests/format/special.R.test

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,68 @@ library(purrr)
221221
map(\(df) lm(mpg~wt, data = df)) |>
222222
map(summary ) |>
223223
map_dbl("r.squared" )
224+
225+
#---- s4_classes
226+
setClass("Person",
227+
slots = list(
228+
name = "character",
229+
age = "numeric"
230+
),
231+
prototype = list(
232+
name = character(0),
233+
age = numeric(0)
234+
)
235+
)
236+
237+
setClass("Employee",
238+
contains = "Person",
239+
slots = list(
240+
department = "character",
241+
salary = "numeric"
242+
),
243+
validity = function(object) {
244+
if (object@salary < 0) "Salary must be non-negative"
245+
else TRUE
246+
}
247+
)
248+
249+
#---- s4_generics
250+
setGeneric("show",
251+
function(object) standardGeneric("show")
252+
)
253+
254+
setGeneric("getName", function(x) {
255+
standardGeneric("getName")
256+
})
257+
258+
setGeneric("setName<-",
259+
function(x, value) standardGeneric("setName<-")
260+
)
261+
262+
#---- s4_methods
263+
setMethod("show", "Person",
264+
function(object) {
265+
cat("Person object:\n")
266+
cat(" Name:", object@name, "\n")
267+
cat(" Age:", object@age, "\n")
268+
}
269+
)
270+
271+
setMethod("getName", "Person",
272+
function(x) x@name
273+
)
274+
275+
setMethod("setName<-", "Person",
276+
function(x, value) {
277+
x@name <- value
278+
x
279+
}
280+
)
281+
282+
setMethod("show", "Employee",
283+
function(object) {
284+
callNextMethod()
285+
cat(" Department:", object@department, "\n")
286+
cat(" Salary:", object@salary, "\n")
287+
}
288+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
setClass(
6+
"Person",
7+
slots = list(
8+
name = "character",
9+
age = "numeric"
10+
),
11+
prototype = list(
12+
name = character(0),
13+
age = numeric(0)
14+
)
15+
)
16+
17+
setClass(
18+
"Employee",
19+
contains = "Person",
20+
slots = list(
21+
department = "character",
22+
salary = "numeric"
23+
),
24+
validity = function(object) {
25+
if (object@salary < 0) {
26+
"Salary must be non-negative"
27+
} else {
28+
TRUE
29+
}
30+
}
31+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
setGeneric(
6+
"show",
7+
function(object) standardGeneric("show")
8+
)
9+
10+
setGeneric("getName", function(x) {
11+
standardGeneric("getName")
12+
})
13+
14+
setGeneric(
15+
"setName<-",
16+
function(x, value) standardGeneric("setName<-")
17+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
setMethod(
6+
"show",
7+
"Person",
8+
function(object) {
9+
cat("Person object:\n")
10+
cat(" Name:", object@name, "\n")
11+
cat(" Age:", object@age, "\n")
12+
}
13+
)
14+
15+
setMethod(
16+
"getName",
17+
"Person",
18+
function(x) x@name
19+
)
20+
21+
setMethod(
22+
"setName<-",
23+
"Person",
24+
function(x, value) {
25+
x@name <- value
26+
x
27+
}
28+
)
29+
30+
setMethod(
31+
"show",
32+
"Employee",
33+
function(object) {
34+
callNextMethod()
35+
cat(" Department:", object@department, "\n")
36+
cat(" Salary:", object@salary, "\n")
37+
}
38+
)

0 commit comments

Comments
 (0)