Skip to content

Commit 21b05b5

Browse files
authored
Merge pull request #397 from stan-dev/testthat-3e
Testthat 3e
2 parents 94f2365 + 1da4d23 commit 21b05b5

30 files changed

+62
-132
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ images/*
1414
book/*
1515
docs/*
1616
Rplots.pdf
17+
.vscode/*
1718
^\.github$
1819
^release-prep\.R$

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ revdep/*
1313
CRAN-RELEASE
1414
CRAN-SUBMISSION
1515
release-prep.R
16+
17+
# vscode/positron/etc settings
18+
.vscode/*

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ Suggests:
5656
scales,
5757
shinystan (>= 2.3.0),
5858
survival,
59-
testthat (>= 2.0.0),
59+
testthat (>= 3.0.0),
6060
vdiffr (>= 1.0.2)
6161
RoxygenNote: 7.3.3
6262
VignetteBuilder: knitr
6363
Encoding: UTF-8
6464
Roxygen: list(markdown = TRUE)
65+
Config/testthat/edition: 3

tests/testthat/test-aesthetics.R

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
library(bayesplot)
2-
context("Aesthetics")
3-
4-
51
# color scheme stuff ------------------------------------------------------
62

73
prepare_colors_for_test <- function(scheme) {
@@ -13,13 +9,13 @@ prepare_colors_for_test <- function(scheme) {
139

1410
test_that("getting and setting the color scheme works", {
1511
color_scheme_set("red")
16-
expect_equivalent(color_scheme_get(), prepare_colors_for_test("red"))
12+
expect_equal(color_scheme_get(), prepare_colors_for_test("red"), ignore_attr = TRUE)
1713
expect_named(prepare_colors_for_test("blue"), scheme_level_names())
1814
expect_named(color_scheme_get(), scheme_level_names())
1915
for (clr in names(master_color_list)) {
2016
color_scheme_set(clr)
21-
expect_equivalent(color_scheme_get(), prepare_colors_for_test(clr),
22-
info = clr)
17+
expect_equal(color_scheme_get(), prepare_colors_for_test(clr),
18+
info = clr, ignore_attr = TRUE)
2319
expect_named(color_scheme_get(), scheme_level_names())
2420
}
2521

@@ -28,7 +24,7 @@ test_that("getting and setting the color scheme works", {
2824
expect_gg(plot(color_scheme_get("mix-blue-green")))
2925

3026
color_scheme_set("blue")
31-
expect_equivalent(color_scheme_get("teal"), prepare_colors_for_test("teal"))
27+
expect_equal(color_scheme_get("teal"), prepare_colors_for_test("teal"), ignore_attr = TRUE)
3228

3329
# error if not character
3430
expect_error(color_scheme_set(7), "'scheme' should be a character vector of length 1 or 6")
@@ -60,10 +56,10 @@ test_that("color_scheme_get with i argument works", {
6056

6157
test_that("setting mixed scheme works", {
6258
color_scheme_set("mix-gray-blue")
63-
expect_equivalent(color_scheme_get(), mixed_scheme("gray", "blue"))
59+
expect_equal(color_scheme_get(), mixed_scheme("gray", "blue"), ignore_attr = TRUE)
6460

6561
color_scheme_set("mix-blue-gray")
66-
expect_equivalent(color_scheme_get(), mixed_scheme("blue", "gray"))
62+
expect_equal(color_scheme_get(), mixed_scheme("blue", "gray"), ignore_attr = TRUE)
6763

6864
expect_error(color_scheme_set("mix-green-reds"), "should be one of")
6965
expect_error(color_scheme_set("mix-greens-red"), "should be one of")
@@ -72,9 +68,9 @@ test_that("setting mixed scheme works", {
7268
test_that("setting brewer scheme works", {
7369
skip_if_not_installed("RColorBrewer")
7470
color_scheme_set("brewer-Blues")
75-
expect_equivalent(unlist(color_scheme_get()), RColorBrewer::brewer.pal(6, "Blues"))
71+
expect_equal(unlist(color_scheme_get()), RColorBrewer::brewer.pal(6, "Blues"), ignore_attr = TRUE)
7672
color_scheme_set("brewer-Spectral")
77-
expect_equivalent(unlist(color_scheme_get()), RColorBrewer::brewer.pal(6, "Spectral"))
73+
expect_equal(unlist(color_scheme_get()), RColorBrewer::brewer.pal(6, "Spectral"), ignore_attr = TRUE)
7874
expect_error(color_scheme_set("brewer-FAKE"), "FAKE is not a valid palette")
7975
})
8076

@@ -106,11 +102,11 @@ test_that("mixed_scheme internal function doesn't error", {
106102
test_that("custom color schemes work", {
107103
color_scheme_set(orange_scheme_ok)
108104
expect_named(color_scheme_get())
109-
expect_equivalent(unlist(color_scheme_get()), orange_scheme_ok)
105+
expect_equal(unlist(color_scheme_get()), orange_scheme_ok, ignore_attr = TRUE)
110106

111107
random_scheme <- colors()[sample(length(colors()), 6)]
112108
color_scheme_set(random_scheme)
113-
expect_equivalent(unlist(color_scheme_get()), random_scheme)
109+
expect_equal(unlist(color_scheme_get()), random_scheme, ignore_attr = TRUE)
114110
})
115111

116112
test_that("get_color returns correct color values", {

tests/testthat/test-available_ppc.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
library(bayesplot)
2-
context("available_mcmc and available_ppc")
3-
4-
51
test_that("available_mcmc works", {
62
a <- available_mcmc()
73
expect_s3_class(a, "bayesplot_function_list")

tests/testthat/test-bayesplot_grid.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
library(bayesplot)
2-
context("bayesplot_grid")
3-
41
skip_if_not_installed("gridExtra")
52

63
y <- example_y_data()

tests/testthat/test-convenience-functions.R

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
1-
library(bayesplot)
21
library(ggplot2)
3-
context("Convenience functions (for ggplot objects)")
4-
52

63
# abline_01, vline_ and hline_ ------------------------------------------
74
test_that("abline_01 returns the correct object", {
85
a <- abline_01(color = "green", linetype = 2)
96
b <- geom_abline(intercept = 0, slope = 1, color = "green", linetype = 2, na.rm = TRUE)
107
a$constructor <- b$constructor <- NULL
11-
expect_equal(a, b, check.environment = FALSE)
8+
expect_equal(a, b, ignore_function_env = TRUE)
129
})
1310
test_that("vline_* and hline_* return correct objects", {
1411
a <- vline_0(color = "red")
1512
b <- geom_vline(xintercept = 0, color = "red", na.rm = TRUE)
1613
a$constructor <- b$constructor <- NULL
17-
expect_equal(a, b, check.environment = FALSE)
14+
expect_equal(a, b, ignore_function_env = TRUE)
1815

1916
a <- hline_0(linewidth = 2, linetype = 3)
2017
b <- geom_hline(yintercept = 0, linewidth = 2, linetype = 3, na.rm = TRUE)
2118
a$constructor <- b$constructor <- NULL
22-
expect_equal(a, b, check.environment = FALSE)
19+
expect_equal(a, b, ignore_function_env = TRUE)
2320

2421
a <- vline_at(c(3,4), na.rm = FALSE)
2522
b <- geom_vline(xintercept = c(3,4))
2623
a$constructor <- b$constructor <- NULL
27-
expect_equal(a, b, check.environment = FALSE)
24+
expect_equal(a, b, ignore_function_env = TRUE)
2825

2926
a <- hline_at(c(3,4), na.rm = FALSE)
3027
b <- geom_hline(yintercept = c(3,4))
3128
a$constructor <- b$constructor <- NULL
32-
expect_equal(a, b, check.environment = FALSE)
29+
expect_equal(a, b, ignore_function_env = TRUE)
3330
})
3431
test_that("vline_at with 'fun' works", {
3532
x <- example_mcmc_draws(chains = 1)
3633
a <- vline_at(x, colMeans)
3734
b <- geom_vline(xintercept = colMeans(x), na.rm = TRUE)
3835
a$constructor <- b$constructor <- NULL
39-
expect_equal(a, b, check.environment = FALSE)
36+
expect_equal(a, b, ignore_function_env = TRUE)
4037
})
4138
test_that("calc_v (internal function) works", {
4239
a <- 1:4
@@ -99,20 +96,21 @@ test_that("facet_bg returns correct theme object", {
9996
test_that("legend_none returns correct theme object", {
10097
none <- legend_none()
10198
expect_s3_class(none, "theme")
102-
expect_equivalent(none, list(legend.position = "none"))
99+
expect_equal(none$legend.position, "none", ignore_attr = TRUE)
103100
expect_false(attr(none, "complete"))
104101
})
105102
test_that("legend_move returns correct theme object", {
106103
left <- legend_move("left")
107104
expect_s3_class(left, "theme")
108-
expect_equivalent(left, list(legend.position = "left"))
105+
expect_equal(left$legend.position, "left", ignore_attr = TRUE)
109106
expect_false(attr(left, "complete"))
110107

111108
pos <- legend_move(c(0.25, 0.5))
112109
expect_s3_class(pos, "theme")
113-
expect_equivalent(
110+
expect_equal(
114111
pos$legend.position.inside %||% pos$legend.position,
115-
c(0.25, 0.5)
112+
c(0.25, 0.5),
113+
ignore_attr = TRUE
116114
)
117115
expect_false(attr(pos, "complete"))
118116
})
@@ -133,9 +131,10 @@ test_that("xaxis_text returns correct theme object", {
133131
})
134132
test_that("yaxis_text returns correct theme object", {
135133
expect_identical(yaxis_text(FALSE), theme(axis.text.y = element_blank()))
136-
expect_equivalent(
134+
expect_equal(
137135
yaxis_text(face = "bold", angle = 30),
138-
theme(axis.text.y = element_text(face = "bold", angle = 30))
136+
theme(axis.text.y = element_text(face = "bold", angle = 30)),
137+
ignore_attr = TRUE
139138
)
140139
})
141140
test_that("facet_text returns correct theme object", {
@@ -185,7 +184,7 @@ test_that("overlay_function returns the correct object", {
185184
a <- overlay_function(fun = "dnorm")
186185
b <- stat_function(fun = "dnorm", inherit.aes = FALSE)
187186
a$constructor <- b$constructor <- NULL
188-
expect_equal(a, b, check.environment = FALSE)
187+
expect_equal(a, b, ignore_function_env = TRUE)
189188
})
190189

191190

tests/testthat/test-example-draws.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
library(bayesplot)
2-
context("Example draws")
3-
41
test_that("example_mcmc_draws throws correct errors", {
52
expect_error(example_mcmc_draws(chains = 5), "chains <= 4")
63
expect_error(example_mcmc_draws(chains = 0), "chains >= 1")
@@ -26,7 +23,7 @@ test_that("example ppc data works", {
2623

2724
yrep <- example_yrep_draws()
2825
expect_type(yrep, "double")
29-
expect_is(yrep, "matrix")
26+
expect_true(is.matrix(yrep))
3027
expect_equal(ncol(yrep), length(y))
3128

3229
group <- example_group_data()

tests/testthat/test-extractors.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
library(bayesplot)
2-
context("Extractors")
3-
41
if (requireNamespace("rstanarm", quietly = TRUE)) {
52
ITER <- 1000
63
CHAINS <- 3
@@ -84,7 +81,7 @@ test_that("neff_ratio.stanreg returns correct structure", {
8481
ratio <- neff_ratio(fit)
8582
expect_named(ratio)
8683
ans <- summary(fit)[1:length(ratio), "n_eff"] / (floor(ITER / 2) * CHAINS)
87-
expect_equal(ratio, ans, tol = 0.001)
84+
expect_equal(ratio, ans, tolerance = 0.001)
8885
})
8986

9087
test_that("rhat.stanfit returns correct structure", {
@@ -107,12 +104,12 @@ test_that("neff_ratio.stanreg returns correct structure", {
107104
ratio <- neff_ratio(fit$stanfit)
108105
expect_named(ratio)
109106
ans <- summary(fit)[, "n_eff"] / denom
110-
expect_equal(ratio, ans, tol = 0.001)
107+
expect_equal(ratio, ans, tolerance = 0.001)
111108

112109
ratio2 <- neff_ratio(fit$stanfit, pars = c("wt", "sigma"))
113110
expect_named(ratio2)
114111
ans2 <- summary(fit, pars = c("wt", "sigma"))[, "n_eff"] / denom
115-
expect_equal(ratio2, ans2, tol = 0.001)
112+
expect_equal(ratio2, ans2, tolerance = 0.001)
116113
})
117114

118115
test_that("cmdstanr methods work", {

tests/testthat/test-helpers-mcmc.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
library(bayesplot)
2-
context("MCMC: misc. functions")
3-
41
source(test_path("data-for-mcmc-tests.R"))
52

6-
7-
83
# melt_mcmc ----------------------------------------------------------------
94
test_that("melt_mcmc does not convert integer parameter names to integers #162", {
105
mat2 <- mat[, 1:2]
@@ -303,7 +298,11 @@ test_that("diagnostic_factor.rhat works", {
303298
high = 1.2, high = 1.7))
304299

305300
r <- diagnostic_factor(unname(rhats))
306-
expect_equivalent(r, as.factor(names(rhats)))
301+
expect_equal(
302+
r,
303+
factor(names(rhats), levels = c("low", "ok", "high")),
304+
ignore_attr = TRUE
305+
)
307306
expect_identical(levels(r), c("low", "ok", "high"))
308307
})
309308
test_that("diagnostic_factor.neff_ratio works", {
@@ -312,7 +311,11 @@ test_that("diagnostic_factor.neff_ratio works", {
312311
high = 0.51, high = 0.99, high = 1))
313312

314313
r <- diagnostic_factor(unname(ratios))
315-
expect_equivalent(r, as.factor(names(ratios)))
314+
expect_equal(
315+
r,
316+
factor(names(ratios), levels = c("low", "ok", "high")),
317+
ignore_attr = TRUE
318+
)
316319
expect_identical(levels(r), c("low", "ok", "high"))
317320
})
318321

0 commit comments

Comments
 (0)