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

236 lines
5.9KB

  1. # test_that()
  2. # style_extra_css() -------------------------------------------------------
  3. describe("style_extra_css", {
  4. css <- list(body = list(color = "#123"))
  5. it("returns text if outfile is NULL", {
  6. expect_equal(
  7. style_extra_css(css, NULL),
  8. "\n\n/* Extra CSS */\nbody {\n color: #123;\n}"
  9. )
  10. expect_equal(
  11. style_extra_css(css, NULL, heading = NULL),
  12. "\nbody {\n color: #123;\n}"
  13. )
  14. expect_equal(
  15. style_extra_css(css, NULL, append = FALSE, heading = NULL),
  16. "body {\n color: #123;\n}"
  17. )
  18. expect_equal(
  19. style_extra_css(css, NULL, append = FALSE, heading = "TEST TEST"),
  20. "/* TEST TEST */\nbody {\n color: #123;\n}"
  21. )
  22. })
  23. tmpfile <- tempfile(fileext = ".css")
  24. first <- style_extra_css(
  25. css = list(.first = list(color = "#123")),
  26. outfile = tmpfile,
  27. append = FALSE,
  28. heading = "First CSS"
  29. )
  30. first_exp <- "/* First CSS */\n.first {\n color: #123;\n}"
  31. it("writes text to the outfile", {
  32. expect_equal(first, first_exp)
  33. expect_equal(
  34. readLines(tmpfile, warn = FALSE),
  35. strsplit(first_exp, "\n")[[1]]
  36. )
  37. })
  38. second <- style_extra_css(
  39. css = list(.second = list(color = "#321")),
  40. outfile = tmpfile,
  41. append = TRUE,
  42. heading = "Second CSS"
  43. )
  44. second_exp <- "\n\n/* Second CSS */\n.second {\n color: #321;\n}"
  45. it("appends to existing outfile", {
  46. expect_equal(second, second_exp)
  47. expect_equal(
  48. readLines(tmpfile, warn = FALSE),
  49. strsplit(paste0(first_exp, "\n", second_exp), "\n")[[1]]
  50. )
  51. })
  52. third <- style_extra_css(
  53. css = list(.third = list(color = "#333")),
  54. outfile = tmpfile,
  55. append = FALSE,
  56. heading = "Third CSS"
  57. )
  58. third_exp <- "/* Third CSS */\n.third {\n color: #333;\n}"
  59. it("over writes text in the outfile if append=FALSE", {
  60. expect_equal(third, third_exp)
  61. expect_equal(
  62. readLines(tmpfile, warn = FALSE),
  63. strsplit(third_exp, "\n")[[1]]
  64. )
  65. })
  66. })
  67. # list2css() --------------------------------------------------------------
  68. describe("list2css()", {
  69. it("converts lists to css", {
  70. css <- list(
  71. ".remark-slide" = list(
  72. "color" = "#FFF",
  73. "font-size" = "30px"
  74. )
  75. )
  76. expected <- ".remark-slide {
  77. color: #FFF;
  78. font-size: 30px;
  79. }"
  80. expect_equal(list2css(css), expected)
  81. css[[".new-class"]] <- list("background-color" = "#000")
  82. expected <- c(expected, ".new-class {\n background-color: #000;\n}")
  83. expect_equal(list2css(css), expected)
  84. })
  85. it("errors if css list is not named", {
  86. css <- list(list(
  87. "color" = "#FFF",
  88. "font-size" = "30px"
  89. ))
  90. expect_error(list2css(css))
  91. })
  92. it("errors if css list has unnamed elements", {
  93. css <- list(
  94. list(
  95. "color" = "#FFF",
  96. "font-size" = "30px"
  97. ),
  98. ".test" = list(color = "red")
  99. )
  100. expect_error(list2css(css))
  101. })
  102. it("errors if css list has unnamed properties", {
  103. css <- list(
  104. ".class" = list(
  105. color = "#FFF",
  106. "font-size" = "30px"
  107. ),
  108. ".test" = list("red")
  109. )
  110. expect_error(list2css(css))
  111. css <- list(
  112. ".class" = list(
  113. "#FFF",
  114. "font-size" = "30px"
  115. ),
  116. ".test" = list("red")
  117. )
  118. expect_error(list2css(css))
  119. })
  120. it("errors if not list within list", {
  121. css <- list(
  122. ".class" = list(
  123. list(color = "red"),
  124. "font-size" = "30px"
  125. ),
  126. ".test" = list("red")
  127. )
  128. expect_error(list2css(css))
  129. })
  130. it("errors if css contains unnamed elements", {
  131. expect_error(list2css(list(list(color = "#bad"))))
  132. expect_error(
  133. list2css(list(.a = list(color = "#bad"), list(`background-color` = "#bad"))),
  134. "elements.+must be named.+2 is"
  135. )
  136. expect_error(
  137. list2css(list(.a = list(color = "#bad"), list(`background-color` = "#bad"), list(`border-color` = "#bad"))),
  138. "elements.+must be named.+2, 3 are"
  139. )
  140. expect_error(
  141. list2css(list(body = list("#bad"))),
  142. "elements.+must be named.+body.+has"
  143. )
  144. expect_error(
  145. list2css(list(body = list("#bad"), thing = list("#bad"))),
  146. "elements.+must be named.+body.+thing.+have"
  147. )
  148. })
  149. it("is okay for multiple entries with the same name", {
  150. expect_equal(
  151. list2css(list("a" = list(color = "red"), a = list(color = "blue"))),
  152. c("a {\n color: red;\n}", "a {\n color: blue;\n}")
  153. )
  154. expect_equal(
  155. list2css(list("a" = list(color = "red", color = "blue"))),
  156. c("a {\n color: red;\n color: blue;\n}")
  157. )
  158. })
  159. })
  160. # list2fonts() ------------------------------------------------------------
  161. import <- function(x) paste0("@import url(", x, ");")
  162. describe("list2fonts()", {
  163. lato_url <- "https://fonts.googleapis.com/css?family=Lato&display=swap"
  164. worksans_url <- "https://fonts.googleapis.com/css?family=Work+Sans&display=swap"
  165. it("handles a list or c() of font urls", {
  166. expect_equal(
  167. list2fonts(list(lato_url, worksans_url)),
  168. import(c(lato_url, worksans_url))
  169. )
  170. expect_equal(list2fonts(c(lato_url, lato_url)), rep(import(lato_url), 2))
  171. })
  172. it("handles single character font name", {
  173. expect_equal(list2fonts(lato_url), import(lato_url))
  174. })
  175. it("handles list of google fonts", {
  176. expect_equal(
  177. list2fonts(list(google_font("Lato"), google_font("Work Sans"))),
  178. import(c(lato_url, worksans_url))
  179. )
  180. })
  181. it("handles mix of google_font() and bare string", {
  182. expect_equal(
  183. list2fonts(list(google_font("Lato"), worksans_url)),
  184. import(c(lato_url, worksans_url))
  185. )
  186. })
  187. it("handles bare google_font()", {
  188. expect_equal(
  189. list2fonts(google_font("Lato")),
  190. import(lato_url)
  191. )
  192. })
  193. it("throws an error when c() used to combine string and google_font()", {
  194. expect_error(
  195. list2fonts(c(lato_url, google_font("Lato"), google_font("Work Sans"))),
  196. "Multiple fonts"
  197. )
  198. expect_error(
  199. list2fonts(c(google_font("Lato"), google_font("Work Sans"))),
  200. "Multiple fonts"
  201. )
  202. })
  203. })