tests/testthat/test-is.labelled.R

# The source code of this helper is in utils.R

test_that("is.labelled recognizes haven-labelled vectors", {
  x <- haven::labelled(
    c(1, 2),
    labels = c(yes = 1, no = 2)
  )

  expect_true(is.labelled(x))
})


test_that("is.labelled recognizes SPSS-labelled vectors", {
  x <- haven::labelled_spss(
    c(1, 2, 99),
    labels = c(yes = 1, no = 2, missing = 99),
    na_values = 99
  )

  expect_true(is.labelled(x))
})


test_that("is.labelled recognizes survey-labelled vectors", {
  x <- labelled_spss_survey(
    x = c(1, 2),
    labels = c(yes = 1, no = 2),
    id = "s1"
  )

  expect_true(is.labelled(x))
})


test_that("is.labelled rejects unrelated objects", {
  for (x in list(
    NULL,
    1:3,
    "text",
    factor("yes"),
    data.frame(x = 1)
  )) {
    expect_false(is.labelled(x))
  }
})

Try the retroharmonize package in your browser

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

retroharmonize documentation built on Nov. 29, 2026, 5:06 p.m.