tests/testthat/test-stringparser_constructor.R

test_that("'stringparser' creates parser with zero or one capture groups", {
  parse_header <- stringparser("^>(\\w+)")
  parse_header_no_output <- stringparser("^>\\w+")
  expect_equal(parse_header(">good_header"), "good_header")
  expect_equal(parse_header("> bad_header"), list())
  expect_silent(parse_header_no_output(">good_header"))
  expect_equal(parse_header_no_output("> bad_header"), list())
})

test_that("'stringparser' creates parser with multiple capture groups and arbitrary output", {
  parse_key_value <- stringparser("(\\w+):\\s?(\\w+)")
  parse_key_value_df <- stringparser("(\\w+):\\s?(\\w+)", function(x) data.frame(key = x[1], value = x[2]))
  expect_equal(parse_key_value("key1 value1"), list())
  expect_equal(parse_key_value("key1: value1"), c("key1", "value1"))
  expect_equal(parse_key_value_df("key1: value1"), data.frame(key = 'key1', value = 'value1'))
})

Try the parcr package in your browser

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

parcr documentation built on Aug. 8, 2025, 6:20 p.m.