From 03f1fa176dae9b56d067d36912cff3f13de5d593 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 19 Mar 2023 20:27:30 +0100 Subject: [PATCH] Better message in facetted_pos_scales #91 --- R/facetted_pos_scales.R | 15 ++++++++++++++- tests/testthat/_snaps/facetted_pos_scales.md | 8 ++++++++ tests/testthat/test-facetted_pos_scales.R | 14 ++++++-------- 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 tests/testthat/_snaps/facetted_pos_scales.md diff --git a/R/facetted_pos_scales.R b/R/facetted_pos_scales.R index 34d8042..c0c8bd5 100644 --- a/R/facetted_pos_scales.R +++ b/R/facetted_pos_scales.R @@ -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") diff --git a/tests/testthat/_snaps/facetted_pos_scales.md b/tests/testthat/_snaps/facetted_pos_scales.md new file mode 100644 index 0000000..0a70edb --- /dev/null +++ b/tests/testthat/_snaps/facetted_pos_scales.md @@ -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. + diff --git a/tests/testthat/test-facetted_pos_scales.R b/tests/testthat/test-facetted_pos_scales.R index bdf3570..70449f2 100644 --- a/tests/testthat/test-facetted_pos_scales.R +++ b/tests/testthat/test-facetted_pos_scales.R @@ -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", {