Skip to content

Commit 26cdea5

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

File tree

2 files changed

+111
-28
lines changed

2 files changed

+111
-28
lines changed

R/source.R

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,43 +73,59 @@ 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
90-
}
91-
92-
test_desc <- as.character(expr[[2]])
93-
if (test_desc != desc) {
94-
next
95-
}
96-
97-
if (found) {
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) {
98108
abort(
99109
"Found multiple tests with specified description",
100110
call = error_call
101111
)
112+
}
113+
include[[i]] <- TRUE
114+
found <- TRUE
115+
exprs[[i]][[3]] <- {
116+
find_matching_expr(expr[[3]], queue[-1])
117+
}
118+
}
102119
}
103-
include[[i]] <- TRUE
104-
found <- TRUE
105-
}
106-
}
107120

108-
if (!found) {
109-
abort("Failed to find test with specified description", call = error_call)
110-
}
121+
if (!found) {
122+
abort("Failed to find test with specified description", call = error_call)
123+
}
111124

112-
exprs[include]
125+
exprs[include]
126+
}
127+
}
128+
find_matching_expr(exprs, desc_levels)
113129
}
114130

115131
#' @rdname source_file

tests/testthat/test-source.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,70 @@ 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+
s()
197+
t()
198+
})
199+
u()
200+
v()
201+
}),
202+
x()
203+
)
204+
205+
expect_equal(filter_desc(code, c("level 0", "level 1 A", "level 2 B")), expected)
206+
})

0 commit comments

Comments
 (0)