## load data
data(ExampleData.DeValues, envir = environment())
data(ExampleData.RLum.Analysis, envir = environment())
temp <- calc_CommonDose(ExampleData.DeValues$CA1, verbose = FALSE)

test_that("input validation", {
  testthat::skip_on_cran()

  expect_error(report_RLum(temp, file = iris),
               "'file' should be of class 'character' and have length 1")
  expect_error(report_RLum(temp, title = iris),
               "'title' should be of class 'character' and have length 1")
  expect_error(report_RLum(temp, compact = NA),
               "'compact' should be a single logical value")
  expect_error(report_RLum(temp, timestamp = "error"),
               "'timestamp' should be a single logical value")
  expect_error(report_RLum(temp, show_report = iris),
               "'show_report' should be a single logical value")
  expect_error(report_RLum(temp, launch.browser = c(12, 13)),
               "'launch.browser' should be a single logical value")
  expect_error(report_RLum(temp, css.file = iris),
               "'css.file' should be of class 'character' or NULL and have length 1")
  expect_error(report_RLum(temp, css.file = "error"),
               "Couldn't find the specified CSS file at 'error'")
  expect_error(report_RLum(temp, quiet = letters),
               "'quiet' should be a single logical value")
  expect_error(report_RLum(temp, clean = c(FALSE, TRUE)),
               "'clean' should be a single logical value")
})

test_that("test functionality", {
  testthat::skip_on_cran()

  ## the test fails on AppVeyor for no obvious reason on the windows
  ## platform ... attempts to reproduce this failure failed. So
  ## we skip this platform for the test
  testthat::skip_on_os("windows")

  # create the standard HTML report
  testthat::expect_null(report_RLum(object = temp, timestamp = FALSE, show_report = FALSE))
  testthat::expect_null(report_RLum(object = temp, timestamp = TRUE,
                                    show_report = FALSE, compact = FALSE))

  ## compact view
  expect_null(report_RLum(list(temp, temp, NULL),
                          show_report = FALSE, compact = TRUE))

  ## data.frame
  expect_null(report_RLum(ExampleData.DeValues$CA1))

  ## list of RLum objects
  report_RLum(IRSAR.RF.Data@records)

  ## array()
  expect_null(report_RLum(array()))

  ## names with spaces or missing
  ll <- as.list(ExampleData.DeValues$CA1)
  names(ll) <- c("", "ED Error")
  expect_null(report_RLum(ll))

  ## more coverage: data frame with a column containing a matrix, raw data,
  ## and the css.file option (any existing file is enough for coverage)
  m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
  df <- data.frame(id = 1, mat = rep(0, nrow(m)), raw = raw(2))
  df$mat <- m
  df$raw[1] <- as.raw(65)
  df$raw[2] <- as.raw(66)
  colnames(df)[2] <- "mat col"
  fake.css <- system.file("CITATION", package = "Luminescence")
  expect_null(report_RLum(df, css.file = fake.css))
})

test_that("regression tests", {
  testthat::skip_on_cran()

  ## issue 1260
  expect_silent(do.call(report_RLum, list(NaN)))
  expect_silent(do.call(report_RLum, list(iris[0, ])))
})
