test_that("weather_options() returns all options when called with no arguments", { # Reset to ensure clean state weather_reset() options <- weather_options() expect_type(options, "list") expect_true("extreme" %in% names(options)) expect_true("require_full_year" %in% names(options)) expect_true("frost_threshold" %in% names(options$extreme)) expect_equal(options$extreme$frost_threshold, 0) }) test_that("weather_options() can set and get match the original type and length", { # Reset to ensure clean state weather_reset() expect_no_error(weather_options(require_full_year = FALSE)) expect_no_error(weather_options(extreme.frost_threshold = 2)) expect_error(weather_options(require_full_year = "false")) expect_error(weather_options(extreme.frost_threshold = "false")) expect_error(weather_options(require_full_year = c(TRUE, FALSE))) expect_error(weather_options(extreme.frost_threshold = c(1, 2))) }) test_that("weather_options() can set and get first level options", { # Reset to ensure clean state weather_reset() # Set frost_threshold weather_options(require_full_year = FALSE) # Verify it was set options <- weather_options() expect_equal(options$require_full_year, FALSE) }) test_that("weather_options() can set and get frost_threshold", { # Reset to ensure clean state weather_reset() # Set frost_threshold weather_options(extreme.frost_threshold = 2) # Verify it was set options <- weather_options() expect_equal(options$extreme$frost_threshold, 2) # Set to negative value weather_options(extreme.frost_threshold = -5) options <- weather_options() expect_equal(options$extreme$frost_threshold, -5) # Set to decimal value weather_options(extreme.frost_threshold = 1.5) options <- weather_options() expect_equal(options$extreme$frost_threshold, 1.5) }) test_that("weather_options() validates argument names", { # Reset to ensure clean state weather_reset() # Unnamed arguments should fail expect_error( weather_options(2), "All arguments must be named" ) # Mixed named and unnamed should fail expect_error( weather_options(extreme.frost_threshold = 2, 5), "All arguments must be named" ) }) test_that("weather_options() validates option keys", { # Reset to ensure clean state weather_reset() # Invalid group should fail expect_error( weather_options(invalid.frost_threshold = 2) ) # Invalid key format (too many dots) should fail expect_error( weather_options(extreme.frost.threshold = 2) ) # Invalid key format (no dots for nested) - this should fail expect_error( weather_options(invalid_top_level = 2) ) }) test_that("weather_options() handles multiple options at once", { # Reset to ensure clean state weather_reset() # Set multiple nested options (when more are available) weather_options(extreme.frost_threshold = 3) # Verify all were set options <- weather_options() expect_equal(options$extreme$frost_threshold, 3) }) test_that("weather_reset() resets options to defaults", { # Set some non-default values weather_options(extreme.frost_threshold = 10) # Verify they were changed options <- weather_options() expect_equal(options$extreme$frost_threshold, 10) # Reset weather_reset() # Verify they're back to defaults options <- weather_options() expect_equal(options$extreme$frost_threshold, 0) }) test_that("weather_options() preserves other settings when updating one", { # Reset to ensure clean state weather_reset() # Get initial state initial_options <- weather_options() # Change frost_threshold weather_options(extreme.frost_threshold = 5) # Verify only frost_threshold changed new_options <- weather_options() expect_equal(new_options$extreme$frost_threshold, 5) # All other options should remain the same initial_options$extreme$frost_threshold <- 5 expect_equal(new_options, initial_options) }) test_that("weather_options() works with different data types", { # Reset to ensure clean state weather_reset() # Test with integer weather_options(extreme.frost_threshold = 2) expect_equal(weather_options()$extreme$frost_threshold, 2) # Test with numeric weather_options(extreme.frost_threshold = 2.5) expect_equal(weather_options()$extreme$frost_threshold, 2.5) # Test with negative weather_options(extreme.frost_threshold = -1.5) expect_equal(weather_options()$extreme$frost_threshold, -1.5) }) test_that("WEATHER_OPTIONS is not directly accessible", { # WEATHER_OPTIONS should not be accessible from global environment # Note: It might be exported during development but shouldn't be in production expect_true(is.function(weather_options)) }) test_that("options functions are properly exported", { # These functions should be available expect_true(exists("weather_options")) expect_true(exists("weather_reset")) }) test_that("weather_options() documentation examples work", { # Reset to ensure clean state weather_reset() # Test the examples from the documentation # Get all options all_options <- weather_options() expect_type(all_options, "list") # Set frost_threshold weather_options(extreme.frost_threshold = 2) expect_equal(weather_options()$extreme$frost_threshold, 2) }) # Clean up after all tests test_that("cleanup after options tests", { weather_reset() expect_equal(weather_options()$extreme$frost_threshold, 0) }) # Clean up after all tests test_that("get_weather_option", { weather_reset() expect_equal(get_weather_option("extreme.frost_threshold"), 0) weather_options(extreme.frost_threshold = 4) expect_equal(get_weather_option("extreme.frost_threshold"), 4) })