tests/testthat/test-create_function.R

testthat::test_that("Function and test files were created on temp proj", {
  temp_project <- fs::path(fs::path_temp(), "funProj")
  fs::dir_create(temp_project)
  test_name <- "test_fun"

  create_function(function_name = test_name, pkg_dir = temp_project)

  fun_res <- fs::file_exists(fs::path(temp_project, "R", test_name, ext = "R"))
  test_res <- fs::file_exists(
    fs::path(
      temp_project, "tests", "testthat", glue::glue("test-{test_name}"),
      ext = "R"
    )
  )
  # Remove names from results
  names(fun_res) <- NULL
  testthat::expect_equal(fun_res, TRUE)
  names(test_res) <- NULL
  testthat::expect_equal(test_res, TRUE)


  # test NULL
  testthat::expect_error(
    object = create_function(pkg_dir = temp_project),
    regexp = "Argument \\{function_name\\} should be set"
  )
  # test empty string
  testthat::expect_error(
    object = create_function(function_name = "", pkg_dir = temp_project),
    regexp = "Argument \\{function_name\\} should not be am empty string"
  )
  # test pkg dir don't exist
  testthat::expect_error(
    create_function(function_name = test_name, pkg_dir = "xxx"),
    regexp = "xxx don't exists"
  )

  # test message
  testthat::expect_message(
    create_function(function_name = test_name, pkg_dir = temp_project),
    "^.*\\.R already exists. Opening it to edit."
  )
})
luciorq/luciolib documentation built on Dec. 18, 2020, 11:43 a.m.