Skip to content
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
2 changes: 1 addition & 1 deletion R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param env Environment in which to evaluate code.
#' @param desc A character vector used to filter tests. This is used to
#' (recursively) filter the content of the file, so that only the non-test
#' code up to and including the match test is run.
#' code up to and including the matching test is run.
Copy link
Member Author

Choose a reason for hiding this comment

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

This documentation only applies to source_file() which is marked as internal.

The user-facing function(s?) that expose desc do not yet hint at the ability to run a nested subtest or to run an it(). I know this applies to test_file() and perhaps that is the only affected function.

#' @param chdir Change working directory to `dirname(path)`?
#' @param wrap Automatically wrap all code within [test_that()]? This ensures
#' that all expectations are reported, even if outside a test block.
Expand Down
2 changes: 1 addition & 1 deletion man/source_file.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/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
Error:
! `env` must be an environment, not the string "x".

# works on code like the describe() example

Code
filter_desc(code, c("math library", "division()", "can handle division by 0"))
Condition
Error:
! Failed to find test with description "can handle division by 0".
Copy link
Member Author

Choose a reason for hiding this comment

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

Just an FYI: the (nested) desc that identifies the targeted it() is c("math library", "division()", "can handle division by 0"). But we only mention the last component here.


# preserve srcrefs

Code
Expand Down
58 changes: 55 additions & 3 deletions tests/testthat/test-source.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ test_that("checks its inputs", {

# filter_desc -------------------------------------------------------------

test_that("works with all tests types", {
test_that("works with all subtest types", {
code <- exprs(
test_that("foo", {}),
describe("bar", {}),
Expand All @@ -95,14 +95,15 @@ test_that("works with all tests types", {
expect_equal(filter_desc(code, "baz"), code[3])
})

test_that("only returns code before subtest", {
test_that("only returns non-subtest code before subtest", {
Copy link
Member Author

Choose a reason for hiding this comment

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

This seemed worth testing to me, i.e. making sure that the test_that() is not retained.

code <- exprs(
f(),
test_that("bar", {}),
describe("foo", {}),
g(),
h()
)
expect_equal(filter_desc(code, "foo"), code[c(1, 2)])
expect_equal(filter_desc(code, "foo"), code[c(1, 3)])
})

test_that("can select recursively", {
Expand Down Expand Up @@ -132,6 +133,57 @@ test_that("can select recursively", {
)
})

test_that("works on code like the describe() example", {
Copy link
Member Author

Choose a reason for hiding this comment

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

More pretty realistic usage that seems worth testing.

code <- exprs(
describe("math library", {
x1 <- 1
x2 <- 1
describe("addition()", {
it("can add two numbers", {
expect_equal(x1 + x2, addition(x1, x2))
})
})
describe("division()", {
x1 <- 10
x2 <- 2
it("can divide two numbers", {
expect_equal(x1 / x2, division(x1, x2))
})
it("can handle division by 0") #not yet implemented
})
})
)

expect_equal(
filter_desc(
code,
c("math library", "division()", "can divide two numbers")
),
exprs(
describe("math library", {
x1 <- 1
x2 <- 1
describe("division()", {
x1 <- 10
x2 <- 2
it("can divide two numbers", {
expect_equal(x1 / x2, division(x1, x2))
})
})
})
)
)

# what happens for an unimplemented specification?
expect_snapshot(
error = TRUE,
filter_desc(
code,
c("math library", "division()", "can handle division by 0")
)
)
})

test_that("preserve srcrefs", {
code <- parse(
keep.source = TRUE,
Expand Down
Loading