tests/testthat/test-util.R

test_that("sys_which", {
  expect_error(sys_which("nonexistantbinary"), "Can not find")
})

test_that("get_password_str", {
  testthat::with_mocked_bindings(
    get_pass = function(prompt) {
      if (grepl("Verify", prompt)) "a" else "b"
    },
    {
      expect_error(get_password_str(TRUE, "password"),
                   "Passwords do not match")
      expect_equal(get_password_str(FALSE, "password"),
                   "b")
    }
  )

  testthat::with_mocked_bindings(
    get_pass = function(prompt) "a", {
      expect_equal(get_password_str(TRUE, "password"), "a")
      expect_equal(get_password_str(FALSE, "password"), "a")
    })
})

test_that("prompt_confirm", {
  testthat::with_mocked_bindings(read_line = function(...) "n",
                      expect_equal(prompt_confirm(), FALSE))
  testthat::with_mocked_bindings(read_line = function(...) "y",
                      expect_equal(prompt_confirm(), TRUE))
  testthat::with_mocked_bindings(read_line = function(...) "",
                      expect_equal(prompt_confirm(), FALSE))

  first <- TRUE
  res <- testthat::with_mocked_bindings(
    read_line = function(...) {
      if (first) {
        first <<- FALSE
        "x"
      } else {
        "y"
      }
    },
    evaluate_promise(prompt_confirm()))
  expect_equal(res$result, TRUE)
  expect_match(res$output, "Invalid choice")
})

test_that("Descend failure", {
  path <- tempfile()
  dir.create(path)
  on.exit(unlink(path, recursive = TRUE))
  expect_null(find_file_descend(".cyphr_foobar", tempdir(), path))
  expect_null(find_file_descend(".cyphr_foobar", "/", path))
  expect_null(find_file_descend(".cyphr_foobar", "/", "/"))
})


test_that("is_directory", {
  path <- tempfile()
  expect_false(is_directory(path))
  file.create(path)
  expect_false(is_directory(path))
  unlink(path)
  dir.create(path)
  expect_true(is_directory(path))
})

Try the cyphr package in your browser

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

cyphr documentation built on April 3, 2025, 11:08 p.m.