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

Inline test files directly into tests #133

Merged
merged 5 commits into from
Jun 18, 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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Imports:
methods
Suggests:
covr,
ggplot2,
ggplot2 (>= 3.3.6),
lattice,
rlang,
testthat (>= 3.0.0),
Expand All @@ -37,4 +37,4 @@ Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
20 changes: 15 additions & 5 deletions R/replay.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
#' @param x result from [evaluate()]
#' @export
#' @examples
#' samples <- system.file("tests", "testthat", package = "evaluate")
#' if (file_test("-d", samples)) {
#' replay(evaluate(file(file.path(samples, "order.R"))))
#' replay(evaluate(file(file.path(samples, "plot.R"))))
#' replay(evaluate(file(file.path(samples, "data.R"))))
#' f1 <- function() {
#' cat("1\n")
#' print("2")
#' warning("3")
#' print("4")
#' message("5")
#' stop("6")
#' }
#' replay(evaluate("f1()"))
#'
#' f2 <- function() {
#' message("Hello")
#' plot(1:10)
#' message("Goodbye")
#' }
#' replay(evaluate("f2()"))
replay <- function(x) UseMethod("replay", x)

#' @export
Expand Down
20 changes: 15 additions & 5 deletions man/replay.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# code errors if stop_on_error == 2L

Code
evaluate("stop(\"1\")", stop_on_error = 2L)
Condition
Error:
! 1

2 changes: 0 additions & 2 deletions tests/testthat/comment.R

This file was deleted.

2 changes: 0 additions & 2 deletions tests/testthat/data.R

This file was deleted.

5 changes: 0 additions & 5 deletions tests/testthat/error-complex.R

This file was deleted.

2 changes: 0 additions & 2 deletions tests/testthat/error.R

This file was deleted.

22 changes: 0 additions & 22 deletions tests/testthat/example-1.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/ggplot-empty-1.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/ggplot-empty-2.R

This file was deleted.

6 changes: 0 additions & 6 deletions tests/testthat/ggplot-loop.R

This file was deleted.

2 changes: 0 additions & 2 deletions tests/testthat/ggplot.R

This file was deleted.

8 changes: 8 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
evaluate_ <- function(text, ...) {
# Trim off leading/trailing new lines and dedent
text <- gsub("^\n {4}", "", text)
text <- gsub("\n {4}", "\n", text)
text <- gsub("\n +$", "", text)

evaluate(text, ...)
}
4 changes: 0 additions & 4 deletions tests/testthat/interleave-1.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/interleave-2.R

This file was deleted.

16 changes: 0 additions & 16 deletions tests/testthat/order.R

This file was deleted.

6 changes: 0 additions & 6 deletions tests/testthat/parse.R

This file was deleted.

2 changes: 0 additions & 2 deletions tests/testthat/plot-additions.R

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testthat/plot-clip.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/plot-last-comment.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/plot-loop.R

This file was deleted.

7 changes: 0 additions & 7 deletions tests/testthat/plot-multi-layout.R

This file was deleted.

9 changes: 0 additions & 9 deletions tests/testthat/plot-multi-layout2.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/plot-multi-missing.R

This file was deleted.

5 changes: 0 additions & 5 deletions tests/testthat/plot-multi.R

This file was deleted.

5 changes: 0 additions & 5 deletions tests/testthat/plot-new.R

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testthat/plot-par.R

This file was deleted.

5 changes: 0 additions & 5 deletions tests/testthat/plot-par2.R

This file was deleted.

8 changes: 0 additions & 8 deletions tests/testthat/plot-persp.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/plot-strwidth.R

This file was deleted.

1 change: 0 additions & 1 deletion tests/testthat/plot.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/raw-output.R

This file was deleted.

21 changes: 16 additions & 5 deletions tests/testthat/test-errors.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
test_that("all code run, even after error", {
ev <- evaluate(file("error.R"))
ev <- evaluate_('stop("1")\n2')
expect_length(ev, 4)
})

test_that("code aborts on error if stop_on_error == 1L", {
ev <- evaluate(file("error.R"), stop_on_error = 1L)
ev <- evaluate('stop("1")\n2', stop_on_error = 1L)
expect_length(ev, 2)
})

test_that("code errors if stop_on_error == 2L", {
expect_error(evaluate(file("error.R"), stop_on_error = 2L), "1")
expect_snapshot(evaluate('stop("1")', stop_on_error = 2L), error = TRUE)
})

test_that("traceback useful if stop_on_error == 2L", {
expect_error(evaluate(file("error-complex.R"), stop_on_error = 2L), "Error")
expect_error(evaluate_('
f <- function() g()
g <- function() h()
h <- function() stop("Error")

f()
', stop_on_error = 2L), "Error")

## Doesn't work because .Traceback not create when code run
## inside try or tryCatch. Can't figure out how to work around.
Expand All @@ -26,6 +32,11 @@ test_that("traceback useful if stop_on_error == 2L", {
})

test_that("capture messages in try() (#88)", {
ev <- evaluate(file("try.R"))
ev <- evaluate_('
g <- function() f("error")
f <- function(x) stop(paste0("Obscure ", x))

try(g())
')
expect_match(ev[[length(ev)]], "Obscure error")
})
45 changes: 34 additions & 11 deletions tests/testthat/test-evaluate.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
test_that("file with only comments runs", {
ev <- evaluate(file("comment.R"))
ev <- evaluate_("
# This test case contains no executable code
# but it shouldn't throw an error
")
expect_length(ev, 2)

expect_equal(classes(ev), c("source", "source"))
Expand All @@ -8,7 +11,10 @@ test_that("file with only comments runs", {
test_that("data sets loaded", {
skip_if_not_installed("lattice")

ev <- evaluate(file("data.R"))
ev <- evaluate_('
data(barley, package = "lattice")
barley
')
expect_length(ev, 3)
})

Expand Down Expand Up @@ -62,21 +68,38 @@ test_that("options(warn = 0) and options(warn = 1) produces warnings", {
# })

test_that("output and plots interleaved correctly", {
ev <- evaluate(file("interleave-1.R"))
expect_equal(classes(ev),
c("source", "character", "recordedplot", "character", "recordedplot"))
ev <- evaluate_("
for (i in 1:2) {
cat(i)
plot(i)
}
")
expect_equal(
classes(ev),
c("source", "character", "recordedplot", "character", "recordedplot")
)

ev <- evaluate(file("interleave-2.R"))
expect_equal(classes(ev),
c("source", "recordedplot", "character", "recordedplot", "character"))
ev <- evaluate_("
for (i in 1:2) {
plot(i)
cat(i)
}
")
expect_equal(
classes(ev),
c("source", "recordedplot", "character", "recordedplot", "character")
)
})

test_that("return value of value handler inserted directly in output list", {
skip_if_not_installed("ggplot2")

ev <- evaluate(
file("raw-output.R"),
output_handler = new_output_handler(value = identity)
ev <- evaluate_('
rnorm(10)
x <- list("I\'m a list!")
suppressPackageStartupMessages(library(ggplot2))
ggplot(mtcars, aes(mpg, wt)) + geom_point()
', output_handler = new_output_handler(value = identity)
)
expect_equal(
classes(ev),
Expand Down
Loading
Loading