Skip to content

Commit

Permalink
Merge pull request #35 from chainsawriot/fix31
Browse files Browse the repository at this point in the history
Fix #31
  • Loading branch information
schochastics authored Sep 25, 2023
2 parents e956dc5 + 5ae265d commit cd526ec
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 192 deletions.
84 changes: 38 additions & 46 deletions R/has.R
Original file line number Diff line number Diff line change
@@ -1,79 +1,71 @@
#' Check if URL has credentials
.has <- function(url, func) {
if (is.null(url)) {
return(logical(0))
}
func(url)
}

#' Check if URL has a certain component
#'
#' These functions check if URL has a certain component.
#' @inheritParams ada_url_parse
#' @return logical
#' @return logical, `NA` if not a valid URL.
#' @examples
#' ada_has_credentials("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' url <- c("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' ada_has_credentials(url)
#' ada_has_empty_hostname(url)
#' ada_has_hostname(url)
#' ada_has_non_empty_username(url)
#' ada_has_non_empty_password(url)
#' ada_has_port(url)
#' ada_has_hash(url)
#' ada_has_search(url)
#' ## these functions are vectorized
#' urls <- c("http://www.google.com", "http://www.google.com:80", "noturl")
#' ada_has_port(urls)
#' @export
ada_has_credentials <- function(url) {
Rcpp_ada_has_credentials(url)
.has(url, Rcpp_ada_has_credentials)
}

#' Check if URL has an empty hostname
#' @inheritParams ada_url_parse
#' @return logical
#' @examples
#' ada_has_empty_hostname("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @rdname ada_has_credentials
#' @export
ada_has_empty_hostname <- function(url) {
Rcpp_ada_has_empty_hostname(url)
.has(url, Rcpp_ada_has_empty_hostname)
}

#' Check if URL has a hostname
#' @inheritParams ada_url_parse
#' @return logical
#' @examples
#' ada_has_hostname("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @rdname ada_has_credentials
#' @export
ada_has_hostname <- function(url) {
Rcpp_ada_has_hostname(url)
.has(url, Rcpp_ada_has_hostname)
}

#' Check if URL has a non empty username
#' @inheritParams ada_url_parse
#' @return logical
#' @examples
#' ada_has_non_empty_username("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @rdname ada_has_credentials
#' @export
ada_has_non_empty_username <- function(url) {
Rcpp_ada_has_non_empty_username(url)
.has(url, Rcpp_ada_has_non_empty_username)
}

#' Check if URL has a non empty password
#' @inheritParams ada_url_parse
#' @return logical
#' @examples
#' ada_has_non_empty_password("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @rdname ada_has_credentials
#' @export
ada_has_non_empty_password <- function(url) {
Rcpp_ada_has_non_empty_password(url)
.has(url, Rcpp_ada_has_non_empty_password)
}

#' Check if URL has a port
#' @inheritParams ada_url_parse
#' @return logical
#' @examples
#' ada_has_port("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @rdname ada_has_credentials
#' @export
ada_has_port <- function(url) {
Rcpp_ada_has_port(url)
.has(url, Rcpp_ada_has_port)
}

#' Check if URL has a hash
#' @inheritParams ada_url_parse
#' @return logical
#' @examples
#' ada_has_hash("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @rdname ada_has_credentials
#' @export
ada_has_hash <- function(url) {
Rcpp_ada_has_hash(url)
.has(url, Rcpp_ada_has_hash)
}

#' Check if URL has a search
#' @inheritParams ada_url_parse
#' @return logical
#' @examples
#' ada_has_search("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @rdname ada_has_credentials
#' @export
ada_has_search <- function(url) {
Rcpp_ada_has_search(url)
.has(url, Rcpp_ada_has_search)
}
40 changes: 36 additions & 4 deletions man/ada_has_credentials.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions man/ada_has_empty_hostname.Rd

This file was deleted.

20 changes: 0 additions & 20 deletions man/ada_has_hash.Rd

This file was deleted.

20 changes: 0 additions & 20 deletions man/ada_has_hostname.Rd

This file was deleted.

20 changes: 0 additions & 20 deletions man/ada_has_non_empty_password.Rd

This file was deleted.

20 changes: 0 additions & 20 deletions man/ada_has_non_empty_username.Rd

This file was deleted.

20 changes: 0 additions & 20 deletions man/ada_has_port.Rd

This file was deleted.

20 changes: 0 additions & 20 deletions man/ada_has_search.Rd

This file was deleted.

22 changes: 20 additions & 2 deletions tests/testthat/test-has.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@ test_that("all has functions work", {
url <- "https://user_1:[email protected]:8080/dir/../api?q=1#frag"
expect_true(ada_has_credentials(url))
expect_false(ada_has_empty_hostname(url))
expect_true(ada_has_hash(url))
expect_true(ada_has_hostname(url))
expect_true(ada_has_non_empty_password(url))
expect_true(ada_has_non_empty_username(url))
expect_true(ada_has_port(url))
expect_true(ada_has_hash(url))
expect_true(ada_has_search(url))
})

has_functions <- c(ada_has_credentials, ada_has_empty_hostname, ada_has_hostname, ada_has_non_empty_username, ada_has_non_empty_password, ada_has_port, ada_has_hash, ada_has_search)

test_that("invalid urls should return NA, #26", {
url <- "thisisnoturl"
for (func in has_functions) {
expect_error(func(url), NA)
}
})

test_that("corners #31", {
for (func in has_functions) {
expect_error(func(c(NA, NA_character_, "")), NA)
}
for (func in has_functions) {
expect_error(func(NULL), NA)
}
})

test_that("ada_has_credentials is vectorized ref #3", {
expect_error(res <- ada_has_credentials(c("https://admin:[email protected]/basic_auth", "https://www.google.com")), NA)
expect_error(res <- ada_has_credentials(c("https://admin:[email protected]/basic_auth", "https://www.google.com")), NA)
})

0 comments on commit cd526ec

Please sign in to comment.