diff --git a/NAMESPACE b/NAMESPACE index 84e470b..181b39b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,10 @@ # Generated by roxygen2: do not edit by hand +S3method(check_type,HTMLInternalDocument) +S3method(check_type,XMLInternalElementNode) +S3method(check_type,XMLNodeSet) +S3method(check_type,character) +S3method(check_type,default) S3method(num_xpath,character) S3method(num_xpath,default) S3method(num_xpath,list) diff --git a/R/setup_and_checks.R b/R/setup_and_checks.R index 1723abf..929f625 100644 --- a/R/setup_and_checks.R +++ b/R/setup_and_checks.R @@ -8,10 +8,12 @@ #' @return a table node check_type <- function(doc, which, ...) UseMethod("check_type") +#' @export check_type.default <- function(doc, which, ...){ stop("doc is of unknown type", call. = FALSE) } +#' @export check_type.XMLNodeSet <- function(doc, which, ...){ Node <- eval.parent(substitute(XML::xmlParse(XML::saveXML(doc[[1]]), list(...)))) @@ -19,6 +21,7 @@ check_type.XMLNodeSet <- function(doc, which, ...){ return(Node) } +#' @export check_type.HTMLInternalDocument <- function(doc, which, ...) { Node <- doc Node <- select_tab(which = which, Node = Node) @@ -26,6 +29,7 @@ check_type.HTMLInternalDocument <- function(doc, which, ...) { return(Node) } +#' @export check_type.XMLInternalElementNode <- function(doc, which, ...) { Node <- doc Node <- select_tab(which = which, Node = Node) @@ -33,6 +37,7 @@ check_type.XMLInternalElementNode <- function(doc, which, ...) { return(Node) } +#' @export check_type.character <- function(doc, which, ...){ isurl <- is_url(doc) diff --git a/tests/testthat/test_inputs.R b/tests/testthat/test_inputs.R index 7f14f4c..2d22ac9 100644 --- a/tests/testthat/test_inputs.R +++ b/tests/testthat/test_inputs.R @@ -6,12 +6,16 @@ test_that("Prompts errors correctly", { test_that("check_type produces class output", { - x <- check_type(doc = "http://christianrubba.com/cran/htmltab/ex/wiki_indian_election2014.html", which = "//table[5]") + skip_if_offline(host = "wikipedia.org") + url <- "https://en.wikipedia.org/w/index.php?title=2014_Indian_general_election&oldid=1007662542" + + x <- check_type(doc = url, which = "//table[5]") expect_that(x, is_a("XMLInternalDocument")) - #expect_error(check_type(doc = "http://christianrubba.com/htmltab/ex/wiki_indian_election2014.html", which = 1)) + #expect_error(check_type(doc = url, which = 1)) - parsed <- XML::htmlParse("http://christianrubba.com/cran/htmltab/ex/wiki_indian_election2014.html") + con <- url(url, method = "libcurl") + parsed <- suppressWarnings(XML::htmlParse(readLines(con))) z <- check_type(doc = parsed, which = 3) expect_that(z, is_a("XMLInternalDocument")) })