| 1 | context("ISOweek2date") |
| 2 | |
| 3 | test_that("ISOweek2date returns proper format", { |
| 4 | expect_that(ISOweek2date("1999-W01-1"), is_a("Date")) |
| 5 | }) |
| 6 | test_that("ISOweek2date returns proper values", { |
| 7 | input_chr <- c("1999-W52-5", "2000-W52-7", "2002-W01-1", "2003-W01-2", "2004-W01-3", |
| 8 | "2004-W53-5", "2005-W52-6", "2006-W52-7", "2008-W01-1", "2009-W01-3", |
| 9 | "2009-W53-4", "2010-W52-5", "2011-W52-6") |
| 10 | result_chr <- paste(1999:2011, "-12-31", sep="") |
| 11 | result_date <- as.Date(result_chr) |
| 12 | expect_that(ISOweek2date(input_chr), equals(result_date)) |
| 13 | }) |
| 14 | test_that("ISOweek2date handles NAs", { |
| 15 | expect_that(ISOweek2date(NA), equals(as.Date(NA))) |
| 16 | expect_that(ISOweek2date(NA_character_), equals(as.Date(NA))) |
| 17 | input_chr <- c("1999-W52-5", "2000-W52-7", "2002-W01-1", "2003-W01-2", "2004-W01-3", |
| 18 | "2004-W53-5", "2005-W52-6", "2006-W52-7", "2008-W01-1", "2009-W01-3", |
| 19 | "2009-W53-4", "2010-W52-5", "2011-W52-6") |
| 20 | result_chr <- paste(1999:2011, "-12-31", sep="") |
| 21 | result_date <- as.Date(result_chr) |
| 22 | idx_NA <- seq(from =2, to = length(input_chr), by = 2) |
| 23 | input_chr_NA <- input_chr |
| 24 | input_chr_NA[idx_NA] <- NA_character_ |
| 25 | result_chr_NA <- result_chr |
| 26 | result_chr_NA[idx_NA] <- NA |
| 27 | result_date_NA <- as.Date(result_chr_NA) |
| 28 | expect_that(ISOweek2date(input_chr_NA), equals(result_date_NA)) |
| 29 | }) |
| 30 | test_that("ISOweek2date handles zero length vectors", { |
| 31 | input_chr <- c("1999-W52-5") |
| 32 | expect_that(length(ISOweek2date(input_chr[0])), equals(0)) |
| 33 | }) |
| 34 | test_that("ISOweek2date stops on invalid parameters", { |
| 35 | expect_that(ISOweek2date(31), throws_error()) |
| 36 | expect_that(ISOweek2date(31.1), throws_error()) |
| 37 | expect_that(ISOweek2date(FALSE), throws_error()) |
| 38 | expect_that(ISOweek2date("31"), throws_error()) |
| 39 | expect_that(ISOweek2date("12/31/1999"), throws_error()) |
| 40 | expect_that(ISOweek2date("1999-W00-1"), throws_error()) |
| 41 | expect_that(ISOweek2date("1999-W54-1"), throws_error()) |
| 42 | expect_that(ISOweek2date("1999-W01-0"), throws_error()) |
| 43 | expect_that(ISOweek2date("1999-W01-8"), throws_error()) |
| 44 | }) |