Skip to content

Commit

Permalink
Make parse NULL aware and add tests (#38)
Browse files Browse the repository at this point in the history
* Test

* more tests

* Make parse `NULL` aware

* More tests

* Update doc
  • Loading branch information
chainsawriot authored Sep 25, 2023
1 parent c0811e1 commit 51cc3e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#' Use ada-url to parse a url
#' @param url character. one or more URL to be parsed
#' @param decode logical. Whether to decode the output (see [utils::URLdecode()]), default to `TRUE`
#' @return A list of components of the url
#' @return A data frame of components of the url
#' @examples
#' ada_url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#' @export
ada_url_parse <- function(url, decode = TRUE) {
if (is.null(url)) {
return(structure(list(href = character(0), protocol = character(0),
username = character(0), password = character(0), host = character(0),
hostname = character(0), port = character(0), pathname = character(0),
search = character(0), hash = character(0)), row.names = integer(0), class = "data.frame"))
}
url_parsed <- Rcpp_ada_parse(url)
if (isTRUE(decode)) {
return(.decoder(url_parsed))
Expand Down
2 changes: 1 addition & 1 deletion man/ada_url_parse.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ test_that("works with utf8", {
expect_equal(res[["pathname"]], "/zone/1/\u6e2f\u805e")
res <- ada_url_parse("http://www.m\u00fcller.de")
expect_equal(res[["host"]], "www.m\u00fcller.de")
res <- ada_url_parse("https://\u4e2d\u56fd\u79fb\u52a8.\u4e2d\u56fd")
expect_equal(res$href[1], "https://xn--fiq02ib9d179b.xn--fiqs8s/")
expect_equal(res$host[1], "\u4e2d\u56fd\u79fb\u52a8.\u4e2d\u56fd")
})

test_that("URLdecode optional #5", {
Expand All @@ -32,3 +35,13 @@ test_that("multiple urls", {
urls <- rep("https://user_1:[email protected]:8080/dir/../api?q=1#frag", 5)
expect_equal(nrow(ada_url_parse(urls)), 5L)
})

test_that("corner cases", {
expect_error(ada_url_parse())
expect_error(ada_url_parse(NULL), NA)
corners <- c(NA, NULL, "", "youcantparsethis", 1)
testthat::expect_error(x <- ada_url_parse(corners), NA)
testthat::expect_error(y <- ada_url_parse(corners, decode = FALSE), NA)
expect_equal(x$host, c(NA_character_, NA_character_, NA_character_, NA_character_))
expect_equal(y$host, c(NA_character_, NA_character_, NA_character_, NA_character_))
})

0 comments on commit 51cc3e4

Please sign in to comment.