Skip to content

Commit

Permalink
feat: hrg functions check their argument (#1699)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Feb 20, 2025
1 parent 22cd83e commit e0879d9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
13 changes: 12 additions & 1 deletion R/hrg.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/hrg.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <igraphHRG> object, not an <igraph> object.

# hrg_tree() checks its argument

Code
hrg_tree(make_ring(10))
Condition
Error in `hrg_tree()`:
! `hrg` must be an <igraphHRG> object, not an <igraph> object.

13 changes: 13 additions & 0 deletions tests/testthat/test-hrg.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

0 comments on commit e0879d9

Please sign in to comment.