😎 Give your xaringan slides some style
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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