tests/testthat/test-todo.R

todo_app_path <- function() {
  system.file("examples/todo.R", package = "Rapp")
}

run_todo_app <- function(args = character(), capture = TRUE) {
  script <- todo_app_path()
  if (capture) {
    capture.output(Rapp::run(script, args = args))
  } else {
    Rapp::run(script, args = args)
  }
}

test_that("todo help output", {
  app_path <- todo_app_path()
  expect_snapshot(
    cat(
      help_lines(app_path),
      sep = "\n"
    )
  )
  expect_snapshot(
    cat(
      help_lines(app_path, "list"),
      sep = "\n"
    )
  )
  expect_snapshot(
    cat(
      help_lines(app_path, "done"),
      sep = "\n"
    )
  )
})

test_that("todo without a command prints help", {
  lines <- run_todo_app()

  expect_true(any(grepl("Todo manager", lines, fixed = TRUE)))
  expect_true(any(grepl("Usage: todo [OPTIONS] <COMMAND>", lines, fixed = TRUE)))
  expect_true(any(grepl("Commands:", lines, fixed = TRUE)))
})

test_that("todo rejects unknown command tokens", {
  expect_error(
    run_todo_app("lisst", capture = FALSE),
    "Arguments not recognized: lisst"
  )
})

test_that("todo commands update the store", {
  store <- tempfile(fileext = ".yml")
  on.exit(unlink(store), add = TRUE)

  run_todo_app(c("add", "buy milk", "--store", store))
  expect_equal(
    yaml12::read_yaml(store),
    "buy milk"
  )

  run_todo_app(c("add", "write tests", "--store", store))
  expect_equal(
    yaml12::read_yaml(store),
    c("buy milk", "write tests")
  )

  run_todo_app(c("add", "call mom", "--store", store))
  expect_equal(
    yaml12::read_yaml(store),
    c("buy milk", "write tests", "call mom")
  )

  done_default <- run_todo_app(c("done", "--store", store))
  expect_match(done_default[[1]], "Completed: buy milk")
  expect_equal(
    yaml12::read_yaml(store),
    c("write tests", "call mom")
  )

  done_output <- run_todo_app(c("done", "-i", "2", "--store", store))
  expect_match(done_output[[1]], "Completed: call mom")
  expect_equal(yaml12::read_yaml(store), "write tests")
})

Try the Rapp package in your browser

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

Rapp documentation built on June 11, 2026, 5:07 p.m.