@@ -59,8 +59,10 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en", igno
5959
6060 wrong_number_fmt <- " got %d lints instead of %d%s"
6161 if (is.null(checks )) {
62- msg <- sprintf(wrong_number_fmt , n_lints , length(checks ), lint_str )
63- return (testthat :: expect(n_lints %== % 0L , msg ))
62+ if (n_lints != 0L ) {
63+ return (testthat :: fail(sprintf(wrong_number_fmt , n_lints , 0L , lint_str )))
64+ }
65+ return (testthat :: succeed())
6466 }
6567
6668 if (! is.list(checks ) || ! is.null(names(checks ))) { # vector or named list
@@ -69,8 +71,7 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en", igno
6971 checks [] <- lapply(checks , fix_names , " message" )
7072
7173 if (n_lints != length(checks )) {
72- msg <- sprintf(wrong_number_fmt , n_lints , length(checks ), lint_str )
73- return (testthat :: expect(FALSE , msg ))
74+ return (testthat :: fail(sprintf(wrong_number_fmt , n_lints , length(checks ), lint_str )))
7475 }
7576
7677 if (ignore_order ) {
@@ -85,42 +86,47 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en", igno
8586 checks <- checks [check_order ]
8687 }
8788
88- local({
89- itr_env <- new.env(parent = emptyenv())
90- itr_env $ itr <- 0L
91- # valid fields are those from Lint(), plus 'linter'
92- lint_fields <- c(names(formals(Lint )), " linter" )
93- Map(
94- function (lint , check ) {
95- itr_env $ itr <- itr_env $ itr + 1L
96- lapply(names(check ), function (field ) {
97- if (! field %in% lint_fields ) {
98- cli_abort(c(
99- x = " Check {.val {itr_env$itr}} has an invalid field: {.field {field}}." ,
100- i = " Valid fields are: {.field {lint_fields}}."
101- ))
102- }
103- check <- check [[field ]]
104- value <- lint [[field ]]
105- msg <- sprintf(
106- " check #%d: %s %s did not match %s" ,
107- itr_env $ itr , field , deparse(value ), deparse(check )
108- )
109- # deparse ensures that NULL, list(), etc are handled gracefully
110- ok <- if (field == " message" ) {
111- re_matches_logical(value , check )
112- } else {
113- isTRUE(all.equal(value , check ))
114- }
115- testthat :: expect(ok , msg )
116- })
117- },
118- lints ,
119- checks
120- )
121- })
89+ expect_lint_impl_(lints , checks )
90+
91+ testthat :: succeed()
92+ }
12293
123- invisible (NULL )
94+ # ' NB: must _not_ succeed(), should only fail() or abort()
95+ # ' @noRd
96+ expect_lint_impl_ <- function (lints , checks ) {
97+ itr <- 0L
98+ # valid fields are those from Lint(), plus 'linter'
99+ lint_fields <- c(names(formals(Lint )), " linter" )
100+
101+ for (i in seq_along(lints )) {
102+ lint <- lints [[i ]]
103+ check <- checks [[i ]]
104+
105+ itr <- itr + 1L
106+
107+ for (field in names(check )) {
108+ if (! field %in% lint_fields ) {
109+ cli_abort(c(
110+ x = " Check {.val {itr}} has an invalid field: {.field {field}}." ,
111+ i = " Valid fields are: {.field {lint_fields}}."
112+ ))
113+ }
114+ check_field <- check [[field ]]
115+ value <- lint [[field ]]
116+ ok <- if (field == " message" ) {
117+ re_matches_logical(value , check_field )
118+ } else {
119+ isTRUE(all.equal(value , check_field ))
120+ }
121+ if (! ok ) {
122+ return (testthat :: fail(sprintf(
123+ " check #%d: %s %s did not match %s" ,
124+ # deparse ensures that NULL, list(), etc are handled gracefully
125+ itr , field , deparse(value ), deparse(check )
126+ )))
127+ }
128+ }
129+ }
124130}
125131
126132# ' @rdname expect_lint
@@ -162,12 +168,11 @@ expect_lint_free <- function(...) {
162168 if (has_lints ) {
163169 lint_output <- format(lints )
164170 }
165- result <- testthat :: expect(
166- ! has_lints ,
167- paste0(" Not lint free\n " , lint_output )
168- )
169171
170- invisible (result )
172+ if (has_lints ) {
173+ return (testthat :: fail(paste0(" Not lint free\n " , lint_output )))
174+ }
175+ testthat :: succeed()
171176}
172177
173178# Helper function to check if testthat is installed.
0 commit comments