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

Use new warn_roxy_tag() and warn_roxy_block() #1317

Merged
merged 19 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion R/namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ roxy_tag_parse.roxy_tag_rawNamespace <- function(x) {
}
#' @export
roxy_tag_ns.roxy_tag_rawNamespace <- function(x, block, env, import_only = FALSE) {
x$val
x$raw
}

#' @export
Expand Down
14 changes: 8 additions & 6 deletions R/rd-raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ format.rd_section_rawRd <- function(x, ...) {
paste(x$value, collapse = "\n")
}

roxy_tag_eval <- function(tag, env) {
roxy_tag_eval <- function(tag, env = new.env(emptyenv())) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
tryCatch({
expr <- parse(text = tag$val)
out <- eval(expr, envir = env)
out <- eval(tag$val, envir = env)

if (!is.character(out)) {
roxy_tag_warning(tag, "did not evaluate to a string")
warn_roxy_tag(tag, "must evaluate to a character vector")
NULL
} else if (anyNA(out)) {
roxy_tag_warning(tag, "result contained NA")
warn_roxy_tag(tag, "must not contain any missing values")
NULL
} else {
out
}
}, error = function(e) {
warn_roxy_tag(tag, "failed to execute", parent = e)
warn_roxy_tag(tag, "failed to evaluate", parent = e)
NULL
})
}
3 changes: 1 addition & 2 deletions R/tag-parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ tag_code <- function(x) {
NULL
} else {
tryCatch({
parse(text = x$raw)
x$val <- x$raw
x$val <- parse(text = x$raw)
x
}, error = function(e) {
warn_roxy_tag(x, "failed to parse", parent = e)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/block.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

# errors are propagated

[<text>:4] @eval failed to execute
[<text>:5] @eval failed to evaluate
Caused by error in `foo()`:
! Uhoh

# must return non-NA string

[<text>:3] @eval did not evaluate to a string
[<text>:3] @eval must evaluate to a character vector

---

[<text>:3] @eval result contained NA
[<text>:3] @eval must not contain any missing values

10 changes: 7 additions & 3 deletions tests/testthat/_snaps/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
1: a +
^

# evalNamespace generates warning when code is invalid
# evalNamespace warns for bad code

[<text>:2] @evalNamespace failed to parse
Caused by error in `parse()`:
! <text>:2:0: unexpected end of input
1: a +
^

# evalNamespace generates warning when code raises error
---

[<text>:2] @evalNamespace failed to execute
[<text>:2] @evalNamespace failed to evaluate
Caused by error:
! Uhoh

---

[<text>:2] @evalNamespace must evaluate to a character vector

31 changes: 20 additions & 11 deletions tests/testthat/_snaps/rd-raw.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
# evalRd must be valid code

[<text>:2] @evalRd failed to parse
Caused by error in `parse()`:
! <text>:2:0: unexpected end of input
1: a +
^

# error-ful evalRd generates warning

[<text>:2] @evalRd failed to execute
Caused by error:
! Uhoh
Code
expect_parse_failure(roxy_tag_eval(roxy_test_tag(val = 1)))
Output
<warning/rlang_warning>
Warning:
[test.R:1] @test must evaluate to a character vector
Code
expect_parse_failure(roxy_tag_eval(roxy_test_tag(val = NA_character_)))
Output
<warning/rlang_warning>
Warning:
[test.R:1] @test must not contain any missing values
Code
expect_parse_failure(roxy_tag_eval(roxy_test_tag(val = quote(stop("Uhoh")))))
Output
<warning/rlang_warning>
Warning:
[test.R:1] @test failed to evaluate
Caused by error:
! Uhoh

1 change: 1 addition & 0 deletions tests/testthat/test-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ test_that("errors are propagated", {
expect_snapshot_warning(
roc_proc_text(rd_roclet(), "
foo <- function() stop('Uhoh')
#' Title
#' @name foo
#' @eval foo()
NULL"
Expand Down
26 changes: 4 additions & 22 deletions tests/testthat/test-namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,47 +247,29 @@ test_that("rawNamespace inserted unchanged", {

# @evalNamespace ----------------------------------------------------------

test_that("evalNamespace generates warning when code is invalid", {
test_that("evalNamespace warns for bad code", {
expect_snapshot_warning(
roc_proc_text(namespace_roclet(), "
#' @evalNamespace a +
#' @name a
#' @title a
NULL")
)
})

test_that("evalNamespace generates warning when code raises error", {
expect_snapshot_warning(
roc_proc_text(namespace_roclet(), "
#' @evalNamespace stop('Uhoh')
#' @name a
#' @title a
NULL")
)
})

test_that("evalNamespace generates warning when code doesn't eval to string", {
# Not character
expect_warning(
roc_proc_text(namespace_roclet(), "
z <- 10
#' @evalNamespace z * 2
#' @name a
#' @title a
NULL"),
"did not evaluate to a string" # From block_eval
)

# NA_character_ not allowed
expect_warning(
expect_snapshot_warning(
roc_proc_text(namespace_roclet(), "
nms <- NA_character_
#' @evalNamespace nms
#' @evalNamespace 1
#' @name a
#' @title a
NULL"),
"result contained NA" # From block_eval
NULL")
)
})

Expand Down
29 changes: 9 additions & 20 deletions tests/testthat/test-rd-raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@ test_that("rawRd inserted unchanged", {
expect_equal(lines[[9]], "#this is a comment")
})

test_that("evalRd must be valid code", {
expect_snapshot_warning(
roc_proc_text(rd_roclet(), "
#' @evalRd a +
#' @name a
#' @title a
NULL")
)
})

test_that("error-ful evalRd generates warning", {
expect_snapshot_warning(
roc_proc_text(rd_roclet(), "
#' @evalRd stop('Uhoh')
#' @name a
#' @title a
NULL")
)
})

test_that("evalRd inserted unchanged", {
out <- roc_proc_text(rd_roclet(), "
z <- 10
Expand All @@ -39,3 +19,12 @@ test_that("evalRd inserted unchanged", {

expect_equal(out$get_value("rawRd"), "20")
})

test_that("error-ful evalRd generates warning", {
expect_snapshot({
expect_parse_failure(roxy_tag_eval(roxy_test_tag(val = 1)))
expect_parse_failure(roxy_tag_eval(roxy_test_tag(val = NA_character_)))
expect_parse_failure(roxy_tag_eval(roxy_test_tag(val = quote(stop('Uhoh')))))
})
})