Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hrg functions check their argument #1699

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})
})
Loading