😎 Give your xaringan slides some style
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

525 lines
19KB

  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. it("throws error for non-hex color functions", {
  82. expect_error(
  83. with_clean_session(function() {
  84. xaringanthemer::style_xaringan(
  85. text_color = "rgb(0, 0, 0)",
  86. outfile = NULL
  87. )
  88. xaringanthemer::theme_xaringan(
  89. text_font_use_google = FALSE,
  90. title_font_use_google = FALSE
  91. )
  92. })
  93. )
  94. })
  95. it("correctly overwrites geom default values", {
  96. fonts <- with_clean_session(function() {
  97. style <- xaringanthemer::style_xaringan(outfile = NULL)
  98. g <- ggplot2::ggplot(mtcars, ggplot2::aes(x = cyl, y = disp, label = carb))
  99. fonts <- list()
  100. plot1 <- g + ggplot2::geom_text() + xaringanthemer::theme_xaringan()
  101. fonts[[1]] <- plot1$layers[[1]]$geom$default_aes$family
  102. plot2 <- g + ggplot2::geom_text() + xaringanthemer::theme_xaringan(text_font = "Ranga", text_font_use_google = TRUE)
  103. fonts[[2]] <- plot2$layers[[1]]$geom$default_aes$family
  104. fonts
  105. })
  106. expect_equal(fonts[[1]], "Noto Sans")
  107. expect_equal(fonts[[2]], "Ranga")
  108. })
  109. })
  110. describe("theme_xaringan_inverse()", {
  111. it("errors if no css file or style_xaringan theme called", {
  112. expect_error(
  113. with_clean_session(function() {
  114. xaringanthemer::theme_xaringan_inverse()
  115. })
  116. )
  117. })
  118. it("returns a theme using the style_xaringan colors", {
  119. theme <- with_clean_session(function() {
  120. xaringanthemer::style_xaringan(
  121. text_color = "#000001",
  122. inverse_text_color = "#100000",
  123. background_color = "#000002",
  124. inverse_background_color = "#200000",
  125. header_color = "#000003",
  126. inverse_header_color = "#300000",
  127. text_bold_color = "#000004",
  128. text_font_family = "Damogran",
  129. text_font_google = NULL,
  130. header_font_family = "Magrathea",
  131. header_font_google = NULL
  132. )
  133. xaringanthemer::theme_xaringan_inverse(
  134. text_font_use_google = FALSE,
  135. title_font_use_google = FALSE
  136. )
  137. })
  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("returns correct colors pulling from css file", {
  145. css <- xaringanthemer::style_xaringan(
  146. text_color = "#000001",
  147. inverse_text_color = "#100000",
  148. background_color = "#000002",
  149. inverse_background_color = "#200000",
  150. header_color = "#000003",
  151. inverse_header_color = "#300000",
  152. text_bold_color = "#000004",
  153. text_font_family = "Damogran",
  154. text_font_google = NULL,
  155. header_font_family = "Magrathea",
  156. header_font_google = NULL,
  157. outfile = NULL
  158. )
  159. theme <- with_clean_session(function(css) {
  160. writeLines(css, "basic.css")
  161. xaringanthemer::theme_xaringan_inverse(
  162. text_font_use_google = FALSE,
  163. title_font_use_google = FALSE
  164. )
  165. }, list(css = css))
  166. expect_equal(theme$text$family, "Damogran")
  167. expect_equal(theme$title$family, "Magrathea")
  168. expect_equal(theme$title$colour, "#300000")
  169. expect_equal(theme$plot.background$colour, "#200000")
  170. expect_equal(theme$plot.background$fill, "#200000")
  171. })
  172. it("works with 3-digit hex colors", {
  173. theme <- with_clean_session(function() {
  174. xaringanthemer::style_xaringan(
  175. text_color = "#001",
  176. background_color = "#002",
  177. header_color = "#003",
  178. inverse_text_color = "#100",
  179. inverse_background_color = "#200",
  180. inverse_header_color = "#300",
  181. text_bold_color = "#004"
  182. )
  183. xaringanthemer::theme_xaringan_inverse(
  184. text_font_use_google = FALSE,
  185. title_font_use_google = FALSE
  186. )
  187. })
  188. expect_equal(theme$title$colour, "#330000")
  189. expect_equal(theme$plot.background$colour, "#220000")
  190. expect_equal(theme$plot.background$fill, "#220000")
  191. })
  192. })
  193. describe("theme_xaringan_base()", {
  194. it("accepts a regular text_font or title_font", {
  195. theme <- with_clean_session(function() {
  196. xaringanthemer::theme_xaringan_base(
  197. text_color = "#000",
  198. accent_color = "#1a2b3c",
  199. background_color = "#FFF",
  200. text_font = "sans", title_font = "serif",
  201. text_font_use_google = FALSE, title_font_use_google = FALSE
  202. )
  203. })
  204. expect_equal(theme$title$colour, "#1a2b3c")
  205. expect_equal(theme$title$family, "serif")
  206. expect_equal(theme$plot.background$colour, "#FFFFFF")
  207. expect_equal(theme$text$colour, "#1A1A1A")
  208. expect_equal(theme$text$family, "sans")
  209. })
  210. })
  211. test_that("theme_xaringan_restore_defaults() restores defaults", {
  212. res <- with_clean_session(function() {
  213. res <- list()
  214. res$nothing <- list(xaringanthemer::theme_xaringan_restore_defaults())
  215. res$original <- list(
  216. colour = ggplot2::geom_line()$geom$default_aes$colour,
  217. fill = ggplot2::geom_bar()$geom$default_aes$fill
  218. )
  219. old_defaults <- xaringanthemer::theme_xaringan_set_defaults(
  220. text_color = "#0088ff",
  221. background_color = "#88ff00",
  222. accent_color = "#FF8800"
  223. )
  224. res$after_set <- list(
  225. line_colour = ggplot2::geom_line()$geom$default_aes$colour,
  226. bar_fill = ggplot2::geom_bar()$geom$default_aes$fill
  227. )
  228. xaringanthemer::theme_xaringan_restore_defaults()
  229. res$after_restore <- list(
  230. line_colour = ggplot2::geom_line()$geom$default_aes$colour,
  231. bar_fill = ggplot2::geom_bar()$geom$default_aes$fill
  232. )
  233. res
  234. })
  235. expect_equal(res$nothing, list(NULL))
  236. expect_equal(res$after_set$line_colour, "#0088ff")
  237. expect_equal(res$after_set$bar_fill, "#FF8800")
  238. expect_equal(res$after_restore$line_colour, res$original$colour)
  239. expect_equal(res$after_restore$bar_fill, res$original$fil)
  240. })
  241. describe("theme_xaringan_get_value()", {
  242. it("errors if no css file or style_xaringan theme called", {
  243. expect_error(
  244. with_clean_session(function() {
  245. xaringanthemer::theme_xaringan_get_value("text_font_family")
  246. })
  247. )
  248. })
  249. it("returns theme values", {
  250. theme <- with_clean_session(function() {
  251. xaringanthemer::style_xaringan(
  252. base_font_size = "33px",
  253. text_font_size = "23px",
  254. text_color = "#000001",
  255. inverse_text_color = "#100000",
  256. background_color = "#000002",
  257. inverse_background_color = "#200000",
  258. header_color = "#000003",
  259. inverse_header_color = "#300000",
  260. text_bold_color = "#000004",
  261. text_font_family = "Damogran",
  262. text_font_google = NULL,
  263. header_font_family = "Magrathea",
  264. header_font_google = NULL,
  265. outfile = NULL
  266. )
  267. list(
  268. text = xaringanthemer::theme_xaringan_get_value(c("text_font_family", "text_font_is_google", "text_color", "text_bold_color")),
  269. code_font_is_google = xaringanthemer::theme_xaringan_get_value("code_font_is_google"),
  270. inverse_text_color = xaringanthemer::theme_xaringan_get_value("inverse_text_color"),
  271. background_color = xaringanthemer::theme_xaringan_get_value("background_color"),
  272. header_color = xaringanthemer::theme_xaringan_get_value("header_color"),
  273. inverse_header_color = xaringanthemer::theme_xaringan_get_value("inverse_header_color"),
  274. base_font_size = xaringanthemer:::get_base_font_size(),
  275. accent_color = xaringanthemer:::get_theme_accent_color(),
  276. accent_color_inverse = xaringanthemer:::get_theme_accent_color(inverse = TRUE)
  277. )
  278. })
  279. expect_equal(theme$text$text_font_family, "Damogran")
  280. expect_equal(theme$text$text_color, "#000001")
  281. expect_false(theme$text$text_font_is_google)
  282. expect_true(theme$code_font_is_google)
  283. expect_equal(theme$text$text_bold_color, "#000004")
  284. expect_equal(theme$inverse_text_color, "#100000")
  285. expect_equal(theme$background_color, "#000002")
  286. expect_equal(theme$header_color, "#000003")
  287. expect_equal(theme$inverse_header_color, "#300000")
  288. expect_equal(theme$base_font_size, 33)
  289. expect_equal(theme$accent_color, "#000003")
  290. expect_equal(theme$accent_color_inverse, "#300000")
  291. })
  292. it("returns theme values from the css file", {
  293. expect_warning(
  294. css <- xaringanthemer::style_xaringan(
  295. base_font_size = "1em",
  296. text_color = "#000001",
  297. inverse_text_color = "#100000",
  298. background_color = "#000002",
  299. inverse_background_color = "#200000",
  300. header_color = "#000003",
  301. inverse_header_color = "#300000",
  302. text_bold_color = "#000004",
  303. text_font_family = "Damogran",
  304. text_font_google = NULL,
  305. header_font_family = "Magrathea",
  306. header_font_google = NULL,
  307. outfile = NULL
  308. )
  309. )
  310. theme <- with_clean_session(function(css) {
  311. writeLines(css, "xaringan-themer.css")
  312. list(
  313. text = xaringanthemer::theme_xaringan_get_value(c("text_font_family", "text_font_is_google", "text_color", "text_bold_color")),
  314. code_font_is_google = xaringanthemer::theme_xaringan_get_value("code_font_is_google"),
  315. inverse_text_color = xaringanthemer::theme_xaringan_get_value("inverse_text_color"),
  316. background_color = xaringanthemer::theme_xaringan_get_value("background_color"),
  317. header_color = xaringanthemer::theme_xaringan_get_value("header_color"),
  318. inverse_header_color = xaringanthemer::theme_xaringan_get_value("inverse_header_color"),
  319. base_font_size = xaringanthemer:::get_base_font_size(),
  320. accent_color = xaringanthemer:::get_theme_accent_color(),
  321. accent_color_inverse = xaringanthemer:::get_theme_accent_color(inverse = TRUE)
  322. )
  323. }, list(css = css))
  324. expect_equal(theme$text$text_font_family, "Damogran")
  325. expect_equal(theme$text$text_color, "#000001")
  326. expect_false(theme$text$text_font_is_google)
  327. expect_true(theme$code_font_is_google)
  328. expect_equal(theme$text$text_bold_color, "#000004")
  329. expect_equal(theme$inverse_text_color, "#100000")
  330. expect_equal(theme$background_color, "#000002")
  331. expect_equal(theme$header_color, "#000003")
  332. expect_equal(theme$inverse_header_color, "#300000")
  333. expect_equal(theme$base_font_size, 16)
  334. expect_equal(theme$accent_color, "#000003")
  335. expect_equal(theme$accent_color_inverse, "#300000")
  336. })
  337. })
  338. describe("web_to_point()", {
  339. it("is invariant when x is pt", {
  340. expect_equal(web_to_point("24pt", 11), 24)
  341. expect_equal(web_to_point("24pt", 11), 24)
  342. expect_equal(web_to_point("24pt", 11, scale = 3), 24)
  343. })
  344. it("scales px", {
  345. expect_equal(web_to_point("24px", 10, 1), 24)
  346. expect_equal(web_to_point("24px", 10, 0.5), 12)
  347. })
  348. it("scales r/em", {
  349. expect_equal(web_to_point("2.4em", 10, 1), 24)
  350. expect_equal(web_to_point("2.4rem", 10, 1), 24)
  351. expect_equal(web_to_point("2.4em", 10, 0.5), 12)
  352. expect_equal(web_to_point("2.4rem", 10, 0.5), 12)
  353. })
  354. it("returns NULL if input is NULL or poorly defined", {
  355. expect_null(web_to_point(NULL))
  356. expect_null(web_to_point("NULL", 1))
  357. })
  358. })
  359. describe("get_theme_accent_color()", {
  360. it("errors if no color or default available", {
  361. expect_error(
  362. with_clean_session(function() {
  363. xaringanthemer:::get_theme_accent_color()
  364. })
  365. )
  366. })
  367. })
  368. describe("scale_xaringan_*", {
  369. scales <- with_clean_session(function() {
  370. xaringanthemer::style_xaringan(
  371. background_color = "#00FF00",
  372. inverse_background_color = "#FF00FF",
  373. header_color = "#FF0000",
  374. inverse_header_color = "#0000FF",
  375. outfile = NULL
  376. )
  377. list(
  378. discrete = list(
  379. fill = xaringanthemer::scale_xaringan_fill_discrete(),
  380. fill_inverse = xaringanthemer::scale_xaringan_fill_discrete(inverse = TRUE),
  381. fill_reverse = xaringanthemer::scale_xaringan_fill_discrete(direction = -1),
  382. color = xaringanthemer::scale_xaringan_color_discrete(),
  383. color_inverse = xaringanthemer::scale_xaringan_color_discrete(inverse = TRUE),
  384. color_reverse = xaringanthemer::scale_xaringan_color_discrete(direction = -1),
  385. colour = xaringanthemer::scale_xaringan_colour_discrete(),
  386. colour_inverse = xaringanthemer::scale_xaringan_colour_discrete(inverse = TRUE),
  387. colour_reverse = xaringanthemer::scale_xaringan_colour_discrete(direction = -1)
  388. ),
  389. continuous = list(
  390. fill = xaringanthemer::scale_xaringan_fill_continuous(),
  391. fill_inverse = xaringanthemer::scale_xaringan_fill_continuous(inverse = TRUE),
  392. fill_reverse = xaringanthemer::scale_xaringan_fill_continuous(begin = 1, end = 0),
  393. color = xaringanthemer::scale_xaringan_color_continuous(),
  394. color_inverse = xaringanthemer::scale_xaringan_color_continuous(inverse = TRUE),
  395. color_reverse = xaringanthemer::scale_xaringan_color_continuous(begin = 1, end = 0),
  396. colour = xaringanthemer::scale_xaringan_colour_continuous(),
  397. colour_inverse = xaringanthemer::scale_xaringan_colour_continuous(inverse = TRUE),
  398. colour_reverse = xaringanthemer::scale_xaringan_colour_continuous(begin = 1, end = 0)
  399. )
  400. )
  401. })
  402. it("discrete scales create ScaleDiscrete ggproto objects", {
  403. for (s in scales$discrete) {
  404. expect_s3_class(s, "ScaleDiscrete")
  405. expect_s3_class(s, "ggproto")
  406. }
  407. })
  408. it("discrete scales use accent color as starting point", {
  409. expect_equal(scales$discrete$fill$palette(n = 1), "#FF0000")
  410. expect_equal(scales$discrete$color$palette(n = 1), "#FF0000")
  411. expect_equal(scales$discrete$colour$palette(n = 1), "#FF0000")
  412. expect_equal(scales$discrete$fill_reverse$palette(n = 1), "#FF0000")
  413. expect_equal(scales$discrete$color_reverse$palette(n = 1), "#FF0000")
  414. expect_equal(scales$discrete$colour_reverse$palette(n = 1), "#FF0000")
  415. })
  416. it("discrete scales use inverse accent color as starting point", {
  417. expect_equal(scales$discrete$fill_inverse$palette(n = 1), "#0000FF")
  418. expect_equal(scales$discrete$color_inverse$palette(n = 1), "#0000FF")
  419. expect_equal(scales$discrete$colour_inverse$palette(n = 1), "#0000FF")
  420. })
  421. it("continuous scales create ScaleContinuous ggproto objects", {
  422. for (s in scales$continuous) {
  423. expect_s3_class(s, "ScaleContinuous")
  424. expect_s3_class(s, "ggproto")
  425. }
  426. })
  427. it("continuous scales use accent color as starting point", {
  428. expect_equal(scales$continuous$fill$palette(x = 1), "#FF0000")
  429. expect_equal(scales$continuous$color$palette(x = 1), "#FF0000")
  430. expect_equal(scales$continuous$colour$palette(x = 1), "#FF0000")
  431. expect_equal(scales$continuous$fill_reverse$palette(x = 1), "#FF0000")
  432. expect_equal(scales$continuous$color_reverse$palette(x = 1), "#FF0000")
  433. expect_equal(scales$continuous$colour_reverse$palette(x = 1), "#FF0000")
  434. })
  435. it("continuous scales use inverse accent color as starting point", {
  436. expect_equal(scales$continuous$fill_inverse$palette(x = 1), "#0000FF")
  437. expect_equal(scales$continuous$color_inverse$palette(x = 1), "#0000FF")
  438. expect_equal(scales$continuous$colour_inverse$palette(x = 1), "#0000FF")
  439. })
  440. it("continuous scales rescale low to high when begin = 0, end = 1", {
  441. expect_equal(scales$continuous$fill$rescaler(1:3), 0:2 / 2)
  442. expect_equal(scales$continuous$color$rescaler(1:3), 0:2 / 2)
  443. expect_equal(scales$continuous$colour$rescaler(1:3), 0:2 / 2)
  444. })
  445. it("continuous scales rescale high to low when begin = 1, end = 0", {
  446. expect_equal(scales$continuous$fill_reverse$rescaler(1:3), 2:0 / 2)
  447. expect_equal(scales$continuous$color_reverse$rescaler(1:3), 2:0 / 2)
  448. expect_equal(scales$continuous$colour_reverse$rescaler(1:3), 2:0 / 2)
  449. })
  450. })
  451. test_that("register_font() returns the name of the font family if {showtext} is missing", {
  452. text_font_family <- xaringanthemer_font_default("text_font_family")
  453. family_showtext_missing <- testthat::with_mock(
  454. `xaringanthemer:::requires_package` = function(pkg, ...) pkg != "showtext",
  455. register_font(text_font_family, google = TRUE)
  456. )
  457. expect_equal(family_showtext_missing, text_font_family)
  458. })
  459. test_that("register_font() returns the name of the font family if {sysfonts} is missing", {
  460. skip_if_not_installed("showtext")
  461. text_font_family <- xaringanthemer_font_default("text_font_family")
  462. family_sysfonts_missing <- testthat::with_mock(
  463. `xaringanthemer:::requires_package` = function(pkg, ...) pkg != "sysfonts",
  464. register_font(text_font_family, google = TRUE)
  465. )
  466. expect_equal(family_sysfonts_missing, text_font_family)
  467. })