Debian Codegraph @

tests/testthat/test-stats_imports.R

raw
1 # library(testthat)
2 # library(dendextend)
3
4
5 context("stats_imports")
6
7
8 test_that("stats_.memberDend works", {
9 x <- matrix(1:4)
10 expect_identical(
11 stats_.memberDend(x),
12 1L
13 )
14 })
15
16
17 test_that("stats_midcache.dendrogram works", {
18
19 # create custom class to reach specific error
20 dend <- as.dendrogram(hclust(dist(mtcars)))
21 class(dend) <- c("custom_dend", class(dend))
22 length.custom_dend <<- function(x) {
23 -1
24 }
25
26 expect_error(
27 stats_midcache.dendrogram(dend)
28 )
29
30 })
31
32
33 options(verbose = TRUE)
34 test_that("stats_plotNode works", {
35
36 hc <- USArrests[1:10, ] %>%
37 dist() %>%
38 hclust()
39 dend <- as.dendrogram(hc)
40 attr(dend[[1]], "edgetext") <- "t"
41 # general case which covers most lines
42 plot.new()
43 capture.output(expect_no_error(
44 stats_plotNode(1, 1, dend, center = T, nodePar = NULL, leaflab = "perpendicular")
45 ))
46 capture.output(expect_no_error(
47 stats_plotNode(1, 0.9, dend, center = F, edgePar = list(), type = "rectangle", nodePar = list("pch" = 1:2), leaflab = "perpendicular", dLeaf = NULL)
48 ))
49 capture.output(expect_no_error(
50 stats_plotNode(1, 0.9, dend, center = F, edgePar = list(), type = "rectangle", nodePar = NULL, leaflab = "perpendicular", dLeaf = NULL)
51 ))
52
53 # specific cases to cover all lines
54 capture.output(expect_no_error(
55 stats_plotNode(0.5, 1, dend, center = T, nodePar = 1, horiz = F, leaflab = "perpendicular", edgePar = list(), type = "rectangle", dLeaf = NULL)
56 ))
57 capture.output(expect_no_error(
58 stats_plotNode(0.5, 1, dend, center = T, nodePar = 1, leaflab = "textlike", edgePar = list(), type = "rectangle", dLeaf = NULL)
59 ))
60
61 # with type triangle
62 capture.output(expect_no_error(
63 stats_plotNode(0.5, 1, dend, center = T, nodePar = 1, leaflab = "textlike", horiz = T, edgePar = list(), type = "triangle", dLeaf = NULL)
64 ))
65 capture.output(expect_no_error(
66 stats_plotNode(0.5, 1, dend, center = T, nodePar = 1, horiz = F, leaflab = "perpendicular", edgePar = list(), type = "triangle", dLeaf = NULL)
67 ))
68
69 # if a leaf is passed in
70 leaf <- dend[[1]]
71 attr(leaf, "label") <- 1
72 capture.output(expect_no_error(
73 stats_plotNode(0.5, 1, leaf, center = T, nodePar = 1, leaflab = "perpendicular", horiz = T, edgePar = list(), type = "triangle", dLeaf = NULL)
74 ))
75 # if ybot is null
76 attr(dend[[1]], "height") <- NULL
77 capture.output(expect_no_error(
78 stats_plotNode(0.5, 1, dend, center = T, nodePar = 1, leaflab = "textlike", horiz = T, edgePar = list(), type = "triangle", dLeaf = NULL)
79 ))
80
81 })
82 options(verbose = FALSE)
83
84
85