tests/testthat/test-cidian.R

test_that("the supported format list is stable", {
  expect_identical(
    cidian_formats(),
    c("scel", "qcel", "qpyd", "bdict", "bcd")
  )
  expect_identical(eval(formals(read_cidian)$format), cidian_formats())
})

test_that("a QCEL dictionary is converted to the common R model", {
  path <- test_path("fixtures", "computer.qcel")

  dictionary <- read_cidian(path)

  expect_s3_class(dictionary, "cidian_dictionary")
  expect_identical(dictionary$format, "qcel")
  expect_s3_class(dictionary$entries, "data.frame")
  expect_identical(
    names(dictionary$entries),
    c("word", "code", "weight")
  )

  expect_type(dictionary$entries$word, "character")
  expect_type(dictionary$entries$code, "list")
  expect_type(dictionary$entries$weight, "double")

  expect_identical(nrow(dictionary$entries), 9646L)
  expect_identical(length(dictionary$entries$code[[1]]), 3L)

  expect_identical(cidian_entries(dictionary), dictionary$entries)
  expect_identical(cidian_metadata(dictionary), dictionary$metadata)
  expect_identical(read_cidian(path, format = NULL)$format, "qcel")
})

test_that("all supported parser formats can be read", {
  fixtures <- c(
    scel = "xiangqi.scel",
    qcel = "computer.qcel",
    qpyd = "sample.qpyd",
    bdict = "game.bdict",
    bcd = "slang.bdict"
  )

  for (format in names(fixtures)) {
    path <- test_path("fixtures", fixtures[[format]])
    dictionary <- read_cidian(path, format = format)
    expect_identical(dictionary$format, format)
    expect_gt(nrow(dictionary$entries), 0L)
  }
})

test_that("an explicit format overrides the file extension", {
  source <- test_path("fixtures", "computer.qcel")
  target <- tempfile(fileext = ".bin")
  on.exit(unlink(target), add = TRUE)
  expect_identical(file.copy(source, target), TRUE)

  dictionary <- read_cidian(target, format = ".QCEL")
  expect_identical(dictionary$format, "qcel")
})

test_that("dictionary methods expose stable summaries", {
  dictionary <- read_cidian(test_path("fixtures", "xiangqi.scel"))

  expect_snapshot(print(dictionary))

  summary <- summary(dictionary)
  expect_s3_class(summary, "summary.cidian_dictionary")
  expect_identical(summary$entries, nrow(dictionary$entries))
})

test_that("as.data.frame() rejects additional arguments", {
  dictionary <- read_cidian(test_path("fixtures", "xiangqi.scel"))

  expect_snapshot(
    as.data.frame(dictionary, row.names = 1:2),
    error = TRUE
  )
})

test_that("format and path validation gives actionable errors", {
  extensionless <- tempfile()
  on.exit(unlink(extensionless), add = TRUE)
  expect_true(file.create(extensionless))

  expect_error(read_cidian(extensionless))
  expect_error(read_cidian("missing.qcel"))
  expect_error(cidian_entries(list()))
  expect_error(read_cidian(NA_character_))

  expect_snapshot(
    read_cidian(test_path("fixtures", "computer.qcel"), format = "unknown"),
    error = TRUE
  )
})

test_that("write_words() writes one word per line", {
  dictionary <- read_cidian(test_path("fixtures", "xiangqi.scel"))
  target <- tempfile()
  on.exit(unlink(target), add = TRUE)

  expect_identical(write_words(dictionary, target), target)
  words <- readLines(target, encoding = "UTF-8")
  expect_identical(words, dictionary$entries$word)
})

test_that("write_words() asks before overwriting", {
  dictionary <- read_cidian(test_path("fixtures", "xiangqi.scel"))
  target <- tempfile()
  on.exit(unlink(target), add = TRUE)
  writeLines("old", target)

  # Answering no aborts without touching the file
  local_mocked_bindings(askYesNo = function(...) FALSE, .package = "utils")
  expect_error(write_words(dictionary, target))

  # Answering yes overwrites
  local_mocked_bindings(askYesNo = function(...) TRUE, .package = "utils")
  write_words(dictionary, target)
  expect_identical(
    readLines(target, encoding = "UTF-8"),
    dictionary$entries$word
  )
})

test_that("write_words() overwrite argument is honoured", {
  dictionary <- read_cidian(test_path("fixtures", "xiangqi.scel"))
  target <- tempfile()
  on.exit(unlink(target), add = TRUE)
  writeLines("old", target)

  expect_error(write_words(dictionary, target, overwrite = FALSE))
  write_words(dictionary, target, overwrite = TRUE)
  expect_identical(
    readLines(target, encoding = "UTF-8"),
    dictionary$entries$word
  )
})

Try the cidian package in your browser

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

cidian documentation built on Sept. 28, 2026, 5:08 p.m.