tests/testthat/test_isfy.R

context("FY")

test_that("is.fy() returns TRUE on FYs", {
  expect_true(is.fy("2012-13"))
  expect_true(is.fy("1999-00"))
  expect_true(is.fy("201415"))
})

test_that("is.fy() returns FALSE on non-FYs", {
  expect_false(is.fy("2012-14"))
  expect_false(is.fy("banana"))
  expect_false(is.fy("2012-13 was a long year"))
  expect_false(is.fy("20121313"))
  expect_false(is.fy("12-13"))
})

test_that("Other fy utils", {
  expect_equal(fy.year(2012), "2011-12")
  # Not an FY
  expect_error(fy2yr("2014-16"))
  expect_error(fy2date("2014-16"))
  
  expect_equal(fy2date("2012-13"), as.Date("2013-06-30"))
})

test_that("all_fy", {
  expect_true(all_fy(c("2000-01", "2010-11", "2013-14", "2020-21")))
  expect_true(all_fy(c("2000-01", "2010-11", "2013-14", "2020-21"),
                     permitted = c("2000-01", "2010-11", "2013-14", "2020-21")))
})

test_that("is_fy2", {
  expect_true(all(is_fy2(c("2000-01", "2010-11", "2013-14", "2020-21"))))
})

test_that("Correct logic when asserting fys", {
  expect_error(fy2date(c("foo", "2015-16")), 
               regexp = "fy.yr contains non-FYs", 
               fixed = TRUE)
  expect_error(fy2yr(c("foo", "2015-16")), 
               regexp = "fy.yr contains non-FYs", 
               fixed = TRUE)
})

test_that("fy.year and yr2fy are identical", {
  x <- 1901:2099
  expect_identical(fy.year(x), yr2fy(x))
})

test_that("grattan.assume1901_2100 options", {
  skip_if_not_installed("rlang")
  skip_on_cran()
  x <- 1900:2099
  rlang::with_options(
    expect_identical(fy.year(x), yr2fy(x)),
    grattan.assume1901_2100 = FALSE
  )
  expect_identical(fy.year(x), yr2fy(x, FALSE))
})

test_that("yr2fy and .yr2fy", {
  x <- 1900:2100
  expect_identical(fy.year(x), .yr2fy(x))
  x <- rep_len(x, 20e3)
  expect_identical(fy.year(x), .yr2fy(x))
})

test_that("range_fy", {
  expect_identical(max_fy2yr(c("2015-16", "2000-01", "2001-02")), 2016L)
  expect_identical(min_fy2yr(c("2015-16", "2000-01", "2001-02")), 2001L)
})

Try the grattan package in your browser

Any scripts or data that you put into this service are public.

grattan documentation built on Oct. 17, 2018, 1:05 a.m.