Skip to content

Commit 171c67d

Browse files
averissimogogonzo
andauthored
Fixes integer shorthand regression (#227)
Co-authored-by: go_gonzo <[email protected]>
1 parent bd5ef47 commit 171c67d

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

R/qenv-eval_code.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#' Thus, if the `qenv` had been instantiated empty, contents of the environment are always a result of the stored code.
66
#'
77
#' @param object (`qenv`)
8-
#' @param code (`character` or `language`) code to evaluate. If `character`, comments are retained.
8+
#' @param code (`character`, `language` or `expression`) code to evaluate.
9+
#' It is possible to preserve original formatting of the `code` by providing a `character` or an
10+
#' `expression` being a result of `parse(keep.source = TRUE)`.
911
#'
1012
#' @return
1113
#' `eval_code` returns a `qenv` object with `expr` evaluated or `qenv.error` if evaluation fails.
@@ -14,6 +16,7 @@
1416
#' # evaluate code in qenv
1517
#' q <- qenv()
1618
#' q <- eval_code(q, "a <- 1")
19+
#' q <- eval_code(q, "b <- 2L # with comment")
1720
#' q <- eval_code(q, quote(library(checkmate)))
1821
#' q <- eval_code(q, expression(assert_number(a)))
1922
#'
@@ -100,7 +103,7 @@ setMethod("eval_code", signature = c("qenv", "expression"), function(object, cod
100103
if (length(srcref)) {
101104
eval_code(object, code = paste(attr(code, "wholeSrcref"), collapse = "\n"))
102105
} else {
103-
eval_code(object, code = paste(lang2calls(code), collapse = "\n"))
106+
Reduce(eval_code, init = object, x = code)
104107
}
105108
})
106109

man/qenv.Rd

Lines changed: 4 additions & 1 deletion
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: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
testthat::test_that("eval_code evaluates the code in the qenvs environment", {
22
q <- qenv()
3-
q1 <- eval_code(q, quote(iris1 <- iris))
4-
q2 <- eval_code(q1, quote(b <- nrow(iris1)))
5-
testthat::expect_identical(q2$b, 150L)
3+
q1 <- eval_code(q, quote(a <- 1L))
4+
q2 <- eval_code(q1, quote(b <- 1))
5+
testthat::expect_equal(q2, list2env(list(a = 1L, b = 1)))
66
})
77

88
testthat::test_that("eval_code locks the environment", {
99
q <- eval_code(qenv(), quote(iris1 <- iris))
10-
testthat::expect_true(environmentIsLocked(q@.xData))
10+
testthat::expect_true(environmentIsLocked(q))
1111
})
1212

1313
testthat::test_that("eval_code doesn't have access to environment where it's called", {
@@ -36,21 +36,29 @@ testthat::test_that("getting object from the package namespace works even if lib
3636
testthat::test_that("eval_code works with character", {
3737
q1 <- eval_code(qenv(), "a <- 1")
3838
testthat::expect_identical(get_code(q1), "a <- 1")
39-
testthat::expect_equal(q1@.xData, list2env(list(a = 1)))
39+
testthat::expect_equal(q1, list2env(list(a = 1)))
4040
})
4141

4242
testthat::test_that("eval_code works with expression", {
43-
q1 <- eval_code(qenv(), as.expression(quote(a <- 1)))
43+
q1 <- eval_code(qenv(), expression(a <- 1, b <- 2))
44+
testthat::expect_identical(get_code(q1), "a <- 1\nb <- 2")
45+
testthat::expect_equal(q1, list2env(list(a = 1, b = 2)))
46+
})
4447

45-
testthat::expect_identical(get_code(q1), "a <- 1")
46-
testthat::expect_equal(q1@.xData, list2env(list(a = 1)))
48+
testthat::test_that("eval_code preserves original formatting when `srcref` is present in the expression", {
49+
code <- "# comment
50+
a <- 1L"
51+
expr <- parse(text = code, keep.source = TRUE)
52+
q1 <- eval_code(qenv(), expr)
53+
testthat::expect_identical(get_code(q1), code)
54+
testthat::expect_equal(q1, list2env(list(a = 1L)))
4755
})
4856

4957
testthat::test_that("eval_code works with quoted", {
5058
q1 <- eval_code(qenv(), quote(a <- 1))
5159

5260
testthat::expect_identical(get_code(q1), "a <- 1")
53-
testthat::expect_equal(q1@.xData, list2env(list(a = 1)))
61+
testthat::expect_equal(q1, list2env(list(a = 1)))
5462
})
5563

5664
testthat::test_that("eval_code works with quoted code block", {
@@ -66,7 +74,7 @@ testthat::test_that("eval_code works with quoted code block", {
6674
get_code(q1),
6775
c("a <- 1\nb <- 2")
6876
)
69-
testthat::expect_equal(q1@.xData, list2env(list(a = 1, b = 2)))
77+
testthat::expect_equal(q1, list2env(list(a = 1, b = 2)))
7078
})
7179

7280
testthat::test_that("eval_code fails with unquoted expression", {
@@ -172,3 +180,8 @@ testthat::test_that("comments passed alone to eval_code that contain @linksto ta
172180
"x"
173181
)
174182
})
183+
184+
testthat::test_that("Code executed with integer shorthand (1L) is the same as original", {
185+
q <- within(qenv(), a <- 1L)
186+
testthat::expect_identical(get_code(q), "a <- 1L")
187+
})

tests/testthat/test-qenv_within.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,17 @@ testthat::test_that("styling of input code does not impact evaluation results",
6464

6565

6666
# return value ----
67-
testthat::test_that("within.qenv renturns a `qenv` where `@.xData` is a deep copy of that in `data`", {
67+
testthat::test_that("within.qenv empty call doesn't change qenv object", {
6868
q <- qenv()
6969
q <- within(qenv(), i <- iris)
7070
qq <- within(q, {})
71+
testthat::expect_identical(q, qq)
72+
})
73+
74+
testthat::test_that("within.qenv renturns a `qenv` where `@.xData` is a deep copy of that in `data`", {
75+
q <- qenv()
76+
q <- within(qenv(), i <- iris)
77+
qq <- within(q, i)
7178
testthat::expect_equal(q@.xData, qq@.xData)
7279
testthat::expect_false(identical(q@.xData, qq@.xData))
7380
})

0 commit comments

Comments
 (0)