From 9507c549731337d0d3bf866698e63de660405dd0 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 22 Jan 2024 12:33:27 -0600 Subject: [PATCH] Test refactoring --- tests/testthat/_snaps/namespace.md | 12 +++-------- tests/testthat/test-namespace.R | 32 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/tests/testthat/_snaps/namespace.md b/tests/testthat/_snaps/namespace.md index 476c5bd7..57e080ee 100644 --- a/tests/testthat/_snaps/namespace.md +++ b/tests/testthat/_snaps/namespace.md @@ -95,21 +95,15 @@ # warns if S3 method not documented Code - roc_proc_text(namespace_roclet(), - "\n foo <- function(x) UseMethod('foo')\n foo.numeric <- function(x) 1\n\n mean.myclass <- function(x) 2\n ") + . <- roc_proc_text(namespace_roclet(), block) Message x :5: S3 method `mean.myclass` needs @export or @exportS3method tag. x :3: S3 method `foo.numeric` needs @export or @exportS3method tag. - Output - character(0) -# correctly interpolates warning +--- Code - roc_proc_text(namespace_roclet(), - "\n foo <- function(x) UseMethod('foo')\n `foo.{` <- function(x) 1\n ") + . <- roc_proc_text(namespace_roclet(), block) Message x :3: S3 method `foo.{` needs @export or @exportS3method tag. - Output - character(0) diff --git a/tests/testthat/test-namespace.R b/tests/testthat/test-namespace.R index 3ec4859f..1dadfa1e 100644 --- a/tests/testthat/test-namespace.R +++ b/tests/testthat/test-namespace.R @@ -472,32 +472,32 @@ test_that("non-syntactic imports can use multiple quoting forms", { # warn_missing_s3_exports ------------------------------------------------- test_that("warns if S3 method not documented", { - expect_snapshot( - roc_proc_text(namespace_roclet(), " + # Need to manually transform since the srcref is coming from the function; + # roc_proc_text() uses fake srcrefs for the blocks themselves + fix_srcref <- function(x) gsub("file[a-z0-9]+", "", x) + + block <- " foo <- function(x) UseMethod('foo') foo.numeric <- function(x) 1 mean.myclass <- function(x) 2 - "), - # Need to manually transform since the srcref is coming from the function; - # roc_proc_text() uses fake srcrefs for the blocks themselves - transform = function(x) gsub("file[a-z0-9]+", "", x) + " + expect_snapshot( + . <- roc_proc_text(namespace_roclet(), block), + transform = fix_srcref ) -}) -test_that("correctly interpolates warning", { + # Works even if method contains { + block <- " + foo <- function(x) UseMethod('foo') + `foo.{` <- function(x) 1 + " expect_snapshot( - roc_proc_text(namespace_roclet(), " - foo <- function(x) UseMethod('foo') - `foo.{` <- function(x) 1 - "), - # Need to manually transform since the srcref is coming from the function; - # roc_proc_text() uses fake srcrefs for the blocks themselves - transform = function(x) gsub("file[a-z0-9]+", "", x) + . <- roc_proc_text(namespace_roclet(), block), + transform = fix_srcref ) }) - test_that("can suppress the warning", { block <- " #' @exportS3Method NULL