tests/testthat/test-tokenize.R

test_that("remove starting spaces", {
  expect_equal(remove_starting_spaces("Hello World"), "Hello World")
  expect_equal(remove_starting_spaces(" Hello World"), "Hello World")
  expect_equal(remove_starting_spaces("     Hello World"), "Hello World")
})

test_that("remove ending spaces", {
  expect_equal(remove_ending_spaces("Hello World"), "Hello World")
  expect_equal(remove_ending_spaces("Hello World  "), "Hello World")
  expect_equal(remove_ending_spaces("  Hello World  "), "  Hello World")
})

test_that("detect token", {
  expect_true(does_token_exist("#> Test", "#>"))
  expect_false(does_token_exist(" #> Test", "#>"))
  expect_false(does_token_exist("#> Test", ">"))
})

test_that("detect malformed line", {
  expect_true(is_line_format_correct("#> my_label: [test]", "#>"))
  expect_false(is_line_format_correct("#> my_label: [test]", "#'"))
  expect_false(is_line_format_correct("#> my_label [test]", "#'"))
  expect_false(is_line_format_correct("#> my_label:: [[test]]", "#>"))
  expect_true(is_line_format_correct("#>     my label:     [test,    test2]", "#>"))
})


test_that("parse label", {
  expect_equal(parse_label("#> my_label: [test]", "#>"), "my_label")
  expect_equal(parse_label("#>     my label:     [test,    test2]", "#>"), "my label")
})

test_that("parse value", {
  expect_equal(parse_value("#> my_label: [test]", "#>"), "test")
  expect_equal(parse_value("#>     my label:     [test,    test2]", "#>"), "test,    test2")
})

test_that("tokenize", {
  expect_equal(
    tokenize(c("#>   authors   :    [   test  ]   ", "#> my_label: [  test1  ,  test2   ]", "#This is not a token"), "#>"),
    list(list(label = "authors", value = "test"), list(label = "my_label", value = c("test1", "test2")))
  )
  expect_warning(tokenize("#> authors ", "#>"), "Expression must be in the form #> label:\\[value\\]")
  expect_equal(
    tokenize(c(
      "# This is a comment",
      "#' This is a Roxygen Comment",
      "#> authors: [akash, test]"
    ), "#>"),
    list(list(label = "authors", value = c("akash", "test")))
  )
})
sada1993/catalogr documentation built on April 15, 2022, 3:32 a.m.