tests/testthat/test-connectGrob_N_centered.R

library(testthat)

test_that("N connector uses straight center branch for one-to-many", {
  start <- boxGrob("Start")
  ends <- list(boxGrob("L"), boxGrob("C"), boxGrob("R"))
  # spread ends symmetric around center
  ends <- spreadHorizontal(ends, from = unit(0.1, "npc"), to = unit(0.9, "npc"), type = "center")

  con <- connectGrob(start, ends, type = "N")
  # one of the returned grobs should be vertical (x coords equal)
  is_vertical <- vapply(con, function(g) {
    xvals <- convertX(attr(g, "line")$x, "npc", valueOnly = TRUE)
    max(xvals) - min(xvals) < 1e-6
  }, logical(1))
  expect_true(any(is_vertical))
})


test_that("N connector uses straight center branch for many-to-one", {
  ends <- list(boxGrob("L"), boxGrob("C"), boxGrob("R"))
  ends <- spreadHorizontal(ends, from = unit(0.1, "npc"), to = unit(0.9, "npc"), type = "center")
  target <- boxGrob("Target")

  con <- connectGrob(ends, target, type = "N")
  # last length(ends) elements are trunks in our implementation
  trunks <- tail(con, length(ends))
  is_vertical <- vapply(trunks, function(g) {
    xvals <- convertX(attr(g, "line")$x, "npc", valueOnly = TRUE)
    max(xvals) - min(xvals) < 1e-6
  }, logical(1))
  expect_true(any(is_vertical))
})

Try the Gmisc package in your browser

Any scripts or data that you put into this service are public.

Gmisc documentation built on March 6, 2026, 9:09 a.m.