Skip to content

Commit e0879d9

Browse files
authored
feat: hrg functions check their argument (#1699)
1 parent 22cd83e commit e0879d9

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

R/hrg.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ hrg <- hrg_create_impl
287287
#' @export
288288
#' @cdocs igraph_from_hrg_dendrogram
289289
hrg_tree <- function(hrg) {
290+
291+
if (!inherits(hrg, "igraphHRG")) {
292+
cli::cli_abort("{.arg hrg} must be an {.cls igraphHRG} object, not {.obj_type_friendly {hrg}}.")
293+
}
294+
290295
out <- from_hrg_dendrogram_impl(hrg)
291296

292297
g <- out$graph
@@ -305,8 +310,14 @@ hrg_tree <- function(hrg) {
305310
#' @family hierarchical random graph functions
306311
#' @export
307312
#' @cdocs igraph_hrg_game
308-
sample_hrg <- hrg_game_impl
313+
sample_hrg <- function(hrg) {
309314

315+
if (!inherits(hrg, "igraphHRG")) {
316+
cli::cli_abort("{.arg hrg} must be an {.cls igraphHRG} object, not {.obj_type_friendly {hrg}}.")
317+
}
318+
319+
hrg_game_impl(hrg)
320+
}
310321
#' Predict edges based on a hierarchical random graph model
311322
#'
312323
#' `predict_edges()` uses a hierarchical random graph model to predict

tests/testthat/_snaps/hrg.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,19 @@
1111
method 1 -none- character
1212
dist.method 1 -none- character
1313

14+
# sample_hrg() checks its argument
15+
16+
Code
17+
sample_hrg(make_ring(10))
18+
Condition
19+
Error in `sample_hrg()`:
20+
! `hrg` must be an <igraphHRG> object, not an <igraph> object.
21+
22+
# hrg_tree() checks its argument
23+
24+
Code
25+
hrg_tree(make_ring(10))
26+
Condition
27+
Error in `hrg_tree()`:
28+
! `hrg` must be an <igraphHRG> object, not an <igraph> object.
29+

tests/testthat/test-hrg.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ test_that("as.hclust.igraphHRG() works", {
1616
summary(as.hclust(hrg))
1717
})
1818
})
19+
20+
test_that("sample_hrg() checks its argument", {
21+
expect_snapshot(error = TRUE, {
22+
sample_hrg(make_ring(10))
23+
})
24+
})
25+
26+
27+
test_that("hrg_tree() checks its argument", {
28+
expect_snapshot(error = TRUE, {
29+
hrg_tree(make_ring(10))
30+
})
31+
})

0 commit comments

Comments
 (0)