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

144 lines
3.3KB

  1. # test_that()
  2. describe("style_extra_css", {
  3. css <- list(body = list(color = "#123"))
  4. it("returns text if outfile is NULL", {
  5. expect_equal(
  6. style_extra_css(css, NULL),
  7. "\n\n/* Extra CSS */\nbody {\n color: #123;\n}"
  8. )
  9. expect_equal(
  10. style_extra_css(css, NULL, heading = NULL),
  11. "\nbody {\n color: #123;\n}"
  12. )
  13. expect_equal(
  14. style_extra_css(css, NULL, append = FALSE, heading = NULL),
  15. "body {\n color: #123;\n}"
  16. )
  17. expect_equal(
  18. style_extra_css(css, NULL, append = FALSE, heading = "TEST TEST"),
  19. "/* TEST TEST */\nbody {\n color: #123;\n}"
  20. )
  21. })
  22. tmpfile <- tempfile(fileext = ".css")
  23. first <- style_extra_css(
  24. css = list(.first = list(color = "#123")),
  25. outfile = tmpfile,
  26. append = FALSE,
  27. heading = "First CSS"
  28. )
  29. first_exp <- "/* First CSS */\n.first {\n color: #123;\n}"
  30. it("writes text to the outfile", {
  31. expect_equal(first, first_exp)
  32. expect_equal(
  33. readLines(tmpfile, warn = FALSE),
  34. strsplit(first_exp, "\n")[[1]]
  35. )
  36. })
  37. second <- style_extra_css(
  38. css = list(.second = list(color = "#321")),
  39. outfile = tmpfile,
  40. append = TRUE,
  41. heading = "Second CSS"
  42. )
  43. second_exp <- "\n\n/* Second CSS */\n.second {\n color: #321;\n}"
  44. it("appends to existing outfile", {
  45. expect_equal(second, second_exp)
  46. expect_equal(
  47. readLines(tmpfile, warn = FALSE),
  48. strsplit(paste0(first_exp, "\n", second_exp), "\n")[[1]]
  49. )
  50. })
  51. third <- style_extra_css(
  52. css = list(.third = list(color = "#333")),
  53. outfile = tmpfile,
  54. append = FALSE,
  55. heading = "Third CSS"
  56. )
  57. third_exp <- "/* Third CSS */\n.third {\n color: #333;\n}"
  58. it("over writes text in the outfile if append=FALSE", {
  59. expect_equal(third, third_exp)
  60. expect_equal(
  61. readLines(tmpfile, warn = FALSE),
  62. strsplit(third_exp, "\n")[[1]]
  63. )
  64. })
  65. })
  66. describe("list2css()", {
  67. it("converts lists to css", {
  68. css <- list(
  69. ".remark-slide" = list(
  70. "color" = "#FFF",
  71. "font-size" = "30px"
  72. )
  73. )
  74. expected <- ".remark-slide {
  75. color: #FFF;
  76. font-size: 30px;
  77. }"
  78. expect_equal(list2css(css), expected)
  79. css[[".new-class"]] <- list("background-color" = "#000")
  80. expected <- c(expected, ".new-class {\n background-color: #000;\n}")
  81. expect_equal(list2css(css), expected)
  82. })
  83. it("errors if css list is not named", {
  84. css <- list(list(
  85. "color" = "#FFF",
  86. "font-size" = "30px"
  87. ))
  88. expect_error(list2css(css))
  89. })
  90. it("errors if css list has unnamed elements", {
  91. css <- list(
  92. list(
  93. "color" = "#FFF",
  94. "font-size" = "30px"
  95. ),
  96. ".test" = list(color = "red")
  97. )
  98. expect_error(list2css(css))
  99. })
  100. it("errors if css list has unnamed properties", {
  101. css <- list(
  102. ".class" = list(
  103. color = "#FFF",
  104. "font-size" = "30px"
  105. ),
  106. ".test" = list("red")
  107. )
  108. expect_error(list2css(css))
  109. css <- list(
  110. ".class" = list(
  111. "#FFF",
  112. "font-size" = "30px"
  113. ),
  114. ".test" = list("red")
  115. )
  116. expect_error(list2css(css))
  117. })
  118. it("errors if not list within list", {
  119. css <- list(
  120. ".class" = list(
  121. list(color = "red"),
  122. "font-size" = "30px"
  123. ),
  124. ".test" = list("red")
  125. )
  126. expect_error(list2css(css))
  127. })
  128. })