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

243 lines
6.2KB

  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. it("only adds prepends `heading` once", {
  67. css <- list(b = list(color = "red"), i = list(color = "blue"))
  68. out <- style_extra_css(css, outfile = NULL, heading = "Extra CSS")
  69. expect_equal(length(out), 1L)
  70. expect_equal(sum(grepl("Extra CSS", out)), 1L)
  71. })
  72. })
  73. # list2css() --------------------------------------------------------------
  74. describe("list2css()", {
  75. it("converts lists to css", {
  76. css <- list(
  77. ".remark-slide" = list(
  78. "color" = "#FFF",
  79. "font-size" = "30px"
  80. )
  81. )
  82. expected <- ".remark-slide {
  83. color: #FFF;
  84. font-size: 30px;
  85. }"
  86. expect_equal(list2css(css), expected)
  87. css[[".new-class"]] <- list("background-color" = "#000")
  88. expected <- c(expected, ".new-class {\n background-color: #000;\n}")
  89. expect_equal(list2css(css), expected)
  90. })
  91. it("errors if css list is not named", {
  92. css <- list(list(
  93. "color" = "#FFF",
  94. "font-size" = "30px"
  95. ))
  96. expect_error(list2css(css))
  97. })
  98. it("errors if css list has unnamed elements", {
  99. css <- list(
  100. list(
  101. "color" = "#FFF",
  102. "font-size" = "30px"
  103. ),
  104. ".test" = list(color = "red")
  105. )
  106. expect_error(list2css(css))
  107. })
  108. it("errors if css list has unnamed properties", {
  109. css <- list(
  110. ".class" = list(
  111. color = "#FFF",
  112. "font-size" = "30px"
  113. ),
  114. ".test" = list("red")
  115. )
  116. expect_error(list2css(css))
  117. css <- list(
  118. ".class" = list(
  119. "#FFF",
  120. "font-size" = "30px"
  121. ),
  122. ".test" = list("red")
  123. )
  124. expect_error(list2css(css))
  125. })
  126. it("errors if not list within list", {
  127. css <- list(
  128. ".class" = list(
  129. list(color = "red"),
  130. "font-size" = "30px"
  131. ),
  132. ".test" = list("red")
  133. )
  134. expect_error(list2css(css))
  135. })
  136. it("errors if css contains unnamed elements", {
  137. expect_error(list2css(list(list(color = "#bad"))))
  138. expect_error(
  139. list2css(list(.a = list(color = "#bad"), list(`background-color` = "#bad"))),
  140. "elements.+must be named.+2 is"
  141. )
  142. expect_error(
  143. list2css(list(.a = list(color = "#bad"), list(`background-color` = "#bad"), list(`border-color` = "#bad"))),
  144. "elements.+must be named.+2, 3 are"
  145. )
  146. expect_error(
  147. list2css(list(body = list("#bad"))),
  148. "elements.+must be named.+body.+has"
  149. )
  150. expect_error(
  151. list2css(list(body = list("#bad"), thing = list("#bad"))),
  152. "elements.+must be named.+body.+thing.+have"
  153. )
  154. })
  155. it("is okay for multiple entries with the same name", {
  156. expect_equal(
  157. list2css(list("a" = list(color = "red"), a = list(color = "blue"))),
  158. c("a {\n color: red;\n}", "a {\n color: blue;\n}")
  159. )
  160. expect_equal(
  161. list2css(list("a" = list(color = "red", color = "blue"))),
  162. c("a {\n color: red;\n color: blue;\n}")
  163. )
  164. })
  165. })
  166. # list2fonts() ------------------------------------------------------------
  167. import <- function(x) paste0("@import url(", x, ");")
  168. describe("list2fonts()", {
  169. lato_url <- "https://fonts.googleapis.com/css?family=Lato&display=swap"
  170. worksans_url <- "https://fonts.googleapis.com/css?family=Work+Sans&display=swap"
  171. it("handles a list or c() of font urls", {
  172. expect_equal(
  173. list2fonts(list(lato_url, worksans_url)),
  174. import(c(lato_url, worksans_url))
  175. )
  176. expect_equal(list2fonts(c(lato_url, lato_url)), rep(import(lato_url), 2))
  177. })
  178. it("handles single character font name", {
  179. expect_equal(list2fonts(lato_url), import(lato_url))
  180. })
  181. it("handles list of google fonts", {
  182. expect_equal(
  183. list2fonts(list(google_font("Lato"), google_font("Work Sans"))),
  184. import(c(lato_url, worksans_url))
  185. )
  186. })
  187. it("handles mix of google_font() and bare string", {
  188. expect_equal(
  189. list2fonts(list(google_font("Lato"), worksans_url)),
  190. import(c(lato_url, worksans_url))
  191. )
  192. })
  193. it("handles bare google_font()", {
  194. expect_equal(
  195. list2fonts(google_font("Lato")),
  196. import(lato_url)
  197. )
  198. })
  199. it("throws an error when c() used to combine string and google_font()", {
  200. expect_error(
  201. list2fonts(c(lato_url, google_font("Lato"), google_font("Work Sans"))),
  202. "Multiple fonts"
  203. )
  204. expect_error(
  205. list2fonts(c(google_font("Lato"), google_font("Work Sans"))),
  206. "Multiple fonts"
  207. )
  208. })
  209. })