Skip to content

Commit 26d7078

Browse files
committed
Feedback for claude
1 parent 97066ea commit 26d7078

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

R/mock-oo.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' These functions allow you to temporarily override S3 and S4 methods that
44
#' already exist. It works by using [registerS3method()]/[setMethod()] to
5-
#' temporarily replace the original defintion.
5+
#' temporarily replace the original definition.
66
#'
77
#' @param generic A string giving the name of the generic.
88
#' @param signature A character vector giving the signature of the method.
@@ -83,12 +83,15 @@ local_mocked_r6_class <- function(
8383
if (!inherits(class, "R6ClassGenerator")) {
8484
stop_input_type(class, "an R6 class definition")
8585
}
86+
if (!is.list(public)) {
87+
stop_input_type(public, "a list")
88+
}
89+
if (!is.list(private)) {
90+
stop_input_type(private, "a list")
91+
}
8692

8793
mocked_class <- mock_r6_class(class, public, private)
88-
local_mocked_bindings(
89-
"{class$classname}" := mocked_class,
90-
.env = caller_env()
91-
)
94+
local_mocked_bindings("{class$classname}" := mocked_class, .env = frame)
9295
}
9396

9497
mock_r6_class <- function(class, public = list(), private = list()) {

man/local_mocked_s3_method.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/mock-oo.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,21 @@
4444
Error in `local_mocked_s4_method()`:
4545
! Can't find existing S4 method `mean(bar)`.
4646

47+
---
48+
49+
Code
50+
local_mocked_r6_class(mean)
51+
Condition
52+
Error in `local_mocked_r6_class()`:
53+
! `class` must be an R6 class definition, not a function.
54+
Code
55+
local_mocked_r6_class(TestMockClass, public = 1)
56+
Condition
57+
Error in `local_mocked_r6_class()`:
58+
! `public` must be a list, not the number 1.
59+
Code
60+
local_mocked_r6_class(TestMockClass, private = 1)
61+
Condition
62+
Error in `local_mocked_r6_class()`:
63+
! `private` must be a list, not the number 1.
64+

tests/testthat/test-mock-oo.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ test_that("can mock all R6 components", {
6565
obj <- TestMockClass$new()
6666
expect_equal(obj$sum(), 0)
6767
})
68+
69+
test_that("validates its inputs", {
70+
expect_snapshot(error = TRUE, {
71+
local_mocked_r6_class(mean)
72+
local_mocked_r6_class(TestMockClass, public = 1)
73+
local_mocked_r6_class(TestMockClass, private = 1)
74+
})
75+
})

0 commit comments

Comments
 (0)