diff --git a/tests/testthat/_snaps/aes-setting.md b/tests/testthat/_snaps/aes-setting.md new file mode 100644 index 0000000000..b0ba47a52a --- /dev/null +++ b/tests/testthat/_snaps/aes-setting.md @@ -0,0 +1,36 @@ +# aesthetic parameters match length of data + + Code + set_colours(rep("red", 2)) + Condition + Error in `geom_point()`: + ! Problem while setting up geom aesthetics. + i Error occurred in the 1st layer. + Caused by error in `check_aesthetics()`: + ! Aesthetics must be either length 1 or the same as the data (5). + x Fix the following mappings: `colour`. + +--- + + Code + set_colours(rep("red", 3)) + Condition + Error in `geom_point()`: + ! Problem while setting up geom aesthetics. + i Error occurred in the 1st layer. + Caused by error in `check_aesthetics()`: + ! Aesthetics must be either length 1 or the same as the data (5). + x Fix the following mappings: `colour`. + +--- + + Code + set_colours(rep("red", 4)) + Condition + Error in `geom_point()`: + ! Problem while setting up geom aesthetics. + i Error occurred in the 1st layer. + Caused by error in `check_aesthetics()`: + ! Aesthetics must be either length 1 or the same as the data (5). + x Fix the following mappings: `colour`. + diff --git a/tests/testthat/_snaps/aes.md b/tests/testthat/_snaps/aes.md index 7f7f3ddc89..dc1c28889b 100644 --- a/tests/testthat/_snaps/aes.md +++ b/tests/testthat/_snaps/aes.md @@ -1,3 +1,14 @@ +# accessing an undefined variable results in an error + + Code + get_layer_data(p) + Condition + Error in `geom_point()`: + ! Problem while computing aesthetics. + i Error occurred in the 1st layer. + Caused by error: + ! object 'foo' not found + # aes evaluation fails with unknown input Unknown input: diff --git a/tests/testthat/_snaps/coord-.md b/tests/testthat/_snaps/coord-.md index c4f74d626c..acf9ad78c6 100644 --- a/tests/testthat/_snaps/coord-.md +++ b/tests/testthat/_snaps/coord-.md @@ -18,3 +18,19 @@ `coord()` has not implemented a `range()` method. +# check coord limits errors only on bad inputs + + Code + check_coord_limits(xlim(1, 2)) + Condition + Error: + ! `xlim(1, 2)` must be a vector of length 2, not a object. + +--- + + Code + check_coord_limits(1:3) + Condition + Error: + ! `1:3` must be a vector of length 2, not an integer vector of length 3. + diff --git a/tests/testthat/_snaps/empty-data.md b/tests/testthat/_snaps/empty-data.md new file mode 100644 index 0000000000..9889c48a91 --- /dev/null +++ b/tests/testthat/_snaps/empty-data.md @@ -0,0 +1,27 @@ +# layers with empty data are silently omitted with facet_wrap + + Code + get_layer_data(d) + Condition + Error in `combine_vars()`: + ! Faceting variables must have at least one value. + +# layers with empty data are silently omitted with facet_grid + + Code + get_layer_data(d) + Condition + Error in `combine_vars()`: + ! Faceting variables must have at least one value. + +# Should error when totally empty data frame because there's no x and y + + Code + get_layer_data(d) + Condition + Error in `geom_point()`: + ! Problem while computing aesthetics. + i Error occurred in the 2nd layer. + Caused by error: + ! object 'wt' not found + diff --git a/tests/testthat/_snaps/facet-.md b/tests/testthat/_snaps/facet-.md index 2efa86bc64..17d76b1f86 100644 --- a/tests/testthat/_snaps/facet-.md +++ b/tests/testthat/_snaps/facet-.md @@ -1,3 +1,19 @@ +# facets reject aes() + + Code + facet_wrap(aes(foo)) + Condition + Error in `validate_facets()`: + ! Please use `vars()` to supply facet variables. + +--- + + Code + facet_grid(aes(foo)) + Condition + Error in `validate_facets()`: + ! Please use `vars()` to supply facet variables. + # facet_grid() fails if passed both a formula and a vars() `rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list. @@ -30,6 +46,14 @@ x Plot is missing `letter` Layer is missing `letter` +# at least one combination must exist in combine_vars() + + Code + combine_vars(list(df), vars = vars(letter = letter)) + Condition + Error in `combine_vars()`: + ! Faceting variables must have at least one value. + # combine_vars() generates the correct combinations At least one layer must contain all faceting variables: `b` and `c` @@ -40,6 +64,15 @@ Faceting variables must have at least one value. +# eval_facet() is tolerant for missing columns (#2963) + + Code + eval_facet(quo(no_such_variable * x), data_frame(foo = 1), possible_columns = c( + "x")) + Condition + Error: + ! object 'no_such_variable' not found + # validate_facets() provide meaningful errors Please use `vars()` to supply facet variables. diff --git a/tests/testthat/_snaps/facet-labels.md b/tests/testthat/_snaps/facet-labels.md new file mode 100644 index 0000000000..6130705bea --- /dev/null +++ b/tests/testthat/_snaps/facet-labels.md @@ -0,0 +1,16 @@ +# labeller() dispatches labellers + + Code + ggplotGrob(p3) + Condition + Error in `resolve_labeller()`: + ! Cannot supply both `rows` and `cols` to `facet_wrap()`. + +--- + + Code + ggplotGrob(p5) + Condition + Error in `labeller()`: + ! Conflict between `.cols` and `cyl`. + diff --git a/tests/testthat/_snaps/fortify.md b/tests/testthat/_snaps/fortify.md index 81c3decea5..2034f092fb 100644 --- a/tests/testthat/_snaps/fortify.md +++ b/tests/testthat/_snaps/fortify.md @@ -3,3 +3,153 @@ `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`, not a object. i Did you accidentally pass `aes()` to the `data` argument? +# fortify.default can handle healthy data-frame-like objects + + Code + fortify(X) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `dim(data)` must return an of length 2. + +--- + + Code + fortify(array(1:60, 5:3)) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `dim(data)` must return an of length 2. + +--- + + Code + fortify(cbind(X, Y, Z, deparse.level = 0)) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `colnames(data)` must return a of length `ncol(data)`. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `dim.foo()`: + ! oops! + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `dim(data)` must return an of length 2. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `dim(data)` must return an of length 2. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `dim(data)` can't have `NA`s or negative values. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `dim(data)` can't have `NA`s or negative values. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `dimnames(x)[[2L]]`: + ! subscript out of bounds + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `colnames(data)` must return a of length `ncol(data)`. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.prevalidate_data_frame_like_object()`: + ! `colnames(data)` must return a of length `ncol(data)`. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `as.data.frame.foo()`: + ! oops! + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.postvalidate_data_frame_like_object()`: + ! `as.data.frame(data)` must return a . + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.postvalidate_data_frame_like_object()`: + ! `as.data.frame(data)` must preserve dimensions. + +--- + + Code + fortify(object) + Condition + Error in `fortify()`: + ! `data` must be a , or an object coercible by `fortify()`, or a valid -like object coercible by `as.data.frame()`. + Caused by error in `.postvalidate_data_frame_like_object()`: + ! `as.data.frame(data)` must preserve column names. + diff --git a/tests/testthat/_snaps/geom-path.md b/tests/testthat/_snaps/geom-path.md index 6516134f98..a8819b10de 100644 --- a/tests/testthat/_snaps/geom-path.md +++ b/tests/testthat/_snaps/geom-path.md @@ -5,3 +5,11 @@ Caused by error in `draw_panel()`: ! `geom_path()` can't have varying colour, linewidth, and/or alpha along the line when linetype isn't solid. +# stairstep() exists with error when an invalid `direction` is given + + Code + stairstep(df, direction = "invalid") + Condition + Error in `stairstep()`: + ! `direction` must be one of "hv", "vh", or "mid", not "invalid". + diff --git a/tests/testthat/_snaps/geom-rect.md b/tests/testthat/_snaps/geom-rect.md new file mode 100644 index 0000000000..8c04cbbb21 --- /dev/null +++ b/tests/testthat/_snaps/geom-rect.md @@ -0,0 +1,9 @@ +# geom_rect can derive corners + + Code + GeomRect$setup_data(test, NULL) + Condition + Error in `resolve_rect()`: + ! `geom_rect()` requires two of the following aesthetics: xmin, xmax, x, or width. + i Currently, x is present. + diff --git a/tests/testthat/_snaps/geom-text.md b/tests/testthat/_snaps/geom-text.md index c9d11b2bc7..ace427b203 100644 --- a/tests/testthat/_snaps/geom-text.md +++ b/tests/testthat/_snaps/geom-text.md @@ -3,3 +3,15 @@ Both `position` and `nudge_x`/`nudge_y` are supplied. i Only use one approach to alter the position. +# geom_text() rejects exotic units + + Code + ggplotGrob(p + geom_text(size = 10, size.unit = "npc")) + Condition + Error in `geom_text()`: + ! Problem while converting geom to grob. + i Error occurred in the 1st layer. + Caused by error in `resolve_text_unit()`: + ! `unit` must be one of "mm", "pt", "cm", "in", or "pc", not "npc". + i Did you mean "pc"? + diff --git a/tests/testthat/_snaps/ggsave.md b/tests/testthat/_snaps/ggsave.md index 8a16fc672b..a1f6ed2b8c 100644 --- a/tests/testthat/_snaps/ggsave.md +++ b/tests/testthat/_snaps/ggsave.md @@ -1,7 +1,67 @@ +# ggsave can create directories + + Code + ggsave(path, p) + Condition + Error in `ggsave()`: + ! Cannot find directory 'PATH' + i Please supply an existing directory or use `create.dir = TRUE`. + +# ggsave warns about empty or multiple filenames + + Code + ggsave(character(), plot) + Condition + Error in `ggsave()`: + ! `filename` must be a single string, not an empty character vector. + +# ggsave fails informatively for no-extension filenames + + Code + ggsave(tempfile(), plot) + Condition + Error in `ggsave()`: + ! Can't save to PATH + i Either supply `filename` with a file extension or supply `device`. + +# warned about large plot unless limitsize = FALSE + + Code + plot_dim(c(50, 50)) + Condition + Error: + ! Dimensions exceed 50 inches (`height` and `width` are specified in inches not pixels). + i If you're sure you want a plot that big, use `limitsize = FALSE`. + +--- + + Code + plot_dim(c(15000, 15000), units = "px") + Condition + Error: + ! Dimensions exceed 50 inches (`height` and `width` are specified in pixels). + i If you're sure you want a plot that big, use `limitsize = FALSE`. + # unknown device triggers error `device` must be a string, function or `NULL`, not the number 1. +--- + + Code + plot_dev("xyz") + Condition + Error: + ! Unknown graphics device "xyz" + +--- + + Code + plot_dev(NULL, "test.xyz") + Condition + Error: + ! Unknown graphics device "xyz" + # invalid single-string DPI values throw an error `dpi` must be one of "screen", "print", or "retina", not "abc". diff --git a/tests/testthat/_snaps/guides.md b/tests/testthat/_snaps/guides.md index be7c84d7f3..f6dfb10b15 100644 --- a/tests/testthat/_snaps/guides.md +++ b/tests/testthat/_snaps/guides.md @@ -63,6 +63,22 @@ Breaks are not formatted correctly for a bin legend. i Use `(, ]` format to indicate bins. +# get_guide_data retrieves keys appropriately + + Code + get_guide_data(b, 1) + Condition + Error in `get_guide_data()`: + ! `aesthetic` must be a single string, not the number 1. + +--- + + Code + get_guide_data(b, "x", panel = "a") + Condition + Error in `get_guide_data()`: + ! `panel` must be a whole number, not the string "a". + # guide_axis_logticks calculates appropriate ticks The `prescale.base` argument will override the scale's log-10 transformation in log-tick positioning. diff --git a/tests/testthat/_snaps/labels.md b/tests/testthat/_snaps/labels.md index 49efe59a72..80b541e2e4 100644 --- a/tests/testthat/_snaps/labels.md +++ b/tests/testthat/_snaps/labels.md @@ -25,3 +25,19 @@ `plot.tag.position` must be one of "topleft", "top", "topright", "left", "right", "bottomleft", "bottom", or "bottomright", not "foobar". +--- + + Code + ggplotGrob(p + theme(plot.tag.position = c(0, 0.5, 1))) + Condition + Error in `theme()`: + ! A `plot.tag.position` theme setting must have length 2. + +--- + + Code + ggplotGrob(p + theme(plot.tag.position = c(0, 0), plot.tag.location = "margin")) + Condition + Error in `theme()`: + ! A `plot.tag.position` cannot be used with `"margin"` as `plot.tag.location`. + diff --git a/tests/testthat/_snaps/layer.md b/tests/testthat/_snaps/layer.md index d95e11bed6..52f2292552 100644 --- a/tests/testthat/_snaps/layer.md +++ b/tests/testthat/_snaps/layer.md @@ -47,6 +47,28 @@ x `fill = after_stat(data)` i Did you map your stat in the wrong layer? +# missing aesthetics trigger informative error + + Code + ggplot_build(ggplot(df) + geom_line()) + Condition + Error in `geom_line()`: + ! Problem while setting up geom. + i Error occurred in the 1st layer. + Caused by error in `compute_geom_1()`: + ! `geom_line()` requires the following missing aesthetics: x and y. + +--- + + Code + ggplot_build(ggplot(df) + geom_col()) + Condition + Error in `geom_col()`: + ! Problem while setting up geom. + i Error occurred in the 1st layer. + Caused by error in `compute_geom_1()`: + ! `geom_col()` requires the following missing aesthetics: x and y. + # function aesthetics are wrapped with after_stat() Problem while computing aesthetics. @@ -89,6 +111,16 @@ All aesthetics have length 1, but the data has 32 rows. i Please consider using `annotate()` or provide this layer with data containing a single row. +# layer names can be resolved + + Code + p + l + l + Condition + Error in `new_layer_names()`: + ! Names must be unique. + x These names are duplicated: + * "foobar" at locations 3 and 4. + # layer_data returns a data.frame `layer_data()` must return a . diff --git a/tests/testthat/_snaps/performance.md b/tests/testthat/_snaps/performance.md new file mode 100644 index 0000000000..153fde6c57 --- /dev/null +++ b/tests/testthat/_snaps/performance.md @@ -0,0 +1,9 @@ +# modifyList is masked + + Code + modifyList(testlist, testappend) + Condition + Error in `modifyList()`: + ! Please use `modify_list()` instead of `modifyList()` for better performance. + i See the vignette ggplot2 internal programming guidelines for details. + diff --git a/tests/testthat/_snaps/position_dodge.md b/tests/testthat/_snaps/position_dodge.md new file mode 100644 index 0000000000..044f2b4392 --- /dev/null +++ b/tests/testthat/_snaps/position_dodge.md @@ -0,0 +1,11 @@ +# position_dodge warns about missing required aesthetics + + Code + ggplot_build(p) + Condition + Error: + ! Problem while computing position. + i Error occurred in the 1st layer. + Caused by error in `setup_params()`: + ! `position_dodge()` requires the following missing aesthetics: x or xmin. + diff --git a/tests/testthat/_snaps/scale-discrete.md b/tests/testthat/_snaps/scale-discrete.md index c668bceba9..a3251c4c4e 100644 --- a/tests/testthat/_snaps/scale-discrete.md +++ b/tests/testthat/_snaps/scale-discrete.md @@ -1,3 +1,39 @@ +# Scale is checked in default colour scale + + Code + scale_colour_discrete(type = scale_colour_gradient) + Condition + Error in `scale_colour_discrete()`: + ! The `type` argument must return a discrete scale for the colour aesthetic. + x The provided scale is continuous. + +--- + + Code + scale_fill_discrete(type = scale_fill_gradient) + Condition + Error in `scale_fill_discrete()`: + ! The `type` argument must return a discrete scale for the fill aesthetic. + x The provided scale is continuous. + +--- + + Code + scale_colour_discrete(type = scale_fill_hue) + Condition + Error in `scale_colour_discrete()`: + ! The `type` argument must return a continuous scale for the colour aesthetic. + x The provided scale works with the following aesthetics: fill. + +--- + + Code + scale_fill_discrete(type = scale_colour_hue) + Condition + Error in `scale_fill_discrete()`: + ! The `type` argument must return a continuous scale for the fill aesthetic. + x The provided scale works with the following aesthetics: colour. + # Aesthetics with no continuous interpretation fails when called A continuous variable cannot be mapped to the linetype aesthetic. @@ -8,3 +44,27 @@ A continuous variable cannot be mapped to the shape aesthetic. i Choose a different aesthetic or use `scale_shape_binned()`. +# mapped_discrete vectors behaves as predicted + + Code + mapped_discrete(letters) + Condition + Error in `mapped_discrete()`: + ! Can't convert `x` to . + +# invalid palettes trigger errors + + Code + ggplot_build(p + scale_x_discrete(palette = function(x) LETTERS[1:3])) + Condition + Error in `scale_x_discrete()`: + ! The `palette` function must return a vector. + +--- + + Code + ggplot_build(p + scale_x_discrete(palette = function(x) 1:2)) + Condition + Error in `scale_x_discrete()`: + ! The `palette` function must return at least 3 values. + diff --git a/tests/testthat/_snaps/scale-manual.md b/tests/testthat/_snaps/scale-manual.md index faf69a7899..70c2fa4dfd 100644 --- a/tests/testthat/_snaps/scale-manual.md +++ b/tests/testthat/_snaps/scale-manual.md @@ -2,3 +2,19 @@ No shared levels found between `names(values)` of the manual scale and the data's colour values. +# insufficient values raise an error + + Code + ggplot_build(p + scale_colour_manual(values = "black")) + Condition + Error in `palette()`: + ! Insufficient values in manual scale. 2 needed but only 1 provided. + +# fewer values (#3451) + + Code + s2$map(c("4", "6", "8")) + Condition + Error in `palette()`: + ! Insufficient values in manual scale. 3 needed but only 2 provided. + diff --git a/tests/testthat/_snaps/scales-breaks-labels.md b/tests/testthat/_snaps/scales-breaks-labels.md new file mode 100644 index 0000000000..fb6b6e8a86 --- /dev/null +++ b/tests/testthat/_snaps/scales-breaks-labels.md @@ -0,0 +1,128 @@ +# labels match breaks + + Code + scale_x_discrete(breaks = 1:3, labels = 1:2) + Condition + Error in `scale_x_discrete()`: + ! `breaks` and `labels` must have the same length. + +--- + + Code + scale_x_continuous(breaks = 1:3, labels = 1:2) + Condition + Error in `scale_x_continuous()`: + ! `breaks` and `labels` must have the same length. + +# suppressing breaks, minor_breask, and labels works + + Code + scale_x_date(breaks = NA, limits = lims)$get_breaks() + Condition + Error in `scale_x_date()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + scale_x_date(labels = NA, limits = lims)$get_labels() + Condition + Error in `scale_x_date()`: + ! Invalid `labels` specification. Use `NULL`, not `NA`. + +--- + + Code + scale_x_date(minor_breaks = NA, limits = lims)$get_breaks_minor() + Condition + Error in `scale_x_date()`: + ! Invalid `minor_breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + scale_x_datetime(breaks = NA, limits = lims)$get_breaks() + Condition + Error in `scale_x_datetime()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + scale_x_datetime(labels = NA, limits = lims)$get_labels() + Condition + Error in `scale_x_datetime()`: + ! Invalid `labels` specification. Use `NULL`, not `NA`. + +--- + + Code + scale_x_datetime(minor_breaks = NA, limits = lims)$get_breaks_minor() + Condition + Error in `scale_x_datetime()`: + ! Invalid `minor_breaks` specification. Use `NULL`, not `NA`. + +# scale_breaks with explicit NA options (deprecated) + + Code + sxc$get_breaks() + Condition + Error in `scale_x_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + sxc$get_breaks_minor() + Condition + Error in `scale_x_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + syc$get_breaks() + Condition + Error in `scale_y_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + syc$get_breaks_minor() + Condition + Error in `scale_y_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + sac$get_breaks() + Condition + Error in `scale_alpha_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + ssc$get_breaks() + Condition + Error in `scale_size_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + sfc$get_breaks() + Condition + Error in `scale_fill_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + +--- + + Code + scc$get_breaks() + Condition + Error in `scale_colour_continuous()`: + ! Invalid `breaks` specification. Use `NULL`, not `NA`. + diff --git a/tests/testthat/_snaps/stat-bin.md b/tests/testthat/_snaps/stat-bin.md index dd7a8127bf..f92e737d94 100644 --- a/tests/testthat/_snaps/stat-bin.md +++ b/tests/testthat/_snaps/stat-bin.md @@ -53,6 +53,26 @@ Caused by error in `bin_breaks_bins()`: ! `bins` must be a whole number larger than or equal to 1, not the number -4. +# setting boundary and center + + Code + comp_bin(df, boundary = 5, center = 0) + Condition + Error in `stat_bin()`: + ! Problem while computing stat. + i Error occurred in the 1st layer. + Caused by error in `setup_params()`: + ! Only one of `boundary` and `center` may be specified in `stat_bin()`. + +# bin errors at high bin counts + + Code + bin_breaks_width(c(1, 2e+06), 1) + Condition + Error in `bin_breaks_width()`: + ! The number of histogram bins must be less than 1,000,000. + i Did you make `binwidth` too small? + # stat_count throws error when both x and y aesthetic present Problem while computing stat. diff --git a/tests/testthat/_snaps/stat-ecdf.md b/tests/testthat/_snaps/stat-ecdf.md index e4da4c47f9..ea9888a2aa 100644 --- a/tests/testthat/_snaps/stat-ecdf.md +++ b/tests/testthat/_snaps/stat-ecdf.md @@ -5,3 +5,11 @@ Caused by error in `setup_params()`: ! `stat_ecdf()` requires an x or y aesthetic. +# weighted ecdf warns about weird weights + + Code + wecdf(1:10, rep(c(-1, 1), 5)) + Condition + Error in `wecdf()`: + ! Cannot compute eCDF when the weight aesthetic sums up to "0". + diff --git a/tests/testthat/_snaps/stats.md b/tests/testthat/_snaps/stats.md index 92a4296185..bde26ba7d0 100644 --- a/tests/testthat/_snaps/stats.md +++ b/tests/testthat/_snaps/stats.md @@ -1,3 +1,14 @@ +# error message is thrown when aesthetics are missing + + Code + ggplot_build(p) + Condition + Error in `stat_sum()`: + ! Problem while computing stat. + i Error occurred in the 1st layer. + Caused by error in `compute_layer()`: + ! `stat_sum()` requires the following missing aesthetics: x and y. + # erroneously dropped aesthetics are found and issue a warning The following aesthetics were dropped during statistical transformation: colour and fill. diff --git a/tests/testthat/_snaps/theme.md b/tests/testthat/_snaps/theme.md index 259a887c1a..fa7237d37d 100644 --- a/tests/testthat/_snaps/theme.md +++ b/tests/testthat/_snaps/theme.md @@ -1,3 +1,19 @@ +# modifying theme element properties with + operator works + + Code + theme_grey() + "asdf" + Condition + Error: + ! Can't add `"asdf"` to a theme object. + +# replacing theme elements with %+replace% operator works + + Code + theme_grey() + "asdf" + Condition + Error: + ! Can't add `"asdf"` to a theme object. + # theme validation happens at build stage The `text` theme element must be a object. @@ -40,6 +56,14 @@ The `blablabla` theme element must be a object. +# elements can be merged + + Code + merge_element(text_base, rect_base) + Condition + Error in `merge_element()`: + ! Only elements of the same class can be merged. + # Theme elements are checked during build `plot.title.position` must be one of "panel" or "plot", not "test". diff --git a/tests/testthat/test-aes-setting.R b/tests/testthat/test-aes-setting.R index 422032017f..2071921c03 100644 --- a/tests/testthat/test-aes-setting.R +++ b/tests/testthat/test-aes-setting.R @@ -7,9 +7,9 @@ test_that("aesthetic parameters match length of data", { } set_colours("red") - expect_error(set_colours(rep("red", 2)), "must be either length 1") - expect_error(set_colours(rep("red", 3)), "must be either length 1") - expect_error(set_colours(rep("red", 4)), "must be either length 1") + expect_snapshot(set_colours(rep("red", 2)), error = TRUE) + expect_snapshot(set_colours(rep("red", 3)), error = TRUE) + expect_snapshot(set_colours(rep("red", 4)), error = TRUE) set_colours(rep("red", 5)) }) @@ -31,7 +31,7 @@ test_that("legend filters out aesthetics not of length 1", { # Ideally would test something in the legend data structure, but # that's not easily accessible currently. - expect_error(ggplot_gtable(ggplot_build(p)), NA) + expect_no_error(ggplot_gtable(ggplot_build(p))) }) test_that("alpha affects only fill colour of solid geoms", { diff --git a/tests/testthat/test-aes.R b/tests/testthat/test-aes.R index 1cb333fcac..dd4f742f16 100644 --- a/tests/testthat/test-aes.R +++ b/tests/testthat/test-aes.R @@ -48,8 +48,10 @@ test_that("aes evaluated in environment where plot created", { df <- data_frame(x = 1, y = 1) p <- ggplot(df, aes(foo, y)) + geom_point() - # Accessing an undefined variable should result in error - expect_error(get_layer_data(p), "'foo' not found") + test_that("accessing an undefined variable results in an error", { + skip_if(getRversion() <= "4.4.0") + expect_snapshot(get_layer_data(p), error = TRUE) + }) # Once it's defined we should get it back foo <- 0 diff --git a/tests/testthat/test-coord-.R b/tests/testthat/test-coord-.R index f1e910c807..b0cef2de26 100644 --- a/tests/testthat/test-coord-.R +++ b/tests/testthat/test-coord-.R @@ -47,10 +47,10 @@ test_that("check coord limits errors only on bad inputs", { expect_null(check_coord_limits(c(1,2))) # Should raise error if Scale object is passed - expect_error(check_coord_limits(xlim(1,2))) + expect_snapshot(check_coord_limits(xlim(1,2)), error = TRUE) # Should raise error if vector of wrong length is passed - expect_error(check_coord_limits(1:3)) + expect_snapshot(check_coord_limits(1:3), error = TRUE) }) test_that("coords append a column to the layout correctly", { diff --git a/tests/testthat/test-empty-data.R b/tests/testthat/test-empty-data.R index bdcc02003c..e6fe24ce38 100644 --- a/tests/testthat/test-empty-data.R +++ b/tests/testthat/test-empty-data.R @@ -34,7 +34,7 @@ test_that("layers with empty data are silently omitted with facet_wrap", { d <- ggplot(df0, aes(mpg, wt)) + geom_point() + facet_wrap(~cyl) - expect_error(get_layer_data(d), "must have at least one value") + expect_snapshot(get_layer_data(d), error = TRUE) d <- d + geom_point(data = mtcars) 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", { d <- ggplot(df0, aes(mpg, wt)) + geom_point() + facet_grid(am ~ cyl) - expect_error(get_layer_data(d), "must have at least one value") + expect_snapshot(get_layer_data(d), error = TRUE) d <- d + geom_point(data = mtcars) expect_equal(nrow(get_layer_data(d, 1)), 0) @@ -53,11 +53,13 @@ test_that("layers with empty data are silently omitted with facet_grid", { }) test_that("empty data overrides plot defaults", { - # Should error when totally empty data frame because there's no x and y - d <- ggplot(mtcars, aes(mpg, wt)) + - geom_point() + - geom_point(data = data_frame()) - expect_error(get_layer_data(d), "not found") + test_that("Should error when totally empty data frame because there's no x and y", { + skip_if(getRversion() <= "4.4.0") + d <- ggplot(mtcars, aes(mpg, wt)) + + geom_point() + + geom_point(data = data_frame()) + expect_snapshot(get_layer_data(d), error = TRUE) + }) # No extra points when x and y vars don't exist but are set d <- ggplot(mtcars, aes(mpg, wt)) + diff --git a/tests/testthat/test-facet-.R b/tests/testthat/test-facet-.R index 5084737622..11e86247ca 100644 --- a/tests/testthat/test-facet-.R +++ b/tests/testthat/test-facet-.R @@ -43,8 +43,8 @@ test_that("as_facets_list() coerces quosures objectss", { }) test_that("facets reject aes()", { - expect_error(facet_wrap(aes(foo)), "Please use `vars()` to supply facet variables", fixed = TRUE) - expect_error(facet_grid(aes(foo)), "Please use `vars()` to supply facet variables", fixed = TRUE) + expect_snapshot(facet_wrap(aes(foo)), error = TRUE) + expect_snapshot(facet_grid(aes(foo)), error = TRUE) }) 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() test_that("at least one combination must exist in combine_vars()", { df <- data_frame(letter = character(0)) - expect_error( + expect_snapshot( combine_vars(list(df), vars = vars(letter = letter)), - "Faceting variables must have at least one value" + error = TRUE ) }) @@ -463,9 +463,9 @@ test_that("eval_facet() is tolerant for missing columns (#2963)", { ) # If the expression contains any non-existent variable, it fails - expect_error( + expect_snapshot( eval_facet(quo(no_such_variable * x), data_frame(foo = 1), possible_columns = c("x")), - "object 'no_such_variable' not found" + error = TRUE ) }) diff --git a/tests/testthat/test-facet-labels.R b/tests/testthat/test-facet-labels.R index c8613bc978..dfe39fe7cf 100644 --- a/tests/testthat/test-facet-labels.R +++ b/tests/testthat/test-facet-labels.R @@ -86,7 +86,7 @@ test_that("labeller() dispatches labellers", { # facet_wrap() shouldn't get both rows and cols p3 <- p + facet_wrap(~cyl, labeller = labeller( .cols = label_both, .rows = label_both)) - expect_error(ggplotGrob(p3)) + expect_snapshot(ggplotGrob(p3), error = TRUE) # facet_grid() can get both rows and cols p4 <- p + facet_grid(am ~ cyl, labeller = labeller( @@ -98,7 +98,7 @@ test_that("labeller() dispatches labellers", { # margin-wide labeller p5 <- p + facet_wrap(~cyl, labeller = labeller( .rows = label_both, cyl = label_value)) - expect_error(ggplotGrob(p5)) + expect_snapshot(ggplotGrob(p5), error = TRUE) # Variables can be attributed labellers p6 <- p + facet_grid(am + cyl ~ ., labeller = labeller( diff --git a/tests/testthat/test-fortify.R b/tests/testthat/test-fortify.R index 3a48c76ba0..e98edad549 100644 --- a/tests/testthat/test-fortify.R +++ b/tests/testthat/test-fortify.R @@ -67,12 +67,12 @@ test_that("fortify.default can handle healthy data-frame-like objects", { # Not even data-frame-like - expect_error(fortify(X)) - expect_error(fortify(array(1:60, 5:3))) + expect_snapshot(fortify(X), error = TRUE) + expect_snapshot(fortify(array(1:60, 5:3)), error = TRUE) # Unhealthy data-frame-like (matrix with no colnames) - expect_error(fortify(cbind(X, Y, Z, deparse.level=0))) + expect_snapshot(fortify(cbind(X, Y, Z, deparse.level=0)), error = TRUE) # Healthy data-frame-like (matrix with colnames) @@ -100,25 +100,27 @@ test_that("fortify.default can handle healthy data-frame-like objects", { # Rejected by fortify.default() because of unhealthy dim() behavior + skip_if(getRversion() <= "4.4.0") + dim.foo <- function(x) stop("oops!") registerS3method("dim", "foo", dim.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) dim.foo <- function(x) c(length(x), 2) registerS3method("dim", "foo", dim.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) dim.foo <- function(x) 5:2 registerS3method("dim", "foo", dim.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) dim.foo <- function(x) c(length(x), NA_integer_) registerS3method("dim", "foo", dim.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) dim.foo <- function(x) c(length(x), -5L) registerS3method("dim", "foo", dim.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) # Repair dim() @@ -129,18 +131,18 @@ test_that("fortify.default can handle healthy data-frame-like objects", { dimnames.foo <- function(x) list() # this breaks colnames() registerS3method("dimnames", "foo", dimnames.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) dimnames.foo <- function(x) list(format(seq_along(x)), toupper) registerS3method("dimnames", "foo", dimnames.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) # Rejected by fortify.default() because behaviors of dim() and colnames() # don't align dimnames.foo <- function(x) list(NULL, c("X1", "X2", "X3")) registerS3method("dimnames", "foo", dimnames.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) # Repair colnames() @@ -151,20 +153,21 @@ test_that("fortify.default can handle healthy data-frame-like objects", { as.data.frame.foo <- function(x, row.names = NULL, ...) stop("oops!") registerS3method("as.data.frame", "foo", as.data.frame.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) as.data.frame.foo <- function(x, row.names = NULL, ...) "whatever" registerS3method("as.data.frame", "foo", as.data.frame.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) as.data.frame.foo <- function(x, row.names = NULL, ...) data.frame() registerS3method("as.data.frame", "foo", as.data.frame.foo) - expect_error(fortify(object)) + + expect_snapshot(fortify(object), error = TRUE) as.data.frame.foo <- function(x, row.names = NULL, ...) { key <- if (is.null(names(x))) rownames(x) else names(x) data.frame(oops=key, value=unname(unclass(x))) } registerS3method("as.data.frame", "foo", as.data.frame.foo) - expect_error(fortify(object)) + expect_snapshot(fortify(object), error = TRUE) }) diff --git a/tests/testthat/test-geom-path.R b/tests/testthat/test-geom-path.R index 3255a2f4cb..cb3128931e 100644 --- a/tests/testthat/test-geom-path.R +++ b/tests/testthat/test-geom-path.R @@ -35,7 +35,7 @@ test_that("stairstep() does not error with too few observations", { test_that("stairstep() exists with error when an invalid `direction` is given", { df <- data_frame(x = 1:3, y = 1:3) - expect_error(stairstep(df, direction="invalid")) + expect_snapshot(stairstep(df, direction = "invalid"), error = TRUE) }) test_that("stairstep() output is correct for direction = 'vh'", { diff --git a/tests/testthat/test-geom-rect.R b/tests/testthat/test-geom-rect.R index a0d90899f8..204df65ef2 100644 --- a/tests/testthat/test-geom-rect.R +++ b/tests/testthat/test-geom-rect.R @@ -29,8 +29,5 @@ test_that("geom_rect can derive corners", { expect_equal(full[, corners], test[, corners]) test <- full[, c("x", "y")] - expect_error( - GeomRect$setup_data(test, NULL), - "requires two of the following aesthetics" - ) + expect_snapshot(GeomRect$setup_data(test, NULL), error = TRUE) }) diff --git a/tests/testthat/test-geom-sf.R b/tests/testthat/test-geom-sf.R index e52a13e917..beb1e3b86a 100644 --- a/tests/testthat/test-geom-sf.R +++ b/tests/testthat/test-geom-sf.R @@ -177,7 +177,7 @@ test_that("geom_sf draws correctly", { # Perform minimal tests pts <- sf::st_sf(a = 1:2, geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2))) plot <- ggplot() + geom_sf(data = pts) - expect_error(regexp = NA, ggplot_build(plot)) + expect_no_error(ggplot_build(plot)) expect_doppelganger("North Carolina county boundaries", ggplot() + geom_sf(data = nc) + coord_sf(datum = 4326) diff --git a/tests/testthat/test-geom-text.R b/tests/testthat/test-geom-text.R index ca5b495968..79220d2cf2 100644 --- a/tests/testthat/test-geom-text.R +++ b/tests/testthat/test-geom-text.R @@ -30,9 +30,9 @@ test_that("geom_text() accepts mm and pt size units", { test_that("geom_text() rejects exotic units", { p <- ggplot(data_frame0(x = 1, y = 1, label = "A"), aes(x, y, label = label)) - expect_error( + expect_snapshot( ggplotGrob(p + geom_text(size = 10, size.unit = "npc")), - "must be one of" + error = TRUE ) }) diff --git a/tests/testthat/test-ggsave.R b/tests/testthat/test-ggsave.R index a5d7a5283c..dbbc890ca5 100644 --- a/tests/testthat/test-ggsave.R +++ b/tests/testthat/test-ggsave.R @@ -16,7 +16,10 @@ test_that("ggsave can create directories", { p <- ggplot(mpg, aes(displ, hwy)) + geom_point() - expect_error(ggsave(path, p)) + expect_snapshot( + ggsave(path, p), error = TRUE, + transform = function(x) gsub("directory '.*'\\.$", "directory 'PATH'", x) + ) expect_false(dir.exists(dirname(path))) # 2 messages: 1 for saving and 1 informing about directory creation @@ -83,17 +86,14 @@ test_that("ggsave warns about empty or multiple filenames", { ) }) - expect_error( - ggsave(character(), plot), - "`filename` must be a single string" - ) + expect_snapshot(ggsave(character(), plot), error = TRUE) }) test_that("ggsave fails informatively for no-extension filenames", { plot <- ggplot(mtcars, aes(disp, mpg)) + geom_point() - expect_error( - ggsave(tempfile(), plot), - "Can't save to" + expect_snapshot( + ggsave(tempfile(), plot), error = TRUE, + transform = function(x) gsub("to .*\\.$", "to PATH", x) ) }) @@ -112,9 +112,9 @@ test_that("uses 7x7 if no graphics device open", { }) test_that("warned about large plot unless limitsize = FALSE", { - expect_error(plot_dim(c(50, 50)), "exceed 50 inches") + expect_snapshot(plot_dim(c(50, 50)), error = TRUE) expect_equal(plot_dim(c(50, 50), limitsize = FALSE), c(50, 50)) - expect_error(plot_dim(c(15000, 15000), units = "px"), "in pixels).") + expect_snapshot(plot_dim(c(15000, 15000), units = "px"), error = TRUE) }) test_that("scale multiplies height & width", { @@ -126,8 +126,8 @@ test_that("scale multiplies height & width", { test_that("unknown device triggers error", { expect_snapshot_error(plot_dev(1)) - expect_error(plot_dev("xyz"), "Unknown graphics device") - expect_error(plot_dev(NULL, "test.xyz"), "Unknown graphics device") + expect_snapshot(plot_dev("xyz"), error = TRUE) + expect_snapshot(plot_dev(NULL, "test.xyz"), error = TRUE) }) diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 23df1f75e2..eb1b936350 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -381,8 +381,8 @@ test_that("get_guide_data retrieves keys appropriately", { # Non-existent panels expect_null(get_guide_data(b, "x", panel = 4)) - expect_error(get_guide_data(b, 1), "must be a single string") - expect_error(get_guide_data(b, "x", panel = "a"), "must be a whole number") + expect_snapshot(get_guide_data(b, 1), error = TRUE) + expect_snapshot(get_guide_data(b, "x", panel = "a"), error = TRUE) }) test_that("get_guide_data retrieves keys from exotic coords", { diff --git a/tests/testthat/test-labels.R b/tests/testthat/test-labels.R index 6a26578c0b..b8b002a3db 100644 --- a/tests/testthat/test-labels.R +++ b/tests/testthat/test-labels.R @@ -116,13 +116,13 @@ test_that("plot.tag.position rejects invalid input", { expect_snapshot_error( ggplotGrob(p + theme(plot.tag.position = "foobar")) ) - expect_error( + expect_snapshot( ggplotGrob(p + theme(plot.tag.position = c(0, 0.5, 1))), - "must have length 2" + error = TRUE ) - expect_error( + expect_snapshot( ggplotGrob(p + theme(plot.tag.position = c(0, 0), plot.tag.location = "margin")), - "cannot be used with `\"margin\"" + error = TRUE ) }) diff --git a/tests/testthat/test-layer.R b/tests/testthat/test-layer.R index 51f0cd9eee..b7023d342f 100644 --- a/tests/testthat/test-layer.R +++ b/tests/testthat/test-layer.R @@ -55,13 +55,13 @@ test_that("column vectors are allowed (#2609)", { test_that("missing aesthetics trigger informative error", { df <- data_frame(x = 1:10) - expect_error( + expect_snapshot( ggplot_build(ggplot(df) + geom_line()), - "requires the following missing aesthetics:" + error = TRUE ) - expect_error( + expect_snapshot( ggplot_build(ggplot(df) + geom_col()), - "requires the following missing aesthetics:" + error = TRUE ) }) @@ -154,10 +154,7 @@ test_that("layer names can be resolved", { expect_equal(names(p$layers), c("foo", "bar")) l <- geom_point(name = "foobar") - expect_error( - p + l + l, - "names are duplicated" - ) + expect_snapshot(p + l + l, error = TRUE) }) diff --git a/tests/testthat/test-performance.R b/tests/testthat/test-performance.R index 1c65622b4a..bd737f7c26 100644 --- a/tests/testthat/test-performance.R +++ b/tests/testthat/test-performance.R @@ -12,7 +12,7 @@ testappend <- list( ) test_that("modifyList is masked", { - expect_error(modifyList(testlist, testappend)) + expect_snapshot(modifyList(testlist, testappend), error = TRUE) }) test_that("modify_list retains unreferenced elements", { diff --git a/tests/testthat/test-position_dodge.R b/tests/testthat/test-position_dodge.R index 9107de1d92..14b79d3cad 100644 --- a/tests/testthat/test-position_dodge.R +++ b/tests/testthat/test-position_dodge.R @@ -51,5 +51,5 @@ test_that("position_dodge warns about missing required aesthetics", { mapping = aes(x = NULL) ) - expect_error(ggplot_build(p), "requires the following missing aesthetics") + expect_snapshot(ggplot_build(p), error = TRUE) }) diff --git a/tests/testthat/test-qplot.R b/tests/testthat/test-qplot.R index 58e8fa1e14..74ab153c39 100644 --- a/tests/testthat/test-qplot.R +++ b/tests/testthat/test-qplot.R @@ -46,7 +46,7 @@ test_that("qplot() evaluates layers in package environment", { } lifecycle::expect_deprecated( - expect_error(p <- qplot(1, 1, geom = "line"), NA) + expect_no_error(p <- qplot(1, 1, geom = "line")) ) }) diff --git a/tests/testthat/test-scale-discrete.R b/tests/testthat/test-scale-discrete.R index 9e8eeaf717..50f7b585fe 100644 --- a/tests/testthat/test-scale-discrete.R +++ b/tests/testthat/test-scale-discrete.R @@ -134,12 +134,12 @@ test_that("discrete scale defaults can be set globally", { test_that("Scale is checked in default colour scale", { # Check scale type - expect_error(scale_colour_discrete(type = scale_colour_gradient)) - expect_error(scale_fill_discrete(type = scale_fill_gradient)) + expect_snapshot(scale_colour_discrete(type = scale_colour_gradient), error = TRUE) + expect_snapshot(scale_fill_discrete(type = scale_fill_gradient), error = TRUE) # Check aesthetic - expect_error(scale_colour_discrete(type = scale_fill_hue)) - expect_error(scale_fill_discrete(type = scale_colour_hue)) + expect_snapshot(scale_colour_discrete(type = scale_fill_hue), error = TRUE) + expect_snapshot(scale_fill_discrete(type = scale_colour_hue), error = TRUE) }) test_that("Aesthetics with no continuous interpretation fails when called", { @@ -153,7 +153,7 @@ test_that("mapped_discrete vectors behaves as predicted", { expect_null(mapped_discrete(NULL)) expect_s3_class(mapped_discrete(c(0, 3.5)), "mapped_discrete") expect_s3_class(mapped_discrete(seq_len(4)), "mapped_discrete") - expect_error(mapped_discrete(letters)) + expect_snapshot(mapped_discrete(letters), error = TRUE) x <- mapped_discrete(1:10) expect_s3_class(x[2:4], "mapped_discrete") @@ -192,14 +192,14 @@ test_that("invalid palettes trigger errors", { p <- ggplot(df, aes(x, y)) + geom_point() - expect_error( + expect_snapshot( ggplot_build(p + scale_x_discrete(palette = function(x) LETTERS[1:3])), - "must return a .+ vector\\." + error = TRUE ) - expect_error( + expect_snapshot( ggplot_build(p + scale_x_discrete(palette = function(x) 1:2)), - "must return at least 3 values" + error = TRUE ) }) diff --git a/tests/testthat/test-scale-manual.R b/tests/testthat/test-scale-manual.R index 2ad47425f7..324485952b 100644 --- a/tests/testthat/test-scale-manual.R +++ b/tests/testthat/test-scale-manual.R @@ -54,8 +54,7 @@ test_that("insufficient values raise an error", { df <- data_frame(x = 1, y = 1:3, z = factor(c(1:2, NA), exclude = NULL)) p <- ggplot(df, aes(x, y, colour = z)) + geom_point() - expect_error(ggplot_build(p + scale_colour_manual(values = "black")), - "Insufficient values") + expect_snapshot(ggplot_build(p + scale_colour_manual(values = "black")), error = TRUE) # Should be sufficient ggplot_build(p + scale_colour_manual(values = c("black", "black"))) @@ -122,7 +121,7 @@ test_that("fewer values (#3451)", { # unnamed character vector s2 <- scale_colour_manual(values = c("4", "8"), na.value = NA) s2$train(c("4", "6", "8")) - expect_error(s2$map(c("4", "6", "8")), "Insufficient values") + expect_snapshot(s2$map(c("4", "6", "8")), error = TRUE) }) test_that("limits and breaks (#4619)", { diff --git a/tests/testthat/test-scales-breaks-labels.R b/tests/testthat/test-scales-breaks-labels.R index c3a314cacc..e15331c1f1 100644 --- a/tests/testthat/test-scales-breaks-labels.R +++ b/tests/testthat/test-scales-breaks-labels.R @@ -7,10 +7,8 @@ test_that("labels match breaks, even when outside limits", { }) test_that("labels match breaks", { - expect_error(scale_x_discrete(breaks = 1:3, labels = 1:2), - "must have the same length") - expect_error(scale_x_continuous(breaks = 1:3, labels = 1:2), - "must have the same length") + expect_snapshot(scale_x_discrete(breaks = 1:3, labels = 1:2), error = TRUE) + expect_snapshot(scale_x_continuous(breaks = 1:3, labels = 1:2), error = TRUE) }) test_that("labels don't have to match null breaks", { @@ -152,20 +150,38 @@ test_that("suppressing breaks, minor_breask, and labels works", { lims <- as.Date(c("2000/1/1", "2000/2/1")) expect_null(scale_x_date(breaks = NULL, limits = lims)$get_breaks()) # NA is defunct, should throw error - expect_error(scale_x_date(breaks = NA, limits = lims)$get_breaks()) + expect_snapshot( + scale_x_date(breaks = NA, limits = lims)$get_breaks(), + error = TRUE + ) expect_null(scale_x_date(labels = NULL, limits = lims)$get_labels()) - expect_error(scale_x_date(labels = NA, limits = lims)$get_labels()) + expect_snapshot( + scale_x_date(labels = NA, limits = lims)$get_labels(), + error = TRUE + ) expect_null(scale_x_date(minor_breaks = NULL, limits = lims)$get_breaks_minor()) - expect_error(scale_x_date(minor_breaks = NA, limits = lims)$get_breaks_minor()) + expect_snapshot( + scale_x_date(minor_breaks = NA, limits = lims)$get_breaks_minor(), + error = TRUE + ) # date, datetime lims <- as.POSIXct(c("2000/1/1 0:0:0", "2010/1/1 0:0:0")) expect_null(scale_x_datetime(breaks = NULL, limits = lims)$get_breaks()) - expect_error(scale_x_datetime(breaks = NA, limits = lims)$get_breaks()) + expect_snapshot( + scale_x_datetime(breaks = NA, limits = lims)$get_breaks(), + error = TRUE + ) expect_null(scale_x_datetime(labels = NULL, limits = lims)$get_labels()) - expect_error(scale_x_datetime(labels = NA, limits = lims)$get_labels()) + expect_snapshot( + scale_x_datetime(labels = NA, limits = lims)$get_labels(), + error = TRUE + ) expect_null(scale_x_datetime(minor_breaks = NULL, limits = lims)$get_breaks_minor()) - expect_error(scale_x_datetime(minor_breaks = NA, limits = lims)$get_breaks_minor()) + expect_snapshot( + scale_x_datetime(minor_breaks = NA, limits = lims)$get_breaks_minor(), + error = TRUE + ) }) test_that("scale_breaks with explicit NA options (deprecated)", { @@ -174,34 +190,34 @@ test_that("scale_breaks with explicit NA options (deprecated)", { # X sxc <- scale_x_continuous(breaks = NA) sxc$train(1:3) - expect_error(sxc$get_breaks()) - expect_error(sxc$get_breaks_minor()) + expect_snapshot(sxc$get_breaks(), error = TRUE) + expect_snapshot(sxc$get_breaks_minor(), error = TRUE) # Y syc <- scale_y_continuous(breaks = NA) syc$train(1:3) - expect_error(syc$get_breaks()) - expect_error(syc$get_breaks_minor()) + expect_snapshot(syc$get_breaks(), error = TRUE) + expect_snapshot(syc$get_breaks_minor(), error = TRUE) # Alpha sac <- scale_alpha_continuous(breaks = NA) sac$train(1:3) - expect_error(sac$get_breaks()) + expect_snapshot(sac$get_breaks(), error = TRUE) # Size ssc <- scale_size_continuous(breaks = NA) ssc$train(1:3) - expect_error(ssc$get_breaks()) + expect_snapshot(ssc$get_breaks(), error = TRUE) # Fill sfc <- scale_fill_continuous(breaks = NA) sfc$train(1:3) - expect_error(sfc$get_breaks()) + expect_snapshot(sfc$get_breaks(), error = TRUE) # Colour scc <- scale_colour_continuous(breaks = NA) scc$train(1:3) - expect_error(scc$get_breaks()) + expect_snapshot(scc$get_breaks(), error = TRUE) }) test_that("breaks can be specified by names of labels", { diff --git a/tests/testthat/test-stat-bin.R b/tests/testthat/test-stat-bin.R index 9b55054604..5baedf9223 100644 --- a/tests/testthat/test-stat-bin.R +++ b/tests/testthat/test-stat-bin.R @@ -194,7 +194,7 @@ test_that("setting boundary and center", { df <- data_frame(x = c(0, 30)) # Error if both boundary and center are specified - expect_error(comp_bin(df, boundary = 5, center = 0), "one of `boundary` and `center`") + expect_snapshot(comp_bin(df, boundary = 5, center = 0), error = TRUE) res <- comp_bin(df, binwidth = 10, boundary = 0, pad = FALSE) expect_identical(res$count, c(1, 0, 1)) @@ -216,7 +216,7 @@ test_that("weights are added", { }) test_that("bin errors at high bin counts", { - expect_error(bin_breaks_width(c(1, 2e6), 1), "The number of histogram bins") + expect_snapshot(bin_breaks_width(c(1, 2e6), 1), error = TRUE) }) # stat_count -------------------------------------------------------------- diff --git a/tests/testthat/test-stat-ecdf.R b/tests/testthat/test-stat-ecdf.R index ce839bb3c4..05fa8bc4b5 100644 --- a/tests/testthat/test-stat-ecdf.R +++ b/tests/testthat/test-stat-ecdf.R @@ -57,10 +57,7 @@ test_that("weighted ecdf warns about weird weights", { ) # Should error when weights sum to 0 - expect_error( - wecdf(1:10, rep(c(-1, 1), 5)), - "Cannot compute eCDF" - ) + expect_snapshot(wecdf(1:10, rep(c(-1, 1), 5)), error = TRUE) }) # See #5113 and #5112 diff --git a/tests/testthat/test-stats.R b/tests/testthat/test-stats.R index 25c30e2fb1..af010a75b9 100644 --- a/tests/testthat/test-stats.R +++ b/tests/testthat/test-stats.R @@ -13,7 +13,7 @@ test_that("plot succeeds even if some computation fails", { test_that("error message is thrown when aesthetics are missing", { p <- ggplot(mtcars) + stat_sum() - expect_error(ggplot_build(p), "x and y\\.$") + expect_snapshot(ggplot_build(p), error = TRUE) }) test_that("erroneously dropped aesthetics are found and issue a warning", { diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 36ad577c65..cf98a1bb3f 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -49,7 +49,7 @@ test_that("modifying theme element properties with + operator works", { t <- theme_grey() + theme() expect_identical(t, theme_grey()) - expect_error(theme_grey() + "asdf") + expect_snapshot(theme_grey() + "asdf", error = TRUE) }) test_that("adding theme object to ggplot object with + operator works", { @@ -115,7 +115,7 @@ test_that("replacing theme elements with %+replace% operator works", { t <- theme_grey() %+replace% theme() expect_identical(t, theme_grey()) - expect_error(theme_grey() + "asdf") + expect_snapshot(theme_grey() + "asdf", error = TRUE) }) test_that("calculating theme element inheritance works", { @@ -368,10 +368,7 @@ test_that("elements can be merged", { merge_element(element_line(colour = "blue"), line_base), element_line(colour = "blue", linewidth = 10) ) - expect_error( - merge_element(text_base, rect_base), - "Only elements of the same class can be merged" - ) + expect_snapshot(merge_element(text_base, rect_base), error = TRUE) }) test_that("theme elements that don't inherit from element can be combined", {