😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

112 lines
4.0KB

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