Selaa lähdekoodia

Fix style_extra_css, add heading arg, add tests

tags/v0.3.0
Garrick Aden-Buie 6 vuotta sitten
vanhempi
commit
32af20667d
3 muutettua tiedostoa jossa 158 lisäystä ja 72 poistoa
  1. +15
    -3
      R/style_extra_css.R
  2. +0
    -69
      tests/testthat/test-list2css.R
  3. +143
    -0
      tests/testthat/test-style_extra_css.R

+ 15
- 3
R/style_extra_css.R Näytä tiedosto

@@ -14,6 +14,7 @@
#' `list(".class-id" = list("css-property" = "value"))`
#' @param append If `TRUE` output will be appended to `outfile`; otherwise,
#' it will overwrite the contents of `outfile`.
#' @param heading Heading added above extra CSS. Use `NULL` to disable.
#'
#' @examples
#' style_extra_css(
@@ -30,15 +31,26 @@
#' )
#' @inheritParams style_xaringan
#' @export
style_extra_css <- function(css, outfile = "xaringan-themer.css", append = TRUE) {
x <- paste("\n\n/* Extra CSS */", list2css(css), sep = "\n")
style_extra_css <- function(
css,
outfile = "xaringan-themer.css",
append = TRUE,
heading = "Extra CSS"
) {
has_heading <- !is.null(heading)
x <- paste0(
if (has_heading) paste0("/* ", heading, " */\n"),
list2css(css)
)
if (append) x <- paste0(if (has_heading) "\n\n" else "\n", x)
if (is.null(outfile)) return(x)
cat(
x,
file = outfile,
append = TRUE,
append = append,
sep = "\n"
)
invisible(x)
}

#' @inheritParams style_extra_css

+ 0
- 69
tests/testthat/test-list2css.R Näytä tiedosto

@@ -1,69 +0,0 @@
context("test-list2css.R")

test_that("list2css converts lists to css", {
css <- list(
".remark-slide" = list(
"color" = "#FFF",
"font-size" = "30px"
)
)
expected <- ".remark-slide {
color: #FFF;
font-size: 30px;
}"
expect_equal(list2css(css), expected)

css[[".new-class"]] <- list("background-color" = "#000")
expected <- c(expected, ".new-class {\n background-color: #000;\n}")
expect_equal(list2css(css), expected)
})

test_that("list2css errors if css list is not named", {
css <- list(list(
"color" = "#FFF",
"font-size" = "30px"
))
expect_error(list2css(css))
})

test_that("list2css errors if css list has unnamed elements", {
css <- list(
list(
"color" = "#FFF",
"font-size" = "30px"
),
".test" = list(color = "red")
)
expect_error(list2css(css))
})

test_that("list2css errors if css list has unnamed properties", {
css <- list(
".class" = list(
color = "#FFF",
"font-size" = "30px"
),
".test" = list("red")
)
expect_error(list2css(css))

css <- list(
".class" = list(
"#FFF",
"font-size" = "30px"
),
".test" = list("red")
)
expect_error(list2css(css))
})

test_that("list2css errors if not list within list", {
css <- list(
".class" = list(
list(color = "red"),
"font-size" = "30px"
),
".test" = list("red")
)
expect_error(list2css(css))
})

+ 143
- 0
tests/testthat/test-style_extra_css.R Näytä tiedosto

@@ -0,0 +1,143 @@
# test_that()

describe("style_extra_css", {
css <- list(body = list(color = "#123"))

it("returns text if outfile is NULL", {
expect_equal(
style_extra_css(css, NULL),
"\n\n/* Extra CSS */\nbody {\n color: #123;\n}"
)
expect_equal(
style_extra_css(css, NULL, heading = NULL),
"\nbody {\n color: #123;\n}"
)
expect_equal(
style_extra_css(css, NULL, append = FALSE, heading = NULL),
"body {\n color: #123;\n}"
)
expect_equal(
style_extra_css(css, NULL, append = FALSE, heading = "TEST TEST"),
"/* TEST TEST */\nbody {\n color: #123;\n}"
)
})

tmpfile <- tempfile(fileext = ".css")
first <- style_extra_css(
css = list(.first = list(color = "#123")),
outfile = tmpfile,
append = FALSE,
heading = "First CSS"
)
first_exp <- "/* First CSS */\n.first {\n color: #123;\n}"

it("writes text to the outfile", {
expect_equal(first, first_exp)
expect_equal(
readLines(tmpfile, warn = FALSE),
strsplit(first_exp, "\n")[[1]]
)
})

second <- style_extra_css(
css = list(.second = list(color = "#321")),
outfile = tmpfile,
append = TRUE,
heading = "Second CSS"
)
second_exp <- "\n\n/* Second CSS */\n.second {\n color: #321;\n}"

it("appends to existing outfile", {
expect_equal(second, second_exp)
expect_equal(
readLines(tmpfile, warn = FALSE),
strsplit(paste0(first_exp, "\n", second_exp), "\n")[[1]]
)
})

third <- style_extra_css(
css = list(.third = list(color = "#333")),
outfile = tmpfile,
append = FALSE,
heading = "Third CSS"
)
third_exp <- "/* Third CSS */\n.third {\n color: #333;\n}"

it("over writes text in the outfile if append=FALSE", {
expect_equal(third, third_exp)
expect_equal(
readLines(tmpfile, warn = FALSE),
strsplit(third_exp, "\n")[[1]]
)
})
})

describe("list2css()", {
it("converts lists to css", {
css <- list(
".remark-slide" = list(
"color" = "#FFF",
"font-size" = "30px"
)
)
expected <- ".remark-slide {
color: #FFF;
font-size: 30px;
}"
expect_equal(list2css(css), expected)

css[[".new-class"]] <- list("background-color" = "#000")
expected <- c(expected, ".new-class {\n background-color: #000;\n}")
expect_equal(list2css(css), expected)
})

it("errors if css list is not named", {
css <- list(list(
"color" = "#FFF",
"font-size" = "30px"
))
expect_error(list2css(css))
})

it("errors if css list has unnamed elements", {
css <- list(
list(
"color" = "#FFF",
"font-size" = "30px"
),
".test" = list(color = "red")
)
expect_error(list2css(css))
})

it("errors if css list has unnamed properties", {
css <- list(
".class" = list(
color = "#FFF",
"font-size" = "30px"
),
".test" = list("red")
)
expect_error(list2css(css))

css <- list(
".class" = list(
"#FFF",
"font-size" = "30px"
),
".test" = list("red")
)
expect_error(list2css(css))
})

it("errors if not list within list", {
css <- list(
".class" = list(
list(color = "red"),
"font-size" = "30px"
),
".test" = list("red")
)
expect_error(list2css(css))
})
})

Loading…
Peruuta
Tallenna