😎 Give your xaringan slides some style
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

117 líneas
4.2KB

  1. test_theme_file <- function(theme = "duo", theme_file = paste0(theme, ".css"), ...) {
  2. local_edition(2)
  3. on.exit(local_edition(3))
  4. theme_fun <- switch(
  5. theme,
  6. "duo" = style_duo,
  7. "duo_accent" = style_duo_accent,
  8. "duo_accent_inverse" = style_duo_accent_inverse,
  9. "mono_accent" = style_mono_accent,
  10. "mono_accent_inverse" = style_mono_accent_inverse,
  11. "mono_dark" = style_mono_dark,
  12. "mono_light" = style_mono_light,
  13. "solarized_dark" = style_solarized_dark,
  14. "solarized_light" = style_solarized_light,
  15. style_xaringan
  16. )
  17. tmpfile <- tempfile()
  18. on.exit(unlink(tmpfile), add = TRUE)
  19. theme_fun(outfile = tmpfile, ..., text_font_google = google_font("Noto Serif"))
  20. theme_css <- readLines(tmpfile)
  21. # Mask package version in test files
  22. theme_css <- sub("( \\* Version: )[\\d.-]+", "\\1a.b.c.d.eeee", theme_css, perl = TRUE)
  23. theme_css <- paste(theme_css, collapse = "\n")
  24. expect_known_output(cat(theme_css), test_path("css", theme_file))
  25. }
  26. test_that("style_duo()", { test_theme_file("duo") })
  27. test_that("style_duo_accent()", { test_theme_file("duo_accent") })
  28. test_that("style_duo_accent_inverse()", { test_theme_file("duo_accent_inverse") })
  29. test_that("style_mono_accent()", { test_theme_file("mono_accent") })
  30. test_that("style_mono_accent_inverse()", { test_theme_file("mono_accent_inverse") })
  31. test_that("style_mono_dark()", { test_theme_file("mono_dark") })
  32. test_that("style_mono_light()", { test_theme_file("mono_light") })
  33. test_that("style_solarized_dark()", { test_theme_file("solarized_dark") })
  34. test_that("style_solarized_light()", { test_theme_file("solarized_light") })
  35. test_that("google fonts in theme", {
  36. test_theme_file(
  37. "google_fonts",
  38. header_font_google = google_font("IBM Plex Serif", "700"),
  39. code_font_google = google_font("IBM Plex Mono")
  40. )
  41. })
  42. test_that("header_background_auto = TRUE", {
  43. test_theme_file("duo", "duo-header_bg.css", header_background_auto = TRUE)
  44. test_theme_file("mono_light", "mono_light-header_bg.css", header_background_auto = TRUE)
  45. test_theme_file("solarized_dark", "solarized_dark-header_bg.css", header_background_auto = TRUE)
  46. })
  47. test_that("style colors are added to themes", {
  48. test_theme_file("xaringan", colors = c('light-blue' = "#bad4ed"))
  49. })
  50. test_that("setting google font overrides individual font", {
  51. theme_vars <- with_clean_session(function() {
  52. xf <- xaringanthemer::style_xaringan(
  53. text_font_family = "Damogran",
  54. header_font_family = "Magrathea",
  55. code_font_google = xaringanthemer::google_font("IBM Plex Mono")
  56. )
  57. xaringanthemer:::read_css_vars(xf)
  58. })
  59. expect_equal(theme_vars$text_font_family, "Damogran")
  60. expect_false(theme_vars$text_font_is_google)
  61. expect_equal(theme_vars$header_font_family, "Magrathea")
  62. expect_false(theme_vars$header_font_is_google)
  63. expect_equal(theme_vars$code_font_family, "'IBM Plex Mono'")
  64. expect_true(theme_vars$code_font_is_google)
  65. })
  66. test_that("default fonts are correctly identified as google font", {
  67. theme_vars <- with_clean_session(function() {
  68. xf <- xaringanthemer::style_xaringan()
  69. xaringanthemer:::read_css_vars(xf)
  70. })
  71. expect_equal(theme_vars$text_font_family,
  72. quote_elements_w_spaces(xaringanthemer_font_default("text_font_family")))
  73. expect_true(theme_vars$text_font_is_google)
  74. expect_equal(theme_vars$header_font_family,
  75. quote_elements_w_spaces(xaringanthemer_font_default("header_font_family")))
  76. expect_true(theme_vars$header_font_is_google)
  77. })
  78. test_that("NULL output returns CSS as text", {
  79. expect_false(
  80. with_clean_session(function() {
  81. xaringanthemer::style_xaringan(outfile = NULL)
  82. file.exists("xaringan-themer.css")
  83. })
  84. )
  85. xt <- with_clean_session(function() {
  86. xaringanthemer::style_xaringan(outfile = NULL)
  87. })
  88. expect_type(xt, "character")
  89. expect_true(any(grepl("generated by xaringanthemer", xt)))
  90. })
  91. test_that("style_xaringan() warns about non-hex colors used by theme_xaringan()", {
  92. expect_error(
  93. with_clean_session(function() {
  94. options(warn = 2)
  95. xaringanthemer::style_xaringan(text_color = "rgb(100, 100, 100)", background_color = "white", outfile = NULL)
  96. }),
  97. regexp = "Colors.+used by.+theme_xaringan"
  98. )
  99. })