diff --git a/R/pal-add-remove.R b/R/pal-add-remove.R index f52393d..9b6ccd6 100644 --- a/R/pal-add-remove.R +++ b/R/pal-add-remove.R @@ -46,6 +46,7 @@ pal_add <- function( #' @rdname pal_add_remove pal_remove <- function(role) { + check_string(role) if (!role %in% list_pals()) { cli::cli_abort("No active pal with the given {.arg role}.") } @@ -55,7 +56,6 @@ pal_remove <- function(role) { c(paste0("system_prompt_", role), paste0("rs_pal_", role)) ) - if (paste0(".last_pal_", role) %in% names(pal_env())) { env_unbind(pal_env(), paste0(".last_pal_", role)) } @@ -67,7 +67,7 @@ supported_interfaces <- c("replace", "prefix", "suffix") # given an interface and role, attaches a function binding in pal's # additional search env -parse_interface <- function(interface, role) { +parse_interface <- function(interface, role, call = caller_env()) { if (isTRUE(identical(interface, supported_interfaces))) { interface <- interface[1] } @@ -76,13 +76,14 @@ parse_interface <- function(interface, role) { !interface %in% supported_interfaces )) { cli::cli_abort( - "{.arg interface} should be one of {.or {.val {supported_interfaces}}}." + "{.arg interface} should be one of {.or {.val {supported_interfaces}}}.", + call = call ) } if (interface == "suffix") { # TODO: implement suffixing - cli::cli_abort("Suffixing not implemented yet.") + cli::cli_abort("Suffixing not implemented yet.", call = call) } .stash_binding( diff --git a/tests/testthat/_snaps/pal-add-remove.md b/tests/testthat/_snaps/pal-add-remove.md index e2da737..34356c3 100644 --- a/tests/testthat/_snaps/pal-add-remove.md +++ b/tests/testthat/_snaps/pal-add-remove.md @@ -6,3 +6,51 @@ -- A boopery pal using claude-3-5-sonnet-20240620. +# pal addition with bad inputs + + Code + pal_add(role = identity, prompt = "hey") + Condition + Error in `pal_add()`: + ! `role` must be a single string, not a function. + +--- + + Code + pal_add(role = "sillyhead", prompt = "hey", interface = "no") + Condition + Error in `pal_add()`: + ! `interface` should be one of "replace", "prefix", or "suffix". + +--- + + Code + pal_add(role = "sillyhead", prompt = "hey", interface = "suffix") + Condition + Error in `pal_add()`: + ! Suffixing not implemented yet. + +--- + + Code + pal_add(role = "sillyhead", prompt = "hey", interface = NULL) + Condition + Error in `pal_add()`: + ! `interface` should be one of "replace", "prefix", or "suffix". + +# pal remove with bad inputs + + Code + pal_remove(role = identity) + Condition + Error in `pal_remove()`: + ! `role` must be a single string, not a function. + +--- + + Code + pal_remove(role = "notAnActivePal") + Condition + Error in `pal_remove()`: + ! No active pal with the given `role`. + diff --git a/tests/testthat/test-pal-add-remove.R b/tests/testthat/test-pal-add-remove.R index fa52d79..e9c3d01 100644 --- a/tests/testthat/test-pal-add-remove.R +++ b/tests/testthat/test-pal-add-remove.R @@ -24,3 +24,37 @@ test_that("pal addition and removal works", { expect_false("system_prompt_boopery" %in% names(pal_env())) expect_false("rs_pal_boopery" %in% names(pal_env())) }) + +test_that("pal addition with bad inputs", { + expect_snapshot( + error = TRUE, + pal_add(role = identity, prompt = "hey") + ) + + # TODO: decide on `prompt` interface and test + + + expect_snapshot( + error = TRUE, + pal_add(role = "sillyhead", prompt = "hey", interface = "no") + ) + expect_snapshot( + error = TRUE, + pal_add(role = "sillyhead", prompt = "hey", interface = "suffix") + ) + expect_snapshot( + error = TRUE, + pal_add(role = "sillyhead", prompt = "hey", interface = NULL) + ) +}) + +test_that("pal remove with bad inputs", { + expect_snapshot( + error = TRUE, + pal_remove(role = identity) + ) + expect_snapshot( + error = TRUE, + pal_remove(role = "notAnActivePal") + ) +})