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('<', '&', '>', ''', '"')
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("''"), "''")
但未能通过以下测试(使用testthat
包):
test_that("xml_reserved_chars_to_named_entities",
{
expect_identical(xml_reserved_chars_to_named_entities("''"), "''")
}
)
我希望它通过该测试,因为它会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 "''".
Lengths differ: 2 vs 1
我的R版本为3.3.0
,而testthat
的版本为1.0.2
。感谢帮助。