| 1 | context("ISOweekday") |
| 2 | |
| 3 | test_that("ISOweekday returns integer values", { |
| 4 | expect_that(ISOweekday("1999-12-31"), is_a("integer")) |
| 5 | }) |
| 6 | test_that("ISOweekday returns proper values", { |
| 7 | input_chr <- paste(1999:2011, "-12-31", sep="") |
| 8 | input_date <- as.Date(input_chr) |
| 9 | result_int <- as.integer(c(5, 7, 1, 2, 3, 5, 6, 7, 1, 3, 4, 5, 6)) |
| 10 | expect_that(ISOweekday(input_date), equals(result_int)) |
| 11 | expect_that(ISOweekday(input_chr), equals(result_int)) |
| 12 | }) |
| 13 | test_that("ISOweekday handles NAs", { |
| 14 | expect_that(ISOweekday(NA), equals(NA_integer_)) |
| 15 | expect_that(ISOweekday(NA_character_), equals(NA_integer_)) |
| 16 | input_chr <- paste(1999:2011, "-12-31", sep="") |
| 17 | result_int <- as.integer(c(5, 7, 1, 2, 3, 5, 6, 7, 1, 3, 4, 5, 6)) |
| 18 | idx_NA <- seq(from =2, to = length(input_chr), by = 2) |
| 19 | input_chr_NA <- input_chr |
| 20 | input_chr_NA[idx_NA] <- NA_character_ |
| 21 | input_date_NA <- as.Date(input_chr_NA) |
| 22 | result_int_NA <- result_int |
| 23 | result_int_NA[idx_NA] <- NA |
| 24 | expect_that(ISOweekday(input_date_NA), equals(result_int_NA)) |
| 25 | expect_that(ISOweekday(input_chr_NA), equals(result_int_NA)) |
| 26 | }) |
| 27 | test_that("ISOweekday handles zero length vectors", { |
| 28 | input_date <- as.Date("1999-12-31") |
| 29 | expect_that(length(ISOweekday(input_date[0])), equals(0)) |
| 30 | }) |
| 31 | test_that("ISOweekday stops on invalid parameters", { |
| 32 | expect_that(ISOweekday(31), throws_error()) |
| 33 | expect_that(ISOweekday(31.1), throws_error()) |
| 34 | expect_that(ISOweekday(FALSE), throws_error()) |
| 35 | expect_that(ISOweekday("31"), throws_error()) |
| 36 | expect_that(ISOweekday("12/31/1999"), throws_error()) |
| 37 | }) |