tests/testthat/test-snapshot-serialize.R

test_that("single test case can roundtrip", {
  x <- list(test = '[1] "x"')
  x_snap <- snap_to_md(x)
  x_lines <- strsplit(x_snap, "\n")[[1]]

  y <- snap_from_md(x_lines)
  expect_equal(x, y)
})

test_that("multiple tests can roundtrip", {
  x <- list(foo = c("a","b"), bar = "d", baz = letters[1:3])
  x_snap <- snap_to_md(x)
  x_lines <- strsplit(x_snap, "\n")[[1]]

  y <- snap_from_md(x_lines)
  expect_equal(x, y)
})

test_that("snapshots always use \n", {
  path <- withr::local_tempfile()
  x <- list(foo = c("a","b"), bar = "d", baz = letters[1:3])
  write_snaps(x, path)

  snap <- brio::read_file(path)
  has_cr <- grepl("\r", snap, fixed = TRUE)
  expect_equal(has_cr, FALSE)
})

Try the testthat package in your browser

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

testthat documentation built on Jan. 21, 2022, 1:10 a.m.