tests/testthat/test-format-tib.R

test_that("can format tib_unspecified()", {
  withr::local_options(cli.num_colors = 1)
  expect_snapshot(tib_unspecified("a"))
})

test_that("format for scalars works", {
  withr::local_options(cli.num_colors = 1)
  expect_snapshot(tib_chr("a"))
  expect_snapshot(tib_date("a"))
  expect_snapshot(tib_dbl("a"))
  expect_snapshot(tib_int("a"))
  expect_snapshot(tib_lgl("a"))

  expect_snapshot(tib_variant("a"))

  expect_snapshot(tib_int("a", .fill = NA_integer_))
  expect_snapshot(tib_int("a", .fill = 1))

  expect_snapshot(tib_int("a", .transform = as.integer))
  expect_snapshot(
    tib_int("a", .fill = NA_integer_, .transform = as.integer)
  )

  expect_snapshot(
    tib_scalar("a", .ptype = new_difftime(units = "mins"))
  )

  expect_snapshot(
    tib_row(
      "a",
      x = tib_int("x"),
      y = tib_dbl("y", .fill = NA_real_),
      z = tib_chr("z", .fill = "abc")
    )
  )

  expect_snapshot(
    tib_int(
      .key = "x",
      .ptype_inner = character(),
      .fill = "a"
    )
  )
})


test_that("format breaks long lines", {
  local_options(cli.num_colors = 1)
  expect_snapshot(
    tib_row(
      "path",
      a_long_name = tib_dbl("a looooooooooooooooooooong name", .fill = 1)
    ) |>
      print(width = 60)
  )

  expect_snapshot(
    tib_row(
      "path",
      a_long_name = tib_dbl("a looooooooooooooooooooong name", .fill = 1)
    ) |>
      print(width = 69)
  )
})


test_that("format for tib_vector works", {
  local_options(cli.num_colors = 1)
  expect_snapshot(tib_chr_vec("a"))
  expect_snapshot(tib_vector("a", .ptype = vctrs::new_duration()))

  expect_snapshot(
    tib_vector(
      "a",
      .ptype = vctrs::new_duration(),
      .input_form = "object",
      .values_to = "vals",
      .names_to = "names"
    )
  )

  # multi value default
  expect_snapshot(tib_int_vec("a", .fill = 1:2))

  expect_snapshot(
    tib_int_vec(
      .key = "x",
      .ptype_inner = character(),
      .fill = 1:2
    )
  )

  expect_snapshot({
    tib_lgl_vec("lgl")
    tib_int_vec("int")
    tib_dbl_vec("dbl")
    tib_chr_vec("chr")
    tib_date_vec("date")
  })
})

test_that("format for tib_chr_date works (#47)", {
  expect_snapshot({
    tib_chr_date("a")
    tib_chr_date("a", .required = FALSE, .fill = "2022-01-01", .format = "%Y")
  })

  expect_snapshot({
    tib_chr_date_vec("a")
    tib_chr_date_vec(
      "a",
      .required = FALSE,
      .fill = as.Date("2022-01-01"),
      .format = "%Y"
    )
  })
})

test_that("format for tib_row works", {
  local_options(cli.num_colors = 1)
  expect_snapshot(
    tib_row(
      "formats",
      text = tib_chr("text", .fill = NA_character_)
    )
  )

  expect_snapshot(
    tib_row(
      "formats",
      text = tib_chr("text"),
      .required = FALSE
    )
  )

  expect_snapshot(
    tib_row(
      "basic_information",
      labels = tib_row(
        "labels",
        name = tib_chr("name"),
        entity_type = tib_chr("entity_type"),
        catno = tib_chr("catno"),
        resource_url = tib_chr("resource_url"),
        id = tib_int("id"),
        entity_type_name = tib_chr("entity_type_name")
      ),
      year = tib_int("year"),
      master_url = tib_chr("master_url", .fill = NA),
      artists = tib_df(
        "artists",
        join = tib_chr("join"),
        name = tib_chr("name"),
        anv = tib_chr("anv"),
        tracks = tib_chr("tracks"),
        role = tib_chr("role"),
        resource_url = tib_chr("resource_url"),
        id = tib_int("id")
      ),
      id = tib_int("id"),
      thumb = tib_chr("thumb"),
      title = tib_chr("title"),
      formats = tib_df(
        "formats",
        descriptions = tib_chr_vec(
          "descriptions",
          .fill = NULL
        ),
        text = tib_chr("text", .fill = NA),
        name = tib_chr("name"),
        qty = tib_chr("qty")
      ),
      cover_image = tib_chr("cover_image"),
      resource_url = tib_chr("resource_url"),
      master_id = tib_int("master_id")
    )
  )
})

test_that("format for tib_variant works", {
  expect_snapshot(tib_variant("a"))
  expect_snapshot(tib_variant("a", .fill = tibble(a = 1:2)))
  expect_snapshot(tib_variant("a", .elt_transform = as.character))
})

test_that("format for tib_df works", {
  local_options(cli.num_colors = 1)
  expect_snapshot(
    tib_df("formats", text = tib_chr("text", .fill = NA_character_))
  )

  expect_snapshot(
    tib_df("formats", text = tib_chr("text"), .required = FALSE)
  )

  expect_snapshot(
    tib_df("formats", .names_to = "nms", text = tib_chr("text"))
  )
})

test_that("format for tib_recursive works (#155)", {
  local_options(cli.num_colors = 1)
  expect_snapshot(
    tib_recursive(
      "data",
      .children = "children",
      tib_int("id"),
      tib_chr("name")
    )
  )

  expect_snapshot(
    tib_recursive(
      "data",
      .children = "children",
      tib_int("id"),
      tib_chr("name"),
      .required = FALSE
    )
  )
})

test_that("format for empty tib_* works", {
  expect_equal(format(tib_df("x")), "tib_df(\n  \"x\",\n)")
  expect_equal(format(tib_row("x")), "tib_row(\n  \"x\",\n)")
})

test_that("format uses trailing comma", {
  expect_equal(
    format(tib_df("x", a = tib_int("a"))),
    'tib_df(\n  "x",\n  tib_int("a"),\n)'
  )
})

test_that("special ptypes correctly formatted", {
  expect_snapshot({
    tib_scalar("a", .ptype = character(), .ptype_inner = Sys.Date()) |> format()
    tib_scalar("a", .ptype = character(), .ptype_inner = Sys.time()) |> format()
  })
})

test_that("special ptypes are correctly formatted", {
  check_native <- function(ptype, ctr) {
    # native
    expect_equal(.format_ptype(ptype), ctr)

    # non-native
    class(ptype) <- c("a", class(ptype))
    expect_equal(.format_ptype(ptype), deparse(ptype))
  }

  check_native(logical(), "logical(0)")
  check_native(character(), "character(0)")
  check_native(integer(), "integer(0)")
  check_native(numeric(), "numeric(0)")
  check_native(vctrs::new_date(), "vctrs::new_date()")
  check_native(vctrs::new_duration(), "vctrs::new_duration()")
})

test_that(".format_tib_f.default returns first class name", {
  obj <- structure(list(), class = c("tib_custom", "tib_collector"))
  expect_equal(.format_tib_f(obj), "tib_custom")
})

Try the tibblify package in your browser

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

tibblify documentation built on May 9, 2026, 5:07 p.m.