| 1 |
# library(testthat) |
| 2 |
# library(dendextend) |
| 3 |
|
| 4 |
|
| 5 |
context("noded_with_condition") |
| 6 |
|
| 7 |
|
| 8 |
test_that("noded_with_condition works", { |
| 9 |
dend <- iris[1:10, -5] %>% |
| 10 |
dist() %>% |
| 11 |
hclust() %>% |
| 12 |
as.dendrogram() |
| 13 |
|
| 14 |
high_enough <- function(sub_dend, height) attr(sub_dend, "height") > height |
| 15 |
expect_equal( |
| 16 |
noded_with_condition(dend, high_enough, height = 0.5, include_leaves = F, include_branches = T, na.rm = T), |
| 17 |
c(T, T, T, F, F, F, T, F, F) |
| 18 |
) |
| 19 |
expect_equal( |
| 20 |
noded_with_condition(dend, high_enough, height = 0.5, include_leaves = T, include_branches = F, na.rm = T), |
| 21 |
c(F, F, F, F, F, F, F, F, F, F) |
| 22 |
) |
| 23 |
|
| 24 |
# if condition parameter missing |
| 25 |
expect_error( |
| 26 |
noded_with_condition(dend) |
| 27 |
) |
| 28 |
|
| 29 |
}) |
| 30 |
|
| 31 |
|
| 32 |
test_that("which_leaf works", { |
| 33 |
# if non-dendrogram object passed in |
| 34 |
expect_error( |
| 35 |
which_leaf(1:4) |
| 36 |
) |
| 37 |
}) |
| 38 |
|
| 39 |
|
| 40 |
test_that("which_node works", { |
| 41 |
dend <- iris[1:10, -5] %>% |
| 42 |
dist() %>% |
| 43 |
hclust() %>% |
| 44 |
as.dendrogram() %>% |
| 45 |
set("labels", 1:10) |
| 46 |
|
| 47 |
expect_equal( |
| 48 |
which_node(dend, c(1, 2)), |
| 49 |
2 |
| 50 |
) |
| 51 |
|
| 52 |
}) |