R base函数`same`l和`testthat``expect_identical`之间的区别?

时间:2016-08-30 14:10:05

标签: r testthat

上下文

xml_reserved_chars_to_named_entities <- function(string) {

  if(string == "" || identical(string, character(0))) return("")
  if(length(string) > 1) sapply(string, xml_reserved_chars_to_named_entities)

  chars <- unlist(strsplit(string, ""))
  pattern <- c('<', '&', '>', "'", '"')
  replacement <- c('&lt;', '&amp;', '&gt;', '&apos;', '&quot;')

  result = chars
  for (i in seq_along(pattern)) {
    result[grep(pattern[i], chars)] = replacement[i]
  }
  paste0(result)
}

问题

上述功能在TRUE

上返回identical(xml_reserved_chars_to_named_entities("''"), "&apos;&apos;")

但未能通过以下测试(使用testthat包):

test_that("xml_reserved_chars_to_named_entities",
{
     expect_identical(xml_reserved_chars_to_named_entities("''"), "&apos;&apos;")
}
)

我希望它通过该测试,因为它会TRUE调用identical。为什么不是这样呢?

以下是testthat对该测试的反馈:

Failed -------------------------------------------------------------------------
1. Failure: xml_reserved_chars_to_named_entities (@test-utils.R#48) ------------
xml_reserved_chars_to_named_entities("''") not identical to "&apos;&apos;".
Lengths differ: 2 vs 1

版本详情

我的R版本为3.3.0,而testthat的版本为1.0.2。感谢帮助。

0 个答案:

没有答案