Skip to content

Commit

Permalink
test errors for pal addition and removal
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Oct 10, 2024
1 parent d466e8c commit cd09117
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")
}
Expand All @@ -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))
}
Expand All @@ -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]
}
Expand All @@ -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(
Expand Down
48 changes: 48 additions & 0 deletions tests/testthat/_snaps/pal-add-remove.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

34 changes: 34 additions & 0 deletions tests/testthat/test-pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
})

0 comments on commit cd09117

Please sign in to comment.