forked from animint/animint2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-renderer1-labels.R
103 lines (84 loc) · 3.38 KB
/
test-renderer1-labels.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
acontext("Labels")
# create some objects that will be reused
ggpoint <- a_plot() + a_geom_point(a_aes(Petal.Width, Sepal.Width), data = iris)
test_that("ggtitle converts", {
viz <- list(scatter=ggpoint + ggtitle("My amazing plot!"))
info <- animint2HTML(viz)
expect_identical(info$plots$scatter$title, "My amazing plot!")
ptitle <- getNodeSet(info$html, "//text[@class='plottitle']")
expect_identical(xmlValue(ptitle[[1]]), "My amazing plot!")
})
test_that("ylab converts", {
viz <- list(scatter=ggpoint + ylab("Sepal Width"))
info <- animint2HTML(viz)
expect_identical(info$plots$scatter$ytitle, "Sepal Width")
ylabel <- getNodeSet(info$html, "//text[@class='ytitle']")
expect_identical(xmlValue(ylabel[[1]]), "Sepal Width")
})
test_that("a_scale_x_continuous(name) converts", {
viz <- list(scatter=ggpoint + a_scale_x_continuous("Petal Width"))
info <- animint2HTML(viz)
expect_identical(info$plots$scatter$xtitle, "Petal Width")
xlabel <- getNodeSet(info$html, "//text[@class='xtitle']")
expect_identical(xmlValue(xlabel[[1]]), "Petal Width")
})
test_that("a_scale_x_continuous(breaks)+xlab(name) converts", {
viz <-
list(scatter=a_plot() +
a_geom_point(a_aes(Petal.Length, Sepal.Length), data = iris) +
a_scale_x_continuous(breaks = c(1.5, 6.5)) +
xlab("Petal Length"))
info <- animint2HTML(viz)
expect_identical(info$plots$scatter$xtitle, "Petal Length")
expect_identical(as.character(info$plots$scatter$axis$xlab), c("1.5", "6.5"))
xlabel <- getNodeSet(info$html, "//text[@class='xtitle']")
expect_identical(xmlValue(xlabel[[1]]), "Petal Length")
xticks <- getNodeSet(
info$html, "//g[contains(@class, 'xaxis')]/g[@class='tick major']")
expect_identical(sapply(xticks, xmlValue), c("1.5", "6.5"))
})
stocks <- data.frame(
time = as.Date('2009-01-01') + 0:89,
vars = rep(c("A", "B", "C"), 30),
value = rnorm(90)
)
series <- a_plot() + a_geom_line(a_aes(x = time, y = value, group = vars), data = stocks)
test_that("a_scale_x_time ticks/labels work", {
info <- animint2HTML(list(series = series))
xticks <- getNodeSet(
info$html, "//g[contains(@class, 'xaxis')]/g[@class='tick major']")
expect_true(length(xticks) > 1)
})
getTickText <- function(html, id){
xpath <- sprintf(
"//g[contains(@class, '%s')]//g[@class='tick major']//text", id)
nodes <- getNodeSet(html, xpath)
sapply(nodes, xmlValue)
}
is.blank <- function(ticks){
all(ticks == "")
}
test_that("plot renders with a_theme(axis.text.x=a_element_blank())", {
viz <- list(series=series+a_theme(axis.text.x=a_element_blank()))
info <- animint2HTML(viz)
xticks <- getTickText(info$html, "xaxis")
expect_true(is.blank(xticks))
yticks <- getTickText(info$html, "yaxis")
expect_true(!is.blank(yticks))
})
test_that("plot renders with a_theme(axis.text.y=a_element_blank())", {
viz <- list(series=series+a_theme(axis.text.y=a_element_blank()))
info <- animint2HTML(viz)
xticks <- getTickText(info$html, "xaxis")
expect_true(!is.blank(xticks))
yticks <- getTickText(info$html, "yaxis")
expect_true(is.blank(yticks))
})
test_that("plot renders with a_theme(axis.text=a_element_blank())", {
viz <- list(series=series+a_theme(axis.text=a_element_blank()))
info <- animint2HTML(viz)
xticks <- getTickText(info$html, "xaxis")
expect_true(is.blank(xticks))
yticks <- getTickText(info$html, "yaxis")
expect_true(is.blank(yticks))
})