# WARNING - Generated by {fusen} from dev/flat_rename_r.Rmd: do not edit by hand

test_that("Valid outputs are consistent", {
  
  library(magrittr)
  data(cars)
  data(mtcars)
  
  mydir <- tempfile()
  dir.create(mydir)
  
  saveRDS(cars, file.path(mydir, "cars.rds"))
  saveRDS(mtcars, file.path(mydir, "mtcars.rds"))
  
  expect_equal(list.files(mydir), 
               c("cars.rds", "mtcars.rds"))
  
  mask_rename_r(input_path = mydir)
  
  expect_equal(list.files(mydir), 
               c("cars.rds", "mask_rename_r.xlsx", "mtcars.rds"))
  
  expect_equal(
    readxl::read_xlsx(file.path(mydir, "mask_rename_r.xlsx")),
    structure(
      list(
        file = c("cars.rds", "mtcars.rds"),
        renamed_file = c(NA, NA),
        to_rename = c(NA, NA)
      ),
      class = c("tbl_df", "tbl", "data.frame"),
      row.names = c(NA, -2L)
    ))
  
  unlink(mydir, recursive = TRUE)
})

test_that("Errors are consistent", {
  
  library(magrittr)
  data(cars)
  data(mtcars)
  
  mydir <- tempfile()
  dir.create(mydir)
  
  saveRDS(cars, file.path(mydir, "cars.rds"))
  # no second save to test error
  
  expect_error(
    mask_rename_r(input_path = mydir %>% paste0("noexist")),
    regexp = "input_path doesn't exist")
  
  expect_error(
    mask_rename_r(input_path = mydir), 
    regexp = "input_path must contains at least two files. Renaming a single file with this function is not worth it.")
  
  file.create(file.path(mydir, "mask_rename_r.xlsx"))
  
  expect_error(
    mask_rename_r(input_path = mydir),
    regexp = "There is apparently already a mask in your folder: ")
  
  file.remove(file.path(mydir, "mask_rename_r.xlsx"))
})
