From 038f6a2c71ff7da562bff31a1ac4c89050587e27 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 10 Dec 2024 14:36:07 -0500 Subject: [PATCH] tests: maybe_convert_font_size_to_rem --- R/bs-theme-preset-brand.R | 11 ++--- tests/testthat/test-bs-theme-preset-brand.R | 46 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/R/bs-theme-preset-brand.R b/R/bs-theme-preset-brand.R index 6db8e8b60..69abad25d 100644 --- a/R/bs-theme-preset-brand.R +++ b/R/bs-theme-preset-brand.R @@ -391,10 +391,7 @@ 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")) } @@ -402,8 +399,8 @@ maybe_convert_font_size_to_rem <- function(x) { "%" = 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 ) @@ -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) diff --git a/tests/testthat/test-bs-theme-preset-brand.R b/tests/testthat/test-bs-theme-preset-brand.R index ba855640f..49d84635f 100644 --- a/tests/testthat/test-bs-theme-preset-brand.R +++ b/tests/testthat/test-bs-theme-preset-brand.R @@ -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") + ) + }) +})