😎 Give your xaringan slides some style
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

484 lines
18KB

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