Skip to content

Commit

Permalink
tests: maybe_convert_font_size_to_rem
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Dec 10, 2024
1 parent 80d4c7f commit 038f6a2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
11 changes: 4 additions & 7 deletions R/bs-theme-preset-brand.R
Original file line number Diff line number Diff line change
Expand Up @@ -391,19 +391,16 @@ maybe_convert_font_size_to_rem <- function(x) {
value <- split_result$value
unit <- split_result$unit

if (unit == "rem") {
return(x)
}
if (unit == "em") {
if (unit %in% c("rem", "em")) {
return(paste0(value, "rem"))
}

scale <- list(
"%" = 100,
"px" = 16,
"pt" = 12,
"in" = 96 / 16, # 96 px/inch
"cm" = 96 / 16 * 2.54, # inch -> cm
"in" = 16 / 96, # 96 px/inch
"cm" = 16 / 96 * 2.54, # inch -> cm
"mm" = 16 / 96 * 25.4 # cm -> mm
)

Expand Down Expand Up @@ -527,7 +524,7 @@ read_brand_yml <- function(path = NULL) {
as_brand_yml <- function(brand = list()) {
stopifnot(is.list(brand))

# Normalize brand internals !! DOES NOT VALIDATE !!
# Normalize brand internals !! MINIMAL VALIDATION !!
brand <- brand_normalize_meta(brand)
brand <- brand_normalize_color(brand)

Expand Down
46 changes: 46 additions & 0 deletions tests/testthat/test-bs-theme-preset-brand.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,49 @@ describe("b_get_color()", {
)
})
})

describe("maybe_convert_font_size_to_rem()", {
it("returns `rem` directly", {
expect_equal(maybe_convert_font_size_to_rem("1rem"), "1rem")
expect_equal(maybe_convert_font_size_to_rem("1.123rem"), "1.123rem")
expect_equal(maybe_convert_font_size_to_rem("1.123 rem"), "1.123rem")
})

it("returns `em` as 1:1 with `rem`", {
expect_equal(maybe_convert_font_size_to_rem("1em"), "1rem")
expect_equal(maybe_convert_font_size_to_rem("1.123em"), "1.123rem")
expect_equal(maybe_convert_font_size_to_rem("1.123 em"), "1.123rem")
})

it("converts `%` as 100%:1rem", {
expect_equal(maybe_convert_font_size_to_rem("100%"), "1rem")
expect_equal(maybe_convert_font_size_to_rem("225%"), "2.25rem")
expect_equal(maybe_convert_font_size_to_rem("50 %"), "0.5rem")
})

it("converts `in`, `cm` and `mm` to `rem`", {
expect_equal(maybe_convert_font_size_to_rem("1in"), "6rem")
expect_equal(maybe_convert_font_size_to_rem("0.5in"), "3rem")

expect_equal(maybe_convert_font_size_to_rem("2.54cm"), "6rem")
expect_equal(maybe_convert_font_size_to_rem("1.27cm"), "3rem")

expect_equal(maybe_convert_font_size_to_rem("25.4mm"), "6rem")
expect_equal(maybe_convert_font_size_to_rem("12.7mm"), "3rem")
})

it("throws for unsupported units", {
expect_error(
maybe_convert_font_size_to_rem("1 foo")
)
expect_error(
maybe_convert_font_size_to_rem("1 foo bar")
)
expect_error(
maybe_convert_font_size_to_rem("1vw")
)
expect_error(
maybe_convert_font_size_to_rem("123")
)
})
})

0 comments on commit 038f6a2

Please sign in to comment.