Skip to content

Commit f2ea828

Browse files
committed
Implemented selection of deeply nested tests
1 parent 9bfe131 commit f2ea828

File tree

2 files changed

+105
-29
lines changed

2 files changed

+105
-29
lines changed

R/source.R

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,43 +73,57 @@ filter_desc <- function(exprs, desc = NULL, error_call = caller_env()) {
7373
if (is.null(desc)) {
7474
return(exprs)
7575
}
76+
desc_levels <- if (is.list(desc)) {
77+
desc
78+
} else {
79+
as.list(desc)
80+
}
7681

77-
found <- FALSE
78-
include <- rep(FALSE, length(exprs))
79-
80-
for (i in seq_along(exprs)) {
81-
expr <- exprs[[i]]
82-
83-
if (!is_call(expr, c("test_that", "describe"), n = 2)) {
84-
if (!found) {
85-
include[[i]] <- TRUE
86-
}
82+
find_matching_expr <- function(exprs, queue) {
83+
if (length(queue) == 0) {
84+
exprs
8785
} else {
88-
if (!is_string(expr[[2]])) {
89-
next
86+
found <- FALSE
87+
include <- rep(FALSE, length(exprs))
88+
desc <- queue[[1]]
89+
90+
for (i in seq_along(exprs)) {
91+
expr <- exprs[[i]]
92+
93+
if (!is_call(expr, c("test_that", "describe", "it"), n = 2)) {
94+
if (!found) {
95+
include[[i]] <- TRUE
96+
}
97+
} else {
98+
if (!is_string(expr[[2]])) {
99+
next
100+
}
101+
102+
test_desc <- as.character(expr[[2]])
103+
if (test_desc != desc) {
104+
next
105+
}
106+
107+
if (found) {
108+
abort(
109+
"Found multiple tests with specified description",
110+
call = error_call
111+
)
112+
}
113+
include[[i]] <- TRUE
114+
found <- TRUE
115+
exprs[[i]][[3]] <- find_matching_expr(expr[[3]], queue[-1])
116+
}
90117
}
91118

92-
test_desc <- as.character(expr[[2]])
93-
if (test_desc != desc) {
94-
next
119+
if (!found) {
120+
abort("Failed to find test with specified description", call = error_call)
95121
}
96122

97-
if (found) {
98-
abort(
99-
"Found multiple tests with specified description",
100-
call = error_call
101-
)
102-
}
103-
include[[i]] <- TRUE
104-
found <- TRUE
123+
exprs[include]
105124
}
106125
}
107-
108-
if (!found) {
109-
abort("Failed to find test with specified description", call = error_call)
110-
}
111-
112-
exprs[include]
126+
find_matching_expr(exprs, desc_levels)
113127
}
114128

115129
#' @rdname source_file

tests/testthat/test-source.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,65 @@ test_that("source_dir()", {
137137
)
138138
expect_equal(res[[1]](), "Hello World")
139139
})
140+
141+
test_that("you can select deeply nested describe(...)", {
142+
code <- exprs(
143+
f(),
144+
describe("level 0", {
145+
g()
146+
describe("level 1 A", {
147+
h()
148+
describe("level 2 A", {
149+
i()
150+
it("level 3 A", {
151+
j()
152+
expect_equal(1, 1)
153+
k()
154+
})
155+
l()
156+
})
157+
m()
158+
describe("level 2 B", {
159+
n()
160+
it("level 3 B", {
161+
o()
162+
expect_equal(1, 1)
163+
p()
164+
})
165+
r()
166+
})
167+
s()
168+
describe("level 2 C", {
169+
expect_equal(1, 1)
170+
})
171+
t()
172+
})
173+
u()
174+
describe("level 1 B", {})
175+
v()
176+
}),
177+
x()
178+
)
179+
180+
expected <- exprs(
181+
f(),
182+
describe("level 0", {
183+
g()
184+
describe("level 1 A", {
185+
h()
186+
m()
187+
describe("level 2 B", {
188+
n()
189+
it("level 3 B", {
190+
o()
191+
expect_equal(1, 1)
192+
p()
193+
})
194+
r()
195+
})
196+
})
197+
})
198+
)
199+
200+
expect_equal(filter_desc(code, c("level 0", "level 1 A", "level 2 B")), expected)
201+
})

0 commit comments

Comments
 (0)