Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme element unit tests #2081

Merged
merged 12 commits into from
Dec 27, 2024
27 changes: 27 additions & 0 deletions tests/testthat/_snaps/theme_elements_gtsummary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# pkgwide-str:language works

Code
show_header_names(gts_2)
Output
Column Name Header level* N* n* p*
label "**Característica**" 200 <int>
stat_1 "**Drug A** \nN = 98" Drug A <chr> 200 <int> 98 <int> 0.490 <dbl>
stat_2 "**Drug B** \nN = 102" Drug B <chr> 200 <int> 102 <int> 0.510 <dbl>
p.value "**p-valor**" 200 <int>

Message
* These values may be dynamically placed into headers (and other locations).
i Review the `modify_header()` (`?gtsummary::modify()`) help for examples.

# pkgwide-str:theme_name works

Code
set_gtsummary_theme(my_theme_5)
Message
Setting theme "Super Cool Themey Theme"

# pkgwide-fun:pre_conversion works

Code
gts_6

212 changes: 212 additions & 0 deletions tests/testthat/test-theme_elements_gtsummary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
# Package-wide Unit Tests-------------------------------------------------------
## pkgwide-fn:prependpvalue_fun-------------------------------------------------
test_that("pkgwide-fn:prependpvalue_fun works", {

# Create a theme#
my_theme_1 <-
list(
# Prepend p value, with 3 place digits
"pkgwide-fn:prependpvalue_fun" = label_style_pvalue(digits = 3, prepend_p = TRUE)
)

# Set the theme
set_gtsummary_theme(my_theme_1)
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved

# Store the table#
gts_1 <-
trial |>
tbl_summary(
by = trt,
include = c(trt, age),
) |>
add_p()

# Test that the p-value has 3 digits#
expect_true(
inline_text(gts_1, variable = age, column = "p.value") |>
grepl(pattern = "p=0.\\d{3}", x = _)
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved
)
# Reset the theming#
reset_gtsummary_theme()
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved
}
)

## Other pkgwide Tests----------------------------------------------------------
# Create a theme#
my_theme_2 <-
list(
# Prepend p value, with 3 place digits
"pkgwide-fn:pvalue_fun" = label_style_pvalue(digits = 1, prefix = "Totally Awesome P Value = ", "OutDec" = ","),
# set messaging to quiet
"pkgwide-lgl:quiet" = TRUE,
# configurar el idioma en español#
"pkgwide-str:language" = "es"
)

# Set the theme
set_gtsummary_theme(my_theme_2)
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved

# Store the table#
gts_2 <-
trial |>
tbl_summary(
by = trt,
statistic = age ~ "{mean} ({sd})",
include = c(trt, age),
) |>
add_p()

### pkgwide-fn:pvalue_fun--------------------------------------------------------
test_that("pkgwide-fn:pvalue_fun works", {

# Test that the p-value has the correct digits and prefix#
expect_true(
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved
inline_text.gtsummary(gts_2, variable = age, column = "p.value") |>
grepl(pattern = "Totally Awesome P Value = 0.\\d{1}", x = _)
)

}
)

## pkgwide-lgl:quiet------------------------------------------------------------
test_that("pkgwide-lgl:quiet works", {

# Test that the lgl value can be found#
expect_true(
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved
get_theme_element("pkgwide-lgl:quiet")
)
}
)

## pkgwide-str:language---------------------------------------------------------
test_that("pkgwide-str:language works", {

# Test that the CI has the correct pattern somewhere#
expect_snapshot(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what this snapshot is testing? can you add some more details about the expected output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was trying to test that translated columns names were returned. Not sure why I did this either, so changed it!

show_header_names(gts_2)
)

}
)

# Reset the theme
reset_gtsummary_theme()
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved

## pkgwide-str:ci.sep-----------------------------------------------------------
test_that("pkgwide-str:ci.sep works", {

# Create a theme#
my_theme_3 <-
list(
# Set CI sep to be something *cute*
"pkgwide-str:ci.sep" = "~*~",
# Have the print engine be kable
"pkgwide-str:print_engine" = "kable"
)

# Set the theme
set_gtsummary_theme(my_theme_3)

gts_3 <-
with_gtsummary_theme(
my_theme_3,
glm(response ~ age + stage, trial, family = binomial) |>
tbl_regression(x = _, exponentiate = TRUE),
)

# Test that the CI has the correct pattern somewhere#
expect_true(
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved
gts_3$table_body$ci |>
grepl("~*~", x = _) |>
any()
)

# Reset the theme
reset_gtsummary_theme()
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved

}
)

# Reset the theming#
reset_gtsummary_theme()
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved

## pkgwide-str:print_engine-----------------------------------------------------
test_that("pkgwide-str:print_engine works", {

# Create a theme#
my_theme_4 <-
list(
# Have the print engine be kable
"pkgwide-str:print_engine" = "kable"
)

# set the theme
set_gtsummary_theme(my_theme_4)

# Set the theme to check that table is in kable format
gts_4 <-
with_gtsummary_theme(
my_theme_4,
trial |>
dplyr::select(death, trt) |>
tbl_summary(by = trt)
)

# This only works when a theme is set explicitly
# And not when it is just temporarily set with 'with_gtsummary_theme'
# Is this intentional behavior?
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved
expect_true(grepl("|:-", gts_4) |> any())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be easier to read as a snapshot test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I'm understanding the print engine thing a bit better, I reverted back to the original test I tried to do when I first started. I can change it to a snapshot if you'd prefer that?


# Reset the theme
reset_gtsummary_theme()
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved
}
)

## pkgwide-str:theme_name-------------------------------------------------------
test_that("pkgwide-str:theme_name works", {
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved

# Create a theme#
my_theme_5 <-
list(
# Set a theme name
"pkgwide-str:theme_name" = "Super Cool Themey Theme"
)

expect_snapshot(
set_gtsummary_theme(my_theme_5)
)

# Reset the theme
reset_gtsummary_theme()
}
)


## pkgwide-fun:pre_conversion---------------------------------------------------
test_that("pkgwide-fun:pre_conversion works", {
Meghansaha marked this conversation as resolved.
Show resolved Hide resolved

# Create a theme#
my_theme_6 <-
list(
# Set a fx to use in pre conversion
"pkgwide-fun:pre_conversion" = add_ci
)

# Set the theme
set_gtsummary_theme(my_theme_6)
gts_6 <-
trial |>
dplyr::select(death, trt) |>
tbl_summary(by = trt)

# This only works when a theme is set explicitly
# And not when it is just temporarily set with 'with_gtsummary_theme'
# Is this intentional behavior?
expect_snapshot(
gts_6
)

# Reset the theme
reset_gtsummary_theme()
}
)