From 4aae4f1a52c990dbaf3b261f9a97158566d93fd0 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 31 Jul 2026 19:03:17 +0200 Subject: [PATCH 1/2] Add snapshot test for show() --- DESCRIPTION | 3 ++- tests/testthat.R | 2 ++ tests/testthat/_snaps/methods.md | 28 ++++++++++++++++++++++++++++ tests/testthat/test-methods.R | 5 +++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/_snaps/methods.md diff --git a/DESCRIPTION b/DESCRIPTION index d0d5e33f..71b51a75 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -51,7 +51,7 @@ Suggests: EBImage, knitr, Rgraphviz, - testthat + testthat (>= 3.0.0) biocViews: DataImport, DataRepresentation, @@ -67,3 +67,4 @@ VignetteBuilder: knitr BugReports: https://github.com/HelenaLC/spatialdataR/issues URL: https://helenalc.github.io/spatialdataR, https://github.com/HelenaLC/spatialdataR Config/roxygen2/version: 8.0.0 +Config/testthat/edition: 3 diff --git a/tests/testthat.R b/tests/testthat.R index 16cea949..114fa01e 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,2 +1,4 @@ library(testthat) +library(spatialdataR) + test_check("spatialdataR") diff --git a/tests/testthat/_snaps/methods.md b/tests/testthat/_snaps/methods.md new file mode 100644 index 00000000..1a4aa190 --- /dev/null +++ b/tests/testthat/_snaps/methods.md @@ -0,0 +1,28 @@ +# show + + Code + show(x) + Output + class: SpatialData + - images(2): + - blobs_image (3,64,64) + - blobs_multiscale_image (3,64,64) + - labels(2): + - blobs_labels (64,64) + - blobs_multiscale_labels (64,64) + - points(1): + - blobs_points (200) + - shapes(3): + - blobs_circles (5,circle) + - blobs_multipolygons (2,polygon) + - blobs_polygons (5,polygon) + - tables(1): + - table (3,10) [blobs_labels] + coordinate systems(5): + - global(8): blobs_image blobs_multiscale_image ... blobs_polygons + blobs_points + - scale(1): blobs_labels + - translation(1): blobs_labels + - affine(1): blobs_labels + - sequence(1): blobs_labels + diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index 7539e948..247d39d0 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -329,3 +329,8 @@ test_that("[,SpatialData", { # infinite 'j' expect_no_error(y <- x[1, Inf]) }) + +# show ---- +test_that("show", { + expect_snapshot(show(x)) +}) \ No newline at end of file From c1689c8534e6bd15aa30773ec83f756573fcd4f9 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 31 Jul 2026 18:08:29 +0200 Subject: [PATCH 2/2] Avoid ifelse() Because of bad performance --- R/methods.R | 2 +- R/misc.R | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/methods.R b/R/methods.R index d0c471dd..173c3029 100644 --- a/R/methods.R +++ b/R/methods.R @@ -375,7 +375,7 @@ f <- \(e) setReplaceMethod(e, \(x, i, ..., value) { nms <- get(paste0(e, "Names"))(x) n <- length(get(paste0(e, "s"))(x)) - i <- ifelse(i > n, paste0(e, n+1), nms[i]) + i <- if(i > n) paste0(e, n+1) else nms[i] set <- get(paste0(e, "<-")) set(x, i, value=value) }) diff --git a/R/misc.R b/R/misc.R index f66aab29..082a5073 100644 --- a/R/misc.R +++ b/R/misc.R @@ -54,7 +54,7 @@ NULL cat(sprintf(" - %s (%s)\n", p, d), sep="") # shapes nc <- vapply(shapes(object), ncol, numeric(1)) - geom <- ifelse(nc == 1, "polygon", "circle") + geom <- c("circle", "polygon")[as.integer(nc == 1) + 1L] d <- vapply(shapes(object), nrow, numeric(1)) d <- paste(d, unname(geom), sep=",") cat(sprintf("- shapes(%s):\n", length(s))) @@ -89,7 +89,7 @@ setMethod("show", "SpatialData", .showSpatialData) #' @importFrom S4Vectors coolcat .showArray <- function(object) { n.object <- length(object@data) - cat("class: ", class(object), ifelse(n.object > 1, "(MultiScale)", ""),"\n") + cat("class: ", class(object), if (n.object > 1) "(MultiScale)" else "", "\n") scales <- vapply(object@data, \(x) paste0(dim(x), collapse=","), character(1)) coolcat("Scales (%d): (%s)", scales) } @@ -132,12 +132,12 @@ setMethod("show", "SpatialDataShape", .showShape) # coordinate transformations CTshow <- \(l) { f <- \(.) { - . <- paste(unlist(.), collapse=",") - ifelse(grepl(",", .), sprintf("[%s]", .), .) + if (length(.) > 1). + sprintf("[%s]", paste(unlist(.), collapse=",")) } g <- \(.) { - na <- is.null(.) || !length(unlist(.)) - ifelse(na, "", paste0(":", f(lapply(., f)))) + na <- !length(unlist(.)) + if (na) "" else paste0(":", f(lapply(., f))) } h <- \(.) sprintf("(%s%s)", .$type, g(.[[.$type]])) if (l$type == "sequence") {