# 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)
  
  # Two example files to rename: 
  saveRDS(cars, file.path(mydir, "cars.rds"))
  saveRDS(mtcars, file.path(mydir, "mtcars.rds"))
  
  # Create the mask: 
  mask_rename_r(input_path = mydir)
  
  # Fill the mask (in practice you can do it manually): 
  mask <- rio::import(file.path(mydir, "mask_rename_r.xlsx"))
  mask[["renamed_file"]] <- c("cars_renamed.rds", "mtcars_renamed.rds")
  mask[["to_rename"]] <- rep(1, 2)
  writexl::write_xlsx(mask, file.path(mydir, "mask_rename_r.xlsx"))
  
  result <- rename_r(mask_filepath = file.path(mydir, "mask_rename_r.xlsx"))
  
  expect_equal(result, list(TRUE, TRUE))
  
  expect_equal(
    list.files(mydir), 
    c("cars_renamed.rds", "mask_rename_r.xlsx", "mtcars_renamed.rds"))
  
  expect_equal(readRDS(file.path(mydir, "cars_renamed.rds")),
               cars)
  
  expect_equal(readRDS(file.path(mydir, "mtcars_renamed.rds")),
               mtcars)
  
  unlink(mydir, recursive = TRUE)
  })

test_that("Errors are consistent", {
  
  library(magrittr)
  data(cars)
  data(mtcars)
  
  mydir <- tempfile()
  dir.create(mydir)
  
  # Two example files to rename: 
  saveRDS(cars, file.path(mydir, "cars.rds"))
  saveRDS(mtcars, file.path(mydir, "mtcars.rds"))
  
  # Create the mask: 
  mask_rename_r(input_path = mydir)
  
  # Fill the mask (in practice you can do it manually): 
  mask <- rio::import(file.path(mydir, "mask_rename_r.xlsx"))
  mask[["renamed_file"]] <- c("cars_renamed.rds", "mtcars_renamed.rds")
  mask[["to_rename"]] <- rep(1, 2)
  
  mask <- dplyr::rename(mask, "to_rename2" = "to_rename")
  writexl::write_xlsx(mask, file.path(mydir, "mask_rename_r.xlsx"))
  
  expect_error(rename_r(file.path(mydir, paste0("mask_rename_r.xlsx", "noexist"))), 
               "mask_filepath doesn't exist. It must be a valid and full path including the xlsx file.")
  
  expect_error(rename_r(file.path(mydir, "mask_rename_r.xlsx")),
               "The mask must have the columns: file, renamed_file, to_rename")
  
  unlink(mydir, recursive = TRUE)
  })
