0

Context

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)
}

Problem

The function above, returns TRUE on identical(xml_reserved_chars_to_named_entities("''"), "&apos;&apos;")

but fails to pass the following test (using testthat package) with:

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

I was expecting it to pass that test as it gives TRUE with identical's call. Why is not that so?

Here is testthat feedback on that test:

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

Version details

My R version is 3.3.0 and that of testthat is 1.0.2. Help is appreciated.

Ramiro Magno
  • 3,085
  • 15
  • 30
  • your `paste0` would either need to be `paste0(result, collapse = '')` or you need to split the `"''"` into two `c("'", "'")` also your problem returns false for me, not true – rawr Aug 30 '16 at 14:15
  • @rawr: Ok, thanks. I see your point. But I still do not see why `identical` and `expect_identical` are behaving differently in this case. – Ramiro Magno Aug 30 '16 at 14:19
  • I get `FALSE` with `identical`. You might want to run it again. – drhagen Aug 30 '16 at 14:20
  • my bad... indeed the function I was calling is of a previous version... sorry guys. – Ramiro Magno Aug 30 '16 at 14:25
  • @plant because your current function gives me `c("'", "'")` and you are testing for equality with `"''"` – rawr Aug 30 '16 at 16:08

0 Answers0