Skip to content

Commit

Permalink
Fix input tests
Browse files Browse the repository at this point in the history
- Use export on S3methods (required in R > 4, see r-lib/devtools#2293 (comment))
- Use versioned wikipedia urls
- Skip if wikipedia cannot be reached
  • Loading branch information
burgerga committed Mar 4, 2021
1 parent 5f703f0 commit 89acf86
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 5 additions & 0 deletions R/setup_and_checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@
#' @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(...))))

return(Node)
}

#' @export
check_type.HTMLInternalDocument <- function(doc, which, ...) {
Node <- doc
Node <- select_tab(which = which, Node = Node)

return(Node)
}

#' @export
check_type.XMLInternalElementNode <- function(doc, which, ...) {
Node <- doc
Node <- select_tab(which = which, Node = Node)

return(Node)
}

#' @export
check_type.character <- function(doc, which, ...){

isurl <- is_url(doc)
Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test_inputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})

0 comments on commit 89acf86

Please sign in to comment.