Skip to content

Commit 024fa56

Browse files
committed
Rearrange functions
1 parent 7a6169c commit 024fa56

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

R/expect-inheritance.R

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,35 +121,6 @@ expect_s3_class <- function(object, class, exact = FALSE) {
121121
pass(act$val)
122122
}
123123

124-
#' @export
125-
#' @rdname inheritance-expectations
126-
expect_s7_class <- function(object, class) {
127-
check_installed("S7")
128-
if (!inherits(class, "S7_class")) {
129-
stop_input_type(class, "an S7 class object")
130-
}
131-
132-
act <- quasi_label(enquo(object))
133-
134-
if (!S7::S7_inherits(object)) {
135-
return(fail(sprintf("%s is not an S7 object", act$lab)))
136-
}
137-
138-
if (!S7::S7_inherits(object, class)) {
139-
obj_class <- setdiff(base::class(object), "S7_object")
140-
class_desc <- paste0("<", obj_class, ">", collapse = "/")
141-
msg <- sprintf(
142-
"%s inherits from %s not <%s>.",
143-
act$lab,
144-
class_desc,
145-
attr(class, "name", TRUE)
146-
)
147-
return(fail(msg))
148-
}
149-
150-
pass(act$val)
151-
}
152-
153124
#' @export
154125
#' @rdname inheritance-expectations
155126
expect_s4_class <- function(object, class) {
@@ -177,13 +148,40 @@ expect_s4_class <- function(object, class) {
177148
}
178149
}
179150
} else {
180-
abort("`class` must be a NA or a character vector")
151+
stop_input_type(class, c("a character vector", "NA"))
181152
}
182153

183154
pass(act$val)
184155
}
185156

186-
isS3 <- function(x) is.object(x) && !isS4(x)
157+
#' @export
158+
#' @rdname inheritance-expectations
159+
expect_s7_class <- function(object, class) {
160+
check_installed("S7")
161+
if (!inherits(class, "S7_class")) {
162+
stop_input_type(class, "an S7 class object")
163+
}
164+
165+
act <- quasi_label(enquo(object))
166+
167+
if (!S7::S7_inherits(object)) {
168+
return(fail(sprintf("%s is not an S7 object", act$lab)))
169+
}
170+
171+
if (!S7::S7_inherits(object, class)) {
172+
obj_class <- setdiff(base::class(object), "S7_object")
173+
class_desc <- paste0("<", obj_class, ">", collapse = "/")
174+
msg <- sprintf(
175+
"%s inherits from %s not <%s>.",
176+
act$lab,
177+
class_desc,
178+
attr(class, "name", TRUE)
179+
)
180+
return(fail(msg))
181+
}
182+
183+
pass(act$val)
184+
}
187185

188186
#' Does an object inherit from a given class?
189187
#'
@@ -228,6 +226,9 @@ expect_is <- function(object, class, info = NULL, label = NULL) {
228226
pass(act$val)
229227
}
230228

229+
# Helpers ----------------------------------------------------------------------
230+
231+
isS3 <- function(x) is.object(x) && !isS4(x)
231232

232233
format_class <- function(x) {
233234
paste0(encodeString(x, quote = "'"), collapse = "/")

man/inheritance-expectations.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/expect-inheritance.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
Error in `expect_s3_class()`:
3232
! `exact` must be `TRUE` or `FALSE`, not the string "yes".
3333

34+
---
35+
36+
Code
37+
expect_s4_class(factor("a"), 1)
38+
Condition
39+
Error in `expect_s4_class()`:
40+
! `class` must be a character vector or NA, not the number 1.
41+
3442
# test_s3_class respects class hierarchy
3543

3644
`x` inherits from 'a'/'b' not 'c'.

tests/testthat/test-expect-inheritance.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ test_that("expect_s4_class allows unquoting of first argument", {
8181
expect_success(expect_s4_class(!!rlang::quo(obj), "new_class"))
8282
})
8383

84+
85+
test_that("expect_s3_class validates its inputs", {
86+
expect_snapshot(error = TRUE, {
87+
expect_s4_class(factor("a"), 1)
88+
})
89+
})
90+
91+
8492
# expect_s7_class --------------------------------------------------------
8593

8694
test_that("can check with actual class", {

0 commit comments

Comments
 (0)