Skip to content

Commit ae57ec7

Browse files
committed
replace expect_error() by expect_snapshot(error = TRUE)
1 parent ddd207e commit ae57ec7

25 files changed

+112
-114
lines changed

tests/testthat/test-aes-setting.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ test_that("aesthetic parameters match length of data", {
77
}
88

99
set_colours("red")
10-
expect_error(set_colours(rep("red", 2)), "must be either length 1")
11-
expect_error(set_colours(rep("red", 3)), "must be either length 1")
12-
expect_error(set_colours(rep("red", 4)), "must be either length 1")
10+
expect_snapshot(set_colours(rep("red", 2)), error = TRUE)
11+
expect_snapshot(set_colours(rep("red", 3)), error = TRUE)
12+
expect_snapshot(set_colours(rep("red", 4)), error = TRUE)
1313
set_colours(rep("red", 5))
1414
})
1515

@@ -31,7 +31,7 @@ test_that("legend filters out aesthetics not of length 1", {
3131

3232
# Ideally would test something in the legend data structure, but
3333
# that's not easily accessible currently.
34-
expect_error(ggplot_gtable(ggplot_build(p)), NA)
34+
expect_no_error(ggplot_gtable(ggplot_build(p)))
3535
})
3636

3737
test_that("alpha affects only fill colour of solid geoms", {

tests/testthat/test-aes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test_that("aes evaluated in environment where plot created", {
4949
p <- ggplot(df, aes(foo, y)) + geom_point()
5050

5151
# Accessing an undefined variable should result in error
52-
expect_error(get_layer_data(p), "'foo' not found")
52+
expect_snapshot(get_layer_data(p), error = TRUE)
5353

5454
# Once it's defined we should get it back
5555
foo <- 0

tests/testthat/test-coord-.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ test_that("check coord limits errors only on bad inputs", {
4747
expect_null(check_coord_limits(c(1,2)))
4848

4949
# Should raise error if Scale object is passed
50-
expect_error(check_coord_limits(xlim(1,2)))
50+
expect_snapshot(check_coord_limits(xlim(1,2)), error = TRUE)
5151

5252
# Should raise error if vector of wrong length is passed
53-
expect_error(check_coord_limits(1:3))
53+
expect_snapshot(check_coord_limits(1:3), error = TRUE)
5454
})
5555

5656
test_that("coords append a column to the layout correctly", {

tests/testthat/test-empty-data.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test_that("layers with empty data are silently omitted with facet_wrap", {
3434
d <- ggplot(df0, aes(mpg, wt)) +
3535
geom_point() +
3636
facet_wrap(~cyl)
37-
expect_error(get_layer_data(d), "must have at least one value")
37+
expect_snapshot(get_layer_data(d), error = TRUE)
3838

3939
d <- d + geom_point(data = mtcars)
4040
expect_equal(nrow(get_layer_data(d, 1)), 0)
@@ -45,7 +45,7 @@ test_that("layers with empty data are silently omitted with facet_grid", {
4545
d <- ggplot(df0, aes(mpg, wt)) +
4646
geom_point() +
4747
facet_grid(am ~ cyl)
48-
expect_error(get_layer_data(d), "must have at least one value")
48+
expect_snapshot(get_layer_data(d), error = TRUE)
4949

5050
d <- d + geom_point(data = mtcars)
5151
expect_equal(nrow(get_layer_data(d, 1)), 0)
@@ -57,7 +57,7 @@ test_that("empty data overrides plot defaults", {
5757
d <- ggplot(mtcars, aes(mpg, wt)) +
5858
geom_point() +
5959
geom_point(data = data_frame())
60-
expect_error(get_layer_data(d), "not found")
60+
expect_snapshot(get_layer_data(d), error = TRUE)
6161

6262
# No extra points when x and y vars don't exist but are set
6363
d <- ggplot(mtcars, aes(mpg, wt)) +

tests/testthat/test-facet-.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ test_that("as_facets_list() coerces quosures objectss", {
4343
})
4444

4545
test_that("facets reject aes()", {
46-
expect_error(facet_wrap(aes(foo)), "Please use `vars()` to supply facet variables", fixed = TRUE)
47-
expect_error(facet_grid(aes(foo)), "Please use `vars()` to supply facet variables", fixed = TRUE)
46+
expect_snapshot(facet_wrap(aes(foo)), error = TRUE)
47+
expect_snapshot(facet_grid(aes(foo)), error = TRUE)
4848
})
4949

5050
test_that("compact_facets() returns a quosures object with compacted", {
@@ -353,9 +353,9 @@ test_that("at least one layer must contain all facet variables in combine_vars()
353353

354354
test_that("at least one combination must exist in combine_vars()", {
355355
df <- data_frame(letter = character(0))
356-
expect_error(
356+
expect_snapshot(
357357
combine_vars(list(df), vars = vars(letter = letter)),
358-
"Faceting variables must have at least one value"
358+
error = TRUE
359359
)
360360
})
361361

@@ -463,9 +463,9 @@ test_that("eval_facet() is tolerant for missing columns (#2963)", {
463463
)
464464

465465
# If the expression contains any non-existent variable, it fails
466-
expect_error(
466+
expect_snapshot(
467467
eval_facet(quo(no_such_variable * x), data_frame(foo = 1), possible_columns = c("x")),
468-
"object 'no_such_variable' not found"
468+
error = TRUE
469469
)
470470
})
471471

tests/testthat/test-facet-labels.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test_that("labeller() dispatches labellers", {
8686
# facet_wrap() shouldn't get both rows and cols
8787
p3 <- p + facet_wrap(~cyl, labeller = labeller(
8888
.cols = label_both, .rows = label_both))
89-
expect_error(ggplotGrob(p3))
89+
expect_snapshot(ggplotGrob(p3), error = TRUE)
9090

9191
# facet_grid() can get both rows and cols
9292
p4 <- p + facet_grid(am ~ cyl, labeller = labeller(
@@ -98,7 +98,7 @@ test_that("labeller() dispatches labellers", {
9898
# margin-wide labeller
9999
p5 <- p + facet_wrap(~cyl, labeller = labeller(
100100
.rows = label_both, cyl = label_value))
101-
expect_error(ggplotGrob(p5))
101+
expect_snapshot(ggplotGrob(p5), error = TRUE)
102102

103103
# Variables can be attributed labellers
104104
p6 <- p + facet_grid(am + cyl ~ ., labeller = labeller(

tests/testthat/test-fortify.R

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ test_that("fortify.default can handle healthy data-frame-like objects", {
6767

6868
# Not even data-frame-like
6969

70-
expect_error(fortify(X))
71-
expect_error(fortify(array(1:60, 5:3)))
70+
expect_snapshot(fortify(X), error = TRUE)
71+
expect_snapshot(fortify(array(1:60, 5:3)), error = TRUE)
7272

7373
# Unhealthy data-frame-like (matrix with no colnames)
7474

75-
expect_error(fortify(cbind(X, Y, Z, deparse.level=0)))
75+
expect_snapshot(fortify(cbind(X, Y, Z, deparse.level=0)), error = TRUE)
7676

7777
# Healthy data-frame-like (matrix with colnames)
7878

@@ -102,23 +102,23 @@ test_that("fortify.default can handle healthy data-frame-like objects", {
102102

103103
dim.foo <- function(x) stop("oops!")
104104
registerS3method("dim", "foo", dim.foo)
105-
expect_error(fortify(object))
105+
expect_snapshot(fortify(object), error = TRUE)
106106

107107
dim.foo <- function(x) c(length(x), 2)
108108
registerS3method("dim", "foo", dim.foo)
109-
expect_error(fortify(object))
109+
expect_snapshot(fortify(object), error = TRUE)
110110

111111
dim.foo <- function(x) 5:2
112112
registerS3method("dim", "foo", dim.foo)
113-
expect_error(fortify(object))
113+
expect_snapshot(fortify(object), error = TRUE)
114114

115115
dim.foo <- function(x) c(length(x), NA_integer_)
116116
registerS3method("dim", "foo", dim.foo)
117-
expect_error(fortify(object))
117+
expect_snapshot(fortify(object), error = TRUE)
118118

119119
dim.foo <- function(x) c(length(x), -5L)
120120
registerS3method("dim", "foo", dim.foo)
121-
expect_error(fortify(object))
121+
expect_snapshot(fortify(object), error = TRUE)
122122

123123
# Repair dim(<foo>)
124124

@@ -129,18 +129,18 @@ test_that("fortify.default can handle healthy data-frame-like objects", {
129129

130130
dimnames.foo <- function(x) list() # this breaks colnames(<foo>)
131131
registerS3method("dimnames", "foo", dimnames.foo)
132-
expect_error(fortify(object))
132+
expect_snapshot(fortify(object), error = TRUE)
133133

134134
dimnames.foo <- function(x) list(format(seq_along(x)), toupper)
135135
registerS3method("dimnames", "foo", dimnames.foo)
136-
expect_error(fortify(object))
136+
expect_snapshot(fortify(object), error = TRUE)
137137

138138
# Rejected by fortify.default() because behaviors of dim() and colnames()
139139
# don't align
140140

141141
dimnames.foo <- function(x) list(NULL, c("X1", "X2", "X3"))
142142
registerS3method("dimnames", "foo", dimnames.foo)
143-
expect_error(fortify(object))
143+
expect_snapshot(fortify(object), error = TRUE)
144144

145145
# Repair colnames(<foo>)
146146

@@ -151,20 +151,21 @@ test_that("fortify.default can handle healthy data-frame-like objects", {
151151

152152
as.data.frame.foo <- function(x, row.names = NULL, ...) stop("oops!")
153153
registerS3method("as.data.frame", "foo", as.data.frame.foo)
154-
expect_error(fortify(object))
154+
expect_snapshot(fortify(object), error = TRUE)
155155

156156
as.data.frame.foo <- function(x, row.names = NULL, ...) "whatever"
157157
registerS3method("as.data.frame", "foo", as.data.frame.foo)
158-
expect_error(fortify(object))
158+
expect_snapshot(fortify(object), error = TRUE)
159159

160160
as.data.frame.foo <- function(x, row.names = NULL, ...) data.frame()
161161
registerS3method("as.data.frame", "foo", as.data.frame.foo)
162-
expect_error(fortify(object))
162+
163+
expect_snapshot(fortify(object), error = TRUE)
163164

164165
as.data.frame.foo <- function(x, row.names = NULL, ...) {
165166
key <- if (is.null(names(x))) rownames(x) else names(x)
166167
data.frame(oops=key, value=unname(unclass(x)))
167168
}
168169
registerS3method("as.data.frame", "foo", as.data.frame.foo)
169-
expect_error(fortify(object))
170+
expect_snapshot(fortify(object), error = TRUE)
170171
})

tests/testthat/test-geom-path.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test_that("stairstep() does not error with too few observations", {
3535

3636
test_that("stairstep() exists with error when an invalid `direction` is given", {
3737
df <- data_frame(x = 1:3, y = 1:3)
38-
expect_error(stairstep(df, direction="invalid"))
38+
expect_snapshot(stairstep(df, direction = "invalid"), error = TRUE)
3939
})
4040

4141
test_that("stairstep() output is correct for direction = 'vh'", {

tests/testthat/test-geom-rect.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,5 @@ test_that("geom_rect can derive corners", {
2929
expect_equal(full[, corners], test[, corners])
3030

3131
test <- full[, c("x", "y")]
32-
expect_error(
33-
GeomRect$setup_data(test, NULL),
34-
"requires two of the following aesthetics"
35-
)
32+
expect_snapshot(GeomRect$setup_data(test, NULL), error = TRUE)
3633
})

tests/testthat/test-geom-sf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ test_that("geom_sf draws correctly", {
177177
# Perform minimal tests
178178
pts <- sf::st_sf(a = 1:2, geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)))
179179
plot <- ggplot() + geom_sf(data = pts)
180-
expect_error(regexp = NA, ggplot_build(plot))
180+
expect_no_error(ggplot_build(plot))
181181

182182
expect_doppelganger("North Carolina county boundaries",
183183
ggplot() + geom_sf(data = nc) + coord_sf(datum = 4326)

0 commit comments

Comments
 (0)