-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make parse
NULL
aware and add tests (#38)
* Test * more tests * Make parse `NULL` aware * More tests * Update doc
- Loading branch information
1 parent
c0811e1
commit 51cc3e4
Showing
3 changed files
with
21 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", { | ||
|
@@ -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_)) | ||
}) |