diff --git a/tests/testthat/test-diagnostics-extra.R b/tests/testthat/test-diagnostics-extra.R new file mode 100644 index 0000000..66e86f1 --- /dev/null +++ b/tests/testthat/test-diagnostics-extra.R @@ -0,0 +1,35 @@ +# Additional coverage tests for AIC / AICweights diagnostics methods +library(testthat) +library(EnvCpt) + +# Minimal envcpt-like object for exercising diagnostics methods directly +fake_envcpt <- list( + summary = matrix( + c(10, 20, + 1, 2), + nrow = 2, + byrow = TRUE + ) +) +class(fake_envcpt) <- "envcpt" + +# AIC.envcpt should respect a user-supplied penalty value +test_that("AIC.envcpt respects custom penalty k", { + out <- AIC(fake_envcpt, k = 3) + expect_equal(out, c(13, 26)) +}) + +# The default AICweights method should return the documented fallback message +test_that("AICweights.default returns fallback message", { + out <- AICweights(1:5) + expect_equal(out, "No default method created for S3 class AICweights.") +}) + +# AICweights.envcpt should return normalized weights derived from AIC values +test_that("AICweights.envcpt returns normalized weights", { + w <- AICweights(fake_envcpt) + aic <- AIC(fake_envcpt) + expected <- exp(-0.5 * (aic - min(aic))) + expected <- expected / sum(expected) + expect_equal(w, expected) +}) \ No newline at end of file diff --git a/tests/testthat/test-plot-extra.R b/tests/testthat/test-plot-extra.R new file mode 100644 index 0000000..227f2b4 --- /dev/null +++ b/tests/testthat/test-plot-extra.R @@ -0,0 +1,43 @@ +# Additional coverage tests for plot.envcpt argument handling +library(testthat) +library(EnvCpt) + +test_that("plot.envcpt errors when x is not envcpt", { + expect_error( + EnvCpt:::plot.envcpt(list()), + "x must be an object with class envcpt" + ) +}) + +# These plotting branches are run locally / outside CRAN +if (identical(Sys.getenv("NOT_CRAN"), "true")) { + set.seed(99) + x <- c(rnorm(40, 0, 1), rnorm(40, 3, 1)) + out <- envcpt(x) +# The function accepts the British spelling "colours", although it currently warns + test_that("plot.envcpt accepts colours alias but warns", { + expect_warning(plot(out, type = "aic", colours = rep("black", 12))) + }) + + test_that("plot.envcpt errors when colors vector is too short", { + expect_error( + plot(out, type = "aic", colors = rep("black", 3)), + "colors must be a vector of length 12" + ) + }) + + test_that("plot.envcpt errors when a color is invalid", { + bad_cols <- c(rep("black", 11), "not_a_colour") + expect_error( + plot(out, type = "aic", colors = bad_cols), + "Atleast one of your colours is not resolvable by col2rgb." + ) + }) + + test_that("plot.envcpt errors on unsupported plot type", { + expect_error( + plot(out, type = "weird"), + "type supplied can only be 'aic', 'bic' or 'fit'" + ) + }) +}