Skip to content

Content of character vector can trip up expect_snapshot_value()Β #1294

@jennybc

Description

@jennybc

Discovered while converting reprex to testthat 3e.

Here's an example of a specific character vector that is difficult to use with expect_snapshot_value(). (The reprex content doesn't matter -- the problem appears to be the backticks associated with fenced code blocks.)

Put this inside a package's tests and run test():

  • The default (no style specified) is unable to save the snapshot (other styles can save snapshot).
  • The default also reports Warning (test-foo.R:12:1): (code run outside of test_that()), which seems wrong.
f <- function() {
  c(
    "``` r",
    "utils::getParseData(parse(text = 'a'))",
    "#>   line1 col1 line2 col2 id parent  token terminal text",
    "#> 1     1    1     1    1  1      3 SYMBOL     TRUE    a",
    "#> 3     1    1     1    1  3      0   expr    FALSE",
    "```"
  )
}

test_that("no style specified", {
  expect_snapshot_value(f())
})

test_that("json2", {
  expect_snapshot_value(f(), style = "json2")
})

test_that("deparse", {
  expect_snapshot_value(f(), style = "deparse")
})

test_that("serialize", {
  expect_snapshot_value(f(), style = "serialize")
})

On the first run (so no snapshots exist yet), I see:

Error (test-foo.R:13:3): no style specified
Error: Serialization round-trip is not symmetric.

`value` is a character vector ('``` r', 'utils::getParseData(parse(text = \'a\'))', '#>   line1 col1 line2 col2 id parent  token terminal text', '#> 1     1    1     1    1  1      3 SYMBOL     TRUE    a', '#> 3     1    1     1    1  3      0   expr    FALSE', ...)
`roundtrip` is a list

i You may need to consider serialization `style`
Backtrace:
 1. testthat::expect_snapshot_value(f()) test-foo.R:13:2
 2. testthat:::expect_snapshot_helper(...)
 3. snapshotter$take_snapshot(val, save = save, load = load, ...)
 4. testthat:::check_roundtrip(value, load(save(value)))

Warning (test-foo.R:12:1): (code run outside of `test_that()`)
Snapshots reset after error/skip

Warning (test-foo.R:17:3): json2
Adding new snapshot:
{
  "type": "character",
  "attributes": {},
  "value": ["``` r", "utils::getParseData(parse(text = 'a'))", "#>   line1 col1 line2 col2 id parent  token terminal text", "#> 1     1    1     1    1  1      3 SYMBOL     TRUE    a", "#> 3     1    1     1    1  3      0   expr    FALSE", "```"]
}

Warning (test-foo.R:21:3): deparse
Adding new snapshot:
c("``` r", "utils::getParseData(parse(text = 'a'))", "#>   line1 col1 line2 col2 id parent  token terminal text", 
"#> 1     1    1     1    1  1      3 SYMBOL     TRUE    a", 
"#> 3     1    1     1    1  3      0   expr    FALSE", "```"
)

Warning (test-foo.R:25:3): serialize
Adding new snapshot:
WAoAAAACAAQAAgACAwAAAAAQAAAABgAEAAkAAAAFYGBgIHIABAAJAAAAJnV0aWxzOjpnZXRQ
YXJzZURhdGEocGFyc2UodGV4dCA9ICdhJykpAAQACQAAADkjPiAgIGxpbmUxIGNvbDEgbGlu
ZTIgY29sMiBpZCBwYXJlbnQgIHRva2VuIHRlcm1pbmFsIHRleHQABAAJAAAAOSM+IDEgICAg
IDEgICAgMSAgICAgMSAgICAxICAxICAgICAgMyBTWU1CT0wgICAgIFRSVUUgICAgYQAEAAkA
AAA0Iz4gMyAgICAgMSAgICAxICAgICAxICAgIDEgIDMgICAgICAwICAgZXhwciAgICBGQUxT
RQAEAAkAAAADYGBg

I understand that certain nontrivial objects might need different methods of saving, hence the existence of style. But I wouldn't expect a character vector to ever need special treatment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions