Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zkamvar committed Jun 21, 2024
1 parent 9850c74 commit 3a122e3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
15 changes: 11 additions & 4 deletions R/add_md.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ shove_nodes_in <- function(body, new, nodes, where = "after") {
nodes <- xml2::xml_find_all(body, nodes, ns = md_ns())
}
if (!inherits(nodes, c("xml_node", "xml_nodeset"))) {
rlang::abort("an object of class `xml_node` or `xml_nodeset` was expected")
rlang::abort("an object of class `xml_node` or `xml_nodeset` was expected",
class = "insert-md-node"
)
}
root <- xml2::xml_root(nodes)
if (!identical(root, body)) {
rlang::abort("nodes must come from the same body as the yarn document")
rlang::abort("nodes must come from the same body as the yarn document",
class = "insert-md-body"
)
}
return(add_nodes_to_nodes(new, old = nodes, where = where))
}
Expand All @@ -61,11 +65,14 @@ add_nodes_to_nodes <- function(nodes, old, where = "after") {
single_node <- inherits(old, "xml_node")
if (n > 0) {
if (!single_node && n < length(old)) {
rlang::abort("Nodes must be either block type or inline, but not both", call. = FALSE)
rlang::abort("Nodes must be either block type or inline, but not both",
class = "insert-md-dual-type",
call. = FALSE
)
}
nodes <- xml2::xml_children(nodes)
}
if (!single_node) {
if (single_node) {
old <- list(old)
}
purrr::walk(old, add_node_siblings, nodes, where = where, remove = FALSE)
Expand Down
74 changes: 72 additions & 2 deletions tests/testthat/test-class-yarn.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ test_that("a yarn object can be reset", {

})

test_that("random markdown can be added", {
test_that("random markdown can be added to the body", {

tmpdir <- withr::local_tempdir()
scarf3 <- withr::local_file(file.path(tmpdir, "yarn-kilroy.md"))
Expand All @@ -146,7 +146,8 @@ test_that("random markdown can be added", {
"[KILROY](https://en.wikipedia.org/wiki/Kilroy_was_here) WAS **HERE**\n\n",
"stop copying me!" # THIS WILL BE COPIED TWICE
)
t1$add_md(paste(newmd, collapse = ""))$add_md(toupper(newmd[[3]]), where = 3)
t1$add_md(paste(newmd, collapse = ""))
t1$add_md(toupper(newmd[[3]]), where = 3)
expect_length(xml2::xml_find_all(t1$body, "md:link", t1$ns), 0L)

t1$write(scarf3)
Expand All @@ -155,6 +156,75 @@ test_that("random markdown can be added", {
})


test_that("markdown can be appended to elements", {
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
# append a note after the first heading
txt <- c("> Hello from *tinkr*!", ">", "> :heart: R")
# Via XPath ------------------------------------------------------------------
ex$append_md(txt, ".//md:heading[1]")
# the block quote has been added to the first heading
expect_length(xml2::xml_find_all(ex$body, ".//md:block_quote", ns = ex$ns), 1)
# Via node -------------------------------------------------------------------
heading2 <- xml2::xml_find_first(ex$body, ".//md:heading[2]", ns = ex$ns)
ex$append_md(txt, heading2)
expect_length(xml2::xml_find_all(ex$body, ".//md:block_quote", ns = ex$ns), 2)
# Because the body is a copy, the original nodeset will throw an error
expect_error(ex$append_md(txt, heading2), class = "insert-md-body")

# Via nodeset ----------------------------------------------------------------
ex$append_md(txt, ".//md:heading")
expect_length(xml2::xml_find_all(ex$body, ".//md:block_quote", ns = ex$ns), 4)
})


test_that("Inline markdown can be appended (to a degree)", {
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
nodes <- xml2::xml_find_all(ex$body,
".//md:code[contains(text(), 'READ THIS')]", ex$ns)
expect_length(nodes, 0)
ex <- tinkr::yarn$new(path)
ex$append_md("`<-- READ THIS`", ".//md:link")
nodes <- xml2::xml_find_all(ex$body,
".//md:code[contains(text(), 'READ THIS')]", ex$ns)
expect_length(nodes, 1)
})


test_that("markdown can be prepended", {
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
nodes <- xml2::xml_find_all(ex$body,
".//node()[contains(text(), 'NERDS')]", ex$ns)
expect_length(nodes, 0)
ex$prepend_md("Table: BIRDS, NERDS", ".//md:table")
nodes <- xml2::xml_find_all(ex$body,
".//node()[contains(text(), 'NERDS')]", ex$ns)
expect_length(nodes, 1)
})


test_that("an error happens when you try to append with a number", {
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
expect_error(ex$append_md("WRONG", 42), class = "insert-md-node")
})



test_that("an error happens when you try to append markdown to disparate elements", {

path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
xpath <- ".//md:text[contains(text(), 'bird')] | .//md:paragraph[md:text[contains(text(), 'Non')]]"

expect_error(ex$append_md("WRONG", xpath), class = "insert-md-dual-type")
})




test_that("md_vec() will convert a query to a markdown vector", {

pathmd <- system.file("extdata", "example1.md", package = "tinkr")
Expand Down

0 comments on commit 3a122e3

Please sign in to comment.