Skip to content

Commit 22fafa4

Browse files
committed
Refine the structure of describe and it
Also evaluate `describe()` with `test_code()`. Then refactor `test_code()` to run `local_test_context()` and find a default reporter, which simplifies calls and makes test code more reliably the same Fixes #2007
1 parent f56ecfd commit 22fafa4

File tree

4 files changed

+34
-51
lines changed

4 files changed

+34
-51
lines changed

R/describe.R

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,16 @@ describe <- function(description, code) {
6262
describe_description <- description
6363

6464
# prepares a new environment for each it-block
65-
describe_environment <- new.env(parent = parent.frame())
66-
describe_environment$it <- function(description, code = NULL) {
65+
describe_env <- new.env(parent = parent.frame())
66+
describe_env$it <- function(description, code = NULL) {
6767
check_string(description, allow_empty = FALSE)
68-
code <- substitute(code)
69-
7068
description <- paste0(describe_description, ": ", description)
71-
describe_it(description, code, describe_environment)
72-
}
7369

74-
eval(substitute(code), describe_environment)
75-
invisible()
76-
}
77-
78-
describe_it <- function(description, code, env = parent.frame()) {
79-
reporter <- get_reporter() %||% local_interactive_reporter()
80-
local_test_context()
70+
code <- substitute(code)
71+
test_code(description, code, env = describe_env, skip_on_empty = FALSE)
72+
}
8173

82-
test_code(
83-
description,
84-
code,
85-
env = env,
86-
reporter = reporter,
87-
skip_on_empty = FALSE
88-
)
74+
test_code(description, code, describe_env, skip_on_empty = FALSE)
8975
}
9076

9177
#' @export
@@ -94,5 +80,5 @@ it <- function(description, code = NULL) {
9480
check_string(description, allow_empty = FALSE)
9581

9682
code <- substitute(code)
97-
describe_it(description, code, env = parent.frame())
83+
test_code(description, code, env = parent.frame(), skip_on_empty = FALSE)
9884
}

R/test-that.R

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,16 @@ test_that <- function(desc, code) {
4646
}
4747
}
4848

49-
# Must initialise interactive reporter before local_test_context()
50-
reporter <- get_reporter() %||% local_interactive_reporter()
51-
local_test_context()
52-
53-
test_code(
54-
desc,
55-
code,
56-
env = parent.frame(),
57-
reporter = reporter
58-
)
49+
test_code(desc, code, env = parent.frame())
5950
}
6051

6152
# Access error fields with `[[` rather than `$` because the
6253
# `$.Throwable` from the rJava package throws with unknown fields
63-
test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
54+
test_code <- function(test, code, env, reporter = NULL, skip_on_empty = TRUE) {
55+
# Must initialise interactive reporter before local_test_context()
56+
reporter <- get_reporter() %||% local_interactive_reporter()
57+
local_test_context()
58+
6459
frame <- caller_env()
6560

6661
if (!is.null(test)) {

tests/testthat/_snaps/describe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# describe: has to have a valid description for the block
1+
# has to have a valid description for the block
22

33
Code
44
describe()

tests/testthat/test-describe.R

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
someExternalVariable <- 1
21
describe("describe", {
32
it("can contain nested describe blocks", {
43
describe("addition", {
@@ -18,36 +17,39 @@ describe("describe", {
1817
it("can be shown that P != NP")
1918
})
2019
})
20+
})
2121

22-
it("has to have a valid description for the block", {
23-
expect_snapshot(error = TRUE, {
24-
describe()
25-
describe("")
26-
describe(c("a", "b"))
27-
28-
it()
29-
it("")
30-
it(c("a", "b"))
31-
})
32-
})
33-
22+
someExternalVariable <- 1
23+
describe("variable scoping", {
3424
someInternalVariable <- 1
3525
it("should be possible to use variables from outer environments", {
3626
expect_equal(1, someExternalVariable)
3727
expect_equal(1, someInternalVariable)
28+
29+
someInternalVariable <- 2
30+
someExternalVariable <- 3
3831
})
3932

4033
# prefix is needed to test this use case
41-
testthat::it("should be possible to use variables from outer environments with package prefix", {
34+
testthat::it("even when using it() directly", {
4235
expect_equal(1, someExternalVariable)
4336
expect_equal(1, someInternalVariable)
4437
})
4538

46-
it("should not be possible to access variables from other specs (1)", {
47-
some_test_var <- 5
39+
it("shouldn't affect other tests", {
40+
expect_equal(1, someExternalVariable)
41+
expect_equal(1, someInternalVariable)
4842
})
43+
})
44+
45+
test_that("has to have a valid description for the block", {
46+
expect_snapshot(error = TRUE, {
47+
describe()
48+
describe("")
49+
describe(c("a", "b"))
4950

50-
it("should not be possible to access variables from other specs (2)", {
51-
expect_false(exists("some_test_var"))
51+
it()
52+
it("")
53+
it(c("a", "b"))
5254
})
5355
})

0 commit comments

Comments
 (0)