Debian Codegraph @

tests/testthat/test-font-objects.R

raw
1 library(htmltools)
2
3 expect_remote_font <- function(x) {
4 input <- list(
5 list("font-variable" = x),
6 list("body {font-family: $font-variable}")
7 )
8 tagz <- renderTags(tags$style(sass(input)))
9 # Produces the correct CSS rules
10 expect_snapshot(tagz$html, cran = TRUE)
11 # Produces the right <link> tag via attached dependency
12 expect_snapshot(tagz$dependencies[[1]]$head, cran = TRUE)
13 }
14
15 test_that("Remote font importing basically works", {
16 # font_link()
17 expect_remote_font(font_link("foo", "bar"))
18 expect_remote_font(font_link("foo", "bar"))
19 # font_face()
20 expect_remote_font(font_face("foo", "bar"))
21 expect_remote_font(
22 font_face(
23 "foo", src = c("bar", "baz"),
24 weight = c(400, 600), display = "auto",
25 style = c("oblique", "30deg", "50deg"),
26 stretch = c("75%", "125%"),
27 unicode_range = c("U+0025-00FF", "U+4??")
28 )
29 )
30 # font_google()
31 expect_remote_font(font_google("Pacifico", local = FALSE))
32 expect_remote_font(font_google("Pacifico", local = FALSE, display = "auto"))
33 expect_remote_font(
34 font_google("Crimson Pro", local = FALSE, wght = "200..900")
35 )
36 expect_remote_font(
37 font_google("Crimson Pro", local = FALSE, wght = "200..900", ital = 1)
38 )
39 expect_remote_font(
40 font_google("Crimson Pro", local = FALSE, wght = c(400, 500), ital = 1)
41 )
42 expect_remote_font(
43 font_google("Crimson Pro", local = FALSE, wght = c(400, 500), ital = c(0, 1))
44 )
45 expect_remote_font(
46 font_google("Crimson Pro", local = FALSE, wght = c(600, 400, 500), ital = c(1, 0))
47 )
48 })
49
50 test_that("font_google(local = TRUE) basically works", {
51 skip_if_offline()
52
53 # Setup a new file cache and pass it to font_google()
54 tmpdir <- tempfile("sass-cache-test-")
55 dir.create(tmpdir)
56 cache <- sass_file_cache(tmpdir)
57 expect_equal(cache$size(), 0)
58
59 scss <- list(
60 list("my-font" = font_google("Pacifico", cache = cache)),
61 list("body {font-family: $my-font}")
62 )
63
64 # 1st time rendering font should add files to cache
65 expect_snapshot(
66 tagz <- renderTags(tags$style(sass(scss))),
67 transform = function(x) gsub("\\(.*\\)", "(<temp-cache>)", x)
68 )
69 size <- cache$size()
70 expect_true(size > 0)
71
72 # 2md time should result in cache hit
73 expect_snapshot(tagz <- renderTags(tags$style(sass(scss))))
74 expect_true(size == cache$size())
75
76 # Make sure the markup structure and files are as expected
77 expect_snapshot(tagz$html, cran = TRUE)
78 src <- tagz$dependencies[[1]]$src$file
79 expect_snapshot_file(
80 dir(src, pattern = "\\.css$", full.names = TRUE),
81 name = "font-css",
82 # Don't run on CRAN since the src is a hash that might get updated
83 cran = FALSE,
84 compare = compare_file_text
85 )
86 woff <- dir(src, pattern = "\\.woff2$", full.names = TRUE)
87 expect_true(length(woff) > 0)
88
89
90 # https://github.com/rstudio/bslib/issues/408
91 scss <- list(
92 list("my-font" = font_google("Crimson Pro")),
93 list("body {font-family: $my-font}")
94 )
95 tagz <- renderTags(tags$style(sass(scss)))
96 expect_equal(tagz$dependencies[[1]]$name, "Crimson_Pro")
97 })
98
99 expect_collection <- function(..., expected) {
100 scss <- as_sass(list(font = font_collection(...)))
101 expect_equal(
102 scss, paste0("$font: ", expected, " !default;"),
103 ignore_attr = TRUE
104 )
105 }
106
107 test_that("font_collection() basically works", {
108 expect_collection("foo", expected = "foo")
109 expect_collection(
110 "foo", "bar",
111 expected = "foo, bar"
112 )
113 expect_collection(
114 "foo", "foo bar",
115 expected = "foo, 'foo bar'"
116 )
117 expect_collection(
118 "foo", "foo bar, baz",
119 expected = "foo, 'foo bar', baz"
120 )
121 expect_collection(
122 "foo", c("foo bar", "baz"),
123 expected = "foo, 'foo bar', baz"
124 )
125 # Don't attempt to quote if , is quoted
126 expect_warning(
127 expect_collection(
128 "foo", "'foo, bar', baz",
129 expected = "foo, 'foo, bar', baz"
130 ),
131 "quote"
132 )
133 # Can suppress the warning with quote = FALSE
134 expect_warning(
135 expect_collection(
136 "'foo, bar', baz", quote = FALSE,
137 expected = "'foo, bar', baz"
138 ),
139 NA
140 )
141 expect_collection(
142 font_google("foo bar baz"), "foo",
143 expected = "'foo bar baz', foo"
144 )
145 expect_collection(
146 font_link("foo bar baz", "link"), "foo",
147 expected = "'foo bar baz', foo"
148 )
149 expect_collection(
150 font_face("foo bar baz", "..."), "foo",
151 expected = "'foo bar baz', foo"
152 )
153 })
154
155 test_that("Special named args in font_collection", {
156 expect_remote_font(
157 font_collection(google = list("foo", local = FALSE))
158 )
159 expect_remote_font(
160 font_collection(link = list("foo", href = "bar"))
161 )
162 expect_remote_font(
163 font_collection(collection = list(google = list("foo", local = FALSE)))
164 )
165 expect_error(
166 font_collection(foo = "bar"),
167 "foo"
168 )
169 expect_error(font_collection(NA))
170 expect_error(font_collection(""))
171 })