tests/testthat/charr/common/test-width-backend.R

# charr-owned file: targeted equivalence tests for Reader-backed width.

width_cp <- function(x) charr_test_leaf("ci_width")(x)


test_that("width preserves ICU property and emoji-context semantics", {
  strings <- charr_test_leaf("ci_replace_all_fixed")(c(
    "a", "A", "ア", "Ω", "ü", "\t", "🙂", "👩‍👧", "🇺🇸", "", NA
  ), "not present", "")
  expect_identical(is_charr_result(strings), charr_altrep())

  expect_identical(
    width_cp(strings),
    c(1L, 2L, 1L, 1L, 1L, 0L, 2L, 2L, 2L, 0L, NA_integer_)
  )
})


test_that("width stays inside adjacent Reader records", {
  strings <- charr_test_leaf("ci_trim_both")(c(
    " a ", " very_long_👩‍👧_record ", " z ", " ü ", " A "
  ))
  expect_identical(is_charr_result(strings), charr_altrep())
  expect_identical(width_cp(strings), c(1L, 19L, 1L, 1L, 2L))
})


test_that("width validates UTF-8 and rejects CE_BYTES", {
  malformed_byte <- rawToChar(as.raw(0xc3))
  Encoding(malformed_byte) <- "UTF-8"
  malformed <- charr_test_leaf("ci_replace_all_fixed")("a", "a", malformed_byte)
  expect_identical(is_charr_result(malformed), charr_altrep())
  expect_error(width_cp(malformed), "invalid UTF-8")

  bytes <- "\xff"
  Encoding(bytes) <- "bytes"
  expect_error(width_cp(bytes), "bytes encoding")
})

Try the charr package in your browser

Any scripts or data that you put into this service are public.

charr documentation built on Nov. 7, 2026, 9:06 a.m.