Skip to content

Commit 1addb90

Browse files
committed
More tests of running a nested subtest
1 parent 7eb22e8 commit 1addb90

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

R/source.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' @param env Environment in which to evaluate code.
88
#' @param desc A character vector used to filter tests. This is used to
99
#' (recursively) filter the content of the file, so that only the non-test
10-
#' code up to and including the match test is run.
10+
#' code up to and including the matching test is run.
1111
#' @param chdir Change working directory to `dirname(path)`?
1212
#' @param wrap Automatically wrap all code within [test_that()]? This ensures
1313
#' that all expectations are reported, even if outside a test block.

man/source_file.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/source.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
Error:
2727
! `env` must be an environment, not the string "x".
2828

29+
# works on code like the describe() example
30+
31+
Code
32+
filter_desc(code, c("math library", "division()", "can handle division by 0"))
33+
Condition
34+
Error:
35+
! Failed to find test with description "can handle division by 0".
36+
2937
# preserve srcrefs
3038

3139
Code

tests/testthat/test-source.R

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ test_that("checks its inputs", {
8484

8585
# filter_desc -------------------------------------------------------------
8686

87-
test_that("works with all tests types", {
87+
test_that("works with all subtest types", {
8888
code <- exprs(
8989
test_that("foo", {}),
9090
describe("bar", {}),
@@ -95,14 +95,15 @@ test_that("works with all tests types", {
9595
expect_equal(filter_desc(code, "baz"), code[3])
9696
})
9797

98-
test_that("only returns code before subtest", {
98+
test_that("only returns non-subtest code before subtest", {
9999
code <- exprs(
100100
f(),
101+
test_that("bar", {}),
101102
describe("foo", {}),
102103
g(),
103104
h()
104105
)
105-
expect_equal(filter_desc(code, "foo"), code[c(1, 2)])
106+
expect_equal(filter_desc(code, "foo"), code[c(1, 3)])
106107
})
107108

108109
test_that("can select recursively", {
@@ -132,6 +133,57 @@ test_that("can select recursively", {
132133
)
133134
})
134135

136+
test_that("works on code like the describe() example", {
137+
code <- exprs(
138+
describe("math library", {
139+
x1 <- 1
140+
x2 <- 1
141+
describe("addition()", {
142+
it("can add two numbers", {
143+
expect_equal(x1 + x2, addition(x1, x2))
144+
})
145+
})
146+
describe("division()", {
147+
x1 <- 10
148+
x2 <- 2
149+
it("can divide two numbers", {
150+
expect_equal(x1 / x2, division(x1, x2))
151+
})
152+
it("can handle division by 0") #not yet implemented
153+
})
154+
})
155+
)
156+
157+
expect_equal(
158+
filter_desc(
159+
code,
160+
c("math library", "division()", "can divide two numbers")
161+
),
162+
exprs(
163+
describe("math library", {
164+
x1 <- 1
165+
x2 <- 1
166+
describe("division()", {
167+
x1 <- 10
168+
x2 <- 2
169+
it("can divide two numbers", {
170+
expect_equal(x1 / x2, division(x1, x2))
171+
})
172+
})
173+
})
174+
)
175+
)
176+
177+
# what happens for an unimplemented specification?
178+
expect_snapshot(
179+
error = TRUE,
180+
filter_desc(
181+
code,
182+
c("math library", "division()", "can handle division by 0")
183+
)
184+
)
185+
})
186+
135187
test_that("preserve srcrefs", {
136188
code <- parse(
137189
keep.source = TRUE,

0 commit comments

Comments
 (0)