Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2eab262

Browse files
authoredJun 4, 2025··
Improvements (i hope so) (#254)
Code balance is + because I've added one test ;]
1 parent bb1ab11 commit 2eab262

File tree

5 files changed

+39
-34
lines changed

5 files changed

+39
-34
lines changed
 

‎R/qenv-eval_code.R

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@
2525
#' q <- eval_code(q, expression(assert_number(a)))
2626
#'
2727
#' @aliases eval_code,qenv-method
28+
#' @aliases eval_code,qenv.error-method
2829
#'
2930
#' @export
3031
setGeneric("eval_code", function(object, code, cache = FALSE, ...) standardGeneric("eval_code"))
3132

3233
setMethod("eval_code", signature = c(object = "qenv"), function(object, code, cache = FALSE, ...) {
33-
code <- .preprocess_code(code) # preprocess code to ensure it is a character vector
34+
if (!is.language(code) && !is.character(code)) {
35+
stop("eval_code accepts code being language or character")
36+
}
37+
code <- .preprocess_code(code)
38+
# preprocess code to ensure it is a character vector
3439
.eval_code(object = object, code = code, cache = cache, ...)
3540
})
3641

3742
setMethod("eval_code", signature = c(object = "qenv.error"), function(object, code, cache = FALSE, ...) object)
3843

3944
#' @keywords internal
4045
.eval_code <- function(object, code, cache = FALSE, ...) {
46+
if (identical(code, "")) {
47+
return(object)
48+
}
4149
parsed_code <- parse(text = code, keep.source = TRUE)
4250
object@.xData <- rlang::env_clone(object@.xData, parent = parent.env(.GlobalEnv))
4351
if (length(parsed_code) == 0) {
@@ -100,17 +108,14 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
100108
}
101109

102110
setGeneric(".preprocess_code", function(code) standardGeneric(".preprocess_code"))
103-
setMethod(".preprocess_code", signature = c("ANY"), function(code) paste(code, collapse = "\n"))
104-
setMethod(".preprocess_code", signature = c("language"), function(code) {
105-
paste(
106-
vapply(lang2calls(code), deparse1, collapse = "\n", character(1L)),
107-
collapse = "\n"
108-
)
109-
})
110-
setMethod(".preprocess_code", signature = c("expression"), function(code) {
111-
if (length(attr(code, "wholeSrcref")) == 0L) {
112-
paste(lang2calls(code), collapse = "\n")
113-
} else {
111+
setMethod(".preprocess_code", signature = c("character"), function(code) paste(code, collapse = "\n"))
112+
setMethod(".preprocess_code", signature = c("ANY"), function(code) {
113+
if (is.expression(code) && length(attr(code, "wholeSrcref"))) {
114114
paste(attr(code, "wholeSrcref"), collapse = "\n")
115+
} else {
116+
paste(
117+
vapply(lang2calls(code), deparse1, collapse = "\n", character(1L)),
118+
collapse = "\n"
119+
)
115120
}
116121
})

‎R/qenv-within.R

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,15 @@
4848
#' @export
4949
#'
5050
within.qenv <- function(data, expr, ...) {
51-
expr <- substitute(expr)
51+
expr <- as.expression(substitute(expr))
5252
extras <- list(...)
5353

54-
# Add braces for consistency.
55-
if (!identical(as.list(expr)[[1L]], as.symbol("{"))) {
56-
expr <- call("{", expr)
57-
}
58-
59-
calls <- as.list(expr)[-1]
60-
6154
# Inject extra values into expressions.
62-
calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras)))
63-
64-
eval_code(object = data, code = as.expression(calls))
55+
calls <- lapply(expr, function(x) do.call(substitute, list(x, env = extras)))
56+
do.call(
57+
eval_code,
58+
utils::modifyList(extras, list(object = data, code = as.expression(calls)))
59+
)
6560
}
6661

6762

‎man/eval_code.Rd

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

‎tests/testthat/test-qenv_eval_code.R

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ testthat::test_that("eval_code works with expression", {
4545
testthat::expect_equal(q1, list2env(list(a = 1, b = 2)))
4646
})
4747

48+
testthat::test_that("eval_code ignores empty code", {
49+
q <- qenv()
50+
testthat::expect_identical(q, eval_code(q, ""))
51+
})
52+
4853
testthat::test_that("eval_code preserves original formatting when `srcref` is present in the expression", {
4954
code <- "# comment
5055
a <- 1L"
@@ -77,12 +82,11 @@ testthat::test_that("eval_code works with quoted code block", {
7782
testthat::expect_equal(q1, list2env(list(a = 1, b = 2)))
7883
})
7984

80-
testthat::test_that("eval_code fails with unquoted expression", {
81-
b <- 3
82-
testthat::expect_error(
83-
eval_code(qenv(), a <- b),
84-
"unable to find an inherited method for function .eval_code. for signature"
85-
)
85+
testthat::test_that("eval_code fails with code not being language nor character", {
86+
msg <- "eval_code accepts code being language or character"
87+
testthat::expect_error(eval_code(qenv(), NULL), msg)
88+
testthat::expect_error(eval_code(qenv(), 1), msg)
89+
testthat::expect_error(eval_code(qenv(), list()), msg)
8690
})
8791

8892
testthat::test_that("an error when calling eval_code returns a qenv.error object which has message and trace", {
@@ -182,8 +186,3 @@ testthat::test_that("comments passed alone to eval_code that contain @linksto ta
182186
"x"
183187
)
184188
})
185-
186-
testthat::test_that("Code executed with integer shorthand (1L) is the same as original", {
187-
q <- within(qenv(), a <- 1L)
188-
testthat::expect_identical(get_code(q), "a <- 1L")
189-
})

‎tests/testthat/test-qenv_within.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@ testthat::describe("within run with `=`", {
149149
testthat::expect_equal(q$i, 1)
150150
})
151151
})
152+
153+
testthat::test_that("Code executed with integer shorthand (1L) is the same as original", {
154+
q <- within(qenv(), a <- 1L)
155+
testthat::expect_identical(get_code(q), "a <- 1L")
156+
})

0 commit comments

Comments
 (0)
Please sign in to comment.