protomapr/tests/testthat/test-styles.R

174 lines
5.2 KiB
R
Raw Normal View History

2026-03-06 15:46:39 +11:00
test_that("pmMinimal creates correct structure", {
style <- pmMinimal()
expect_s3_class(style, "pm_style")
expect_true("colors" %in% names(style))
expect_true("labelRules" %in% names(style))
expect_equal(length(style$labelRules), 0)
})
test_that("pmMinimal with labels creates label rules", {
style <- pmMinimal(labels = TRUE)
expect_equal(length(style$labelRules), 1)
expect_equal(style$labelRules[[1]]$dataLayer, "places")
})
test_that("pmMinimal uses custom colors", {
style <- pmMinimal(land = "#ffffff", water = "#000000")
expect_equal(style$colors$earth, "#ffffff")
expect_equal(style$colors$water, "#000000")
expect_equal(style$colors$background, "#ffffff")
})
test_that("pmStyle returns valid presets", {
minimal <- pmStyle("minimal")
expect_s3_class(minimal, "pm_style")
dark <- pmStyle("minimal-dark")
expect_s3_class(dark, "pm_style")
expect_equal(dark$colors$earth, "#1a1a1a")
muted <- pmStyle("muted")
expect_s3_class(muted, "pm_style")
expect_true(length(muted$labelRules) > 0)
watercolor <- pmStyle("watercolor")
expect_s3_class(watercolor, "pm_style")
})
test_that("pmStyle validates name argument", {
expect_error(pmStyle("invalid"), "should be one of")
})
test_that("pmHideFeatures hides roads", {
hidden <- pmHideFeatures("roads", background = "#fff")
expect_equal(hidden$highway, "#fff")
expect_equal(hidden$major, "#fff")
expect_equal(hidden$minor, "#fff")
})
test_that("pmHideFeatures hides buildings", {
hidden <- pmHideFeatures("buildings", background = "#eee")
expect_equal(hidden$buildings, "#eee")
})
test_that("pmHideFeatures hides multiple categories", {
hidden <- pmHideFeatures(c("roads", "buildings", "landuse"), background = "#ccc")
expect_equal(hidden$highway, "#ccc")
expect_equal(hidden$buildings, "#ccc")
expect_equal(hidden$park_a, "#ccc")
})
test_that("pmCityLabels creates hierarchical labels", {
labels <- pmCityLabels("hierarchical")
expect_true(length(labels) >= 4) # 4 size levels + optional region
expect_equal(labels[[1]]$dataLayer, "places")
})
test_that("pmCityLabels creates major-only labels", {
labels <- pmCityLabels("major-only", include_regions = FALSE)
expect_equal(length(labels), 1)
})
test_that("pmCityLabels respects include_regions", {
with_regions <- pmCityLabels("major-only", include_regions = TRUE)
without_regions <- pmCityLabels("major-only", include_regions = FALSE)
expect_equal(length(with_regions), length(without_regions) + 1)
})
test_that("addProtomaps accepts style parameter", {
skip_if_not_installed("leaflet")
map <- leaflet::leaflet()
# Use a mock URL since we're just testing the R code, not actual tile loading
result <- addProtomaps(map, url = "https://example.com/tiles.pmtiles", style = pmStyle("minimal"))
expect_s3_class(result, "leaflet")
})
# New style presets
test_that("pmStyle returns ink preset", {
ink <- pmStyle("ink")
expect_s3_class(ink, "pm_style")
expect_equal(ink$colors$background, "#ffffff")
expect_equal(ink$colors$highway, "#000000")
})
test_that("pmStyle returns terrain preset", {
terrain <- pmStyle("terrain")
expect_s3_class(terrain, "pm_style")
expect_true(length(terrain$labelRules) >= 1)
})
test_that("pmStyle returns transit preset", {
transit <- pmStyle("transit")
expect_s3_class(transit, "pm_style")
expect_equal(transit$colors$railway, "#e63946")
})
# print.pm_style
test_that("print.pm_style outputs formatted text", {
style <- pmMinimal(land = "#ffffff", water = "#000000", labels = TRUE)
output <- capture.output(print(style))
expect_true(any(grepl("<pm_style>", output)))
expect_true(any(grepl("Colors:", output)))
expect_true(any(grepl("Label Rules:", output)))
})
test_that("print.pm_style handles style with no labels", {
style <- pmMinimal()
output <- capture.output(print(style))
expect_true(any(grepl("Label Rules: none", output)))
})
test_that("print.pm_style summarizes uniform colors", {
style <- pmMinimal()
output <- capture.output(print(style))
expect_true(any(grepl("uniform", output)))
})
# pmModifyStyle
test_that("pmModifyStyle preserves class", {
modified <- pmModifyStyle(pmMinimal(), water = "#000000")
expect_s3_class(modified, "pm_style")
})
test_that("pmModifyStyle merges colors", {
original <- pmMinimal(land = "#ffffff", water = "#aaaaaa")
modified <- pmModifyStyle(original, water = "#000000")
expect_equal(modified$colors$water, "#000000")
expect_equal(modified$colors$earth, "#ffffff")
})
test_that("pmModifyStyle appends label rules by default", {
original <- pmMinimal(labels = TRUE)
new_rules <- list(pmLabelRule("water", pmCenteredTextSymbolizer()))
modified <- pmModifyStyle(original, labelRules = new_rules)
expect_equal(length(modified$labelRules), 2)
})
test_that("pmModifyStyle replaces labels when requested", {
original <- pmMinimal(labels = TRUE)
new_rules <- list(pmLabelRule("water", pmCenteredTextSymbolizer()))
modified <- pmModifyStyle(original, labelRules = new_rules, replace_labels = TRUE)
expect_equal(length(modified$labelRules), 1)
expect_equal(modified$labelRules[[1]]$dataLayer, "water")
})
test_that("pmModifyStyle requires pm_style input", {
expect_error(pmModifyStyle(list()), "must be a pm_style")
})