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