diff --git a/R/hrg.R b/R/hrg.R index 9fb2bc0e6f..76431457bd 100644 --- a/R/hrg.R +++ b/R/hrg.R @@ -287,6 +287,11 @@ hrg <- hrg_create_impl #' @export #' @cdocs igraph_from_hrg_dendrogram hrg_tree <- function(hrg) { + + if (!inherits(hrg, "igraphHRG")) { + cli::cli_abort("{.arg hrg} must be an {.cls igraphHRG} object, not {.obj_type_friendly {hrg}}.") + } + out <- from_hrg_dendrogram_impl(hrg) g <- out$graph @@ -305,8 +310,14 @@ hrg_tree <- function(hrg) { #' @family hierarchical random graph functions #' @export #' @cdocs igraph_hrg_game -sample_hrg <- hrg_game_impl +sample_hrg <- function(hrg) { + if (!inherits(hrg, "igraphHRG")) { + cli::cli_abort("{.arg hrg} must be an {.cls igraphHRG} object, not {.obj_type_friendly {hrg}}.") + } + + hrg_game_impl(hrg) +} #' Predict edges based on a hierarchical random graph model #' #' `predict_edges()` uses a hierarchical random graph model to predict diff --git a/tests/testthat/_snaps/hrg.md b/tests/testthat/_snaps/hrg.md index 8dd969e9d2..239f540018 100644 --- a/tests/testthat/_snaps/hrg.md +++ b/tests/testthat/_snaps/hrg.md @@ -11,3 +11,19 @@ method 1 -none- character dist.method 1 -none- character +# sample_hrg() checks its argument + + Code + sample_hrg(make_ring(10)) + Condition + Error in `sample_hrg()`: + ! `hrg` must be an object, not an object. + +# hrg_tree() checks its argument + + Code + hrg_tree(make_ring(10)) + Condition + Error in `hrg_tree()`: + ! `hrg` must be an object, not an object. + diff --git a/tests/testthat/test-hrg.R b/tests/testthat/test-hrg.R index c584be3d63..1262bbb77e 100644 --- a/tests/testthat/test-hrg.R +++ b/tests/testthat/test-hrg.R @@ -16,3 +16,16 @@ test_that("as.hclust.igraphHRG() works", { summary(as.hclust(hrg)) }) }) + +test_that("sample_hrg() checks its argument", { + expect_snapshot(error = TRUE, { + sample_hrg(make_ring(10)) + }) +}) + + +test_that("hrg_tree() checks its argument", { + expect_snapshot(error = TRUE, { + hrg_tree(make_ring(10)) + }) +})