From 98a5a40d85ee35d8f506d0cba54b7d9e9b10135d Mon Sep 17 00:00:00 2001 From: ArslanBatyrov Date: Fri, 27 Mar 2026 08:40:47 +0000 Subject: [PATCH 1/5] Add plot.envcpt argument validation tests --- tests/testthat/test-plot-extra.R | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/testthat/test-plot-extra.R diff --git a/tests/testthat/test-plot-extra.R b/tests/testthat/test-plot-extra.R new file mode 100644 index 0000000..f298342 --- /dev/null +++ b/tests/testthat/test-plot-extra.R @@ -0,0 +1,41 @@ +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" + ) +}) + +if (identical(Sys.getenv("NOT_CRAN"), "true")) { + set.seed(99) + x <- c(rnorm(40, 0, 1), rnorm(40, 3, 1)) + out <- envcpt(x) + + 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'" + ) + }) +} \ No newline at end of file From 536d43d824caef5a6dfe3f1b2071b31645bbcca0 Mon Sep 17 00:00:00 2001 From: ArslanBatyrov Date: Fri, 27 Mar 2026 09:10:06 +0000 Subject: [PATCH 2/5] Add diagnostics coverage tests for AIC and AICweights --- tests/testthat/test-diagnostics-extra.R | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/testthat/test-diagnostics-extra.R diff --git a/tests/testthat/test-diagnostics-extra.R b/tests/testthat/test-diagnostics-extra.R new file mode 100644 index 0000000..080acff --- /dev/null +++ b/tests/testthat/test-diagnostics-extra.R @@ -0,0 +1,30 @@ +library(testthat) +library(EnvCpt) + +fake_envcpt <- list( + summary = matrix( + c(10, 20, + 1, 2), + nrow = 2, + byrow = TRUE + ) +) +class(fake_envcpt) <- "envcpt" + +test_that("AIC.envcpt respects custom penalty k", { + out <- AIC(fake_envcpt, k = 3) + expect_equal(out, c(13, 26)) +}) + +test_that("AICweights.default returns fallback message", { + out <- AICweights(1:5) + expect_equal(out, "No default method created for S3 class AICweights.") +}) + +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 From 0011472cd56c74b10275a32e5e97477d72d91d9c Mon Sep 17 00:00:00 2001 From: ArslanBatyrov Date: Fri, 27 Mar 2026 09:24:21 +0000 Subject: [PATCH 3/5] Normalize newline in plot test file --- tests/testthat/test-plot-extra.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-plot-extra.R b/tests/testthat/test-plot-extra.R index f298342..e0b70fc 100644 --- a/tests/testthat/test-plot-extra.R +++ b/tests/testthat/test-plot-extra.R @@ -38,4 +38,4 @@ if (identical(Sys.getenv("NOT_CRAN"), "true")) { "type supplied can only be 'aic', 'bic' or 'fit'" ) }) -} \ No newline at end of file +} From c87ff17f44ef16c1cf1aae710cd5635a3de173a6 Mon Sep 17 00:00:00 2001 From: ArslanBatyrov Date: Fri, 27 Mar 2026 10:16:16 +0000 Subject: [PATCH 4/5] Add comments to new coverage tests --- tests/testthat/test-diagnostics-extra.R | 6 ++++++ tests/testthat/test-plot-extra.R | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-diagnostics-extra.R b/tests/testthat/test-diagnostics-extra.R index 080acff..12fff4c 100644 --- a/tests/testthat/test-diagnostics-extra.R +++ b/tests/testthat/test-diagnostics-extra.R @@ -1,6 +1,9 @@ +# Additional coverage tests for AIC / AICweights diagnostics methods +# 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, @@ -11,16 +14,19 @@ fake_envcpt <- list( ) 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) diff --git a/tests/testthat/test-plot-extra.R b/tests/testthat/test-plot-extra.R index e0b70fc..227f2b4 100644 --- a/tests/testthat/test-plot-extra.R +++ b/tests/testthat/test-plot-extra.R @@ -1,3 +1,4 @@ +# Additional coverage tests for plot.envcpt argument handling library(testthat) library(EnvCpt) @@ -8,11 +9,12 @@ test_that("plot.envcpt errors when x is not 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))) }) From b7d06449195b8d97e215ffed20aa5274ee9e7d9e Mon Sep 17 00:00:00 2001 From: ArslanBatyrov Date: Fri, 27 Mar 2026 10:31:53 +0000 Subject: [PATCH 5/5] Remove duplicates in diagnostics --- tests/testthat/test-diagnostics-extra.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-diagnostics-extra.R b/tests/testthat/test-diagnostics-extra.R index 12fff4c..66e86f1 100644 --- a/tests/testthat/test-diagnostics-extra.R +++ b/tests/testthat/test-diagnostics-extra.R @@ -1,5 +1,4 @@ # Additional coverage tests for AIC / AICweights diagnostics methods -# Additional coverage tests for AIC / AICweights diagnostics methods library(testthat) library(EnvCpt)