簡體   English   中英

{testthat} 帶有 `expect_match` 的 `quasi_label` 拋出“錯誤:參數類型無效”

[英]{testthat} `quasi_label` with `expect_match` throws “Error: invalid argument type”

我是 {testthat} 的新手,正在為修改字符串的 function 構建測試,預計會為某些輸入模式生成特定的 output。

作為一個例子(下面的代表), add_excitement在其輸入字符串中添加了一個感嘆號。 當輸入“hello”時,它應該返回“hello;”; 當給定任何其他輸入時,它不應返回“hello”。 我想對一系列模式進行{testthat} 行為並返回信息性錯誤,這些錯誤指定導致錯誤的模式。

基於 {testthat} package 文檔,我相信我應該使用expect_match 但是,這會引發“無效參數類型”錯誤,而expect_identical有效。 我不明白為什么會這樣。 我的問題是:

  • 為什么expect_identical而不是expect_match接受quasi_label參數?
  • 我可以出於我的目的使用expect_identical而不是expect_match ,還是這會冒其他錯誤的風險?

這是一個代表:

library(testthat)
library(purrr)

patterns = c("hello", "goodbye", "cheers")
add_excitement <- function(pattern) paste0(pattern, "!")

# For a single pattern
show_failure(expect_identical(add_excitement(!!patterns[2]), "hello!"))
#> Failed expectation:
#> add_excitement("goodbye") not identical to "hello!".
#> 1/1 mismatches
#> x[1]: "goodbye!"
#> y[1]: "hello!"
try(
  show_failure(expect_match(add_excitement(!!patterns[2]), "hello!", fixed = TRUE,all = TRUE))
)
#> Error in !patterns[2] : invalid argument type

# For multiple patterns
purrr::map(
  patterns, 
  ~ show_failure(expect_identical(add_excitement(!!.), "hello!"))
)
#> Failed expectation:
#> add_excitement("goodbye") not identical to "hello!".
#> 1/1 mismatches
#> x[1]: "goodbye!"
#> y[1]: "hello!"
#> Failed expectation:
#> add_excitement("cheers") not identical to "hello!".
#> 1/1 mismatches
#> x[1]: "cheers!"
#> y[1]: "hello!"
#> [[1]]
#> NULL
#> 
#> [[2]]
#> NULL
#> 
#> [[3]]
#> NULL

try(
  purrr::map(
    patterns, 
    ~ show_failure(expect_match(add_excitement(!!.), "hello!", 
                                fixed = TRUE, all = TRUE)
    )
  )
)
#> Error in !. : invalid argument type

reprex package (v0.3.0) 於 2021 年 2 月 4 日創建

謝謝您的幫助!

在使用非標准評估bquote bquote()eval() (而不是quasi_label() ) 以產生更多信息錯誤。

library(testthat)
library(purrr)

patterns = c("hello", "goodbye", "cheers")
add_excitement <- function(pattern) paste0(pattern, "!")

show_failure(eval(bquote(expect_match(add_excitement(.(patterns[2])), "hello!", fixed = TRUE, all = TRUE))))
#> Failed expectation:
#> add_excitement("goodbye") does not match "hello!".
#> Actual value: "goodbye!"


purrr::walk(
  patterns,
  ~ show_failure(eval(bquote(expect_match(add_excitement(.(.)), "hello!", fixed = TRUE, all = TRUE))))
)
#> Failed expectation:
#> add_excitement("goodbye") does not match "hello!".
#> Actual value: "goodbye!"
#> Failed expectation:
#> add_excitement("cheers") does not match "hello!".
#> Actual value: "cheers!"

reprex package (v0.3.0) 於 2021 年 2 月 4 日創建

或者對於一個整潔的版本:

library(testthat)
library(purrr)

patterns = c("hello", "goodbye", "cheers")
add_excitement <- function(pattern) paste0(pattern, "!")

expect_hello <- function(pattern) {
  show_failure(eval(bquote(expect_match(add_excitement(.(pattern)), "hello!", fixed = TRUE, all = TRUE))))
}

expect_hello(patterns[2])
#> Failed expectation:
#> add_excitement("goodbye") does not match "hello!".
#> Actual value: "goodbye!"

walk(patterns, expect_hello)
#> Failed expectation:
#> add_excitement("goodbye") does not match "hello!".
#> Actual value: "goodbye!"
#> Failed expectation:
#> add_excitement("cheers") does not match "hello!".
#> Actual value: "cheers!"

reprex package (v0.3.0) 於 2021 年 2 月 4 日創建

該錯誤與tidyeval bang bang 的使用有關!!運營商
這已被取代,您提供的示例無需:

show_failure(expect_identical(add_excitement(patterns[2]), "hello!"))

# Failed expectation:
# add_excitement(patterns[2]) not identical to "hello!".
# 1/1 mismatches
# x[1]: "goodbye!"
# y[1]: "hello!"

show_failure(expect_match(add_excitement(patterns[2]), "hello!", fixed = TRUE,all = TRUE))

# Failed expectation:
# add_excitement(patterns[2]) does not match "hello!".
# Actual value: "goodbye!"

purrr::map(
  patterns, 
  ~ show_failure(expect_match(add_excitement(.x), "hello!", 
                              fixed = TRUE, all = TRUE)
  )
)

Failed expectation:
add_excitement(.x) does not match "hello!".
Actual value: "goodbye!"
Failed expectation:
add_excitement(.x) does not match "hello!".
Actual value: "cheers!"
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM