tests/testthat/test_mNumber.R

library(checkmate)
context("mNumber")


test_that("output is data.frame", {
  input <- rtRasters$categorical
  bin <- rBinarise(rtRasters$continuous, thresh = 40)

  output <- mNumber(obj = bin, scale = "patch")
  expect_data_frame(output, ncols = 2, nrows = 1, any.missing = FALSE)

  output <- mNumber(obj = input, scale = "patch")
  expect_data_frame(output, ncols = 2, nrows = 9, any.missing = FALSE)

  output <- mNumber(obj = input, scale = "class")
  expect_data_frame(output, ncols = 2, nrows = 1, any.missing = FALSE)
})

test_that("determines patches, when binarised input is provided and 'scale = patch'", {
  input <- rtRasters$categorical
  bin <- rBinarise(rtRasters$continuous, thresh = 40)

  output <- mNumber(obj = bin, scale = "patch")
  expect_equal(output$patches, 26)

  output <- mNumber(obj = bin, scale = "class")
  expect_equal(output$classes, 2)
})

test_that("output has the correct columm names", {
  input <- rtRasters$categorical
  bin <- rBinarise(rtRasters$continuous, thresh = 40)

  output <- mNumber(obj = bin, scale = "patch")
  expect_names(names(output), identical.to = c("landscape", "patches"))

  output <- mNumber(obj = input, layer = "categorical", scale = "patch")
  expect_names(names(output), identical.to = c("class", "patches"))

  output <- mNumber(obj = input, scale = "class")
  expect_names(names(output), identical.to = c("landscape", "classes"))
})

test_that("Error if arguments have wrong value", {
  input <- rtRasters$categorical
  mat <- as.matrix(input)

  expect_error(mNumber(obj = mat))
  expect_error(mNumber(obj = input, scale = "landscape"))
  expect_error(mNumber(obj = input, layer = 1))
})

test_that("bibliography item has been created", {
  options(bibliography = NULL)
  input <- rtRasters$categorical
  
  output <- mNumber(obj = input, scale = "class")
  theBib <- getOption("bibliography")
  expect_class(theBib, classes =  "bibentry")
  
  options(bibliography = NULL)
  bin <- rBinarise(rtRasters$continuous, thresh = 40)
  disEuc <- rDistance(bin)
  
  output <- mNumber(obj = input, scale = "class")
  theBib <- getOption("bibliography")
  expect_class(theBib, classes =  "bibentry")
})
EhrmannS/rasterTools documentation built on Sept. 4, 2019, 10:34 a.m.