Debian Codegraph @

tests/testthat/test-dendlist.R

raw
1 # library(testthat)
2 # suppressPackageStartupMessages(library(dendextend))
3
4 context("dendlist")
5
6 test_that("dendlist works", {
7 dend <- iris[, -5] %>%
8 dist() %>%
9 hclust() %>%
10 as.dendrogram()
11 dend2 <- iris[, -5] %>%
12 dist() %>%
13 hclust(method = "single") %>%
14 as.dendrogram()
15
16 expect_error(dendlist(1:4, 5, a = dend))
17
18 expect_equal(dendlist(dend) %>% class(), "dendlist")
19
20 # dendlist <- function (...) list(...)
21 # check lengths
22 expect_equal(dendlist(dend) %>% length(), 1L)
23 expect_equal(dendlist(dend, dend) %>% length(), 2L)
24 expect_equal(dendlist(dend, dend, dendlist(dend)) %>% length(), 3L)
25
26 # Checking that the piping works
27 expect_identical(
28 dendlist(dend, dend2),
29 dendlist(dend) %>% dendlist(dend2)
30 )
31
32 # the dendlist of a dendlist should be a dendlist!
33 expect_identical(
34 dendlist(dend), dendlist(dendlist(dend))
35 )
36 # dendextend:::all.equal.dendlist(dendlist(dend), dendlist(dendlist(dend)))
37 # This fails?
38
39
40 expect_identical(
41 dendlist(dend, dend), dendlist(dendlist(dend, dend))
42 )
43
44 # dendlist does not handle weird stuff.
45 expect_error(
46 dendlist(unclass(dendlist(dend, dend)))
47 )
48
49 ### I removed the warning when creating an empty dendlist
50 # # but it handles empty lists just fine:
51 # expect_warning(
52 # dendlist()
53 # )
54 # expect_warning(
55 # dendlist(list())
56 # )
57 # # It can merge a dendlist of an empty list with a dend just fine:
58 # expect_warning(
59 # dendlist(dendlist(list()), dend)
60 # )
61
62 # and the result is of length 1! (as we wanted it to)
63 expect_equal(
64 suppressWarnings(length(dendlist(dendlist(list()), dend))),
65 1
66 )
67
68
69 # checks is.dendlist
70 expect_true(suppressWarnings(is.dendlist(dendlist())))
71 expect_true(is.dendlist(dendlist(dend)))
72
73 # if hclust object passed in and/or 'which' parameter used
74 hc <- iris[, -5] %>%
75 dist() %>%
76 hclust()
77 result <- dendlist(dend, hc, dend, which = c(1,2))
78 expect_true(is.dendlist(result))
79 })
80
81
82
83
84
85 test_that("all.equal.dendlist works", {
86 dend <- iris[, -5] %>%
87 dist() %>%
88 hclust() %>%
89 as.dendrogram()
90
91 expect_true(
92 all.equal(dendlist(dend, dend))
93 )
94 expect_true(
95 all.equal(dendlist(dend, dend, dend))
96 )
97
98 expect_true(
99 all.equal(dendlist(dend, dend), dendlist(dend, dend))
100 )
101
102 p_dend <- prune(dend, "1")
103 expect_true(
104 is.character(
105 all.equal(dendlist(dend, p_dend))
106 )
107 )
108 })
109
110
111
112
113
114 test_that("plot.dendlist works", {
115 dend <- iris[1:10, -5] %>%
116 dist() %>%
117 hclust() %>%
118 as.dendrogram()
119 dend2 <- iris[1:10, -5] %>%
120 dist() %>%
121 hclust(method = "single") %>%
122 as.dendrogram()
123
124 dend12 <- dendlist(dend, dend2)
125 expect_no_error(
126 plot.dendlist(dend12)
127 )
128 dend12 <- dendlist(dend)
129 expect_no_error(
130 plot.dendlist(dend12)
131 )
132
133 # if nondendlist object passed in
134 expect_error(
135 plot.dendlist(dend)
136 )
137
138 })
139
140
141
142
143
144 test_that("as.dendlist works", {
145 hc <- iris[1:10, -5] %>%
146 dist() %>%
147 hclust()
148 dend <- as.dendrogram(hc)
149
150 # automatically removes non-dend objects
151 x <- list(1, hc, dend, list())
152 expect_equal(
153 length(as.dendlist(x)),
154 2
155 )
156
157 })
158
159
160
161
162
163 test_that("head.dendlist works", {
164 dend <- iris[1:10, -5] %>%
165 dist() %>%
166 hclust() %>%
167 as.dendrogram()
168 dend2 <- iris[1:10, -5] %>%
169 dist() %>%
170 hclust(method = "single") %>%
171 as.dendrogram()
172 dend12 <- dendlist(dend, dend2)
173
174 capture.output(expect_no_error(
175 head.dendlist(dend12)
176 ))
177 })