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

# dat1
data1 <- 1 : 20
dim(data1) <- c(time = 20)
data1 <- list(data = data1,
              dims = dim(data1))
class(data1) <- 's2dv_cube'

data2 <- 21 : 30
dim(data2) <- c(time = 10)
data2 <- list(data = data2,
              dims = dim(data2))
class(data2) <- 's2dv_cube'

data3 <- data2
dim(data3$data) <- c(time = 10, sdate = 1)
data3$dims <- dim(data3$data)
##############################################

test_that("1. Input checks", {
  expect_error(
    CST_BindDim(x = 1),
    paste0("'x' must contain at least two 's2dv_cube' objects.")
  )
  expect_error(
    CST_BindDim(x = list(1, 1)),
    paste0("All elements of 'x' must be of the class 's2dv_cube'.")
  )
  expect_error(
    CST_BindDim(x = list(data1, data2), along = "sdate"),
    paste0("The dimension specified in 'along' must be present in ",
           "all the s2dv_cubes in 'x'.")
  )
  expect_error(
    CST_BindDim(x = list(data1, data3), along = "time"),
    paste0("All elements of 'x' must have the same dimension names and length,",
           " except for the dimension specified in 'along'.")
  )
  expect_error(
    suppressWarnings(
      CST_BindDim(x = list(data1, data2), along = "time", var_dim = 1)),
    "Parameter 'var_dim' must be a character string."
  )
  expect_error(
    suppressWarnings(
      CST_BindDim(x = list(data1, data2), along = "time", dat_dim = 1)),
    "Parameter 'dat_dim' must be a character string."
  )
  expect_warning(
    CST_BindDim(x = list(data1, data2), along = "time", var_dim = "var"),
    paste0("Parameter 'dat_dim' not specified, dataset metadata of the ",
           "output might be inconsistent with the input.")
  )
  expect_warning(
    CST_BindDim(x = list(data1, data2), along = "time", dat_dim = "dat"),
    paste0("Parameter 'var_dim' not specified, variable metadata of the ",
           "output might be inconsistent with the input.")
  )
})

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

output1 <- list()
output1$data <- 1:30
output1$dims <- c(time = 30)
output1$coords <- list(time = 1:30)
attr(output1$coords$time, "indices") <- TRUE
output1$attrs$source_files <- array(list(NULL, NULL), dim = c(2, 1))
class(output1) <- "s2dv_cube"

res1 <- suppressWarnings(CST_BindDim(x = list(data1, data2), along = "time"))

test_that("2. Output checks", {
  expect_equal(
    class(res1),
    "s2dv_cube"
  )
  expect_equal(
    as.vector(res1$data),
    as.vector(output1$data)
  )
  expect_equal(
    res1$dims,
    output1$dims
  )
  expect_equal(
    res1$coords,
    output1$coords
  )
  expect_equal(
    res1$attrs$source_files,
    output1$attrs$source_files
  )
})

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

test_that("3. Output checks: sample data", {
  res2 <- suppressWarnings(
            exp_obs_combined <- CST_BindDim(x = list(lonlat_temp$exp, lonlat_temp$obs),
                                            along = "member",
                                            var_dim = NULL,
                                            dat_dim = "dataset")
          )
  expect_equal(
    dim(res2$data),
    c(dataset = 1, member = 16, sdate = 6, ftime = 3, lat = 22, lon = 53)
  )
  expect_equal(
    res2$dims,
    c(dataset = 1, member = 16, sdate = 6, ftime = 3, lat = 22, lon = 53)
  )
  expect_equal(
    as.vector(res2$coords$member),
    1:16
  )

})
