
#===============================================================
# data
#===============================================================


# data1
set.seed(0)
data1 <- array(rep(0:10, each = 120),dim = c(lat = 30, lon = 44))
lons1 <- -10:33
lats1 <- 31:60
cols_white_red1 <- s2dv::clim.palette("bluered")(41)[21:41]



#===============================================================
# tests
#===============================================================


test_that("1. Input checks", {
  
  # Check lon, lat
  expect_error(
    VizEquiMap(lon = c()), 
    "Parameters 'lon' and 'lat' must be numeric vectors."
  )
  expect_error(
    VizEquiMap(lon = lons1, lat = c()), 
    "Parameters 'lon' and 'lat' must be numeric vectors."
  )
  
  # Check var
  expect_error(
    VizEquiMap(var = NULL, lon = lons1, lat = lats1), 
    "Parameter 'var' cannot be NULL."
  )
  
  # Check varu and varv
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, varu = "test1"), 
    "Only one of the components 'varu' or 'varv' has been provided. Both must be provided."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, varu = "test1", varv = "test2"), 
    "Parameter 'varu' must be a numerical array with two dimensions."
  )
  
  # Check contours
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, contours = "test"), 
    "Parameter 'contours' must be a numerical array with two dimensions."
  )
  
  # Check toptitle
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, toptitle = 1), 
    "Parameter 'toptitle' must be a character string."
  )
  
  # Check caption
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, caption = 1), 
    "Parameter 'caption' must be a character string."
  )
  
  # Check include_lower_boundary and include_upper_boundary
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, include_lower_boundary = 1), 
    "Parameter 'include_lower_boundary' must be a logical element."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, include_upper_boundary = 1), 
    "Parameter 'include_upper_boundary' must be a logical element."
  )
  
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, vertical = "test"), 
    "Parameter 'vertical' must be TRUE or FALSE."
  )
  
  # Check colNA
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, colNA = "test"), 
    "Parameter 'colNA' must be a valid colour identifier."
  )
  
  # Check square
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, square = "test"), 
    "Parameter 'square' must be logical."
  )
  
  # Check filled.continents
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, filled.continents = "test"), 
    "Parameter 'filled.continents' must be logical or a colour identifier."
  )
  
  # Check filled.oceans
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, filled.oceans = "test"), 
    "Parameter 'filled.oceans' must be logical or a colour identifier."
  )
  
  # Check country.borders
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, country.borders = "test"), 
    "Parameter 'country.borders' must be logical."
  )
  
  # Check coast_color
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, coast_color = "test"), 
    "Parameter 'coast_color' must be a valid colour identifier."
  )
  
  # Check coast_width
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, coast_width = "test"), 
    "Parameter 'coast_width' must be numeric."
  )
  
  # Check lake_color
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, lake_color = "test"), 
    "Parameter 'lake_color' must be a valid colour identifier."
  )
  
  # Check shapefile
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, shapefile = list()), 
    "The list names of the object in 'shapefile' .rds file should have at least 'x' and 'y'."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, shapefile = 1), 
    "Parameter 'shapefile' must be a .rds file or a list."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, shapefile = "test"), 
    "Parameter 'shapefile' is not a valid file."
  )
  
  # Check shapefile_col
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, shapefile_color = "test"), 
    "Parameter 'shapefile_color' must be a valid colour identifier."
  )
  
  # Check contour_lwd
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, contour_lwd = "test"), 
    "Parameter 'contour_lwd' must be numeric."
  )
  
  # Check contour_color
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, contour_color = "test"), 
    "Parameter 'contour_color' must be a valid colour identifier."
  )
  
  # Check contour_lty
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, contour_lty = c()), 
    "Parameter 'contour_lty' must be either a number or a character string."
  )
  
  # Check contour_draw_label
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, contour_draw_label = "test"), 
    "Parameter 'contour_draw_label' must be logical."
  )
  
  # Check contour_label_scale
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, contour_label_scale = "test"), 
    "Parameter 'contour_label_scale' must be numeric."
  )
  
  # Check dots
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, dots = "test"), 
    "Parameter 'dots' must be a logical array with two or three dimensions."
  )
  
  # Check mask
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, mask = "test"), 
    "Parameter 'mask' must have two dimensions named as the longitude and latitude dimensions in 'var'."
  )
  
  # Check arrow parameters
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, arr_subsamp = "test"), 
    "Parameter 'arr_subsamp' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, arr_scale = "test"), 
    "Parameter 'arr_scale' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, arr_ref_len = "test"), 
    "Parameter 'arr_ref_len' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, arr_units = 1), 
    "Parameter 'arr_units' must be character."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, arr_scale_shaft = "test"), 
    "Parameter 'arr_scale_shaft' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, arr_scale_shaft_angle = "test"), 
    "Parameter 'arr_scale_shaft_angle' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, arr_scale_shaft_angle = "test"), 
    "Parameter 'arr_scale_shaft_angle' must be numeric."
  )
  
  # Check axis parameters
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, axelab = "test"), 
    "Parameter 'axelab' must be logical."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, labW = "test"), 
    "Parameter 'labW' must be logical."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, lab_dist_x = "test"), 
    "Parameter 'lab_dist_x' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, lab_dist_y = "test"), 
    "Parameter 'lab_dist_y' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, intylat = "test"), 
    "Parameter 'intylat' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, intxlon = "test"), 
    "Parameter 'intxlon' must be numeric."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, xlonshft = "test"), 
    "Parameter 'xlonshft' must be a number."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, ylatshft = "test"), 
    "Parameter 'ylatshft' must be a number."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, xlabels = 1), 
    "Parameter 'xlabels' must be a vector of character string."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, ylabels = 1), 
    "Parameter 'ylabels' must be a vector of character string."
  )
  
  # Check legend parameters
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, drawleg = "test"), 
    "Parameter 'drawleg' must be logical."
  )
  
  # Check margin_scale
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, margin_scale = "test"), 
    "Parameter 'margin_scale' must be a numeric vector of length 4."
  )
  
  # Check title_scale
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, title_scale = "test"), 
    "Parameter 'title_scale' must be numeric."
  )
  
  # Check caption_size
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, caption_size = "test"), 
    "Parameter 'caption_size' must be numeric."
  )
  
  # Check axes_tick_scale
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, axes_tick_scale = "test"), 
    "Parameter 'axes_tick_scale' must be numeric."
  )
  
  # Check axes_label_scale
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, axes_label_scale = "test"), 
    "Parameter 'axes_label_scale' must be numeric."
  )
  
  # Check numbfig
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, numbfig = "test"), 
    "Parameter 'numbfig' must be numeric."
  )
  
  # Check hatching_mask
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, hatching_mask = "test"), 
    "Parameter 'hatching_mask' must have two dimensions named as the longitude and latitude dimensions in 'var'."
  )
  
})


#----------------------------------------------------------------


save_fun <- function(...) {
  path <- tempfile(fileext = ".png")
  do.call(VizEquiMap, list(..., fileout = path))
  path
}


test_that("2. Output checks", {

  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F)),
    name = 'VizEquiMap_1a.png'
  )
  
  # check include_lower_boundary and include_upper_boundary
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F),
             include_lower_boundary = FALSE),
    name = 'VizEquiMap_1b.png'
  )
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F),
             include_upper_boundary = FALSE),
    name = 'VizEquiMap_1c.png'
  )
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F),
             include_lower_boundary = FALSE, include_upper_boundary = FALSE),
    name = 'VizEquiMap_1d.png'
  )
  
  # check triangle_ends
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(1, 9), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(T, T),
             col_inf = "lightblue", col_sup = "lightblue"),
    name = 'VizEquiMap_triangle_ends.png'
  )
  
  # check filled.continents
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = T,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F)),
    name = 'VizEquiMap_filled.continents.png'
  )
  
  # check mask
  mask <- array(rep(c(rep(0, 60), rep(1, 60)), 10), dim = c(lat = 30, lon = 44))
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F),
             mask = mask),
    name = 'VizEquiMap_mask.png'
  )
  
  # check title_scale
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F), 
             title_scale = 0.5),
    name = 'VizEquiMap_title_scale.png'
  )
  
  # check bar_label_scale
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F), 
             bar_label_scale = 0.5),
    name = 'VizEquiMap_bar_label_scale.png'
  )
  
  # check margin_scale
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F), 
             margin_scale = rep(10, 4)),
    name = 'VizEquiMap_margin_scale.png'
  )

})
