[英]{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.