From 8d76d7b5feed56a9b674f48a1fe3b5124d1daa1b Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 10:11:53 +0200 Subject: [PATCH 1/5] Fix #31 --- R/has.R | 23 +++++++++++++++-------- tests/testthat/test-has.R | 22 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/R/has.R b/R/has.R index 1f44b49..5325547 100644 --- a/R/has.R +++ b/R/has.R @@ -1,3 +1,10 @@ +.has <- function(url, func) { + if (is.null(url)) { + return(logical(0)) + } + func(utf8::as_utf8(url)) +} + #' Check if URL has credentials #' @inheritParams ada_url_parse #' @return logical @@ -5,7 +12,7 @@ #' ada_has_credentials("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_credentials <- function(url) { - Rcpp_ada_has_credentials(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_credentials) } #' Check if URL has an empty hostname @@ -15,7 +22,7 @@ ada_has_credentials <- function(url) { #' ada_has_empty_hostname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_empty_hostname <- function(url) { - Rcpp_ada_has_empty_hostname(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_empty_hostname) } #' Check if URL has a hostname @@ -25,7 +32,7 @@ ada_has_empty_hostname <- function(url) { #' ada_has_hostname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_hostname <- function(url) { - Rcpp_ada_has_hostname(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_hostname) } #' Check if URL has a non empty username @@ -35,7 +42,7 @@ ada_has_hostname <- function(url) { #' ada_has_non_empty_username("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_non_empty_username <- function(url) { - Rcpp_ada_has_non_empty_username(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_non_empty_username) } #' Check if URL has a non empty password @@ -45,7 +52,7 @@ ada_has_non_empty_username <- function(url) { #' ada_has_non_empty_password("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_non_empty_password <- function(url) { - Rcpp_ada_has_non_empty_password(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_non_empty_password) } #' Check if URL has a port @@ -55,7 +62,7 @@ ada_has_non_empty_password <- function(url) { #' ada_has_port("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_port <- function(url) { - Rcpp_ada_has_port(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_port) } #' Check if URL has a hash @@ -65,7 +72,7 @@ ada_has_port <- function(url) { #' ada_has_hash("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_hash <- function(url) { - Rcpp_ada_has_hash(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_hash) } #' Check if URL has a search @@ -75,5 +82,5 @@ ada_has_hash <- function(url) { #' ada_has_search("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_has_search <- function(url) { - Rcpp_ada_has_search(utf8::as_utf8(url)) + .has(url, Rcpp_ada_has_search) } diff --git a/tests/testthat/test-has.R b/tests/testthat/test-has.R index d4a0a82..d9341a0 100644 --- a/tests/testthat/test-has.R +++ b/tests/testthat/test-has.R @@ -2,14 +2,32 @@ test_that("all has functions work", { url <- "https://user_1:password_1@example.org: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_non_empty_password, ada_has_non_empty_username, 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", { + 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:admin@the-internet.herokuapp.com/basic_auth", "https://www.google.com")), NA) + expect_error(res <- ada_has_credentials(c("https://admin:admin@the-internet.herokuapp.com/basic_auth", "https://www.google.com")), NA) }) From 3bcce4be31c94ca1e064b0d7515f990a95681739 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 10:14:36 +0200 Subject: [PATCH 2/5] Add tests --- tests/testthat/test-has.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-has.R b/tests/testthat/test-has.R index d9341a0..68fae8f 100644 --- a/tests/testthat/test-has.R +++ b/tests/testthat/test-has.R @@ -19,7 +19,7 @@ test_that("invalid urls should return NA, #26", { } }) -test_that("corners", { +test_that("corners #31", { for (func in has_functions) { expect_error(func(c(NA, NA_character_, "")), NA) } From 7f0f442cfb5d3f76790718eac6e1d33605a56e79 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 10:24:27 +0200 Subject: [PATCH 3/5] Consolidate the documentation ref #28 --- R/has.R | 63 ++++++++++++------------------- man/ada_has_credentials.Rd | 42 +++++++++++++++++++-- man/ada_has_empty_hostname.Rd | 20 ---------- man/ada_has_hash.Rd | 20 ---------- man/ada_has_hostname.Rd | 20 ---------- man/ada_has_non_empty_password.Rd | 20 ---------- man/ada_has_non_empty_username.Rd | 20 ---------- man/ada_has_port.Rd | 20 ---------- man/ada_has_search.Rd | 20 ---------- 9 files changed, 63 insertions(+), 182 deletions(-) delete mode 100644 man/ada_has_empty_hostname.Rd delete mode 100644 man/ada_has_hash.Rd delete mode 100644 man/ada_has_hostname.Rd delete mode 100644 man/ada_has_non_empty_password.Rd delete mode 100644 man/ada_has_non_empty_username.Rd delete mode 100644 man/ada_has_port.Rd delete mode 100644 man/ada_has_search.Rd diff --git a/R/has.R b/R/has.R index 5325547..596cf35 100644 --- a/R/has.R +++ b/R/has.R @@ -5,81 +5,68 @@ func(utf8::as_utf8(url)) } -#' Check if URL has credentials +#' 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:password_1@example.org:8080/dir/../api?q=1#frag") +#' url <- c("https://user_1:password_1@example.org: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_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) { .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:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_has_credentials #' @export ada_has_empty_hostname <- function(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:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_has_credentials #' @export ada_has_hostname <- function(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:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_has_credentials #' @export ada_has_non_empty_username <- function(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:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_has_credentials #' @export ada_has_non_empty_password <- function(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:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_has_credentials #' @export ada_has_port <- function(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:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_has_credentials #' @export ada_has_hash <- function(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:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_has_credentials #' @export ada_has_search <- function(url) { .has(url, Rcpp_ada_has_search) diff --git a/man/ada_has_credentials.Rd b/man/ada_has_credentials.Rd index 4f3bad1..b13cc8a 100644 --- a/man/ada_has_credentials.Rd +++ b/man/ada_has_credentials.Rd @@ -2,19 +2,53 @@ % Please edit documentation in R/has.R \name{ada_has_credentials} \alias{ada_has_credentials} -\title{Check if URL has credentials} +\alias{ada_has_empty_hostname} +\alias{ada_has_hostname} +\alias{ada_has_non_empty_username} +\alias{ada_has_non_empty_password} +\alias{ada_has_port} +\alias{ada_has_hash} +\alias{ada_has_search} +\title{Check if URL has a certain component} \usage{ 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) } \arguments{ \item{url}{character. one or more URL to be parsed} } \value{ -logical +logical, \code{NA} if not a valid URL. } \description{ -Check if URL has credentials +These functions check if URL has a certain component. } \examples{ -ada_has_credentials("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +url <- c("https://user_1:password_1@example.org: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_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) } diff --git a/man/ada_has_empty_hostname.Rd b/man/ada_has_empty_hostname.Rd deleted file mode 100644 index d687961..0000000 --- a/man/ada_has_empty_hostname.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/has.R -\name{ada_has_empty_hostname} -\alias{ada_has_empty_hostname} -\title{Check if URL has an empty hostname} -\usage{ -ada_has_empty_hostname(url) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} -} -\value{ -logical -} -\description{ -Check if URL has an empty hostname -} -\examples{ -ada_has_empty_hostname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_has_hash.Rd b/man/ada_has_hash.Rd deleted file mode 100644 index 9cbe36b..0000000 --- a/man/ada_has_hash.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/has.R -\name{ada_has_hash} -\alias{ada_has_hash} -\title{Check if URL has a hash} -\usage{ -ada_has_hash(url) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} -} -\value{ -logical -} -\description{ -Check if URL has a hash -} -\examples{ -ada_has_hash("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_has_hostname.Rd b/man/ada_has_hostname.Rd deleted file mode 100644 index 6f1a8bc..0000000 --- a/man/ada_has_hostname.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/has.R -\name{ada_has_hostname} -\alias{ada_has_hostname} -\title{Check if URL has a hostname} -\usage{ -ada_has_hostname(url) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} -} -\value{ -logical -} -\description{ -Check if URL has a hostname -} -\examples{ -ada_has_hostname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_has_non_empty_password.Rd b/man/ada_has_non_empty_password.Rd deleted file mode 100644 index 5ee0fce..0000000 --- a/man/ada_has_non_empty_password.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/has.R -\name{ada_has_non_empty_password} -\alias{ada_has_non_empty_password} -\title{Check if URL has a non empty password} -\usage{ -ada_has_non_empty_password(url) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} -} -\value{ -logical -} -\description{ -Check if URL has a non empty password -} -\examples{ -ada_has_non_empty_password("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_has_non_empty_username.Rd b/man/ada_has_non_empty_username.Rd deleted file mode 100644 index 3cd2224..0000000 --- a/man/ada_has_non_empty_username.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/has.R -\name{ada_has_non_empty_username} -\alias{ada_has_non_empty_username} -\title{Check if URL has a non empty username} -\usage{ -ada_has_non_empty_username(url) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} -} -\value{ -logical -} -\description{ -Check if URL has a non empty username -} -\examples{ -ada_has_non_empty_username("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_has_port.Rd b/man/ada_has_port.Rd deleted file mode 100644 index 994da5e..0000000 --- a/man/ada_has_port.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/has.R -\name{ada_has_port} -\alias{ada_has_port} -\title{Check if URL has a port} -\usage{ -ada_has_port(url) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} -} -\value{ -logical -} -\description{ -Check if URL has a port -} -\examples{ -ada_has_port("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_has_search.Rd b/man/ada_has_search.Rd deleted file mode 100644 index 09b854c..0000000 --- a/man/ada_has_search.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/has.R -\name{ada_has_search} -\alias{ada_has_search} -\title{Check if URL has a search} -\usage{ -ada_has_search(url) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} -} -\value{ -logical -} -\description{ -Check if URL has a search -} -\examples{ -ada_has_search("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} From 9d97b1616b0af97eb7115424d03eaf043daab7d1 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 10:29:28 +0200 Subject: [PATCH 4/5] Anticipate #34 [no ci] --- R/has.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/has.R b/R/has.R index 596cf35..e8597ae 100644 --- a/R/has.R +++ b/R/has.R @@ -2,7 +2,7 @@ if (is.null(url)) { return(logical(0)) } - func(utf8::as_utf8(url)) + func(url) } #' Check if URL has a certain component From 9939385ca7af028a5cdfbfa59258185d661cdb9e Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 10:39:43 +0200 Subject: [PATCH 5/5] Correct tests and docs [no ci] --- R/has.R | 2 -- man/ada_has_credentials.Rd | 2 -- tests/testthat/test-has.R | 2 +- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/R/has.R b/R/has.R index e8597ae..fcef008 100644 --- a/R/has.R +++ b/R/has.R @@ -16,8 +16,6 @@ #' ada_has_empty_hostname(url) #' ada_has_hostname(url) #' ada_has_non_empty_username(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) diff --git a/man/ada_has_credentials.Rd b/man/ada_has_credentials.Rd index b13cc8a..8882142 100644 --- a/man/ada_has_credentials.Rd +++ b/man/ada_has_credentials.Rd @@ -42,8 +42,6 @@ ada_has_credentials(url) ada_has_empty_hostname(url) ada_has_hostname(url) ada_has_non_empty_username(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) diff --git a/tests/testthat/test-has.R b/tests/testthat/test-has.R index 68fae8f..8a85667 100644 --- a/tests/testthat/test-has.R +++ b/tests/testthat/test-has.R @@ -10,7 +10,7 @@ test_that("all has functions work", { expect_true(ada_has_search(url)) }) -has_functions <- c(ada_has_credentials, ada_has_empty_hostname, ada_has_non_empty_password, ada_has_non_empty_username, ada_has_port, ada_has_hash, ada_has_search) +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"