##############################################

# data1
shp_file1 <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', 
                    'NUTS_RG_60M_2021_4326.shp')
ref_grid1 <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/',
                    'tas_f1h/tas_201006.nc')
NUTS_id1 <- paste0("FI1D", c(1:3, 5, 7:9))  
NUTS_name1 <- list(FI = c('Lappi', 'Kainuu'), SI = c('Pomurska', 'Podravska'))

# data2
ref_grid2 <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5))

##############################################

test_that("1. Input checks", {
  expect_error(
    ShapeToMask(NULL),
    "Parameter 'shp_file' cannot be NULL."
  )
  expect_error(
    ShapeToMask(1),
    "Parameter 'shp_file' must be a character string."
  )
  expect_error(
    ShapeToMask(shp_file1, ref_grid = NULL),
    "Parameter 'ref_grid' cannot be NULL."
  )
  expect_error(
    ShapeToMask(shp_file1, ref_grid = 1),
    "Parameter 'ref_grid' must be a character string or a list."
  )
})

##############################################

test_that("2. Output", {
  mask1 <- ShapeToMask(shp_file1, ref_grid1, reg_ids = NUTS_id1)
  expect_equal(
    dim(mask1),
    c(lon = 3600, lat = 1801)
  )
  expect_equal(
    sum(mask1),
    c(20899)
  )
  mask2 <- ShapeToMask(shp_file1, ref_grid = ref_grid2, reg_ids = NUTS_id1)
  expect_equal(
    sum(mask2),
    830
  )
  expect_equal(
    dim(mask2),
    c(lon = 61, lat = 91)
  )
  mask3 <- ShapeToMask(shp_file1, ref_grid = ref_grid2, reg_ids = NUTS_id1, 
                       region = TRUE)
  expect_equal(
    dim(mask3),
    c(lon = 61, lat = 91, region = 7)
  )
  expect_equal(
    sum(mask3),
    176
  )
})