Skip to content

Commit

Permalink
New tests: themes (#1673)
Browse files Browse the repository at this point in the history
* TST getting and setting themes
TST Style -> css

* FIX test formatting; restore opts_knit and opts_chunk
  • Loading branch information
patr1ckm authored and yihui committed Mar 18, 2019
1 parent d411348 commit 7ece117
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -69,6 +69,7 @@ Authors@R: c(
person("Michael", "Friendly", role = "ctb"),
person("Michal", "Bojanowski", role = "ctb"),
person("Michel", "Kuhlmann", role = "ctb"),
person("Miller", "Patrick", role = "ctb"),
person("Nacho", "Caballero", role = "ctb"),
person("Nick", "Salkowski", role = "ctb"),
person("Noam", "Ross", role = "ctb"),
Expand Down
23 changes: 23 additions & 0 deletions tests/testit/acid.style
@@ -0,0 +1,23 @@

Description="Acid"

Default = { Colour="#000000" }
Canvas = { Colour="#eeeeee" }
Number = { Colour="#800080", Bold=true }
Escape = { Colour="#ff00ff", Bold=true }
String = { Colour="#a68500" }
StringPreProc = { Colour="#0000ff" }
BlockComment = { Colour="#ff8000" }
LineComment = { Colour="#f27900" }
PreProcessor = { Colour="#0080c0", Bold=true }
LineNum = { Colour="#303030" }
Operator = { Colour="#ff0080", Bold=true }
Interpolation = { Colour="#9E5DFF" }

Keywords = {
{ Colour= "#bb7977", Bold=true },
{ Colour= "#8080c0", Bold=true },
{ Colour= "#0080c0" },
{ Colour= "#004466" },
}

57 changes: 57 additions & 0 deletions tests/testit/test-themes.R
@@ -0,0 +1,57 @@
library(testit)

# getting and setting themes

formats = c("markdown", "latex")
for (frm in formats) {
opts_knit$set(out.format = frm)
thm = knit_theme$get('acid')
knit_theme$set(thm)
assert('knit_theme$get returns theme(s)', {
(identical(length(knit_theme$get()),
length(dir(system.file("themes", package = "knitr")))))
(!is.null(thm))
})

assert('knit_theme$set updates highlight and background', {
(thm$highlight %==% as.character(opts_knit$get('header')[1]))
(thm$background %==% opts_chunk$get('background'))
})
opts_knit$restore()
opts_chunk$restore()
}

# style -> theme

tl = theme2list('acid.style')
assert('theme2list parsing from .style to CSS', {
(tl$Description %==% "Acid")
(tl$Canvas$Colour %==% "#eeeeee")
(isTRUE(tl$Number$Bold))
(is.null(tl$Keywords))
})

# theme -> css

css = list2css(tl)
assert('css is generated for a theme', {
(grepl(tl$Canvas$Colour, css[1]))
(grepl("font-weight: bold;", css[2]))
(grepl(tl$String$Colour, css[3]))
})

fdir = file.path(tempdir(), "themes")
dir.create(fdir)
res = lapply(1:2, function(i) {
fn = paste0(fdir, "/acid", i, '.theme', collapse = "")
writeLines(readLines('acid.style'), con = fn)
return()
})

cssdir = file.path(tempdir(), "css")
dir.create(cssdir)
suppressMessages(themes2css(fdir, cssdir))
assert('themes2css creates two css files from test data', {
(length(dir(cssdir)) == 2)
(dir(cssdir) %==% c("acid1.css", "acid2.css"))
})

0 comments on commit 7ece117

Please sign in to comment.