From 4f0fb745900441712b0e0790eec7a235e95e8404 Mon Sep 17 00:00:00 2001 From: nikosbosse Date: Mon, 28 Oct 2024 22:14:26 +0100 Subject: [PATCH 1/2] Fix get_forecast_type --- R/get-forecast-type.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/get-forecast-type.R b/R/get-forecast-type.R index 3f6fe1ab..5d2eb5d1 100644 --- a/R/get-forecast-type.R +++ b/R/get-forecast-type.R @@ -4,14 +4,14 @@ #' Character vector of length one with the forecast type. #' @keywords internal_input_check get_forecast_type <- function(forecast) { - classname <- class(forecast)[1] - if (grepl("forecast_", classname, fixed = TRUE)) { - type <- gsub("forecast_", "", classname, fixed = TRUE) - return(type) + classname <- class(forecast) + forecast_class <- classname[grepl("forecast_", classname, fixed = TRUE)] + if (length(forecast_class) == 1) { + return(gsub("forecast_", "", forecast_class, fixed = TRUE)) } else { cli_abort( "Input is not a valid forecast object - (it's first class should begin with `forecast_`)." + (There should be a single class beginning with `forecast_`)." ) } } From cf10925adb0c99dc0f0dd11712f8132672eb5c60 Mon Sep 17 00:00:00 2001 From: nikosbosse Date: Wed, 30 Oct 2024 22:21:02 +0100 Subject: [PATCH 2/2] add test --- tests/testthat/test-get-forecast-type.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-get-forecast-type.R b/tests/testthat/test-get-forecast-type.R index 51deb134..0368f280 100644 --- a/tests/testthat/test-get-forecast-type.R +++ b/tests/testthat/test-get-forecast-type.R @@ -21,6 +21,11 @@ test_that("get_forecast_type() works as expected", { get_forecast_type(test), "Input is not a valid forecast object", ) + + # get_forecast_type() should still work even if a new class is added + testclassobject <- data.table::copy(example_quantile) + class(testclassobject) <- c("something", class(testclassobject)) + expect_equal(get_forecast_type(testclassobject), "quantile") })