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 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 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 Down
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test is tested anymore. Historically, as the others, all those tests were before testthat structure, and inside inst/tests.

So probably not testing something we still need - I didn't find what interweave was

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither! Maybe it's the original name of evaluate?

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is used in examples:

#' replay(evaluate(file(file.path(samples, "order.R"))))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! I'll inline them into the examples too.

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.

22 changes: 17 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,12 @@ test_that("traceback useful if stop_on_error == 2L", {
})

test_that("capture messages in try() (#88)", {
ev <- evaluate(file("try.R"))
# TODO: figure out why this doesn't work interactively
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is not working for this exactly ? Out of curiosity I tried locally and it seemed to work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea 😬 I'll have to look again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah seems fine now so I'm not sure what the problem was.

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