Skip to content

Commit

Permalink
Better message in facetted_pos_scales #91
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Mar 19, 2023
1 parent 84044c2 commit 03f1fa1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
15 changes: 14 additions & 1 deletion R/facetted_pos_scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,20 @@ facetted_pos_scales <- function(x = NULL, y = NULL) {
x_test <- check_facetted_scale(x, "x")
y_test <- check_facetted_scale(y, "y")
if (!(x_test & y_test)) {
stop("Invalid facetted scale specifications.")
if (!x_test & !y_test) {
arg <- "The {.arg x} and {.arg y} arguments "
type <- "appropriate"
} else if (!x_test) {
arg <- "The {.arg x} argument "
type <- "{.field x}"
} else {
arg <- "The {.arg y} argument "
type <- "{.field y}"
}
cli::cli_abort(paste0(
arg, "should be {.val NULL}, or a list of formulas and/or ",
"position scales with the ", type, " aesthetic."
))
}

x <- validate_facetted_scale(x, "x")
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/facetted_pos_scales.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# facetted_pos_scales warns about invalid scales

The `x` argument should be "NULL", or a list of formulas and/or position scales with the x aesthetic.

---

The `y` argument should be "NULL", or a list of formulas and/or position scales with the y aesthetic.

14 changes: 6 additions & 8 deletions tests/testthat/test-facetted_pos_scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,14 @@ test_that("facetted_pos_scales can handle date scales", {
test_that("facetted_pos_scales warns about invalid scales", {

# Nonsensical input
test <- substitute(facetted_pos_scales(y = list(
NULL, scale_y_continuous(), "nonsense")
))
expect_error(eval(test), "Invalid facetted scale")
expect_snapshot_error(
facetted_pos_scales(x = list(NULL, scale_x_continuous(), "nonsense"))
)

# Incompatible aesthetics (x-scale to y-argument)
test <- substitute(facetted_pos_scales(y = list(
NULL, scale_x_continuous()
)))
expect_error(eval(test), "Invalid facetted scale")
expect_snapshot_error(
facetted_pos_scales(y = list(NULL, scale_x_continuous()))
)
})

test_that("facetted_pos_scales warns about invalid scales in formulas", {
Expand Down

0 comments on commit 03f1fa1

Please sign in to comment.