Skip to content

Commit df392f3

Browse files
committed
clarify tests structure
1 parent c2975fe commit df392f3

File tree

1 file changed

+35
-56
lines changed

1 file changed

+35
-56
lines changed

tests/testthat/test-spin.R

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
# file specific helpers ----
2+
create_test_script <- function(
3+
name,
4+
content = c("x <- 1", "y <- 2"),
5+
envir = rlang::caller_env()
6+
) {
7+
tmp_dir <- withr::local_tempdir(.local_envir = envir)
8+
withr::local_dir(tmp_dir, .local_envir = envir)
9+
script <- name
10+
xfun::write_utf8(content, script)
11+
script
12+
}
13+
14+
expect_preamble_added <- function(script, snapshot_name) {
15+
expect_message(
16+
result <- add_spin_preamble(script),
17+
"Added spin preamble"
18+
)
19+
expect_equal(result, script)
20+
21+
announce_snapshot_file(name = snapshot_name)
22+
expect_snapshot_file(script, snapshot_name)
23+
}
24+
25+
# Tests ----
26+
127
test_that("add_spin_preamble checks for file existence", {
228
expect_snapshot(
329
error = TRUE,
@@ -6,29 +32,12 @@ test_that("add_spin_preamble checks for file existence", {
632
})
733

834
test_that("add_spin_preamble adds preamble to file without one", {
9-
# Create temporary file
10-
tmp_dir <- withr::local_tempdir()
11-
withr::local_dir(tmp_dir)
12-
script <- "report.R"
13-
writeLines(c("x <- 1", "y <- 2"), script)
14-
15-
# Add preamble
16-
expect_message(
17-
result <- add_spin_preamble(script),
18-
"Added spin preamble"
19-
)
20-
21-
# Check return value
22-
expect_equal(result, script)
23-
24-
announce_snapshot_file(name = "spin_preamble.R")
25-
expect_snapshot_file(script, "spin_preamble.R")
35+
script <- create_test_script("report.R")
36+
expect_preamble_added(script, "spin_preamble.R")
2637

2738
skip_on_cran()
2839
skip_if_no_quarto("1.4.511")
29-
expect_no_error(
30-
quarto_render(script, quiet = TRUE)
31-
)
40+
expect_no_error(quarto_render(script, quiet = TRUE))
3241
expect_true(file.exists(xfun::with_ext(script, "html")))
3342
})
3443

@@ -78,46 +87,25 @@ test_that("add_spin_preamble detects preamble with leading whitespace", {
7887
})
7988

8089
test_that("add_spin_preamble works with empty file", {
81-
tmp_dir <- withr::local_tempdir()
82-
withr::local_dir(tmp_dir)
83-
script <- "report.R"
84-
writeLines("", script) # Empty file
85-
86-
# Add preamble
87-
expect_message(
88-
result <- add_spin_preamble(script),
89-
"Added spin preamble"
90-
)
91-
92-
# Check return value
93-
expect_equal(result, script)
94-
95-
announce_snapshot_file(name = "spin_preamble-empty.R")
96-
expect_snapshot_file(script, "spin_preamble-empty.R")
90+
script <- create_test_script("report.R", "")
91+
expect_preamble_added(script, "spin_preamble-empty.R")
9792
})
9893

9994
test_that("add_spin_preamble works with custom title", {
100-
tmp_dir <- withr::local_tempdir()
101-
withr::local_dir(tmp_dir)
102-
script <- "analysis.R"
103-
xfun::write_utf8(c("x <- 1", "y <- 2"), script)
95+
script <- create_test_script("analysis.R")
10496

10597
expect_message(
10698
result <- add_spin_preamble(script, title = "Custom Analysis"),
10799
"Added spin preamble"
108100
)
109-
110101
expect_equal(result, script)
111102

112103
announce_snapshot_file(name = "spin_preamble-custom-title.R")
113104
expect_snapshot_file(script, "spin_preamble-custom-title.R")
114105
})
115106

116107
test_that("add_spin_preamble works with custom preamble", {
117-
tmp_dir <- withr::local_tempdir()
118-
withr::local_dir(tmp_dir)
119-
script <- "report.R"
120-
xfun::write_utf8(c("library(ggplot2)", "plot(1:10)"), script)
108+
script <- create_test_script("report.R", c("library(ggplot2)", "plot(1:10)"))
121109

122110
expect_message(
123111
result <- add_spin_preamble(
@@ -130,18 +118,14 @@ test_that("add_spin_preamble works with custom preamble", {
130118
),
131119
"Added spin preamble"
132120
)
133-
134121
expect_equal(result, script)
135122

136123
announce_snapshot_file(name = "spin_preamble-custom-preamble.R")
137124
expect_snapshot_file(script, "spin_preamble-custom-preamble.R")
138125
})
139126

140127
test_that("title parameter overrides preamble title", {
141-
tmp_dir <- withr::local_tempdir()
142-
withr::local_dir(tmp_dir)
143-
script <- "override.R"
144-
writeLines("x <- 1", script)
128+
script <- create_test_script("override.R", "x <- 1")
145129

146130
expect_message(
147131
result <- add_spin_preamble(
@@ -151,18 +135,14 @@ test_that("title parameter overrides preamble title", {
151135
),
152136
"Added spin preamble"
153137
)
154-
155138
expect_equal(result, script)
156139

157140
announce_snapshot_file(name = "spin_preamble-title-override.R")
158141
expect_snapshot_file(script, "spin_preamble-title-override.R")
159142
})
160143

161144
test_that("preamble title is used when title parameter is NULL", {
162-
tmp_dir <- withr::local_tempdir()
163-
withr::local_dir(tmp_dir)
164-
script <- "preamble_title.R"
165-
writeLines("x <- 1", script)
145+
script <- create_test_script("preamble_title.R", "x <- 1")
166146

167147
expect_message(
168148
result <- add_spin_preamble(
@@ -172,7 +152,6 @@ test_that("preamble title is used when title parameter is NULL", {
172152
),
173153
"Added spin preamble"
174154
)
175-
176155
expect_equal(result, script)
177156

178157
announce_snapshot_file(name = "spin_preamble-preamble-title.R")

0 commit comments

Comments
 (0)