From 1094a7e2a43cf95af2dfeeb953334ac5f659f4bc Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Tue, 26 Feb 2019 22:08:54 -0800 Subject: [PATCH] fix #300 fix cpc_prcp, us=TRUE should allow back to 1948 --- R/cpc.R | 7 ++++++- tests/testthat/test-cpc_prcp.R | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/R/cpc.R b/R/cpc.R index 35897f44..6c4e42d9 100644 --- a/R/cpc.R +++ b/R/cpc.R @@ -52,8 +52,13 @@ cpc_prcp <- function(date, us = FALSE, drop_undefined = FALSE, ...) { assert(date, c("character", "Date")) assert(us, 'logical') + stopifnot(length(us) == 1) dates <- str_extract_all_(date, "[0-9]+")[[1]] - assert_range(dates[1], 1979:format(Sys.Date(), "%Y")) + if (us) { + assert_range(dates[1], 1948:format(Sys.Date(), "%Y")) + } else { + assert_range(dates[1], 1979:format(Sys.Date(), "%Y")) + } assert_range(as.numeric(dates[2]), 1:12) assert_range(as.numeric(dates[3]), 1:31) diff --git a/tests/testthat/test-cpc_prcp.R b/tests/testthat/test-cpc_prcp.R index e9218c83..ff6544cb 100644 --- a/tests/testthat/test-cpc_prcp.R +++ b/tests/testthat/test-cpc_prcp.R @@ -61,4 +61,12 @@ test_that("cpc_prcp fails well", { "date must be of class character, Date") expect_error(cpc_prcp("2017-01-15", us = 5), "us must be of class logical") + + # acceptable years + ## non us + expect_error(cpc_prcp("1945-01-01"), "must be between") + expect_error(cpc_prcp(Sys.Date() + 365), "must be between") + ## us + expect_error(cpc_prcp("1947-12-31", us = TRUE), "must be between") + expect_error(cpc_prcp(Sys.Date() + 365, us = TRUE), "must be between") })