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

454 lines
16KB

  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_base()", {
  166. it("accepts a regular text_font or title_font", {
  167. theme <- with_clean_session(function() {
  168. xaringanthemer::theme_xaringan_base(
  169. text_color = "#000",
  170. accent_color = "#1a2b3c",
  171. background_color = "#FFF",
  172. text_font = "sans", title_font = "serif",
  173. text_font_use_google = FALSE, title_font_use_google = FALSE
  174. )
  175. })
  176. expect_equal(theme$title$colour, "#1a2b3c")
  177. expect_equal(theme$title$family, "serif")
  178. expect_equal(theme$plot.background$colour, "#FFFFFF")
  179. expect_equal(theme$text$colour, "#1A1A1A")
  180. expect_equal(theme$text$family, "sans")
  181. })
  182. })
  183. test_that("theme_xaringan_restore_defaults() restores defaults", {
  184. res <- with_clean_session(function() {
  185. res <- list()
  186. res$original <- list(
  187. colour = ggplot2::geom_line()$geom$default_aes$colour,
  188. fill = ggplot2::geom_bar()$geom$default_aes$fill
  189. )
  190. old_defaults <- xaringanthemer::theme_xaringan_set_defaults(
  191. text_color = "#0088ff",
  192. background_color = "#88ff00",
  193. accent_color = "#FF8800"
  194. )
  195. res$after_set <- list(
  196. line_colour = ggplot2::geom_line()$geom$default_aes$colour,
  197. bar_fill = ggplot2::geom_bar()$geom$default_aes$fill
  198. )
  199. xaringanthemer::theme_xaringan_restore_defaults()
  200. res$after_restore <- list(
  201. line_colour = ggplot2::geom_line()$geom$default_aes$colour,
  202. bar_fill = ggplot2::geom_bar()$geom$default_aes$fill
  203. )
  204. res
  205. })
  206. expect_equal(res$after_set$line_colour, "#0088ff")
  207. expect_equal(res$after_set$bar_fill, "#FF8800")
  208. expect_equal(res$after_restore$line_colour, res$original$colour)
  209. expect_equal(res$after_restore$bar_fill, res$original$fil)
  210. })
  211. describe("theme_xaringan_get_value()", {
  212. it("errors if no css file or style_xaringan theme called", {
  213. expect_error(
  214. with_clean_session(function() {
  215. xaringanthemer::theme_xaringan_get_value("text_font_family")
  216. })
  217. )
  218. })
  219. it("returns theme values", {
  220. theme <- with_clean_session(function() {
  221. xaringanthemer::style_xaringan(
  222. base_font_size = "33px",
  223. text_font_size = "23px",
  224. text_color = "#000001",
  225. inverse_text_color = "#100000",
  226. background_color = "#000002",
  227. inverse_background_color = "#200000",
  228. header_color = "#000003",
  229. inverse_header_color = "#300000",
  230. text_bold_color = "#000004",
  231. text_font_family = "Damogran",
  232. text_font_google = NULL,
  233. header_font_family = "Magrathea",
  234. header_font_google = NULL,
  235. outfile = NULL
  236. )
  237. list(
  238. text = xaringanthemer::theme_xaringan_get_value(c("text_font_family", "text_font_is_google", "text_color", "text_bold_color")),
  239. code_font_is_google = xaringanthemer::theme_xaringan_get_value("code_font_is_google"),
  240. inverse_text_color = xaringanthemer::theme_xaringan_get_value("inverse_text_color"),
  241. background_color = xaringanthemer::theme_xaringan_get_value("background_color"),
  242. header_color = xaringanthemer::theme_xaringan_get_value("header_color"),
  243. inverse_header_color = xaringanthemer::theme_xaringan_get_value("inverse_header_color"),
  244. base_font_size = xaringanthemer:::get_base_font_size(),
  245. accent_color = xaringanthemer:::get_theme_accent_color(),
  246. accent_color_inverse = xaringanthemer:::get_theme_accent_color(inverse = TRUE)
  247. )
  248. })
  249. expect_equal(theme$text$text_font_family, "Damogran")
  250. expect_equal(theme$text$text_color, "#000001")
  251. expect_false(theme$text$text_font_is_google)
  252. expect_true(theme$code_font_is_google)
  253. expect_equal(theme$text$text_bold_color, "#000004")
  254. expect_equal(theme$inverse_text_color, "#100000")
  255. expect_equal(theme$background_color, "#000002")
  256. expect_equal(theme$header_color, "#000003")
  257. expect_equal(theme$inverse_header_color, "#300000")
  258. expect_equal(theme$base_font_size, 33)
  259. expect_equal(theme$accent_color, "#000003")
  260. expect_equal(theme$accent_color_inverse, "#300000")
  261. })
  262. it("returns theme values from the css file", {
  263. expect_warning(
  264. css <- xaringanthemer::style_xaringan(
  265. base_font_size = "1em",
  266. text_color = "#000001",
  267. inverse_text_color = "#100000",
  268. background_color = "#000002",
  269. inverse_background_color = "#200000",
  270. header_color = "#000003",
  271. inverse_header_color = "#300000",
  272. text_bold_color = "#000004",
  273. text_font_family = "Damogran",
  274. text_font_google = NULL,
  275. header_font_family = "Magrathea",
  276. header_font_google = NULL,
  277. outfile = NULL
  278. )
  279. )
  280. theme <- with_clean_session(function(css) {
  281. writeLines(css, "xaringan-themer.css")
  282. list(
  283. text = xaringanthemer::theme_xaringan_get_value(c("text_font_family", "text_font_is_google", "text_color", "text_bold_color")),
  284. code_font_is_google = xaringanthemer::theme_xaringan_get_value("code_font_is_google"),
  285. inverse_text_color = xaringanthemer::theme_xaringan_get_value("inverse_text_color"),
  286. background_color = xaringanthemer::theme_xaringan_get_value("background_color"),
  287. header_color = xaringanthemer::theme_xaringan_get_value("header_color"),
  288. inverse_header_color = xaringanthemer::theme_xaringan_get_value("inverse_header_color"),
  289. base_font_size = xaringanthemer:::get_base_font_size(),
  290. accent_color = xaringanthemer:::get_theme_accent_color(),
  291. accent_color_inverse = xaringanthemer:::get_theme_accent_color(inverse = TRUE)
  292. )
  293. }, list(css = css))
  294. expect_equal(theme$text$text_font_family, "Damogran")
  295. expect_equal(theme$text$text_color, "#000001")
  296. expect_false(theme$text$text_font_is_google)
  297. expect_true(theme$code_font_is_google)
  298. expect_equal(theme$text$text_bold_color, "#000004")
  299. expect_equal(theme$inverse_text_color, "#100000")
  300. expect_equal(theme$background_color, "#000002")
  301. expect_equal(theme$header_color, "#000003")
  302. expect_equal(theme$inverse_header_color, "#300000")
  303. expect_equal(theme$base_font_size, 16)
  304. expect_equal(theme$accent_color, "#000003")
  305. expect_equal(theme$accent_color_inverse, "#300000")
  306. })
  307. })
  308. describe("web_to_point()", {
  309. it("is invariant when x is pt", {
  310. expect_equal(web_to_point("24pt", 11), 24)
  311. expect_equal(web_to_point("24pt", 11), 24)
  312. expect_equal(web_to_point("24pt", 11, scale = 3), 24)
  313. })
  314. it("scales px", {
  315. expect_equal(web_to_point("24px", 10, 1), 24)
  316. expect_equal(web_to_point("24px", 10, 0.5), 12)
  317. })
  318. it("scales r/em", {
  319. expect_equal(web_to_point("2.4em", 10, 1), 24)
  320. expect_equal(web_to_point("2.4rem", 10, 1), 24)
  321. expect_equal(web_to_point("2.4em", 10, 0.5), 12)
  322. expect_equal(web_to_point("2.4rem", 10, 0.5), 12)
  323. })
  324. it("returns NULL if input is NULL or poorly defined", {
  325. expect_null(web_to_point(NULL))
  326. expect_null(web_to_point("NULL", 1))
  327. })
  328. })
  329. describe("get_theme_accent_color()", {
  330. it("errors if no color or default available", {
  331. expect_error(
  332. with_clean_session(function() {
  333. xaringanthemer:::get_theme_accent_color()
  334. })
  335. )
  336. })
  337. })
  338. describe("scale_xaringan_*", {
  339. scales <- with_clean_session(function() {
  340. xaringanthemer::style_xaringan(
  341. background_color = "#00FF00",
  342. inverse_background_color = "#FF00FF",
  343. header_color = "#FF0000",
  344. inverse_header_color = "#0000FF",
  345. outfile = NULL
  346. )
  347. list(
  348. discrete = list(
  349. fill = xaringanthemer::scale_xaringan_fill_discrete(),
  350. fill_inverse = xaringanthemer::scale_xaringan_fill_discrete(inverse = TRUE),
  351. fill_reverse = xaringanthemer::scale_xaringan_fill_discrete(direction = -1),
  352. color = xaringanthemer::scale_xaringan_color_discrete(),
  353. color_inverse = xaringanthemer::scale_xaringan_color_discrete(inverse = TRUE),
  354. color_reverse = xaringanthemer::scale_xaringan_color_discrete(direction = -1),
  355. colour = xaringanthemer::scale_xaringan_colour_discrete(),
  356. colour_inverse = xaringanthemer::scale_xaringan_colour_discrete(inverse = TRUE),
  357. colour_reverse = xaringanthemer::scale_xaringan_colour_discrete(direction = -1)
  358. ),
  359. continuous = list(
  360. fill = xaringanthemer::scale_xaringan_fill_continuous(),
  361. fill_inverse = xaringanthemer::scale_xaringan_fill_continuous(inverse = TRUE),
  362. fill_reverse = xaringanthemer::scale_xaringan_fill_continuous(begin = 1, end = 0),
  363. color = xaringanthemer::scale_xaringan_color_continuous(),
  364. color_inverse = xaringanthemer::scale_xaringan_color_continuous(inverse = TRUE),
  365. color_reverse = xaringanthemer::scale_xaringan_color_continuous(begin = 1, end = 0),
  366. colour = xaringanthemer::scale_xaringan_colour_continuous(),
  367. colour_inverse = xaringanthemer::scale_xaringan_colour_continuous(inverse = TRUE),
  368. colour_reverse = xaringanthemer::scale_xaringan_colour_continuous(begin = 1, end = 0)
  369. )
  370. )
  371. })
  372. it("discrete scales create ScaleDiscrete ggproto objects", {
  373. for (s in scales$discrete) {
  374. expect_s3_class(s, "ScaleDiscrete")
  375. expect_s3_class(s, "ggproto")
  376. }
  377. })
  378. it("discrete scales use accent color as starting point", {
  379. expect_equal(scales$discrete$fill$palette(n = 1), "#FF0000")
  380. expect_equal(scales$discrete$color$palette(n = 1), "#FF0000")
  381. expect_equal(scales$discrete$colour$palette(n = 1), "#FF0000")
  382. expect_equal(scales$discrete$fill_reverse$palette(n = 1), "#FF0000")
  383. expect_equal(scales$discrete$color_reverse$palette(n = 1), "#FF0000")
  384. expect_equal(scales$discrete$colour_reverse$palette(n = 1), "#FF0000")
  385. })
  386. it("discrete scales use inverse accent color as starting point", {
  387. expect_equal(scales$discrete$fill_inverse$palette(n = 1), "#0000FF")
  388. expect_equal(scales$discrete$color_inverse$palette(n = 1), "#0000FF")
  389. expect_equal(scales$discrete$colour_inverse$palette(n = 1), "#0000FF")
  390. })
  391. it("continuous scales create ScaleContinuous ggproto objects", {
  392. for (s in scales$continuous) {
  393. expect_s3_class(s, "ScaleContinuous")
  394. expect_s3_class(s, "ggproto")
  395. }
  396. })
  397. it("continuous scales use accent color as starting point", {
  398. expect_equal(scales$continuous$fill$palette(x = 1), "#FF0000")
  399. expect_equal(scales$continuous$color$palette(x = 1), "#FF0000")
  400. expect_equal(scales$continuous$colour$palette(x = 1), "#FF0000")
  401. expect_equal(scales$continuous$fill_reverse$palette(x = 1), "#FF0000")
  402. expect_equal(scales$continuous$color_reverse$palette(x = 1), "#FF0000")
  403. expect_equal(scales$continuous$colour_reverse$palette(x = 1), "#FF0000")
  404. })
  405. it("continuous scales use inverse accent color as starting point", {
  406. expect_equal(scales$continuous$fill_inverse$palette(x = 1), "#0000FF")
  407. expect_equal(scales$continuous$color_inverse$palette(x = 1), "#0000FF")
  408. expect_equal(scales$continuous$colour_inverse$palette(x = 1), "#0000FF")
  409. })
  410. })