Skip to content

Commit

Permalink
use withr for tests & +/- unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdequeljoe committed May 1, 2019
1 parent 88a72a4 commit 66da0fb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 76 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ Version: 0.1.0
Author: person("Matthew", "de Queljoe", email = "[email protected]", role = c("aut", "cre")
Maintainer: Matthew de Queljoe <[email protected]>
Description: Extends `tools::Rd2txt` by adding customisable text and colour formatting to R documentation
contents for terminal or console output. If used in a terminal, output will carried out via `file.show`
otherwise contents will be printed in sections. Formatting is done via crayon<https://cran.r-project.org/web/packages/crayon/index.html>
and prettycode<https://cran.r-project.org/web/packages/prettycode/index.html>.
contents. If used from a terminal, output will be displayed via `file.show` otherwise contents
will be printed in sections. Also provides stand-in replacements for `?` and `help`.
License: GPL-3
Encoding: UTF-8
LazyData: true
Expand All @@ -21,4 +20,5 @@ Imports:
RoxygenNote: 6.1.1
Suggests:
testthat,
mockery
mockery,
withr
40 changes: 26 additions & 14 deletions tests/testthat/test_base.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
context("base R docs")
get_help_file <- getFromNamespace(".getHelpFile", "utils")
options(rdoc.header = FALSE)
options(rdoc.by_section = FALSE)
text_fmt <- rdoc_text_formats(
table = NULL,
href = NULL,
link = NULL,
email = NULL,
url = NULL)
options(rdoc.text_formats = text_fmt)

get_rdo <- function(topic, pkg){
d <- as.call(list(utils::`help`, topic, pkg))
d <- eval(d)[1]
Expand All @@ -19,7 +9,12 @@ get_rdo <- function(topic, pkg){
check_original <- function(topic, pkg = NULL) {
o <- get_rdo(topic, pkg)
w <- getOption('width')
capture.output(tools::Rd2txt(o, options = list(width = w)))
capture.output(
tools::Rd2txt(
o,
options = list(width = w, itemBullet = ">")
)
)
}

strip_lines <- function(x) {
Expand Down Expand Up @@ -75,7 +70,24 @@ test_pkg <- function(pkg, partial = FALSE, n = 100L, exclude = NULL) {
}

test_that("base package docs output without error or warning", {
test_pkg("utils", TRUE, 30)
test_pkg("base", TRUE, 30, exclude = c("intToUtf8", "utf8ToInt"))
test_pkg("stats", TRUE, 30)
text_fmt <- rdoc_text_formats(
table = NULL,
href = NULL,
link = NULL,
email = NULL,
url = NULL
)
withr::with_options(
list(
rdoc.header = FALSE,
rdoc.by_section = FALSE,
rdoc.text_formats = text_fmt,
rdoc.item_bullet = ">"
),
{
test_pkg("utils", TRUE, 30)
test_pkg("base", TRUE, 30, exclude = c("intToUtf8", "utf8ToInt"))
test_pkg("stats", TRUE, 30)
}
)
})
112 changes: 61 additions & 51 deletions tests/testthat/test_rd.R
Original file line number Diff line number Diff line change
@@ -1,71 +1,81 @@
context("rd")
options(rdoc.by_section = FALSE)
options(rdoc.header = TRUE)
cap_ <- capture.output
test_ <- function(x){
expect_true(length(x) > 0L)
expect_true(is.character(x))
expect_true(length(x) > 0L && is.character(x))
expect_true(grepl("min", x[1]))
expect_true(grepl("\\{base\\}", x[1]))
}
opts <- list(
rdoc.by_section = FALSE,
rdoc.header = TRUE
)

test_that("rd produces text output", {

expect_true(is.function(rd_()))
x <- cap_(rdoc("min"))
test_(x)

xd <- cap_(rdoc_details("min"))
test_(xd)
expect_true(length(xd) < length(x))

xe <- cap_(rdoc_examples("min"))
test_(xe)
expect_true(length(xe) < length(x))

x <- cap_(rdoc(min))
test_(x)

fn <- "min"
x <- cap_(rdoc(fn))
test_(x)

pkg <- "base"
x <- cap_(rdoc("min", package = pkg))
test_(x)

x <- cap_(rdoc_details(min, package = base))
test_(x)

x <- cap_(rdoc(min, package = "base"))
test_(x)

expect_true(inherits(rdoc("function_is_not_found"), "help_files_with_topic"))
withr::with_options(
opts,
{
expect_true(is.function(rd_()))
x <- cap_(rdoc("min"))
test_(x)

xd <- cap_(rdoc_details("min"))
test_(xd)
expect_true(length(xd) < length(x))

xe <- cap_(rdoc_examples("min"))
test_(xe)
expect_true(length(xe) < length(x))

x <- cap_(rdoc(min))
test_(x)

fn <- "min"
x <- cap_(rdoc(fn))
test_(x)

pkg <- "base"
x <- cap_(rdoc("min", package = pkg))
test_(x)

x <- cap_(rdoc_details(min, package = base))
test_(x)

x <- cap_(rdoc(min, package = "base"))
test_(x)

x <- cap_(rdoc(rdoc_options))
x_ <- cap_(rdoc("rdoc_options"))
expect_equal(x, x_)
expect_true(inherits(rdoc("function_is_not_found"), "help_files_with_topic"))
}
)
})

test_that("rd_question produces text output", {
withr::with_options(opts, {
x <- cap_(rdoc_question(min))
test_(x)

x <- cap_(rdoc_question(min))
test_(x)

x <- cap_(rdoc_question(base::min))
test_(x)

x <- cap_(rdoc_question("min"))
test_(x)
x <- cap_(rdoc_question(base::min))
test_(x)

expect_true(inherits(
rdoc_question("function_is_not_found"),
"help_files_with_topic"
))
x <- cap_(rdoc_question("min"))
test_(x)

expect_true(inherits(
rdoc_question("function_is_not_found"),
"help_files_with_topic"
))
})
})

test_that("Rd files read", {
f <- system.file("extdata/rdoc_test.Rd", package = "rdoc")
x <- cap_(rdoc_rd(f))
expect_true(length(x) > 0L)
expect_true(is.character(x))
withr::with_options(opts, {
f <- system.file("extdata/rdoc_test.Rd", package = "rdoc")
x <- cap_(rdoc_rd(f))
expect_true(length(x) > 0L)
expect_true(is.character(x))
})
})

test_that("rdoc overrides ?", {
Expand Down
7 changes: 0 additions & 7 deletions tests/testthat/test_reflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ test_that("line reflow works as expected", {
xr <- reflow_lines(x, exclude = "4")
expect_equal(xr, c("1 2 3 9", "1 2 3", "4 5"))

x <- c("a b c d",
crayon::red("a b"),
"c d")

x2 <- reflow_lines(x)
expect_equal(x2[2], paste(x[2], x[3]))

x <- c("a b c d",
crayon::red("a b"),
"c d e")
Expand Down

0 comments on commit 66da0fb

Please sign in to comment.