1

我是 {testthat} 的新手,并且正在为修改字符串的函数构建测试,并且预计会为某些输入模式生成特定的输出。

作为示例(下面的表示形式),add_excitement在其输入字符串中添加一个感叹号。当输入“hello”时,它应该返回“hello!”;当给出任何其他输入时,它不应该返回“你好!”。我想对一系列模式进行{testthat} 行为并返回信息性错误,这些错误指定导致错误的模式。

基于 {testthat} 包文档,我相信我应该使用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 包于 2021-02-04 创建(v0.3.0)

谢谢您的帮助!

4

2 回答 2

1

我能够在https://r-pkgs.org/tests.html#building-your-own-testing-tools之后解决这个问题,它使用非标准评估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 包于 2021-02-04 创建(v0.3.0)

或者对于一个整洁的版本:

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 包于 2021-02-04 创建(v0.3.0)

于 2021-02-04T18:07:32.307 回答
0

该错误与tidyevalbang bang !!operator的使用有关。
这已被取代,您提供的示例无需:

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
于 2021-02-04T13:06:44.073 回答