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

test_that("Sanity checks", {
  expect_error(
    CST_MergeDims(data = 1),
    paste0("Parameter 'data' must be of the class 's2dv_cube'."))
data <- list(data = 1:10)
class(data) <- 's2dv_cube'
  expect_error(
    CST_MergeDims(data = data),
    paste0("Parameter 'data' must have dimensions."))

  data <- 1 : 20
  dim(data) <- c(time = 20)
  data <- list(data = data)
  class(data) <- 's2dv_cube'
  expect_error(
    CST_MergeDims(data = data),
    "Parameter 'merge_dims' must match with dimension names in parameter 'data'.")
  expect_error(
    CST_MergeDims(data = data, merge_dims = 1),
    paste0("Parameter 'merge_dims' must be a character vector indicating the names",
           " of the dimensions to be merged."))
  expect_error(
    CST_MergeDims(data = data, merge_dims = 'time'),
    "Parameter 'merge_dims' must be of length two.")
  expect_error(
    CST_MergeDims(data = data, merge_dims = c('time', 'sdates')),
    paste0("Parameter 'merge_dims' must match with dimension ",
             "names in parameter 'data'."))

  exp <- 1 : 20
  dim(exp) <- c(time = 10, lat = 2)
  exp <- list(data = exp)
  class(exp) <- 's2dv_cube'
  expect_equal(
    CST_MergeDims(data = exp, merge_dims = c('time', 'lat')), data)
 
  expect_warning(
    CST_MergeDims(data = exp, merge_dims = c('time', 'lat', 'lon')),
    paste0("Only two dimensions can be merge, only the first two dimension",
           " will be used. To merge further dimensions consider to use this ",
           "function multiple times."))
  expect_warning(
    CST_MergeDims(data = exp, merge_dims = c('time', 'lat'), rename_dim = c('lat', 'lon')),
    paste0("Parameter 'rename_dim' has length greater than 1 and only the ",
           "first element will be used."))
  names(dim(data$data)) <- 'Dim1'
  expect_equal(
    CST_MergeDims(data = exp, merge_dims = c('time', 'lat'), rename_dim = 'Dim1'), 
    data)

  expect_equal(
    CST_MergeDims(data = exp, merge_dims = c('time', 'lat'),
                  rename_dim = 'Dim1', na.rm = TRUE), data)

  exp$data[1,] <- NA
  data <- c(2 : 10, 12 : 20)
  dim(data) <- c(Dim1 = 18)
  data <- list(data = data)
  class(data) <- 's2dv_cube'
  expect_equal(
    CST_MergeDims(data = exp, merge_dims = c('time', 'lat'),
                  rename_dim = 'Dim1', na.rm = TRUE), data)
})
