From 51cc3e4aa449a0e9e99ad43d82e874c1c0cd073f Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 12:46:10 +0200 Subject: [PATCH] Make parse `NULL` aware and add tests (#38) * Test * more tests * Make parse `NULL` aware * More tests * Update doc --- R/parse.R | 8 +++++++- man/ada_url_parse.Rd | 2 +- tests/testthat/test-parse.R | 13 +++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/R/parse.R b/R/parse.R index b373d3e..ab228b0 100644 --- a/R/parse.R +++ b/R/parse.R @@ -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:password_1@example.org: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)) diff --git a/man/ada_url_parse.Rd b/man/ada_url_parse.Rd index cab7156..3006cff 100644 --- a/man/ada_url_parse.Rd +++ b/man/ada_url_parse.Rd @@ -12,7 +12,7 @@ ada_url_parse(url, decode = TRUE) \item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} } \value{ -A list of components of the url +A data frame of components of the url } \description{ Use ada-url to parse a url diff --git a/tests/testthat/test-parse.R b/tests/testthat/test-parse.R index a324b7a..db22d53 100644 --- a/tests/testthat/test-parse.R +++ b/tests/testthat/test-parse.R @@ -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:password_1@example.org: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_)) +})