Skip to content

Commit e6f5aa7

Browse files
committed
Update tests with gg2list return as figure
1 parent 6b2a8e8 commit e6f5aa7

35 files changed

+314
-333
lines changed

tests/testthat/test-cookbook-axes.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ expect_traces <- function(gg, n.traces, name){
88
stopifnot(is.numeric(n.traces))
99
save_outputs(gg, paste0("cookbook-axes-", name))
1010
L <- gg2list(gg)
11-
is.trace <- names(L) == ""
12-
all.traces <- L[is.trace]
11+
all.traces <- L$data
1312
no.data <- sapply(all.traces, function(tr) {
1413
is.null(tr[["x"]]) && is.null(tr[["y"]])
1514
})
1615
has.data <- all.traces[!no.data]
1716
expect_equal(length(has.data), n.traces)
18-
list(traces=has.data, kwargs=L$kwargs)
17+
list(traces=has.data, layout=L$layout)
1918
}
2019

2120
# Reverse the order of a discrete-valued axis
@@ -44,13 +43,13 @@ test_that("ylim hides points", {
4443
bp.scale.hide <- bp + scale_y_continuous(limits=c(5, 7.5))
4544
test_that("scale_y(limits) hides points", {
4645
info <- expect_traces(bp.scale.hide, 3, "scale.hide")
47-
expect_equal(info$kwargs$layout$yaxis$range, c(5, 7.5))
46+
expect_equal(info$layout$yaxis$range, c(5, 7.5))
4847
})
4948

5049
bp.coord <- bp + coord_cartesian(ylim=c(5, 7.5))
5150
test_that("Using coord_cartesian zooms into the area", {
5251
info <- expect_traces(bp.coord, 3, "coord-ylim")
53-
expect_equal(info$kwargs$layout$yaxis$range, c(5, 7.5))
52+
expect_equal(info$layout$yaxis$range, c(5, 7.5))
5453
})
5554

5655
# Create some noisy exponentially-distributed data
@@ -148,7 +147,7 @@ bp.fonts <- bp +
148147

149148
test_that("element_text face, colour, size, angle, vjust, size", {
150149
info <- expect_traces(bp.fonts, 3, "fonts")
151-
x <- info$kwargs$layout$xaxis
150+
x <- info$layout$xaxis
152151
xtitle <- x[["titlefont"]]
153152
xtick <- x[["tickfont"]]
154153
expect_identical(xtitle$color, toRGB("#990000"))

tests/testthat/test-ggplot-abline.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ test_that("Second trace be the a-b line", {
1212

1313
L <- gg2list(gg)
1414

15-
expect_equal(length(L), 3)
16-
expect_true(L[[2]]$x[1] <= 0)
17-
expect_true(L[[2]]$x[2] >= 3.5)
18-
expect_identical(L[[2]]$mode, "lines")
19-
expect_identical(L[[2]]$line$shape, "linear")
20-
expect_equal(L[[2]]$line$width, 8)
15+
expect_equal(length(L$data), 2)
16+
expect_true(L$data[[2]]$x[1] <= 0)
17+
expect_true(L$data[[2]]$x[2] >= 3.5)
18+
expect_identical(L$data[[2]]$mode, "lines")
19+
expect_identical(L$data[[2]]$line$shape, "linear")
20+
expect_equal(L$data[[2]]$line$width, 8)
2121

22-
expect_identical(L[[1]]$showlegend, FALSE)
23-
expect_identical(L[[2]]$showlegend, FALSE)
22+
expect_identical(L$data[[1]]$showlegend, FALSE)
23+
expect_identical(L$data[[2]]$showlegend, FALSE)
2424

2525
save_outputs(gg, "abline")
2626
})

tests/testthat/test-ggplot-area.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ ar <- ggplot(huron) + geom_area(aes(x=year, y=level))
77
L <- gg2list(ar)
88

99
test_that("sanity check for geom_area", {
10-
expect_equal(length(L), 2)
11-
expect_identical(L[[1]]$type, "scatter")
12-
expect_equal(L[[1]]$x, c(huron$year[1], huron$year, tail(huron$year, n=1)))
13-
expect_equal(L[[1]]$y, c(0, huron$level, 0))
14-
expect_identical(L[[1]]$line$color, "transparent")
10+
expect_equal(length(L$data), 1)
11+
expect_identical(L$data[[1]]$type, "scatter")
12+
expect_equal(L$data[[1]]$x, c(huron$year[1], huron$year, tail(huron$year, n=1)))
13+
expect_equal(L$data[[1]]$y, c(0, huron$level, 0))
14+
expect_identical(L$data[[1]]$line$color, "transparent")
1515
})
1616

1717
save_outputs(ar, "area")
@@ -21,8 +21,8 @@ gg <- ggplot(huron) + geom_area(aes(x=year, y=level), alpha=0.4)
2121
L <- gg2list(gg)
2222

2323
test_that("transparency alpha in geom_area is converted", {
24-
expect_identical(L[[1]]$line$color, "transparent")
25-
expect_identical(L[[1]]$fillcolor, "rgba(51,51,51,0.4)")
24+
expect_identical(L$data[[1]]$line$color, "transparent")
25+
expect_identical(L$data[[1]]$fillcolor, "rgba(51,51,51,0.4)")
2626
})
2727

2828
save_outputs(gg, "area-fillcolor")

tests/testthat/test-ggplot-bar.R

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
context("bar")
22

3-
expect_traces <- function(gg, n.traces, name){
3+
expect_traces <- function(gg, n.traces, name) {
44
stopifnot(is.ggplot(gg))
55
stopifnot(is.numeric(n.traces))
66
save_outputs(gg, paste0("bar-", name))
77
L <- gg2list(gg)
8-
is.trace <- names(L) == ""
9-
all.traces <- L[is.trace]
8+
all.traces <- L$data
109
no.data <- sapply(all.traces, function(tr) {
1110
is.null(tr[["x"]]) && is.null(tr[["y"]])
1211
})
1312
has.data <- all.traces[!no.data]
1413
expect_equal(length(has.data), n.traces)
15-
list(traces=has.data, kwargs=L$kwargs)
14+
list(traces=has.data, layout=L$layout)
1615
}
1716

1817
researchers <-
@@ -29,7 +28,7 @@ test_that("position_dodge is translated to barmode=group", {
2928
trs <- info$traces
3029
trace.names <- sapply(trs[1:2], "[[", "name")
3130
expect_true(all(c("Math", "Bio") %in% trace.names))
32-
expect_identical(info$kwargs$layout$barmode, "group")
31+
expect_identical(info$layout$barmode, "group")
3332
# Check x values
3433
expect_identical(as.character(trs[[1]]$x), c("Canada", "Germany"))
3534
expect_identical(as.character(trs[[2]]$x), c("Canada", "USA"))
@@ -41,7 +40,7 @@ test_that("position_stack is translated to barmode=stack", {
4140
trs <- info$traces
4241
trace.names <- sapply(trs[1:2], "[[", "name")
4342
expect_true(all(c("Math", "Bio") %in% trace.names))
44-
expect_identical(info$kwargs$layout$barmode, "stack")
43+
expect_identical(info$layout$barmode, "stack")
4544
})
4645

4746
test_that("position_identity is translated to barmode=stack", {
@@ -50,7 +49,7 @@ test_that("position_identity is translated to barmode=stack", {
5049
trs <- info$traces
5150
trace.names <- sapply(trs[1:2], "[[", "name")
5251
expect_true(all(c("Math", "Bio") %in% trace.names))
53-
expect_identical(info$kwargs$layout$barmode, "stack")
52+
expect_identical(info$layout$barmode, "stack")
5453
})
5554

5655
test_that("dates work well with bar charts", {
@@ -60,7 +59,7 @@ test_that("dates work well with bar charts", {
6059
geom_bar(stat="identity")
6160
info <- expect_traces(gd, 2, "dates")
6261
trs <- info$traces
63-
expect_identical(info$kwargs$layout$xaxis$type, "date")
62+
expect_identical(info$layout$xaxis$type, "date")
6463
# plotly likes time in milliseconds
6564
t <- as.numeric(unique(researchers$month)) * 24 * 60 * 60 * 1000
6665
expect_identical(trs[[1]]$x, t)
@@ -79,8 +78,8 @@ test_that("Very basic bar graph", {
7978
expect_null(tr$marker$line$color)
8079
expect_null(tr$marker$line$width)
8180
}
82-
expect_null(info$kwargs$layout$annotations)
83-
expect_false(info$kwargs$layout$showlegend)
81+
expect_null(info$layout$annotations)
82+
expect_false(info$layout$showlegend)
8483
})
8584

8685
test_that("Map the time of day to different fill colors", {
@@ -93,8 +92,8 @@ test_that("Map the time of day to different fill colors", {
9392
expect_null(tr$marker$line$width)
9493
expect_true(tr$showlegend)
9594
}
96-
expect_match(info$kwargs$layout$annotations[[1]]$text, "time")
97-
expect_true(info$kwargs$layout$showlegend)
95+
expect_match(info$layout$annotations[[1]]$text, "time")
96+
expect_true(info$layout$showlegend)
9897
})
9998

10099
test_that("Add a black outline", {
@@ -107,8 +106,8 @@ test_that("Add a black outline", {
107106
expect_equal(tr$marker$line$width, 1)
108107
expect_true(tr$showlegend)
109108
}
110-
expect_match(info$kwargs$layout$annotations[[1]]$text, "time")
111-
expect_true(info$kwargs$layout$showlegend)
109+
expect_match(info$layout$annotations[[1]]$text, "time")
110+
expect_true(info$layout$showlegend)
112111
})
113112

114113
test_that("guides(fill=FALSE) hides fill legend", {
@@ -121,8 +120,8 @@ test_that("guides(fill=FALSE) hides fill legend", {
121120
expect_identical(tr$marker$line$color, toRGB("black"))
122121
expect_equal(tr$marker$line$width, 1)
123122
}
124-
expect_null(info$kwargs$layout$annotations)
125-
expect_false(info$kwargs$layout$showlegend)
123+
expect_null(info$layout$annotations)
124+
expect_false(info$layout$showlegend)
126125
})
127126

128127
test_that('guides(fill="none") hides fill legend', {
@@ -135,8 +134,8 @@ test_that('guides(fill="none") hides fill legend', {
135134
expect_identical(tr$marker$line$color, toRGB("black"))
136135
expect_equal(tr$marker$line$width, 1)
137136
}
138-
expect_null(info$kwargs$layout$annotations)
139-
expect_false(info$kwargs$layout$showlegend)
137+
expect_null(info$layout$annotations)
138+
expect_false(info$layout$showlegend)
140139
})
141140

142141
test_that('guides(colour="none") does not affect fill legend', {
@@ -150,8 +149,8 @@ test_that('guides(colour="none") does not affect fill legend', {
150149
expect_equal(tr$marker$line$width, 1)
151150
expect_true(tr$showlegend)
152151
}
153-
expect_match(info$kwargs$layout$annotations[[1]]$text, "time")
154-
expect_true(info$kwargs$layout$showlegend)
152+
expect_match(info$layout$annotations[[1]]$text, "time")
153+
expect_true(info$layout$showlegend)
155154
})
156155

157156
test_that("guides(fill=FALSE) does not affect colour legend", {
@@ -165,16 +164,16 @@ test_that("guides(fill=FALSE) does not affect colour legend", {
165164
expect_equal(tr$marker$line$width, 1)
166165
expect_true(tr$showlegend)
167166
}
168-
expect_match(info$kwargs$layout$annotations[[1]]$text, "time")
169-
expect_true(info$kwargs$layout$showlegend)
167+
expect_match(info$layout$annotations[[1]]$text, "time")
168+
expect_true(info$layout$showlegend)
170169
})
171170

172171

173172
base <- ggplot(mtcars, aes(factor(vs), fill=factor(cyl)))
174173

175174
test_that("geom_bar() stacks counts", {
176175
info <- expect_traces(base + geom_bar(), 3, "position-stack")
177-
expect_identical(info$kwargs$layout$barmode, "stack")
176+
expect_identical(info$layout$barmode, "stack")
178177
trs <- info$traces
179178
# sum of y values for each trace
180179
test <- as.numeric(sort(sapply(trs, function(x) sum(x$y))))
@@ -184,7 +183,7 @@ test_that("geom_bar() stacks counts", {
184183

185184
test_that("geom_bar(position = 'fill') stacks proportions", {
186185
info <- expect_traces(base + geom_bar(position = "fill"), 3, "position-fill")
187-
expect_identical(info$kwargs$layout$barmode, "stack")
186+
expect_identical(info$layout$barmode, "stack")
188187
trs <- info$traces
189188
# sum of y-values *conditioned* on a x-value
190189
prop <- sum(sapply(sapply(trs, "[[", "y"), "[", 1))

tests/testthat/test-ggplot-boxplot.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ test_that("geom_boxplot gives a boxplot", {
66
L <- gg2list(gg)
77

88
# right nb. traces
9-
expect_equal(length(L), 4)
9+
expect_equal(length(L$data), 3)
1010
# right type for 1st trace
11-
expect_identical(L[[1]]$type, "box")
11+
expect_identical(L$data[[1]]$type, "box")
1212
# right data for 1st trace
13-
expect_identical(sort(L[[1]]$y),
13+
expect_identical(sort(L$data[[1]]$y),
1414
sort(mtcars$mpg[mtcars$cyl == 4]))
1515

1616
save_outputs(gg, "boxplot")
@@ -22,11 +22,11 @@ test_that("geom_violin is equated to geom_boxplot for now", {
2222
L <- gg2list(gg)
2323

2424
# right nb. traces
25-
expect_equal(length(L), 4)
25+
expect_equal(length(L$data), 3)
2626
# right type for 1st trace
27-
expect_identical(L[[1]]$type, "box")
27+
expect_identical(L$data[[1]]$type, "box")
2828
# right data for 1st trace
29-
expect_identical(sort(L[[1]]$y),
29+
expect_identical(sort(L$data[[1]]$y),
3030
sort(mtcars$mpg[mtcars$cyl == 4]))
3131

3232
save_outputs(gg, "violin")
@@ -42,9 +42,9 @@ test_that("you can make a boxplot for a distribution of datetimes", {
4242

4343
L <- gg2list(bp)
4444

45-
expect_equal(length(L), 2) # 1 trace + layout
46-
expect_identical(L[[1]]$type, "box")
47-
expect_identical(L[[1]]$y, as.character(df$y))
45+
expect_equal(length(L$data), 1) # 1 trace
46+
expect_identical(L$data[[1]]$type, "box")
47+
expect_identical(L$data[[1]]$y, as.character(df$y))
4848

4949
save_outputs(bp, "boxplot-datetime")
5050
})

tests/testthat/test-ggplot-categorical.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ d <- head(diamonds, 50)
55
test_that("axis type=category when we plot factors", {
66
gg <- qplot(cut, price, data=d)
77
info <- gg2list(gg)
8-
l <- info$kwargs$layout
8+
l <- info$layout
99
expect_identical(l$xaxis$type, "category")
1010
expect_identical(l$yaxis$type, "linear")
1111

@@ -16,12 +16,12 @@ test_that("Bar charts of type=category show category names", {
1616
gbar <- ggplot(d, aes(cut, price)) + geom_bar(stat="identity")
1717
info <- gg2list(gbar)
1818

19-
expect_equal(length(info), 2) # 1 trace + layout
20-
expect_identical(info$kwargs$layout$xaxis$type, "category")
21-
expect_identical(info$kwargs$layout$xaxis$title, "cut")
22-
expect_identical(info$kwargs$layout$yaxis$type, "linear")
19+
expect_equal(length(info$data), 1) # 1 trace
20+
expect_identical(info$layout$xaxis$type, "category")
21+
expect_identical(info$layout$xaxis$title, "cut")
22+
expect_identical(info$layout$yaxis$type, "linear")
2323
expect_true(all(c("Fair", "Good", "Very Good", "Premium", "Ideal") %in%
24-
info[[1]]$x))
24+
info$data[[1]]$x))
2525

2626
save_outputs(gbar, "bar-category-names")
2727
})

tests/testthat/test-ggplot-contour.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ gg <- ggplot(volcano3d) + geom_contour(aes(x=x, y=y, z=z))
77
L <- gg2list(gg)
88

99
test_that("geom_contour is translated to type=contour", {
10-
expect_equal(length(L), 2)
11-
expect_identical(L[[1]]$type, "contour")
10+
expect_equal(length(L$data), 1)
11+
expect_identical(L$data[[1]]$type, "contour")
1212
})
1313

1414
test_that("geom_contour uses line contours by default", {
15-
expect_identical(L[[1]]$contours$coloring, "lines")
15+
expect_identical(L$data[[1]]$contours$coloring, "lines")
1616
})
1717

1818
save_outputs(gg, "contour")

tests/testthat/test-ggplot-date.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ test_that("datetimes are converted to e.g. 2013-01-02 05:00:00", {
99
data.frame(who="you", time.obj, dollars=c(10.2, 0)))
1010
gg <- qplot(time.obj, dollars, data=df, color=who, geom="line")
1111
info <- gg2list(gg)
12-
expect_equal(length(info), 3)
13-
expect_identical(info$kwargs$layout$xaxis$type, "date")
14-
for(trace in info[1:2]){
12+
expect_equal(length(info$data), 2)
13+
expect_identical(info$layout$xaxis$type, "date")
14+
for(trace in info$data[1:2]){
1515
expect_identical(trace$x, out.str)
1616
}
1717

@@ -24,9 +24,9 @@ test_that("class Date is supported", {
2424
df$x <- as.Date(df$x)
2525
gg <- ggplot(df) + geom_line(aes(x=x, y=y))
2626
info <- gg2list(gg)
27-
expect_equal(length(info), 2)
28-
expect_identical(info$kwargs$layout$xaxis$type, "date")
29-
expect_identical(info[[1]]$x[1], "2013-01-01 00:00:00")
27+
expect_equal(length(info$data), 1)
28+
expect_identical(info$layout$xaxis$type, "date")
29+
expect_identical(info$data[[1]]$x[1], "2013-01-01 00:00:00")
3030

3131
save_outputs(gg, "date-class-Date")
3232
})
@@ -46,13 +46,13 @@ test_that("scale_x_date and irregular time series work", {
4646
info <- gg2list(dt)
4747
info_w_scale <- gg2list(g)
4848

49-
expect_equal(length(info), 2) # one trace + layout
50-
expect_identical(info[[1]]$x[31], "2122-02-09 00:00:00")
51-
expect_equal(length(info_w_scale), 2) # one trace + layout
52-
expect_identical(info_w_scale[[1]]$x[31], "2122-02-09 00:00:00")
53-
expect_identical(info$kwargs$layout$xaxis$type, "date")
54-
expect_identical(info_w_scale$kwargs$layout$xaxis$type, "date")
55-
expect_equal(length(info_w_scale[[2]]), length(info[[2]])) # similar layout
49+
expect_equal(length(info$data), 1) # one trace
50+
expect_identical(info$data[[1]]$x[31], "2122-02-09 00:00:00")
51+
expect_equal(length(info_w_scale$data), 1) # one trace
52+
expect_identical(info_w_scale$data[[1]]$x[31], "2122-02-09 00:00:00")
53+
expect_identical(info$layout$xaxis$type, "date")
54+
expect_identical(info_w_scale$layout$xaxis$type, "date")
55+
expect_equal(length(info_w_scale$layout), length(info$layout)) # similar layout
5656

5757
save_outputs(dt, "date-irregular-time-series")
5858
})

tests/testthat/test-ggplot-density.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ expect_traces <- function(gg, n.traces, name) {
55
stopifnot(is.numeric(n.traces))
66
save_outputs(gg, paste0("density-", name))
77
L <- gg2list(gg)
8-
is.trace <- names(L) == ""
9-
all.traces <- L[is.trace]
8+
all.traces <- L$data
109
no.data <- sapply(all.traces, function(tr) {
1110
is.null(tr[["x"]]) && is.null(tr[["y"]])
1211
})
1312
has.data <- all.traces[!no.data]
1413
expect_equal(length(has.data), n.traces)
15-
list(traces=has.data, kwargs=L$kwargs)
14+
list(traces=has.data, layout=L$layout)
1615
}
1716

1817
# Draw a probability density estimation using geom_density

0 commit comments

Comments
 (0)