Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jan 19, 2024
1 parent 7ccc449 commit 2f82c7b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
16 changes: 2 additions & 14 deletions tests/testthat/_snaps/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,12 @@
Message
x <text>:2: @evalNamespace must evaluate to a character vector.

# Invalid imports throw a helpful error
# invalid imports generate helpful message

Code
out <- roc_proc_text(namespace_roclet(), block)
Message
x <text>:2: @importFrom Excluding unknown export from utils: `InvalidUtilsFunction`.

---

Code
out <- roc_proc_text(namespace_roclet(), block)
Message
x <text>:2: @importFrom Excluding unknown export from utils: `InvalidUtilsFunction`.
x <text>:2: @importFrom Excluding unknown export from utils: `InvalidUtilsFunction1`.

---

Expand All @@ -99,11 +92,6 @@
Message
x <text>:2: @importFrom Excluding unknown exports from utils: `InvalidUtilsFunction1` and `InvalidUtilsFunction2`.

---

Code
out <- roc_proc_text(namespace_roclet(), block)

# warns if S3 method not documented

Code
Expand Down
51 changes: 28 additions & 23 deletions tests/testthat/test-namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -394,59 +394,64 @@ test_that("can extract non-imports from namespace preserving source", {
expect_equal(namespace_exports(path), lines[c(1:3, 5)])
})

test_that("Invalid imports throw a helpful error", {
test_that("invalid imports generate correct declarations", {
# No matched functions --> no output
block <- "
#' @importFrom utils InvalidUtilsFunction
NULL
"
expect_snapshot(out <- roc_proc_text(namespace_roclet(), block))
expect_message(out <- roc_proc_text(namespace_roclet(), block))
expect_equal(out, character())

# Matched functions --> only drop unmatched functions
block <- "
#' @importFrom utils head InvalidUtilsFunction
NULL
"
expect_snapshot(out <- roc_proc_text(namespace_roclet(), block))
expect_message(out <- roc_proc_text(namespace_roclet(), block))
expect_equal(out, "importFrom(utils,head)")
})

test_that("invalid imports generate helpful message", {
block <- "
#' @importFrom utils head InvalidUtilsFunction1
NULL
"
expect_snapshot(out <- roc_proc_text(namespace_roclet(), block))

# pluralization
block <- "
#' @importFrom utils head InvalidUtilsFunction1 InvalidUtilsFunction2
NULL
"
expect_snapshot(out <- roc_proc_text(namespace_roclet(), block))
expect_equal(out, "importFrom(utils,head)")
})

# If the package is not available at roxygenize() run time, nothing we can do
test_that("nothing we can do if package isn't installed", {
block <- "
#' @importFrom AnUnknownUnavailablePackage Unchecked
NULL
"
expect_snapshot(out <- roc_proc_text(namespace_roclet(), block))
expect_no_message(out <- roc_proc_text(namespace_roclet(), block))
expect_equal(out, "importFrom(AnUnknownUnavailablePackage,Unchecked)")
})

# make sure to match several forms of non-syntactic names
expect_no_warning(expect_equal(
roc_proc_text(namespace_roclet(), "#' @importFrom stringr %>%\nNULL"),
'importFrom(stringr,"%>%")'
))
expect_no_warning(expect_equal(
roc_proc_text(namespace_roclet(), "#' @importFrom stringr '%>%'\nNULL"),
"importFrom(stringr,'%>%')"
))
expect_no_warning(expect_equal(
roc_proc_text(namespace_roclet(), "#' @importFrom stringr `%>%`\nNULL"),
test_that("non-syntactic imports can use multiple quoting forms", {
lines <- c(
"#' @importFrom stringr %>%",
"#' @importFrom stringr `%>%`",
"#' @importFrom stringr '%>%'",
"#' @importFrom stringr \"%>%\"",
"NULL"
)

import <- expect_no_warning(roc_proc_text(namespace_roclet(), lines))
expect_equal(import, c(
"importFrom(stringr,\"%>%\")",
"importFrom(stringr,'%>%')",
"importFrom(stringr,`%>%`)"
))
expect_no_warning(expect_equal(
roc_proc_text(namespace_roclet(), '#\' @importFrom stringr "%>%"\nNULL'),
'importFrom(stringr,"%>%")'
))
})


# warn_missing_s3_exports -------------------------------------------------

test_that("warns if S3 method not documented", {
Expand Down

0 comments on commit 2f82c7b

Please sign in to comment.