diff --git a/DESCRIPTION b/DESCRIPTION index 6061899..cecb01c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -13,9 +13,12 @@ Encoding: UTF-8 LazyData: true RoxygenNote: 7.2.3 Depends: - R (>= 2.10) + R (>= 4.1.0) URL: https://eodagmbh.github.io/r-maplibregl/ Imports: geojsonsf, htmlwidgets, purrr +Suggests: + testthat (>= 3.0.0) +Config/testthat/edition: 3 diff --git a/R/layer.R b/R/layer.R index b5a8fb0..df8a1d6 100644 --- a/R/layer.R +++ b/R/layer.R @@ -8,16 +8,15 @@ #' #' @export Layer <- function(type, id, source = NULL, paint = NULL, layout = NULL, ...) { - lay <- list( + list( type = type, id = id, source = source, paint = paint, layout = layout ) |> - purrr::compact() - class(lay) <- "MapLibreLayer" - return(lay) + purrr::compact() |> + set_maplibre_class("MapLibreLayer") } diff --git a/R/marker.R b/R/marker.R index 12c44f5..ee3a88a 100644 --- a/R/marker.R +++ b/R/marker.R @@ -4,30 +4,27 @@ #' @param popup #' @export #' @example examples/markers.R -Marker <- function(lngLat, popup , ...) { - mark <- list( - lngLat = lngLat, +Marker <- function(lngLat, popup = NULL, ...) { + list( + lngLat = lngLat, popup = popup, options = MarkerOptions(...) ) |> - purrr::compact() - class(mark) <- "MapLibreMarker" - return(mark) + purrr::compact() |> + set_maplibre_class("MapLibreMarker") } -#' Title -#' -#' @param ... -#' -#' @return -#' @export -#' -#' @examples -MarkerOptions <- function(...){ +MarkerOptions <- function(...) { marker_options <- list(...) - stopifnot(sapply(marker_options[c("anchor", "color", "pitchAlignment", "rotationAlignment")], function(x){is.null(x) | is.character(x)})) - stopifnot(sapply(marker_options["draggable"], function(x) {is.null(x) | is.logical(x)})) - stopifnot(sapply(marker_options[c("rotation", "scale")], function(x) {is.null(x) | is.numeric(x)})) + stopifnot(sapply(marker_options[c("anchor", "color", "pitchAlignment", "rotationAlignment")], function(x) { + is.null(x) | is.character(x) + })) + stopifnot(sapply(marker_options["draggable"], function(x) { + is.null(x) | is.logical(x) + })) + stopifnot(sapply(marker_options[c("rotation", "scale")], function(x) { + is.null(x) | is.numeric(x) + })) marker_options <- marker_options |> purrr::compact() class(marker_options) <- "MarkerOptions" diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..e5a170f --- /dev/null +++ b/R/utils.R @@ -0,0 +1,4 @@ +set_maplibre_class <- function(.obj, class_name) { + class(.obj) <- c(class(.obj), class_name) + return(.obj) +} diff --git a/data-raw/basemaps.R b/data-raw/basemaps.R index 69dd3ad..0da0cd6 100644 --- a/data-raw/basemaps.R +++ b/data-raw/basemaps.R @@ -3,10 +3,10 @@ PROVIDERS <- list( url = "https://basemaps.cartocdn.com/gl/%s-gl-style/style.json", themes = c("dark-matter", "voyager", "positron") ) - #, mapbox = list( + # , mapbox = list( # url = "mapbox://styles/mapbox/%s", # themes = c("streets-v11", "outdoors-v11", "light-v10", "dark-v10", "satellite-v9", "satellite-steets-v11") - #) + # ) ) build_urls <- function(provider) { diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..e29e8d3 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + +library(testthat) +library(maplibre) + +test_check("maplibre") diff --git a/tests/testthat/test-layer.R b/tests/testthat/test-layer.R new file mode 100644 index 0000000..ba736e1 --- /dev/null +++ b/tests/testthat/test-layer.R @@ -0,0 +1,17 @@ +test_that("layer spec", { + # Prepare + id <- "test" + type <- "fill" + + # Act + layer <- Layer( + id = id, + type = type + ) + + # Assert + expect_equal(layer$id, id) + expect_equal(layer$type, type) + expect_s3_class(layer, c("list")) + expect_s3_class(layer, c("MapLibreLayer")) +}) diff --git a/tests/testthat/test-marker.R b/tests/testthat/test-marker.R new file mode 100644 index 0000000..38bb360 --- /dev/null +++ b/tests/testthat/test-marker.R @@ -0,0 +1,12 @@ +test_that("marker spec", { + # Prepare + lng_lat <- c(5, 5) + + # Act + marker <- Marker(lng_lat) + + # Assert + expect_equal(marker$lngLat, lng_lat) + expect_s3_class(marker, "list") + expect_s3_class(marker, "MapLibreMarker") +})