library(cermbTools)
context("Fire year")
.doTest <- function(transform_fn) {
# create character vector of random fire years
yrs <- sample(1900:2100, 1000, replace = TRUE)
nextYrs <- yrs+1
fireYrs <- stringr::str_c(yrs, stringr::str_sub(nextYrs, 3, 4))
# transform to required input data type
x <- transform_fn( fireYrs )
# test
expect_equal(getFireYear(x), yrs)
}
test_that("getFireYear works with integer values", {
.doTest(as.integer)
})
test_that("getFireYear works with character values", {
.doTest(identity)
})
test_that("getFireYear works with double values", {
.doTest(as.double)
})
test_that("getFireYear works with factor values from strings", {
.doTest(as.factor)
})
test_that("getFireYear works with factor values from integers", {
.doTest(function(x) as.factor(as.integer(x)))
})
test_that("getFireYear detects invalid second year", {
expect_error(getFireYear(199191))
expect_error(getFireYear(199190))
expect_error(getFireYear(199193))
})
test_that("getFireYear detects input value with invalid length", {
expect_error(getFireYear(1990)) # too short
expect_error(getFireYear(20000102)) # too long
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.