Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
refactor: factor out assert_character() to test it once
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Oct 8, 2024
1 parent 6473e34 commit c05da65
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
^LICENSE\.md$
^\.github$
^README\.Rmd$
^man/universe_query\.Rd$
^_pkgdown\.yml$
^docs$
^pkgdown$
^man/universe_query\.Rd$
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Depends:
R (>= 4.2.0)
Imports:
cli,
httr2
httr2,
rlang
URL: https://docs.ropensci.org/starchart/, https://github.com/ropenscilabs/starchart
BugReports: https://github.com/ropenscilabs/starchart/issues
Config/Needs/build: moodymudskipper/devtag
Expand Down
2 changes: 2 additions & 0 deletions R/global-search.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' global_search(query = '"weather data"', limit = 1)
#' global_search(query = 'needs:httr2', limit = 1)
global_search <- function(query, limit = 100) {
assert_character(query, "query")

httr2::request("https://r-universe.dev") |>
httr2::req_url_path("api") |>
httr2::req_url_path_append("search") |>
Expand Down
21 changes: 6 additions & 15 deletions R/universe.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
#' universe_ls("ropensci")
#' @family universe
universe_ls <- function(universe) {
if (!is.character(universe) || length(universe) != 1) {
cli::cli_abort("{.arg universe} must be a character of length 1.")
}
assert_character(universe, "universe")
# TODO assert that universe is an universe

universe_query(
Expand All @@ -35,9 +33,7 @@ universe_ls <- function(universe) {
#' universe_packages("ropensci")
#' @family universe
universe_packages <- function(universe, limit = 100) {
if (!is.character(universe) || length(universe) != 1) {
cli::cli_abort("{.arg universe} must be a character of length 1.")
}
assert_character(universe, "universe")
# TODO assert that universe is an universe

universe_query(
Expand All @@ -60,9 +56,8 @@ universe_packages <- function(universe, limit = 100) {
#' universe_package("jeroen", package = "curl")
#' @family universe
universe_package <- function(universe, package) {
if (!is.character(universe) || length(universe) != 1) {
cli::cli_abort("{.arg universe} must be a character of length 1.")
}
assert_character(universe, "universe")
assert_character(package, "package")
# TODO assert that universe is an universe

if (!package %in% universe_ls(universe)) {
Expand Down Expand Up @@ -90,12 +85,8 @@ universe_package <- function(universe, package) {
#' universe_search("ropensci", query = 'needs:httr2')
#' @family universe
universe_search <- function(universe, query, limit) {
if (!is.character(universe) || length(universe) != 1) {
cli::cli_abort("{.arg universe} must be a character of length 1.")
}
if (!is.character(query) || length(query) != 1) {
cli::cli_abort("{.arg query} must be a character of length 1.")
}
assert_character(universe, "universe")
assert_character(query, "query")
# TODO assert that universe is an universe

universe_query(
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ universe_query <- function(universe_url, path, query_params = NULL) {
httr2::req_perform(request) |>
httr2::resp_body_json()
}

assert_character <- function(x, name, call = rlang::caller_env()) {
if (!is.character(x) || length(x) != 1) {
cli::cli_abort("{.arg name} must be a character of length 1.", call = call)
}
}
13 changes: 13 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# assert_character() works

Code
assert_character(1, "universe")
Condition
Error:
! `name` must be a character of length 1.
Code
assert_character(c("bla", "blop"), "universe")
Condition
Error:
! `name` must be a character of length 1.

6 changes: 6 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("assert_character() works", {
expect_snapshot(error = TRUE, {
assert_character(1, "universe")
assert_character(c("bla", "blop"), "universe")
})
})

0 comments on commit c05da65

Please sign in to comment.