Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop parse_all() default method; add tests #186

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ S3method(parse_all,"function")
S3method(parse_all,call)
S3method(parse_all,character)
S3method(parse_all,connection)
S3method(parse_all,default)
S3method(print,evaluate_evaluation)
S3method(replay,character)
S3method(replay,condition)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# evaluate (development version)

* `parse_all()` no longer has a default method, which will generate better errors if you pass in something unexpectected.
* The package now depends on R 4.0.0 in order to decrease our maintenance burden.
* `evaluate()` automatically strips calls from conditions emitted by top-level code (these incorrectly get calls because they're wrapped inside `eval()`) (#150).
* `evalute(include_timing)` has been deprecated. I can't find any use of it on GitHub, and it adds substantial code complexity for little gain.
Expand Down
19 changes: 5 additions & 14 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ append_break <- function(x) {
#' @export
parse_all.connection <- function(x, filename = NULL, ...) {
if (!isOpen(x, "r")) {
open(x, "r")
on.exit(close(x))
open(x, "r")
defer(close(x))
}
text <- readLines(x)
if (is.null(filename))
filename <- summary(x)$description
filename <- filename %||% summary(x)$description

parse_all(text, filename, ...)
}

Expand All @@ -146,19 +146,10 @@ parse_all.function <- function(x, filename = NULL, ...) {
parse_all(find_function_body(x), filename = filename, ...)
}

#' @export
parse_all.default <- function(x, filename = NULL, ...) {
if (is.null(filename))
filename <- "<expression>"
parse_all(deparse(x), filename, ...)
}

# Calls are already parsed and always length one
#' @export
parse_all.call <- function(x, filename = NULL, ...) {
out <- parse_all.default(x, filename = filename, ...)
out$expr <- list(as.expression(x))
out
parse_all(deparse(x), filename = filename, ...)
}

find_function_body <- function(f) {
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,49 @@ test_that("can ignore parse errors", {
expect_error(evaluate('x <-', stop_on_error = 0), NA)
})

# input types ------------------------------------------------------------------

test_that("can parse a call", {
out <- parse_all(quote(f(a, b, c)))
expect_equal(out$src, "f(a, b, c)")
expect_equal(
out$expr,
I(list(expression(f(a, b, c)))),
ignore_attr = "srcref"
)
})

test_that("can parse a connection", {
path <- withr::local_tempfile(lines = c("# 1", "1 + 1"))
cur_cons <- getAllConnections()

con <- file(path)
out <- parse_all(con)

expect_equal(out$src, c("# 1\n", "1 + 1"))
expect_equal(
out$expr,
I(list(expression(), expression(1 + 1))),
ignore_attr = "srcref"
)

# Doesn't leave any connections around
expect_equal(getAllConnections(), cur_cons)
})

test_that("can parse a function", {
out <- parse_all(function() {
# Hi
1 + 1
})
expect_equal(out$src, c("# Hi\n", "1 + 1"))
expect_equal(
out$expr,
I(list(expression(), expression(1 + 1))),
ignore_attr = "srcref"
)
})

# find_function_body -----------------------------------------------------------

test_that("parsing a function parses its body", {
Expand Down
Loading