😎 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.

321 lines
11KB

  1. test_that("requires_xaringanthemer_env() errors if no style function called", {
  2. expect_error(
  3. with_clean_session(function() {
  4. xaringanthemer::requires_xaringanthemer_env(NULL, FALSE)
  5. })
  6. )
  7. })
  8. describe("theme_xaringan()", {
  9. it("errors if no css file or style_xaringan theme called", {
  10. expect_error(
  11. with_clean_session(function() {
  12. xaringanthemer::theme_xaringan()
  13. })
  14. )
  15. })
  16. it("returns a theme using the style_xaringan colors", {
  17. theme <- with_clean_session(function() {
  18. xaringanthemer::style_xaringan(
  19. text_color = "#000001",
  20. background_color = "#000002",
  21. header_color = "#000003",
  22. text_bold_color = "#000004",
  23. text_font_family = "Damogran",
  24. text_font_google = NULL,
  25. header_font_family = "Magrathea",
  26. header_font_google = NULL
  27. )
  28. xaringanthemer::theme_xaringan(
  29. text_font_use_google = FALSE,
  30. title_font_use_google = FALSE
  31. )
  32. })
  33. expect_equal(theme$text$family, "Damogran")
  34. expect_equal(theme$title$family, "Magrathea")
  35. expect_equal(theme$title$colour, "#000003")
  36. expect_equal(theme$plot.background$colour, "#000002")
  37. expect_equal(theme$plot.background$fill, "#000002")
  38. })
  39. it("returns correct theme reading from a css file", {
  40. css <- xaringanthemer::style_xaringan(
  41. text_color = "#000001",
  42. background_color = "#000002",
  43. header_color = "#000003",
  44. text_bold_color = "#000004",
  45. text_font_family = "Damogran",
  46. text_font_google = NULL,
  47. header_font_family = "Magrathea",
  48. header_font_google = NULL,
  49. outfile = NULL
  50. )
  51. theme <- with_clean_session(function(css) {
  52. writeLines(css, "xaringan-themer.css")
  53. xaringanthemer::theme_xaringan(
  54. text_font_use_google = FALSE,
  55. title_font_use_google = FALSE
  56. )
  57. }, list(css = css))
  58. expect_equal(theme$text$family, "Damogran")
  59. expect_equal(theme$title$family, "Magrathea")
  60. expect_equal(theme$title$colour, "#000003")
  61. expect_equal(theme$plot.background$colour, "#000002")
  62. expect_equal(theme$plot.background$fill, "#000002")
  63. })
  64. it("works with 3-digit hex colors", {
  65. theme <- with_clean_session(function() {
  66. xaringanthemer::style_xaringan(
  67. text_color = "#001",
  68. background_color = "#002",
  69. header_color = "#003",
  70. text_bold_color = "#004"
  71. )
  72. xaringanthemer::theme_xaringan(
  73. text_font_use_google = FALSE,
  74. title_font_use_google = FALSE
  75. )
  76. })
  77. expect_equal(theme$title$colour, "#000033")
  78. expect_equal(theme$plot.background$colour, "#000022")
  79. expect_equal(theme$plot.background$fill, "#000022")
  80. })
  81. })
  82. describe("theme_xaringan_inverse()", {
  83. it("errors if no css file or style_xaringan theme called", {
  84. expect_error(
  85. with_clean_session(function() {
  86. xaringanthemer::theme_xaringan_inverse()
  87. })
  88. )
  89. })
  90. it("returns a theme using the style_xaringan colors", {
  91. theme <- with_clean_session(function() {
  92. xaringanthemer::style_xaringan(
  93. text_color = "#000001",
  94. inverse_text_color = "#100000",
  95. background_color = "#000002",
  96. inverse_background_color = "#200000",
  97. header_color = "#000003",
  98. inverse_header_color = "#300000",
  99. text_bold_color = "#000004",
  100. text_font_family = "Damogran",
  101. text_font_google = NULL,
  102. header_font_family = "Magrathea",
  103. header_font_google = NULL
  104. )
  105. xaringanthemer::theme_xaringan_inverse(
  106. text_font_use_google = FALSE,
  107. title_font_use_google = FALSE
  108. )
  109. })
  110. expect_equal(theme$text$family, "Damogran")
  111. expect_equal(theme$title$family, "Magrathea")
  112. expect_equal(theme$title$colour, "#300000")
  113. expect_equal(theme$plot.background$colour, "#200000")
  114. expect_equal(theme$plot.background$fill, "#200000")
  115. })
  116. it("returns correct colors pulling from css file", {
  117. css <- xaringanthemer::style_xaringan(
  118. text_color = "#000001",
  119. inverse_text_color = "#100000",
  120. background_color = "#000002",
  121. inverse_background_color = "#200000",
  122. header_color = "#000003",
  123. inverse_header_color = "#300000",
  124. text_bold_color = "#000004",
  125. text_font_family = "Damogran",
  126. text_font_google = NULL,
  127. header_font_family = "Magrathea",
  128. header_font_google = NULL,
  129. outfile = NULL
  130. )
  131. theme <- with_clean_session(function(css) {
  132. writeLines(css, "basic.css")
  133. xaringanthemer::theme_xaringan_inverse(
  134. text_font_use_google = FALSE,
  135. title_font_use_google = FALSE
  136. )
  137. }, list(css = css))
  138. expect_equal(theme$text$family, "Damogran")
  139. expect_equal(theme$title$family, "Magrathea")
  140. expect_equal(theme$title$colour, "#300000")
  141. expect_equal(theme$plot.background$colour, "#200000")
  142. expect_equal(theme$plot.background$fill, "#200000")
  143. })
  144. it("works with 3-digit hex colors", {
  145. theme <- with_clean_session(function() {
  146. xaringanthemer::style_xaringan(
  147. text_color = "#001",
  148. background_color = "#002",
  149. header_color = "#003",
  150. inverse_text_color = "#100",
  151. inverse_background_color = "#200",
  152. inverse_header_color = "#300",
  153. text_bold_color = "#004"
  154. )
  155. xaringanthemer::theme_xaringan_inverse(
  156. text_font_use_google = FALSE,
  157. title_font_use_google = FALSE
  158. )
  159. })
  160. expect_equal(theme$title$colour, "#330000")
  161. expect_equal(theme$plot.background$colour, "#220000")
  162. expect_equal(theme$plot.background$fill, "#220000")
  163. })
  164. })
  165. describe("theme_xaringan_get_value()", {
  166. it("errors if no css file or style_xaringan theme called", {
  167. expect_error(
  168. with_clean_session(function() {
  169. xaringanthemer::theme_xaringan_get_value("text_font_family")
  170. })
  171. )
  172. })
  173. it("returns theme values", {
  174. theme <- with_clean_session(function() {
  175. xaringanthemer::style_xaringan(
  176. base_font_size = "33px",
  177. text_font_size = "23px",
  178. text_color = "#000001",
  179. inverse_text_color = "#100000",
  180. background_color = "#000002",
  181. inverse_background_color = "#200000",
  182. header_color = "#000003",
  183. inverse_header_color = "#300000",
  184. text_bold_color = "#000004",
  185. text_font_family = "Damogran",
  186. text_font_google = NULL,
  187. header_font_family = "Magrathea",
  188. header_font_google = NULL,
  189. outfile = NULL
  190. )
  191. list(
  192. text = xaringanthemer::theme_xaringan_get_value(c("text_font_family", "text_font_is_google", "text_color", "text_bold_color")),
  193. code_font_is_google = xaringanthemer::theme_xaringan_get_value("code_font_is_google"),
  194. inverse_text_color = xaringanthemer::theme_xaringan_get_value("inverse_text_color"),
  195. background_color = xaringanthemer::theme_xaringan_get_value("background_color"),
  196. header_color = xaringanthemer::theme_xaringan_get_value("header_color"),
  197. inverse_header_color = xaringanthemer::theme_xaringan_get_value("inverse_header_color"),
  198. base_font_size = xaringanthemer:::get_base_font_size(),
  199. accent_color = xaringanthemer:::get_theme_accent_color(),
  200. accent_color_inverse = xaringanthemer:::get_theme_accent_color(inverse = TRUE)
  201. )
  202. })
  203. expect_equal(theme$text$text_font_family, "Damogran")
  204. expect_equal(theme$text$text_color, "#000001")
  205. expect_false(theme$text$text_font_is_google)
  206. expect_true(theme$code_font_is_google)
  207. expect_equal(theme$text$text_bold_color, "#000004")
  208. expect_equal(theme$inverse_text_color, "#100000")
  209. expect_equal(theme$background_color, "#000002")
  210. expect_equal(theme$header_color, "#000003")
  211. expect_equal(theme$inverse_header_color, "#300000")
  212. expect_equal(theme$base_font_size, 33)
  213. expect_equal(theme$accent_color, "#000003")
  214. expect_equal(theme$accent_color_inverse, "#300000")
  215. })
  216. it("returns theme values from the css file", {
  217. expect_warning(
  218. css <- xaringanthemer::style_xaringan(
  219. base_font_size = "1em",
  220. text_color = "#000001",
  221. inverse_text_color = "#100000",
  222. background_color = "#000002",
  223. inverse_background_color = "#200000",
  224. header_color = "#000003",
  225. inverse_header_color = "#300000",
  226. text_bold_color = "#000004",
  227. text_font_family = "Damogran",
  228. text_font_google = NULL,
  229. header_font_family = "Magrathea",
  230. header_font_google = NULL,
  231. outfile = NULL
  232. )
  233. )
  234. theme <- with_clean_session(function(css) {
  235. writeLines(css, "xaringan-themer.css")
  236. list(
  237. text = xaringanthemer::theme_xaringan_get_value(c("text_font_family", "text_font_is_google", "text_color", "text_bold_color")),
  238. code_font_is_google = xaringanthemer::theme_xaringan_get_value("code_font_is_google"),
  239. inverse_text_color = xaringanthemer::theme_xaringan_get_value("inverse_text_color"),
  240. background_color = xaringanthemer::theme_xaringan_get_value("background_color"),
  241. header_color = xaringanthemer::theme_xaringan_get_value("header_color"),
  242. inverse_header_color = xaringanthemer::theme_xaringan_get_value("inverse_header_color"),
  243. base_font_size = xaringanthemer:::get_base_font_size(),
  244. accent_color = xaringanthemer:::get_theme_accent_color(),
  245. accent_color_inverse = xaringanthemer:::get_theme_accent_color(inverse = TRUE)
  246. )
  247. }, list(css = css))
  248. expect_equal(theme$text$text_font_family, "Damogran")
  249. expect_equal(theme$text$text_color, "#000001")
  250. expect_false(theme$text$text_font_is_google)
  251. expect_true(theme$code_font_is_google)
  252. expect_equal(theme$text$text_bold_color, "#000004")
  253. expect_equal(theme$inverse_text_color, "#100000")
  254. expect_equal(theme$background_color, "#000002")
  255. expect_equal(theme$header_color, "#000003")
  256. expect_equal(theme$inverse_header_color, "#300000")
  257. expect_equal(theme$base_font_size, 16)
  258. expect_equal(theme$accent_color, "#000003")
  259. expect_equal(theme$accent_color_inverse, "#300000")
  260. })
  261. })
  262. describe("web_to_point()", {
  263. it("is invariant when x is pt", {
  264. expect_equal(web_to_point("24pt", 11), 24)
  265. expect_equal(web_to_point("24pt", 11), 24)
  266. expect_equal(web_to_point("24pt", 11, scale = 3), 24)
  267. })
  268. it("scales px", {
  269. expect_equal(web_to_point("24px", 10, 1), 24)
  270. expect_equal(web_to_point("24px", 10, 0.5), 12)
  271. })
  272. it("scales r/em", {
  273. expect_equal(web_to_point("2.4em", 10, 1), 24)
  274. expect_equal(web_to_point("2.4rem", 10, 1), 24)
  275. expect_equal(web_to_point("2.4em", 10, 0.5), 12)
  276. expect_equal(web_to_point("2.4rem", 10, 0.5), 12)
  277. })
  278. it("returns NULL if input is NULL or poorly defined", {
  279. expect_null(web_to_point(NULL))
  280. expect_null(web_to_point("NULL", 1))
  281. })
  282. })
  283. describe("scale_xaringan_colour_continuous()", {
  284. it("errors if no color or default available", {
  285. expect_error(
  286. with_clean_session(function() {
  287. xaringanthemer:::get_theme_accent_color()
  288. })
  289. )
  290. })
  291. })